Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(ui): coroutine based user status retrieval. #13927

Merged
merged 2 commits into from
Nov 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions app/src/main/java/com/nextcloud/ui/ChooseAccountDialogFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import androidx.fragment.app.DialogFragment
import androidx.lifecycle.lifecycleScope
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import com.nextcloud.client.account.User
import com.nextcloud.client.account.UserAccountManager
Expand All @@ -33,10 +34,10 @@ import com.owncloud.android.ui.activity.BaseActivity
import com.owncloud.android.ui.activity.DrawerActivity
import com.owncloud.android.ui.adapter.UserListAdapter
import com.owncloud.android.ui.adapter.UserListItem
import com.owncloud.android.ui.asynctasks.RetrieveStatusAsyncTask
import com.owncloud.android.utils.DisplayUtils
import com.owncloud.android.utils.DisplayUtils.AvatarGenerationListener
import com.owncloud.android.utils.theme.ViewThemeUtils
import kotlinx.coroutines.launch
import javax.inject.Inject

private const val ARG_CURRENT_USER_PARAM = "currentUser"
Expand Down Expand Up @@ -151,12 +152,23 @@ class ChooseAccountDialogFragment :
binding.statusView.visibility = View.VISIBLE
}

RetrieveStatusAsyncTask(user, this, clientFactory).execute()
loadAndSetUserStatus(user)
}

themeViews()
}

private fun loadAndSetUserStatus(user: User) {
viewLifecycleOwner.lifecycleScope.launch {
val status = retrieveUserStatus(user, clientFactory)

if (isAdded && !isDetached) {
val context = requireContext()
setStatus(status, context)
}
}
}

private fun themeViews() {
viewThemeUtils.platform.themeDialogDivider(binding.separatorLine)
viewThemeUtils.platform.themeDialog(binding.root)
Expand Down
37 changes: 37 additions & 0 deletions app/src/main/java/com/nextcloud/ui/RetrieveStatus.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Nextcloud - Android Client
*
* SPDX-FileCopyrightText: 2024 Edvard Holst <[email protected]>
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

package com.nextcloud.ui

import com.nextcloud.client.account.User
import com.nextcloud.client.network.ClientFactory
import com.owncloud.android.lib.resources.users.GetStatusRemoteOperation
import com.owncloud.android.lib.resources.users.Status
import com.owncloud.android.lib.resources.users.StatusType
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import java.io.IOException

suspend fun retrieveUserStatus(user: User, clientFactory: ClientFactory): Status {
return withContext(Dispatchers.IO) {
try {
val client = clientFactory.createNextcloudClient(user)
val result = GetStatusRemoteOperation().execute(client)
if (result.isSuccess && result.resultData is Status) {
result.resultData as Status
} else {
offlineStatus()
}
} catch (e: ClientFactory.CreationException) {
offlineStatus()
} catch (e: IOException) {
offlineStatus()
}
}
}

private fun offlineStatus() = Status(StatusType.OFFLINE, "", "", -1)

This file was deleted.

Loading