diff --git a/app/src/main/java/com/tangem/tap/common/redux/global/GlobalState.kt b/app/src/main/java/com/tangem/tap/common/redux/global/GlobalState.kt index c9f2314efc..c4becfb45b 100644 --- a/app/src/main/java/com/tangem/tap/common/redux/global/GlobalState.kt +++ b/app/src/main/java/com/tangem/tap/common/redux/global/GlobalState.kt @@ -15,6 +15,7 @@ import com.tangem.tap.network.exchangeServices.CurrencyExchangeManager import org.rekotlin.StateType data class GlobalState( + @Deprecated("Use scan response from selected user wallet") val scanResponse: ScanResponse? = null, val onboardingState: OnboardingState = OnboardingState(), val cardVerifiedOnline: Boolean = false, diff --git a/app/src/main/java/com/tangem/tap/domain/userWalletList/implementation/BiometricUserWalletsListManager.kt b/app/src/main/java/com/tangem/tap/domain/userWalletList/implementation/BiometricUserWalletsListManager.kt index 78f74e0c3d..4ea180e270 100644 --- a/app/src/main/java/com/tangem/tap/domain/userWalletList/implementation/BiometricUserWalletsListManager.kt +++ b/app/src/main/java/com/tangem/tap/domain/userWalletList/implementation/BiometricUserWalletsListManager.kt @@ -118,7 +118,9 @@ internal class BiometricUserWalletsListManager( } .flatMap { updatedUserWallet -> saveInternal(updatedUserWallet, changeSelectedUserWallet = false) - .map { updatedUserWallet } + } + .flatMap { + get(userWalletId) } } diff --git a/app/src/main/java/com/tangem/tap/domain/walletStores/repository/implementation/DefaultWalletAmountsRepository.kt b/app/src/main/java/com/tangem/tap/domain/walletStores/repository/implementation/DefaultWalletAmountsRepository.kt index e276bd51db..22dd64bff1 100644 --- a/app/src/main/java/com/tangem/tap/domain/walletStores/repository/implementation/DefaultWalletAmountsRepository.kt +++ b/app/src/main/java/com/tangem/tap/domain/walletStores/repository/implementation/DefaultWalletAmountsRepository.kt @@ -7,16 +7,11 @@ import com.tangem.blockchain.common.Wallet import com.tangem.blockchain.common.WalletManager import com.tangem.blockchain.extensions.Result.Failure import com.tangem.blockchain.extensions.Result.Success -import com.tangem.common.CompletionResult -import com.tangem.common.catching +import com.tangem.common.* import com.tangem.common.core.TangemError -import com.tangem.common.flatMap -import com.tangem.common.flatMapOnFailure -import com.tangem.common.fold -import com.tangem.common.map import com.tangem.datasource.api.tangemTech.TangemTechApi -import com.tangem.domain.common.util.hasDerivation import com.tangem.domain.common.util.UserWalletId +import com.tangem.domain.common.util.hasDerivation import com.tangem.domain.models.scan.ScanResponse import com.tangem.tap.common.TestActions import com.tangem.tap.common.entities.FiatCurrency @@ -25,15 +20,7 @@ import com.tangem.tap.domain.model.UserWallet import com.tangem.tap.domain.model.WalletStoreModel import com.tangem.tap.domain.walletStores.WalletStoresError import com.tangem.tap.domain.walletStores.repository.WalletAmountsRepository -import com.tangem.tap.domain.walletStores.repository.implementation.utils.replaceWalletStore -import com.tangem.tap.domain.walletStores.repository.implementation.utils.replaceWalletStores -import com.tangem.tap.domain.walletStores.repository.implementation.utils.updateWithAmounts -import com.tangem.tap.domain.walletStores.repository.implementation.utils.updateWithDemoAmounts -import com.tangem.tap.domain.walletStores.repository.implementation.utils.updateWithError -import com.tangem.tap.domain.walletStores.repository.implementation.utils.updateWithFiatRates -import com.tangem.tap.domain.walletStores.repository.implementation.utils.updateWithMissedDerivation -import com.tangem.tap.domain.walletStores.repository.implementation.utils.updateWithRent -import com.tangem.tap.domain.walletStores.repository.implementation.utils.updateWithUnreachable +import com.tangem.tap.domain.walletStores.repository.implementation.utils.* import com.tangem.tap.domain.walletStores.storage.WalletManagerStorage import com.tangem.tap.domain.walletStores.storage.WalletStoresStorage import com.tangem.tap.features.demo.DemoHelper @@ -45,13 +32,8 @@ import com.tangem.tap.features.wallet.models.getPendingTransactions import com.tangem.tap.proxy.redux.DaggerGraphState import com.tangem.tap.store import com.tangem.utils.coroutines.CoroutineDispatcherProvider -import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.async -import kotlinx.coroutines.awaitAll -import kotlinx.coroutines.coroutineScope -import kotlinx.coroutines.delay +import kotlinx.coroutines.* import kotlinx.coroutines.flow.firstOrNull -import kotlinx.coroutines.withContext import timber.log.Timber import java.math.BigDecimal import kotlin.time.Duration @@ -200,37 +182,40 @@ internal class DefaultWalletAmountsRepository( walletStore: WalletStoreModel, walletManager: WalletManager?, ): CompletionResult { - val hasMissedDerivations = with(walletStore) { + val isDerivationMissed = with(walletStore) { derivationPath != null && !scanResponse.hasDerivation(blockchain, derivationPath.rawPath) } return when { - hasMissedDerivations -> { + isDerivationMissed -> { updateWalletStoreWithMissedDerivation(walletStore) } walletManager == null -> { updateWalletStoreWithUnreachable(walletStore) } else -> { - updateWalletManager(scanResponse, walletManager).map { - updateWalletManagerInStorage( - userWalletId, - walletManager, - ) - }.flatMap { - updateWalletStoreWithAmounts( - walletStore = walletStore, - updatedWallet = walletManager.wallet, - // FIXME: move DemoHelper to Demo core module maybe - isDemo = DemoHelper.isDemoCardId(scanResponse.card.cardId), - ) - }.flatMap { fetchWalletStoreRentIfNeeded(walletStore, walletManager) }.flatMapOnFailure { error -> - updateWalletStoreWithError( - walletStore = walletStore, - wallet = walletManager.wallet, - error = error, - ) - } + updateWalletManager(scanResponse, walletManager) + .map { + updateWalletManagerInStorage(userWalletId, walletManager) + } + .flatMap { + updateWalletStoreWithAmounts( + walletStore = walletStore, + updatedWallet = walletManager.wallet, + // FIXME: move DemoHelper to Demo core module maybe + isDemo = DemoHelper.isDemoCardId(scanResponse.card.cardId), + ) + } + .flatMap { + fetchWalletStoreRentIfNeeded(walletStore, walletManager) + } + .flatMapOnFailure { error -> + updateWalletStoreWithError( + walletStore = walletStore, + wallet = walletManager.wallet, + error = error, + ) + } } } } diff --git a/app/src/main/java/com/tangem/tap/features/wallet/redux/middlewares/MultiWalletMiddleware.kt b/app/src/main/java/com/tangem/tap/features/wallet/redux/middlewares/MultiWalletMiddleware.kt index 607d8c63c4..d86a41c389 100644 --- a/app/src/main/java/com/tangem/tap/features/wallet/redux/middlewares/MultiWalletMiddleware.kt +++ b/app/src/main/java/com/tangem/tap/features/wallet/redux/middlewares/MultiWalletMiddleware.kt @@ -9,7 +9,7 @@ import com.tangem.tap.common.analytics.events.Token.ButtonRemoveToken import com.tangem.tap.common.extensions.addContext import com.tangem.tap.common.extensions.dispatchDialogShow import com.tangem.tap.common.extensions.dispatchErrorNotification -import com.tangem.tap.common.extensions.dispatchOnMain +import com.tangem.tap.common.extensions.dispatchWithMain import com.tangem.tap.common.redux.global.GlobalAction import com.tangem.tap.common.redux.navigation.AppScreen import com.tangem.tap.common.redux.navigation.NavigationAction @@ -114,7 +114,8 @@ class MultiWalletMiddleware { ) } .doOnSuccess { updatedUserWallet -> - store.dispatchOnMain(WalletAction.MultiWallet.AddMissingDerivations(emptyList())) + store.dispatchWithMain(WalletAction.MultiWallet.AddMissingDerivations(emptyList())) + store.dispatchWithMain(GlobalAction.SaveScanResponse(updatedUserWallet.scanResponse)) store.state.globalState.tapWalletManager.loadData(updatedUserWallet, refresh = true) } } diff --git a/app/src/main/java/com/tangem/tap/features/wallet/redux/middlewares/WalletMiddleware.kt b/app/src/main/java/com/tangem/tap/features/wallet/redux/middlewares/WalletMiddleware.kt index c7bde94141..d7a0da7703 100644 --- a/app/src/main/java/com/tangem/tap/features/wallet/redux/middlewares/WalletMiddleware.kt +++ b/app/src/main/java/com/tangem/tap/features/wallet/redux/middlewares/WalletMiddleware.kt @@ -17,6 +17,7 @@ import com.tangem.tap.common.analytics.events.MainScreen import com.tangem.tap.common.analytics.events.Token import com.tangem.tap.common.extensions.* import com.tangem.tap.common.redux.AppState +import com.tangem.tap.common.redux.global.GlobalAction import com.tangem.tap.common.redux.navigation.AppScreen import com.tangem.tap.common.redux.navigation.NavigationAction import com.tangem.tap.domain.TapError @@ -97,12 +98,12 @@ class WalletMiddleware { Timber.e("Unable to create wallet, no user wallet selected") return@launch } + val updatedScanResponse = selectedUserWallet.scanResponse.copy( + card = result.data, + ) + store.dispatchWithMain(GlobalAction.SaveScanResponse(updatedScanResponse)) userWalletsListManager.update(selectedUserWallet.walletId) { userWallet -> - userWallet.copy( - scanResponse = userWallet.scanResponse.copy( - card = result.data, - ), - ) + userWallet.copy(scanResponse = updatedScanResponse) } } is CompletionResult.Failure -> Unit diff --git a/app/src/main/java/com/tangem/tap/proxy/AppStateHolder.kt b/app/src/main/java/com/tangem/tap/proxy/AppStateHolder.kt index aab3cfd808..35c2d83718 100644 --- a/app/src/main/java/com/tangem/tap/proxy/AppStateHolder.kt +++ b/app/src/main/java/com/tangem/tap/proxy/AppStateHolder.kt @@ -19,6 +19,7 @@ import javax.inject.Inject */ class AppStateHolder @Inject constructor() { + @Deprecated("Use scan response from selected user wallet") var scanResponse: ScanResponse? = null var walletState: WalletState? = null var userTokensRepository: UserTokensRepository? = null