From 4f46a0db3a3d1831d701683c0cdc74b41b46facb Mon Sep 17 00:00:00 2001 From: joragua Date: Wed, 6 Nov 2024 13:32:02 +0100 Subject: [PATCH] feat: added a new UseCase (GetUserQuotasAsStreamUseCase) to recover a flow with a list of all user quotas --- .../android/dependecyinjection/UseCaseModule.kt | 2 ++ .../accounts/ManageAccountsDialogFragment.kt | 10 +++------- .../accounts/ManageAccountsViewModel.kt | 8 ++++---- .../data/user/datasources/LocalUserDataSource.kt | 3 +++ .../implementation/OCLocalUserDataSource.kt | 8 ++++++++ .../com/owncloud/android/data/user/db/UserDao.kt | 4 ++++ .../data/user/repository/OCUserRepository.kt | 4 ++++ .../owncloud/android/domain/user/UserRepository.kt | 2 ++ .../user/usecases/GetUserQuotasAsStreamUseCase.kt | 13 +++++++++++++ 9 files changed, 43 insertions(+), 11 deletions(-) create mode 100644 owncloudDomain/src/main/java/com/owncloud/android/domain/user/usecases/GetUserQuotasAsStreamUseCase.kt diff --git a/owncloudApp/src/main/java/com/owncloud/android/dependecyinjection/UseCaseModule.kt b/owncloudApp/src/main/java/com/owncloud/android/dependecyinjection/UseCaseModule.kt index 15666123522..f3dd35d990b 100644 --- a/owncloudApp/src/main/java/com/owncloud/android/dependecyinjection/UseCaseModule.kt +++ b/owncloudApp/src/main/java/com/owncloud/android/dependecyinjection/UseCaseModule.kt @@ -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 @@ -255,6 +256,7 @@ val useCaseModule = module { factoryOf(::GetUserAvatarAsyncUseCase) factoryOf(::GetUserInfoAsyncUseCase) factoryOf(::GetUserQuotasUseCase) + factoryOf(::GetUserQuotasAsStreamUseCase) factoryOf(::RefreshUserQuotaFromServerAsyncUseCase) // Server diff --git a/owncloudApp/src/main/java/com/owncloud/android/presentation/accounts/ManageAccountsDialogFragment.kt b/owncloudApp/src/main/java/com/owncloud/android/presentation/accounts/ManageAccountsDialogFragment.kt index e76660b6e66..b6d16bde3d5 100644 --- a/owncloudApp/src/main/java/com/owncloud/android/presentation/accounts/ManageAccountsDialogFragment.kt +++ b/owncloudApp/src/main/java/com/owncloud/android/presentation/accounts/ManageAccountsDialogFragment.kt @@ -69,6 +69,7 @@ class ManageAccountsDialogFragment : DialogFragment(), ManageAccountsAdapter.Acc parentActivity = requireActivity() as ToolbarActivity currentAccount = requireArguments().getParcelable(KEY_CURRENT_ACCOUNT) + subscribeToViewModels() } @@ -191,12 +192,7 @@ class ManageAccountsDialogFragment : DialogFragment(), ManageAccountsAdapter.Acc */ override fun run(future: AccountManagerFuture) { 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 @@ -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 diff --git a/owncloudApp/src/main/java/com/owncloud/android/presentation/accounts/ManageAccountsViewModel.kt b/owncloudApp/src/main/java/com/owncloud/android/presentation/accounts/ManageAccountsViewModel.kt index 0af65ad6e81..fe242aea383 100644 --- a/owncloudApp/src/main/java/com/owncloud/android/presentation/accounts/ManageAccountsViewModel.kt +++ b/owncloudApp/src/main/java/com/owncloud/android/presentation/accounts/ManageAccountsViewModel.kt @@ -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 @@ -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>?>(null) val cleanAccountLocalStorageFlow: StateFlow>?> = _cleanAccountLocalStorageFlow - private val _userQuotas = MutableStateFlow>(emptyList()) - val userQuotas: StateFlow> get() = _userQuotas + val userQuotas: Flow> = getUserQuotasAsStreamUseCase(Unit) private var automaticUploadsConfiguration: AutomaticUploadsConfiguration? = null init { viewModelScope.launch(coroutinesDispatcherProvider.io) { automaticUploadsConfiguration = getAutomaticUploadsConfigurationUseCase(Unit).getDataOrNull() - _userQuotas.value = getUserQuotasUseCase(Unit) } } diff --git a/owncloudData/src/main/java/com/owncloud/android/data/user/datasources/LocalUserDataSource.kt b/owncloudData/src/main/java/com/owncloud/android/data/user/datasources/LocalUserDataSource.kt index 5d6d56af417..90653ee66ca 100644 --- a/owncloudData/src/main/java/com/owncloud/android/data/user/datasources/LocalUserDataSource.kt +++ b/owncloudData/src/main/java/com/owncloud/android/data/user/datasources/LocalUserDataSource.kt @@ -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( @@ -33,6 +34,8 @@ interface LocalUserDataSource { fun getAllUserQuotas(): List + fun getAllUserQuotasAsStream(): Flow> + fun deleteQuotaForAccount( accountName: String ) diff --git a/owncloudData/src/main/java/com/owncloud/android/data/user/datasources/implementation/OCLocalUserDataSource.kt b/owncloudData/src/main/java/com/owncloud/android/data/user/datasources/implementation/OCLocalUserDataSource.kt index 2426181e03f..887a94efb16 100644 --- a/owncloudData/src/main/java/com/owncloud/android/data/user/datasources/implementation/OCLocalUserDataSource.kt +++ b/owncloudData/src/main/java/com/owncloud/android/data/user/datasources/implementation/OCLocalUserDataSource.kt @@ -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 @@ -45,6 +47,12 @@ class OCLocalUserDataSource( } } + override fun getAllUserQuotasAsStream(): Flow> { + return userDao.getAllUserQuotasAsStream().map { userQuotasList -> + userQuotasList.map { it.toModel() } + } + } + override fun deleteQuotaForAccount(accountName: String) { userDao.deleteQuotaForAccount(accountName = accountName) } diff --git a/owncloudData/src/main/java/com/owncloud/android/data/user/db/UserDao.kt b/owncloudData/src/main/java/com/owncloud/android/data/user/db/UserDao.kt index e64aa920fcb..c8c15b6c9c8 100644 --- a/owncloudData/src/main/java/com/owncloud/android/data/user/db/UserDao.kt +++ b/owncloudData/src/main/java/com/owncloud/android/data/user/db/UserDao.kt @@ -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 { @@ -35,6 +36,9 @@ interface UserDao { @Query(SELECT_ALL_QUOTAS) fun getAllUserQuotas(): List + @Query(SELECT_ALL_QUOTAS) + fun getAllUserQuotasAsStream(): Flow> + @Insert(onConflict = OnConflictStrategy.REPLACE) fun insertOrReplace(userQuotaEntity: UserQuotaEntity) diff --git a/owncloudData/src/main/java/com/owncloud/android/data/user/repository/OCUserRepository.kt b/owncloudData/src/main/java/com/owncloud/android/data/user/repository/OCUserRepository.kt index 5680f3ebd97..46c4c03e9f4 100644 --- a/owncloudData/src/main/java/com/owncloud/android/data/user/repository/OCUserRepository.kt +++ b/owncloudData/src/main/java/com/owncloud/android/data/user/repository/OCUserRepository.kt @@ -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, @@ -44,6 +45,9 @@ class OCUserRepository( override fun getAllUserQuotas(): List = localUserDataSource.getAllUserQuotas() + override fun getAllUserQuotasAsStream(): Flow> = + localUserDataSource.getAllUserQuotasAsStream() + override fun getUserAvatar(accountName: String): UserAvatar = remoteUserDataSource.getUserAvatar(accountName) } diff --git a/owncloudDomain/src/main/java/com/owncloud/android/domain/user/UserRepository.kt b/owncloudDomain/src/main/java/com/owncloud/android/domain/user/UserRepository.kt index 738b82ccdbf..007beb0baf2 100644 --- a/owncloudDomain/src/main/java/com/owncloud/android/domain/user/UserRepository.kt +++ b/owncloudDomain/src/main/java/com/owncloud/android/domain/user/UserRepository.kt @@ -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 + fun getAllUserQuotasAsStream(): Flow> fun getUserAvatar(accountName: String): UserAvatar } diff --git a/owncloudDomain/src/main/java/com/owncloud/android/domain/user/usecases/GetUserQuotasAsStreamUseCase.kt b/owncloudDomain/src/main/java/com/owncloud/android/domain/user/usecases/GetUserQuotasAsStreamUseCase.kt new file mode 100644 index 00000000000..3deb68b9664 --- /dev/null +++ b/owncloudDomain/src/main/java/com/owncloud/android/domain/user/usecases/GetUserQuotasAsStreamUseCase.kt @@ -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>, Unit>() { + override fun run(params: Unit): Flow> = + userRepository.getAllUserQuotasAsStream() +}