diff --git a/features/details/impl/src/main/kotlin/com/tangem/features/details/model/UserWalletListModel.kt b/features/details/impl/src/main/kotlin/com/tangem/features/details/model/UserWalletListModel.kt index 202758486c..12f18cc1f4 100644 --- a/features/details/impl/src/main/kotlin/com/tangem/features/details/model/UserWalletListModel.kt +++ b/features/details/impl/src/main/kotlin/com/tangem/features/details/model/UserWalletListModel.kt @@ -13,27 +13,22 @@ import com.tangem.features.details.utils.UserWalletsFetcher import com.tangem.utils.coroutines.CoroutineDispatcherProvider import kotlinx.collections.immutable.ImmutableList import kotlinx.collections.immutable.persistentListOf -import kotlinx.coroutines.flow.* +import kotlinx.coroutines.flow.MutableStateFlow +import kotlinx.coroutines.flow.combine +import kotlinx.coroutines.flow.launchIn +import kotlinx.coroutines.flow.update import javax.inject.Inject @ComponentScoped internal class UserWalletListModel @Inject constructor( - private val shouldSaveUserWalletsUseCase: ShouldSaveUserWalletsUseCase, + userWalletsFetcher: UserWalletsFetcher, + shouldSaveUserWalletsUseCase: ShouldSaveUserWalletsUseCase, private val userWalletSaver: UserWalletSaver, - private val userWalletsFetcher: UserWalletsFetcher, override val dispatchers: CoroutineDispatcherProvider, ) : Model() { private val isWalletSavingInProgress: MutableStateFlow = MutableStateFlow(value = false) - private val userWallets: SharedFlow> = userWalletsFetcher - .userWallets - .share() - - private val shouldSaveUserWallets: SharedFlow = shouldSaveUserWalletsUseCase() - .distinctUntilChanged() - .share() - val state: MutableStateFlow = MutableStateFlow( value = UserWalletListUM( userWallets = persistentListOf(), @@ -45,14 +40,14 @@ internal class UserWalletListModel @Inject constructor( init { combine( - userWallets, - shouldSaveUserWallets, + userWalletsFetcher.userWallets, + shouldSaveUserWalletsUseCase(), isWalletSavingInProgress, transform = ::updateState, ).launchIn(modelScope) } - private suspend fun updateState( + private fun updateState( userWallets: ImmutableList, shouldSaveUserWallets: Boolean, isWalletSavingInProgress: Boolean, diff --git a/features/details/impl/src/main/kotlin/com/tangem/features/details/utils/UserWalletsFetcher.kt b/features/details/impl/src/main/kotlin/com/tangem/features/details/utils/UserWalletsFetcher.kt index e606030df5..5377be02ea 100644 --- a/features/details/impl/src/main/kotlin/com/tangem/features/details/utils/UserWalletsFetcher.kt +++ b/features/details/impl/src/main/kotlin/com/tangem/features/details/utils/UserWalletsFetcher.kt @@ -23,44 +23,43 @@ import com.tangem.domain.wallets.usecase.GetWalletsUseCase import com.tangem.features.details.entity.UserWalletListUM.UserWalletUM import com.tangem.features.details.impl.R import kotlinx.collections.immutable.ImmutableList -import kotlinx.coroutines.flow.* +import kotlinx.coroutines.ExperimentalCoroutinesApi +import kotlinx.coroutines.flow.Flow +import kotlinx.coroutines.flow.collect +import kotlinx.coroutines.flow.combine +import kotlinx.coroutines.flow.transformLatest import javax.inject.Inject @ComponentScoped internal class UserWalletsFetcher @Inject constructor( - private val getWalletsUseCase: GetWalletsUseCase, + getWalletsUseCase: GetWalletsUseCase, private val getWalletTotalBalanceUseCase: GetWalletTotalBalanceUseCase, private val getSelectedAppCurrencyUseCase: GetSelectedAppCurrencyUseCase, private val router: Router, private val messageSender: UiMessageSender, ) { - val userWallets: Flow> = getWalletsUseCase() - .distinctUntilChanged() - .transform { wallets -> - if (wallets.isEmpty()) { - error("Wallets must not be empty") - } else { - emit(wallets.toUiModels(onClick = ::navigateToWalletSettings)) - } + @OptIn(ExperimentalCoroutinesApi::class) + val userWallets: Flow> = getWalletsUseCase().transformLatest { wallets -> + emit(wallets.toUiModels(onClick = ::navigateToWalletSettings)) - combine( - getSelectedAppCurrencyUseCase(), - getWalletTotalBalanceUseCase(wallets.map(UserWallet::walletId)), - ) { maybeAppCurrency, maybeBalances -> - val models = createUiModels(wallets, maybeAppCurrency, maybeBalances).getOrElse( - ifLoading = { return@combine }, - ifError = { - val message = resourceReference(R.string.common_unknown_error) - messageSender.send(SnackbarMessage(message)) + combine( + getSelectedAppCurrencyUseCase(), + getWalletTotalBalanceUseCase(wallets.map(UserWallet::walletId)), + ) { maybeAppCurrency, maybeBalances -> + val models = createUiModels(wallets, maybeAppCurrency, maybeBalances).getOrElse( + ifLoading = { return@combine }, + ifError = { + val message = resourceReference(R.string.common_unknown_error) + messageSender.send(SnackbarMessage(message)) - return@combine - }, - ) + return@combine + }, + ) - emit(models) - }.collect() - } + emit(models) + }.collect() + } private fun createUiModels( wallets: List, diff --git a/features/wallet-settings/impl/src/main/kotlin/com/tangem/feature/walletsettings/model/WalletSettingsModel.kt b/features/wallet-settings/impl/src/main/kotlin/com/tangem/feature/walletsettings/model/WalletSettingsModel.kt index 7926ce7f87..188a3ba043 100644 --- a/features/wallet-settings/impl/src/main/kotlin/com/tangem/feature/walletsettings/model/WalletSettingsModel.kt +++ b/features/wallet-settings/impl/src/main/kotlin/com/tangem/feature/walletsettings/model/WalletSettingsModel.kt @@ -31,12 +31,12 @@ import javax.inject.Inject @Suppress("LongParameterList") @ComponentScoped internal class WalletSettingsModel @Inject constructor( + getWalletUseCase: GetUserWalletUseCase, + paramsContainer: ParamsContainer, private val router: Router, private val messageSender: UiMessageSender, - private val getWalletUseCase: GetUserWalletUseCase, private val deleteWalletUseCase: DeleteWalletUseCase, private val itemsBuilder: ItemsBuilder, - private val paramsContainer: ParamsContainer, override val dispatchers: CoroutineDispatcherProvider, ) : Model() {