11/*
22 * Nextcloud - Android Client
33 *
4+ * SPDX-FileCopyrightText: 2026 Alper Ozturk <alper.ozturk@nextcloud.com>
45 * SPDX-FileCopyrightText: 2022 Tobias Kaminsky <tobias@kaminsky.me>
56 * SPDX-FileCopyrightText: 2022 Nextcloud GmbH
67 * SPDX-License-Identifier: AGPL-3.0-or-later OR GPL-2.0-only
@@ -32,7 +33,6 @@ import com.owncloud.android.ui.adapter.DashboardWidgetListAdapter
3233import com.owncloud.android.ui.dialog.AccountChooserInterface
3334import com.owncloud.android.ui.dialog.MultipleAccountsDialog
3435import com.owncloud.android.utils.theme.ViewThemeUtils
35- import kotlinx.coroutines.CoroutineScope
3636import kotlinx.coroutines.Dispatchers
3737import kotlinx.coroutines.launch
3838import kotlinx.coroutines.withContext
@@ -43,7 +43,7 @@ class DashboardWidgetConfigurationActivity :
4343 DashboardWidgetConfigurationInterface ,
4444 Injectable ,
4545 AccountChooserInterface {
46- private lateinit var mAdapter : DashboardWidgetListAdapter
46+ private lateinit var adapter : DashboardWidgetListAdapter
4747 private lateinit var binding: DashboardWidgetConfigurationLayoutBinding
4848 private lateinit var currentUser: User
4949
@@ -78,7 +78,7 @@ class DashboardWidgetConfigurationActivity :
7878
7979 val layoutManager = LinearLayoutManager (this )
8080 // TODO follow our new architecture
81- mAdapter = DashboardWidgetListAdapter (
81+ adapter = DashboardWidgetListAdapter (
8282 lifecycleScope,
8383 accountManager,
8484 clientFactory,
@@ -87,7 +87,7 @@ class DashboardWidgetConfigurationActivity :
8787 )
8888 binding.list.apply {
8989 setHasFooter(false )
90- setAdapter(mAdapter)
90+ adapter = this @DashboardWidgetConfigurationActivity.adapter
9191 setLayoutManager(layoutManager)
9292 setEmptyView(binding.emptyView.emptyListView)
9393 }
@@ -112,9 +112,11 @@ class DashboardWidgetConfigurationActivity :
112112 visibility = View .VISIBLE
113113 text = currentUser.accountName
114114 setOnClickListener {
115- val dialog = MultipleAccountsDialog ()
116- dialog.highlightCurrentlyActiveAccount = false
117- dialog.show(supportFragmentManager, null )
115+ MultipleAccountsDialog ().apply {
116+ highlightCurrentlyActiveAccount = false
117+ }.also {
118+ it.show(supportFragmentManager, null )
119+ }
118120 }
119121 }
120122 }
@@ -141,48 +143,72 @@ class DashboardWidgetConfigurationActivity :
141143 binding.accountName.text = user.accountName
142144 }
143145
144- try {
145- CoroutineScope (Dispatchers .IO ).launch {
146+ lifecycleScope.launch {
147+ val result = fetchWidgets(user)
148+ if (result == null ) {
149+ showErrorState(user)
150+ return @launch
151+ }
152+
153+ handleResult(result)
154+ }
155+ }
156+
157+ @Suppress(" DEPRECATION" )
158+ private suspend fun fetchWidgets (user : User ): RemoteOperationResult <Map <String , DashboardWidget >>? =
159+ withContext(Dispatchers .IO ) {
160+ try {
146161 val client = clientFactory.createNextcloudClient(user)
147- val result = DashboardListWidgetsRemoteOperation ().execute(client)
148-
149- withContext(Dispatchers .Main ) {
150- if (result.isSuccess) {
151- mAdapter.setWidgetList(result.resultData)
152- } else if (result.code == RemoteOperationResult .ResultCode .FILE_NOT_FOUND ) {
153- mAdapter.setWidgetList(null )
154- binding.emptyView.root.visibility = View .VISIBLE
155- binding.emptyView.emptyListViewHeadline.setText(R .string.widgets_not_available_title)
156-
157- binding.emptyView.emptyListIcon.apply {
158- setImageResource(R .drawable.ic_list_empty_error)
159- visibility = View .VISIBLE
160- }
161- binding.emptyView.emptyListViewText.apply {
162- text = String .format(
163- getString(R .string.widgets_not_available),
164- getString(R .string.app_name)
165- )
166- visibility = View .VISIBLE
167- }
168- }
169- }
162+ DashboardListWidgetsRemoteOperation ().execute(client)
163+ } catch (e: CreationException ) {
164+ Log_OC .e(this @DashboardWidgetConfigurationActivity, " Error loading widgets for user $user " , e)
165+ null
170166 }
171- } catch (e: CreationException ) {
172- Log_OC .e(this , " Error loading widgets for user $user " , e)
167+ }
168+
169+ private fun handleResult (result : RemoteOperationResult <Map <String , DashboardWidget >>) {
170+ if (result.isSuccess) {
171+ adapter.setWidgetList(result.resultData)
172+ return
173+ }
173174
174- mAdapter.setWidgetList(null )
175- binding.emptyView.root.visibility = View .VISIBLE
175+ if (result.code == RemoteOperationResult .ResultCode .FILE_NOT_FOUND ) {
176+ showWidgetsNotAvailableState()
177+ }
178+ }
179+
180+ private fun showWidgetsNotAvailableState () {
181+ adapter.setWidgetList(null )
182+ binding.emptyView.run {
183+ root.visibility = View .VISIBLE
184+ emptyListViewHeadline.setText(R .string.widgets_not_available_title)
185+ emptyListIcon.apply {
186+ setImageResource(R .drawable.ic_list_empty_error)
187+ visibility = View .VISIBLE
188+ }
189+ emptyListViewText.apply {
190+ text = String .format(
191+ getString(R .string.widgets_not_available),
192+ getString(R .string.app_name)
193+ )
194+ visibility = View .VISIBLE
195+ }
196+ }
197+ }
176198
177- binding.emptyView.emptyListIcon.apply {
199+ private fun showErrorState (user : User ) {
200+ adapter.setWidgetList(null )
201+ binding.emptyView.run {
202+ root.visibility = View .VISIBLE
203+ emptyListIcon.apply {
178204 setImageResource(R .drawable.ic_list_empty_error)
179205 visibility = View .VISIBLE
180206 }
181- binding.emptyView. emptyListViewText.apply {
207+ emptyListViewText.apply {
182208 setText(R .string.common_error)
183209 visibility = View .VISIBLE
184210 }
185- binding.emptyView. emptyListViewAction.apply {
211+ emptyListViewAction.apply {
186212 visibility = View .VISIBLE
187213 setText(R .string.reload)
188214 setOnClickListener {
0 commit comments