From 32ed01c94579d70cd8f3902b922c207bafde5a2e Mon Sep 17 00:00:00 2001 From: joragua Date: Wed, 18 Dec 2024 12:39:52 +0100 Subject: [PATCH] refactor: replaced `BaseUseCase` by `BaseUseCaseWithResult` to have all possible quota cases --- .../presentation/common/DrawerViewModel.kt | 18 +- .../android/ui/activity/DrawerActivity.kt | 187 ++++++++++-------- .../usecases/GetStoredQuotaAsStreamUseCase.kt | 4 +- 3 files changed, 122 insertions(+), 87 deletions(-) diff --git a/owncloudApp/src/main/java/com/owncloud/android/presentation/common/DrawerViewModel.kt b/owncloudApp/src/main/java/com/owncloud/android/presentation/common/DrawerViewModel.kt index ba4b70fbe53..8d8bea931a4 100644 --- a/owncloudApp/src/main/java/com/owncloud/android/presentation/common/DrawerViewModel.kt +++ b/owncloudApp/src/main/java/com/owncloud/android/presentation/common/DrawerViewModel.kt @@ -32,11 +32,15 @@ import com.owncloud.android.data.providers.LocalStorageProvider import com.owncloud.android.domain.user.model.UserQuota import com.owncloud.android.domain.user.usecases.GetStoredQuotaAsStreamUseCase import com.owncloud.android.domain.user.usecases.GetUserQuotasUseCase +import com.owncloud.android.domain.utils.Event +import com.owncloud.android.extensions.ViewModelExt.runUseCaseWithResult import com.owncloud.android.presentation.authentication.AccountUtils import com.owncloud.android.providers.ContextProvider import com.owncloud.android.providers.CoroutinesDispatcherProvider import com.owncloud.android.usecases.accounts.RemoveAccountUseCase import kotlinx.coroutines.flow.Flow +import kotlinx.coroutines.flow.MutableStateFlow +import kotlinx.coroutines.flow.StateFlow import kotlinx.coroutines.launch import timber.log.Timber @@ -50,7 +54,19 @@ class DrawerViewModel( accountName: String, ) : ViewModel() { - val userQuota: Flow = getStoredQuotaAsStreamUseCase(GetStoredQuotaAsStreamUseCase.Params(accountName)) + private val _userQuota = MutableStateFlow>>?>(null) + val userQuota: StateFlow>>?> = _userQuota + + init { + runUseCaseWithResult( + coroutineDispatcher = coroutinesDispatcherProvider.io, + requiresConnection = false, + showLoading = true, + flow = _userQuota, + useCase = getStoredQuotaAsStreamUseCase, + useCaseParams = GetStoredQuotaAsStreamUseCase.Params(accountName = accountName), + ) + } fun getAccounts(context: Context): List { return AccountUtils.getAccounts(context).asList() diff --git a/owncloudApp/src/main/java/com/owncloud/android/ui/activity/DrawerActivity.kt b/owncloudApp/src/main/java/com/owncloud/android/ui/activity/DrawerActivity.kt index 801d7be0660..726c40f54c0 100644 --- a/owncloudApp/src/main/java/com/owncloud/android/ui/activity/DrawerActivity.kt +++ b/owncloudApp/src/main/java/com/owncloud/android/ui/activity/DrawerActivity.kt @@ -57,6 +57,7 @@ import com.google.android.material.navigation.NavigationView import com.owncloud.android.R import com.owncloud.android.domain.capabilities.model.OCCapability import com.owncloud.android.domain.files.model.FileListOption +import com.owncloud.android.domain.user.model.UserQuota import com.owncloud.android.domain.user.model.UserQuotaState import com.owncloud.android.domain.utils.Event import com.owncloud.android.extensions.collectLatestLifecycleFlow @@ -309,104 +310,121 @@ abstract class DrawerActivity : ToolbarActivity() { */ private fun updateQuota() { Timber.d("Update Quota") - collectLatestLifecycleFlow(drawerViewModel.userQuota) { userQuota -> - when { - userQuota.available == -4L -> { // Light users (oCIS) - getAccountQuotaText()?.text = getString(R.string.drawer_unavailable_used_storage) - getAccountQuotaBar()?.isVisible = false - getAccountQuotaStatusText()?.isVisible = false + collectLatestLifecycleFlow(drawerViewModel.userQuota) { event -> + event?.let { + when (val uiResult = event.peekContent()) { + is UIResult.Success -> { + uiResult.data?.let { userQuotaData -> + collectLatestLifecycleFlow(userQuotaData) { quota -> + quota?.let { onUpdateQuotaIsSuccessful(it) } + } + } + } + is UIResult.Loading -> getAccountQuotaText()?.text = getString(R.string.drawer_loading_quota) + is UIResult.Error -> getAccountQuotaText()?.text = getString(R.string.drawer_unavailable_used_storage) } + } + } + } - userQuota.available < 0 -> { // Pending, unknown or unlimited free storage - getAccountQuotaBar()?.apply { - isVisible = true - progress = 0 - progressTintList = ColorStateList.valueOf(resources.getColor(R.color.color_accent)) + private fun onUpdateQuotaIsSuccessful(userQuota: UserQuota) { + when { + userQuota.available == -4L -> { // Light users (oCIS) + getAccountQuotaText()?.text = getString(R.string.drawer_unavailable_used_storage) + getAccountQuotaBar()?.isVisible = false + getAccountQuotaStatusText()?.isVisible = false + } + + userQuota.available < 0 -> { // Pending, unknown or unlimited free storage + getAccountQuotaBar()?.apply { + isVisible = true + progress = 0 + progressTintList = ColorStateList.valueOf(resources.getColor(R.color.color_accent)) + } + getAccountQuotaText()?.text = String.format( + getString(R.string.drawer_unavailable_free_storage), + DisplayUtils.bytesToHumanReadable(userQuota.used, this, true) + ) + getAccountQuotaStatusText()?.visibility = View.GONE + } + + userQuota.available == 0L -> { // Exceeded storage. The value is over 100%. + getAccountQuotaBar()?.apply { + isVisible = true + progress = 100 + progressTintList = ColorStateList.valueOf(resources.getColor(R.color.quota_exceeded)) + } + + if (userQuota.state == UserQuotaState.EXCEEDED) { // oCIS + getAccountQuotaText()?.apply { + text = String.format( + getString(R.string.drawer_quota), + DisplayUtils.bytesToHumanReadable(userQuota.used, context, true), + DisplayUtils.bytesToHumanReadable(userQuota.getTotal(), context, true), + userQuota.getRelative() + ) } - getAccountQuotaText()?.text = String.format( - getString(R.string.drawer_unavailable_free_storage), - DisplayUtils.bytesToHumanReadable(userQuota.used, this, true) - ) + getAccountQuotaStatusText()?.apply { + visibility = View.VISIBLE + text = getString(R.string.drawer_exceeded_quota) + } + } else { // oC10 + getAccountQuotaText()?.text = getString(R.string.drawer_exceeded_quota) getAccountQuotaStatusText()?.visibility = View.GONE } + } - userQuota.available == 0L -> { // Exceeded storage. The value is over 100%. + else -> { // Limited storage. Value under 100% + if (userQuota.state == UserQuotaState.NEARING) { // Nearing storage. Value between 75% and 90% getAccountQuotaBar()?.apply { isVisible = true - progress = 100 + progress = userQuota.getRelative().toInt() progressTintList = ColorStateList.valueOf(resources.getColor(R.color.quota_exceeded)) } - - if (userQuota.state == UserQuotaState.EXCEEDED) { // oCIS - getAccountQuotaText()?.apply { - text = String.format( - getString(R.string.drawer_quota), - DisplayUtils.bytesToHumanReadable(userQuota.used, context, true), - DisplayUtils.bytesToHumanReadable(userQuota.getTotal(), context, true), - userQuota.getRelative() - ) - } - getAccountQuotaStatusText()?.apply { - visibility = View.VISIBLE - text = getString(R.string.drawer_exceeded_quota) - } - } else { // oC10 - getAccountQuotaText()?.text = getString(R.string.drawer_exceeded_quota) - getAccountQuotaStatusText()?.visibility = View.GONE + getAccountQuotaText()?.apply { + text = String.format( + getString(R.string.drawer_quota), + DisplayUtils.bytesToHumanReadable(userQuota.used, context, true), + DisplayUtils.bytesToHumanReadable(userQuota.getTotal(), context, true), + userQuota.getRelative() + ) } - } - - else -> { // Limited storage. Value under 100% - if (userQuota.state == UserQuotaState.NEARING) { // Nearing storage. Value between 75% and 90% - getAccountQuotaBar()?.apply { - isVisible = true - progress = userQuota.getRelative().toInt() - progressTintList = ColorStateList.valueOf(resources.getColor(R.color.color_accent)) - } - getAccountQuotaText()?.apply { - text = String.format( - getString(R.string.drawer_quota), - DisplayUtils.bytesToHumanReadable(userQuota.used, context, true), - DisplayUtils.bytesToHumanReadable(userQuota.getTotal(), context, true), - userQuota.getRelative() - ) - } - getAccountQuotaStatusText()?.apply { - visibility = View.VISIBLE - text = getString(R.string.drawer_nearing_quota) - } - } else if (userQuota.state == UserQuotaState.CRITICAL || userQuota.state == UserQuotaState.EXCEEDED) { // Critical storage. Value over 90% - getAccountQuotaBar()?.apply { - isVisible = true - progress = userQuota.getRelative().toInt() - progressTintList = ColorStateList.valueOf(resources.getColor(R.color.quota_exceeded)) - } - getAccountQuotaText()?.apply { - text = String.format( - getString(R.string.drawer_quota), - DisplayUtils.bytesToHumanReadable(userQuota.used, context, true), - DisplayUtils.bytesToHumanReadable(userQuota.getTotal(), context, true), - userQuota.getRelative() - ) - } - getAccountQuotaStatusText()?.apply { - visibility = View.VISIBLE - text = getString(R.string.drawer_critical_quota) - } - } else { // Normal storage. Value under 75% - getAccountQuotaBar()?.apply { - progress = userQuota.getRelative().toInt() - isVisible = true - progressTintList = ColorStateList.valueOf(resources.getColor(R.color.color_accent)) - } - getAccountQuotaText()?.text = String.format( + getAccountQuotaStatusText()?.apply { + visibility = View.VISIBLE + text = getString(R.string.drawer_nearing_quota) + } + } else if (userQuota.state == UserQuotaState.CRITICAL || + userQuota.state == UserQuotaState.EXCEEDED) { // Critical storage. Value over 90% + getAccountQuotaBar()?.apply { + isVisible = true + progress = userQuota.getRelative().toInt() + progressTintList = ColorStateList.valueOf(resources.getColor(R.color.quota_exceeded)) + } + getAccountQuotaText()?.apply { + text = String.format( getString(R.string.drawer_quota), - DisplayUtils.bytesToHumanReadable(userQuota.used, this, true), - DisplayUtils.bytesToHumanReadable(userQuota.getTotal(), this, true), + DisplayUtils.bytesToHumanReadable(userQuota.used, context, true), + DisplayUtils.bytesToHumanReadable(userQuota.getTotal(), context, true), userQuota.getRelative() ) - getAccountQuotaStatusText()?.visibility = View.GONE } + getAccountQuotaStatusText()?.apply { + visibility = View.VISIBLE + text = getString(R.string.drawer_critical_quota) + } + } else { // Normal storage. Value under 75% + getAccountQuotaBar()?.apply { + progress = userQuota.getRelative().toInt() + isVisible = true + progressTintList = ColorStateList.valueOf(resources.getColor(R.color.color_accent)) + } + getAccountQuotaText()?.text = String.format( + getString(R.string.drawer_quota), + DisplayUtils.bytesToHumanReadable(userQuota.used, this, true), + DisplayUtils.bytesToHumanReadable(userQuota.getTotal(), this, true), + userQuota.getRelative() + ) + getAccountQuotaStatusText()?.visibility = View.GONE } } } @@ -490,7 +508,8 @@ abstract class DrawerActivity : ToolbarActivity() { findItem(R.id.nav_settings)?.contentDescription = "${getString(R.string.actionbar_settings)} $roleAccessibilityDescription" findItem(R.id.drawer_menu_feedback)?.contentDescription = "${getString(R.string.drawer_feedback)} $roleAccessibilityDescription" findItem(R.id.drawer_menu_help)?.contentDescription = "${getString(R.string.prefs_help)} $roleAccessibilityDescription" - findItem(R.id.drawer_menu_privacy_policy)?.contentDescription = "${getString(R.string.prefs_privacy_policy)} $roleAccessibilityDescription" + findItem(R.id.drawer_menu_privacy_policy)?.contentDescription = + "${getString(R.string.prefs_privacy_policy)} $roleAccessibilityDescription" } } } diff --git a/owncloudDomain/src/main/java/com/owncloud/android/domain/user/usecases/GetStoredQuotaAsStreamUseCase.kt b/owncloudDomain/src/main/java/com/owncloud/android/domain/user/usecases/GetStoredQuotaAsStreamUseCase.kt index 3307ff00c01..b514833ce6b 100644 --- a/owncloudDomain/src/main/java/com/owncloud/android/domain/user/usecases/GetStoredQuotaAsStreamUseCase.kt +++ b/owncloudDomain/src/main/java/com/owncloud/android/domain/user/usecases/GetStoredQuotaAsStreamUseCase.kt @@ -20,14 +20,14 @@ package com.owncloud.android.domain.user.usecases -import com.owncloud.android.domain.BaseUseCase +import com.owncloud.android.domain.BaseUseCaseWithResult import com.owncloud.android.domain.user.UserRepository import com.owncloud.android.domain.user.model.UserQuota import kotlinx.coroutines.flow.Flow class GetStoredQuotaAsStreamUseCase( private val userRepository: UserRepository -) : BaseUseCase, GetStoredQuotaAsStreamUseCase.Params>() { +) : BaseUseCaseWithResult, GetStoredQuotaAsStreamUseCase.Params>() { override fun run(params: Params): Flow = userRepository.getStoredUserQuotaAsFlow(params.accountName)