Skip to content

Commit

Permalink
refactor: changed the structure of updateQuota method and spacesListV…
Browse files Browse the repository at this point in the history
…iewModel declaration
  • Loading branch information
joragua committed Nov 13, 2024
1 parent 7d5cfc8 commit cf310ff
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 112 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -155,32 +155,36 @@ class ManageAccountsAdapter(
fun getItem(position: Int) = accountItemsList[position]

private fun updateQuota(quotaText: TextView, quotaBar: ProgressBar, userQuota: UserQuota, context: Context) {
if (userQuota.available < 0) { // Pending, unknown or unlimited free storage. The progress bar is hid
quotaBar.visibility = View.GONE
quotaText.text = DisplayUtils.bytesToHumanReadable(userQuota.used, context, false)

} else if (userQuota.state == UserQuotaState.EXCEEDED) { // Exceeded storage. Value over 100%
quotaBar.apply {
progress = 100
progressTintList = ColorStateList.valueOf(resources.getColor(R.color.quota_exceeded))
when {
userQuota.available < 0 -> { // Pending, unknown or unlimited free storage. The progress bar is hid
quotaBar.visibility = View.GONE
quotaText.text = DisplayUtils.bytesToHumanReadable(userQuota.used, context, false)
}

userQuota.available == 0L -> { // Exceeded storage. Value over 100%
quotaBar.apply {
progress = 100
progressTintList = ColorStateList.valueOf(resources.getColor(R.color.quota_exceeded))
}
if (userQuota.state == UserQuotaState.EXCEEDED) { // oCIS
quotaText.text = String.format(
context.getString(R.string.manage_accounts_quota),
DisplayUtils.bytesToHumanReadable(userQuota.used, context, false),
DisplayUtils.bytesToHumanReadable(userQuota.getTotal(), context, false)
)
} else { // oC10
quotaText.text = context.getString(R.string.drawer_exceeded_quota)
}
}

else -> { // Limited storage. Value under 100%
quotaBar.progress = userQuota.getRelative().toInt()
quotaText.text = String.format(
context.getString(R.string.manage_accounts_quota),
DisplayUtils.bytesToHumanReadable(userQuota.used, context, false),
DisplayUtils.bytesToHumanReadable(userQuota.getTotal(), context, false)
)
}
quotaText.text = String.format(
context.getString(R.string.manage_accounts_quota),
DisplayUtils.bytesToHumanReadable(userQuota.used, context, false),
DisplayUtils.bytesToHumanReadable(userQuota.getTotal(), context, false)
)

} else if (userQuota.available == 0L) { // Exceeded storage in oC10. The progress bar is hid
quotaBar.visibility = View.GONE
quotaText.text = context.getString(R.string.drawer_unavailable_used_storage)

} else { // Limited storage. Value under 100%
quotaBar.progress = userQuota.getRelative().toInt()
quotaText.text = String.format(
context.getString(R.string.manage_accounts_quota),
DisplayUtils.bytesToHumanReadable(userQuota.used, context, false),
DisplayUtils.bytesToHumanReadable(userQuota.getTotal(), context, false)
)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -311,99 +311,102 @@ abstract class DrawerActivity : ToolbarActivity() {
when (val uiResult = event.peekContent()) {
is UIResult.Success -> {
uiResult.data?.let { userQuota ->
if (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

} else if (userQuota.state == UserQuotaState.EXCEEDED) { // Exceeded storage. The value is over 100%
getAccountQuotaBar()?.apply {
isVisible = true
progress = 100
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_exceeded_quota)
}


} else if (userQuota.available == 0L) { // Exceeded storage in oC10, quota 0, guest users
getAccountQuotaBar()?.isVisible = false
getAccountQuotaText()?.text = getString(R.string.drawer_unavailable_used_storage)
getAccountQuotaStatusText()?.visibility = View.GONE

} else 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()
when {
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()?.apply {
visibility = View.VISIBLE
text = getString(R.string.drawer_nearing_quota)
getAccountQuotaStatusText()?.visibility = View.GONE
}

} else if (userQuota.state == UserQuotaState.CRITICAL) { // Critical storage. Value over 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_critical_quota)
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()
)
}
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
}
}

} else { // Limited storage. Value under 75%
// Update progress bar rounding up to next int. Example: quota is 0.54 => 1
getAccountQuotaBar()?.apply {
progress = userQuota.getRelative().toInt()
isVisible = true
progressTintList = ColorStateList.valueOf(resources.getColor(R.color.color_accent))
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) { // Critical storage. Value over 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_critical_quota)
}
} else { // Normal storage. Value under 75%
// Update progress bar rounding up to next int. Example: quota is 0.54 => 1
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
}
}
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
}
}
}

is UIResult.Loading -> getAccountQuotaText()?.text = getString(R.string.drawer_loading_quota)
is UIResult.Error -> getAccountQuotaText()?.text = getString(R.string.drawer_unavailable_used_storage)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,6 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.launch
import org.koin.android.ext.android.inject
import org.koin.androidx.viewmodel.ext.android.getViewModel
import org.koin.androidx.viewmodel.ext.android.viewModel
import org.koin.core.parameter.parametersOf
import timber.log.Timber
Expand Down Expand Up @@ -184,7 +183,9 @@ class FileDisplayActivity : FileActivity(),

private val fileOperationsViewModel: FileOperationsViewModel by viewModel()
private val transfersViewModel: TransfersViewModel by viewModel()
private lateinit var spacesListViewModel: SpacesListViewModel

private val spacesListViewModelLazy: Lazy<SpacesListViewModel> = inject { parametersOf(account.name, false) }
private val spacesListViewModel: SpacesListViewModel get() = spacesListViewModelLazy.value

private val sharedPreferences: SharedPreferencesProvider by inject()

Expand Down Expand Up @@ -381,7 +382,6 @@ class FileDisplayActivity : FileActivity(),
}
}

spacesListViewModel = getViewModel { parametersOf(account.name, false) }
spacesListViewModel.refreshSpacesFromServer()
}

Expand Down

0 comments on commit cf310ff

Please sign in to comment.