Updated on 2026-08-14

This commit is contained in:
Tangem 2024-07-11 18:09:00 +04:00
parent ca40e0cea6
commit 0d255aa2cf
3 changed files with 35 additions and 41 deletions

View file

@ -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<Boolean> = MutableStateFlow(value = false)
private val userWallets: SharedFlow<ImmutableList<UserWalletUM>> = userWalletsFetcher
.userWallets
.share()
private val shouldSaveUserWallets: SharedFlow<Boolean> = shouldSaveUserWalletsUseCase()
.distinctUntilChanged()
.share()
val state: MutableStateFlow<UserWalletListUM> = 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<UserWalletUM>,
shouldSaveUserWallets: Boolean,
isWalletSavingInProgress: Boolean,

View file

@ -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<ImmutableList<UserWalletUM>> = 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<ImmutableList<UserWalletUM>> = 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<UserWallet>,