Skip to content

Commit

Permalink
feat: added a new UseCase (GetUserQuotasAsStreamUseCase) to recover a…
Browse files Browse the repository at this point in the history
… flow with a list of all user quotas
  • Loading branch information
joragua committed Nov 6, 2024
1 parent bcf2545 commit 4f46a0d
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ import com.owncloud.android.domain.user.usecases.GetStoredQuotaUseCase
import com.owncloud.android.domain.user.usecases.GetUserAvatarAsyncUseCase
import com.owncloud.android.domain.user.usecases.GetUserInfoAsyncUseCase
import com.owncloud.android.domain.user.usecases.GetUserQuotasUseCase
import com.owncloud.android.domain.user.usecases.GetUserQuotasAsStreamUseCase
import com.owncloud.android.domain.user.usecases.RefreshUserQuotaFromServerAsyncUseCase
import com.owncloud.android.domain.webfinger.usecases.GetOwnCloudInstanceFromWebFingerUseCase
import com.owncloud.android.domain.webfinger.usecases.GetOwnCloudInstancesFromAuthenticatedWebFingerUseCase
Expand Down Expand Up @@ -255,6 +256,7 @@ val useCaseModule = module {
factoryOf(::GetUserAvatarAsyncUseCase)
factoryOf(::GetUserInfoAsyncUseCase)
factoryOf(::GetUserQuotasUseCase)
factoryOf(::GetUserQuotasAsStreamUseCase)
factoryOf(::RefreshUserQuotaFromServerAsyncUseCase)

// Server
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ class ManageAccountsDialogFragment : DialogFragment(), ManageAccountsAdapter.Acc

parentActivity = requireActivity() as ToolbarActivity
currentAccount = requireArguments().getParcelable(KEY_CURRENT_ACCOUNT)

subscribeToViewModels()
}

Expand Down Expand Up @@ -191,12 +192,7 @@ class ManageAccountsDialogFragment : DialogFragment(), ManageAccountsAdapter.Acc
*/
override fun run(future: AccountManagerFuture<Boolean>) {
if (future.isDone) {
if (currentAccount == manageAccountsViewModel.getCurrentAccount()) {
// Create new adapter with the remaining accounts
collectLatestLifecycleFlow(manageAccountsViewModel.userQuotas) { listUserQuotas ->
accountListAdapter.submitAccountList(accountList = getAccountListItems(listUserQuotas))
}
} else if (manageAccountsViewModel.getLoggedAccounts().isEmpty()) {
if (manageAccountsViewModel.getLoggedAccounts().isEmpty()) {
// Show create account screen if there isn't any account
createAccount()
} else { // At least one account left
Expand Down Expand Up @@ -282,7 +278,7 @@ class ManageAccountsDialogFragment : DialogFragment(), ManageAccountsAdapter.Acc
}

// Add Create Account item at the end of account list if multi-account is enabled
if (resources.getBoolean(R.bool.multiaccount_support) || accountList.isEmpty() || userQuotasList.isNotEmpty()) {
if (resources.getBoolean(R.bool.multiaccount_support) || accountList.isEmpty()) {
provisionalAccountList.add(ManageAccountsAdapter.AccountRecyclerItem.NewAccount)
}
return provisionalAccountList
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,14 @@ import com.owncloud.android.domain.user.model.UserQuota
import com.owncloud.android.domain.user.usecases.GetUserQuotasUseCase
import com.owncloud.android.domain.automaticuploads.model.AutomaticUploadsConfiguration
import com.owncloud.android.domain.automaticuploads.usecases.GetAutomaticUploadsConfigurationUseCase
import com.owncloud.android.domain.user.usecases.GetUserQuotasAsStreamUseCase
import com.owncloud.android.domain.utils.Event
import com.owncloud.android.extensions.ViewModelExt.runUseCaseWithResult
import com.owncloud.android.presentation.common.UIResult
import com.owncloud.android.providers.AccountProvider
import com.owncloud.android.providers.CoroutinesDispatcherProvider
import com.owncloud.android.usecases.files.RemoveLocalFilesForAccountUseCase
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.launch
Expand All @@ -44,22 +46,20 @@ class ManageAccountsViewModel(
private val accountProvider: AccountProvider,
private val removeLocalFilesForAccountUseCase: RemoveLocalFilesForAccountUseCase,
private val getAutomaticUploadsConfigurationUseCase: GetAutomaticUploadsConfigurationUseCase,
private val getUserQuotasUseCase: GetUserQuotasUseCase,
getUserQuotasAsStreamUseCase: GetUserQuotasAsStreamUseCase,
private val coroutinesDispatcherProvider: CoroutinesDispatcherProvider,
) : ViewModel() {

private val _cleanAccountLocalStorageFlow = MutableStateFlow<Event<UIResult<Unit>>?>(null)
val cleanAccountLocalStorageFlow: StateFlow<Event<UIResult<Unit>>?> = _cleanAccountLocalStorageFlow

private val _userQuotas = MutableStateFlow<List<UserQuota>>(emptyList())
val userQuotas: StateFlow<List<UserQuota>> get() = _userQuotas
val userQuotas: Flow<List<UserQuota>> = getUserQuotasAsStreamUseCase(Unit)

private var automaticUploadsConfiguration: AutomaticUploadsConfiguration? = null

init {
viewModelScope.launch(coroutinesDispatcherProvider.io) {
automaticUploadsConfiguration = getAutomaticUploadsConfigurationUseCase(Unit).getDataOrNull()
_userQuotas.value = getUserQuotasUseCase(Unit)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package com.owncloud.android.data.user.datasources

import com.owncloud.android.domain.user.model.UserQuota
import kotlinx.coroutines.flow.Flow

interface LocalUserDataSource {
fun saveQuotaForAccount(
Expand All @@ -33,6 +34,8 @@ interface LocalUserDataSource {

fun getAllUserQuotas(): List<UserQuota>

fun getAllUserQuotasAsStream(): Flow<List<UserQuota>>

fun deleteQuotaForAccount(
accountName: String
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ import com.owncloud.android.data.user.db.UserDao
import com.owncloud.android.data.user.db.UserQuotaEntity
import com.owncloud.android.domain.user.model.UserQuotaStatus
import com.owncloud.android.domain.user.model.UserQuota
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map

class OCLocalUserDataSource(
private val userDao: UserDao
Expand All @@ -45,6 +47,12 @@ class OCLocalUserDataSource(
}
}

override fun getAllUserQuotasAsStream(): Flow<List<UserQuota>> {
return userDao.getAllUserQuotasAsStream().map { userQuotasList ->
userQuotasList.map { it.toModel() }
}
}

override fun deleteQuotaForAccount(accountName: String) {
userDao.deleteQuotaForAccount(accountName = accountName)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.Query
import com.owncloud.android.data.ProviderMeta
import kotlinx.coroutines.flow.Flow

@Dao
interface UserDao {
Expand All @@ -35,6 +36,9 @@ interface UserDao {
@Query(SELECT_ALL_QUOTAS)
fun getAllUserQuotas(): List<UserQuotaEntity>

@Query(SELECT_ALL_QUOTAS)
fun getAllUserQuotasAsStream(): Flow<List<UserQuotaEntity>>

@Insert(onConflict = OnConflictStrategy.REPLACE)
fun insertOrReplace(userQuotaEntity: UserQuotaEntity)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import com.owncloud.android.domain.user.UserRepository
import com.owncloud.android.domain.user.model.UserAvatar
import com.owncloud.android.domain.user.model.UserInfo
import com.owncloud.android.domain.user.model.UserQuota
import kotlinx.coroutines.flow.Flow

class OCUserRepository(
private val localUserDataSource: LocalUserDataSource,
Expand All @@ -44,6 +45,9 @@ class OCUserRepository(
override fun getAllUserQuotas(): List<UserQuota> =
localUserDataSource.getAllUserQuotas()

override fun getAllUserQuotasAsStream(): Flow<List<UserQuota>> =
localUserDataSource.getAllUserQuotasAsStream()

override fun getUserAvatar(accountName: String): UserAvatar =
remoteUserDataSource.getUserAvatar(accountName)
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,13 @@ package com.owncloud.android.domain.user
import com.owncloud.android.domain.user.model.UserAvatar
import com.owncloud.android.domain.user.model.UserInfo
import com.owncloud.android.domain.user.model.UserQuota
import kotlinx.coroutines.flow.Flow

interface UserRepository {
fun getUserInfo(accountName: String): UserInfo
fun getUserQuota(accountName: String): UserQuota
fun getStoredUserQuota(accountName: String): UserQuota?
fun getAllUserQuotas(): List<UserQuota>
fun getAllUserQuotasAsStream(): Flow<List<UserQuota>>
fun getUserAvatar(accountName: String): UserAvatar
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.owncloud.android.domain.user.usecases

import com.owncloud.android.domain.BaseUseCase
import com.owncloud.android.domain.user.UserRepository
import com.owncloud.android.domain.user.model.UserQuota
import kotlinx.coroutines.flow.Flow

class GetUserQuotasAsStreamUseCase(
private val userRepository: UserRepository
) : BaseUseCase<Flow<List<UserQuota>>, Unit>() {
override fun run(params: Unit): Flow<List<UserQuota>> =
userRepository.getAllUserQuotasAsStream()
}

0 comments on commit 4f46a0d

Please sign in to comment.