Skip to content

Commit

Permalink
refactor: replaced BaseUseCase by BaseUseCaseWithResult to have a…
Browse files Browse the repository at this point in the history
…ll possible quota cases
  • Loading branch information
joragua committed Dec 18, 2024
1 parent f9c7f99 commit d7f96a2
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 87 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -50,7 +54,19 @@ class DrawerViewModel(
accountName: String,
) : ViewModel() {

val userQuota: Flow<UserQuota> = getStoredQuotaAsStreamUseCase(GetStoredQuotaAsStreamUseCase.Params(accountName))
private val _userQuota = MutableStateFlow<Event<UIResult<Flow<UserQuota?>>>?>(null)
val userQuota: StateFlow<Event<UIResult<Flow<UserQuota?>>>?> = _userQuota

init {
runUseCaseWithResult(
coroutineDispatcher = coroutinesDispatcherProvider.io,
requiresConnection = false,
showLoading = true,
flow = _userQuota,
useCase = getStoredQuotaAsStreamUseCase,
useCaseParams = GetStoredQuotaAsStreamUseCase.Params(accountName = accountName),
)
}

fun getAccounts(context: Context): List<Account> {
return AccountUtils.getAccounts(context).asList()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
}
}
Expand Down Expand Up @@ -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"
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Flow<UserQuota?>, GetStoredQuotaAsStreamUseCase.Params>() {
) : BaseUseCaseWithResult<Flow<UserQuota?>, GetStoredQuotaAsStreamUseCase.Params>() {

override fun run(params: Params): Flow<UserQuota?> =
userRepository.getStoredUserQuotaAsFlow(params.accountName)
Expand Down

0 comments on commit d7f96a2

Please sign in to comment.