Updated on 2026-08-14

This commit is contained in:
Tangem 2024-05-17 18:53:42 +03:00
parent 9868dd2bc2
commit a3295ea181
12 changed files with 123 additions and 107 deletions

View file

@ -5,7 +5,6 @@ import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.tangem.core.analytics.api.AnalyticsEventHandler
import com.tangem.core.navigation.AppScreen
import com.tangem.domain.balancehiding.GetBalanceHidingSettingsUseCase
import com.tangem.domain.settings.CanUseBiometryUseCase
import com.tangem.domain.settings.IsWalletsScrollPreviewEnabled
@ -32,10 +31,11 @@ import com.tangem.utils.coroutines.CoroutineDispatcherProvider
import com.tangem.utils.coroutines.JobHolder
import com.tangem.utils.coroutines.saveIn
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.*
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.*
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import timber.log.Timber
import javax.inject.Inject
@Suppress("LongParameterList")
@ -197,9 +197,9 @@ internal class WalletViewModel @Inject constructor(
is WalletsUpdateActionResolver.Action.UpdateWalletName -> {
stateHolder.update(transformer = RenameWalletTransformer(action.selectedWalletId, action.name))
}
is WalletsUpdateActionResolver.Action.NoAccessibleWallets -> closeScreen(screen = AppScreen.Welcome)
is WalletsUpdateActionResolver.Action.NoWallets -> closeScreen(screen = AppScreen.Home)
is WalletsUpdateActionResolver.Action.Unknown -> Unit
is WalletsUpdateActionResolver.Action.Unknown -> {
Timber.w("Unable to perfom action: $action")
}
}
}
@ -313,13 +313,6 @@ internal class WalletViewModel @Inject constructor(
)
}
private fun closeScreen(screen: AppScreen) {
if (!screenLifecycleProvider.isBackgroundState.value) {
stateHolder.clear()
router.popBackStack(screen = screen)
}
}
private fun scrollToWallet(index: Int, onConsume: () -> Unit = {}) {
stateHolder.update(
ScrollToWalletTransformer(

View file

@ -1,5 +1,6 @@
package com.tangem.feature.wallet.presentation.wallet.viewmodels
import arrow.core.getOrElse
import com.tangem.domain.wallets.models.UserWallet
import com.tangem.domain.wallets.models.UserWalletId
import com.tangem.domain.wallets.usecase.GetSelectedWalletSyncUseCase
@ -23,16 +24,14 @@ internal class WalletsUpdateActionResolver @Inject constructor(
) {
fun resolve(wallets: List<UserWallet>, currentState: WalletScreenState): Action {
val selectedWallet = wallets.getSelectedWallet()
val selectedWallet = getSelectedWalletSyncUseCase().getOrElse {
error("Unable to find selected wallet: $it")
}
val action = if (selectedWallet == null) {
createNoSelectedWalletAction(wallets)
val action = if (isFirstInitialization(currentState)) {
createInitializeWalletsAction(wallets, selectedWallet)
} else {
if (isFirstInitialization(currentState)) {
createInitializeWalletsAction(wallets, selectedWallet)
} else {
getUpdateContentAction(currentState, wallets, selectedWallet)
}
getUpdateContentAction(currentState, wallets, selectedWallet)
}
Timber.d("Resolved action: $action")
@ -40,22 +39,6 @@ internal class WalletsUpdateActionResolver @Inject constructor(
return action
}
private fun List<UserWallet>.getSelectedWallet(): UserWallet? {
return when {
isEmpty() -> null
size == 1 -> if (first().isLocked) null else first()
else -> getSelectedWalletSyncUseCase().fold(ifLeft = { null }, ifRight = { it })
}
}
private fun createNoSelectedWalletAction(wallets: List<UserWallet>): Action {
return when {
wallets.isEmpty() -> Action.NoWallets
wallets.all(UserWallet::isLocked) -> Action.NoAccessibleWallets
else -> Action.Unknown
}
}
private fun isFirstInitialization(state: WalletScreenState): Boolean {
return state.selectedWalletIndex == NOT_INITIALIZED_WALLET_INDEX
}
@ -289,10 +272,6 @@ internal class WalletsUpdateActionResolver @Inject constructor(
}
}
data object NoAccessibleWallets : Action()
data object NoWallets : Action()
data object Unknown : Action()
}
}

View file

@ -1,7 +1,11 @@
package com.tangem.feature.wallet.presentation.wallet.viewmodels.intents
import arrow.core.getOrElse
import com.tangem.core.analytics.api.AnalyticsEventHandler
import com.tangem.domain.card.DeleteSavedAccessCodesUseCase
import com.tangem.core.navigation.AppScreen
import com.tangem.core.navigation.NavigationAction
import com.tangem.core.navigation.ReduxNavController
import com.tangem.domain.redux.ReduxStateHolder
import com.tangem.domain.wallets.models.UserWalletId
import com.tangem.domain.wallets.usecase.DeleteWalletUseCase
@ -43,6 +47,7 @@ internal class WalletCardClickIntentsImplementor @Inject constructor(
private val deleteSavedAccessCodesUseCase: DeleteSavedAccessCodesUseCase,
private val analyticsEventHandler: AnalyticsEventHandler,
private val reduxStateHolder: ReduxStateHolder,
private val reduxNavController: ReduxNavController,
private val dispatchers: CoroutineDispatcherProvider,
) : BaseWalletClickIntents(), WalletCardClickIntents {
@ -81,18 +86,26 @@ internal class WalletCardClickIntentsImplementor @Inject constructor(
viewModelScope.launch(dispatchers.main) {
walletScreenContentLoader.cancel(userWalletId)
val deletedUserWallet = getUserWalletUseCase(userWalletId).getOrNull() ?: return@launch
val walletToDelete = getUserWalletUseCase(userWalletId).getOrNull() ?: return@launch
val hasUserWallets = deleteWalletUseCase(userWalletId).getOrElse {
Timber.e("Unable to delete user wallet: $it")
return@launch
}
deleteSavedAccessCodesUseCase(cardId = deletedUserWallet.cardId)
.onLeft { Timber.e(it.toString()) }
deleteSavedAccessCodesUseCase(cardId = walletToDelete.cardId).onLeft {
Timber.e("Unable to delete user wallet access code: $it")
}
deleteWalletUseCase(userWalletId)
.onRight {
getSelectedWalletSyncUseCase().getOrNull()?.let {
reduxStateHolder.onUserWalletSelected(it)
}
if (hasUserWallets) {
val selectedWallet = getSelectedWalletSyncUseCase().getOrElse {
error("Unable to find selected wallet: $it")
}
.onLeft { Timber.e(it.toString()) }
reduxStateHolder.onUserWalletSelected(selectedWallet)
} else {
stateHolder.clear()
reduxNavController.navigate(NavigationAction.PopBackTo(AppScreen.Home))
}
}
}
}