diff --git a/app/src/main/java/com/tangem/tap/features/tokens/legacy/redux/TokensMiddleware.kt b/app/src/main/java/com/tangem/tap/features/tokens/legacy/redux/TokensMiddleware.kt index ceeefb7c60..7f8d65c2d5 100644 --- a/app/src/main/java/com/tangem/tap/features/tokens/legacy/redux/TokensMiddleware.kt +++ b/app/src/main/java/com/tangem/tap/features/tokens/legacy/redux/TokensMiddleware.kt @@ -16,7 +16,6 @@ import com.tangem.domain.models.scan.ScanResponse import com.tangem.domain.tokens.AddCryptoCurrenciesUseCase import com.tangem.domain.tokens.TokensAction import com.tangem.domain.tokens.model.CryptoCurrency -import com.tangem.domain.walletconnect.WalletConnectActions import com.tangem.domain.wallets.models.UserWallet import com.tangem.domain.wallets.models.UserWalletId import com.tangem.operations.derivation.ExtendedPublicKeysMap @@ -202,9 +201,7 @@ object TokensMiddleware { userWalletId = userWallet.walletId, update = { it.copy(scanResponse = updatedScanResponse) }, ).doOnSuccess { - addCryptoCurrenciesUseCase(userWallet.walletId, currencyList).onRight { - store.dispatch(action = WalletConnectActions.New.SetupUserChains(userWallet = userWallet)) - } + addCryptoCurrenciesUseCase(userWallet.walletId, currencyList) } } store.dispatchOnMain(NavigationAction.PopBackTo()) diff --git a/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/domain/GetMultiWalletWarningsFactory.kt b/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/domain/GetMultiWalletWarningsFactory.kt index 83355647bd..574705c9f4 100644 --- a/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/domain/GetMultiWalletWarningsFactory.kt +++ b/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/domain/GetMultiWalletWarningsFactory.kt @@ -52,10 +52,7 @@ internal class GetMultiWalletWarningsFactory @Inject constructor( flow = getTokenListUseCase(userWallet.walletId).conflate(), flow2 = isReadyToShowRateAppUseCase().conflate(), flow3 = isNeedToBackupUseCase(userWallet.walletId).conflate(), - // flow4 = getMissedAddressCryptoCurrenciesUseCase(userWallet.walletId).conflate(), ) { maybeTokenList, isReadyToShowRating, isNeedToBackup -> - // maybeTokenList.onRight { Timber.e(it.toString()) } - // maybeMissedAddressCurrencies.onRight { Timber.e(it.toString()) } readyForRateAppNotification = true buildList { addCriticalNotifications(cardTypesResolver) diff --git a/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/loaders/implementors/MultiWalletContentLoader.kt b/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/loaders/implementors/MultiWalletContentLoader.kt index 4f27285be7..7f988fc301 100644 --- a/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/loaders/implementors/MultiWalletContentLoader.kt +++ b/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/loaders/implementors/MultiWalletContentLoader.kt @@ -1,6 +1,7 @@ package com.tangem.feature.wallet.presentation.wallet.loaders.implementors import com.tangem.domain.appcurrency.model.AppCurrency +import com.tangem.domain.redux.ReduxStateHolder import com.tangem.domain.tokens.GetTokenListUseCase import com.tangem.domain.wallets.models.UserWallet import com.tangem.feature.wallet.presentation.wallet.analytics.utils.TokenListAnalyticsSender @@ -9,6 +10,7 @@ import com.tangem.feature.wallet.presentation.wallet.domain.WalletWithFundsCheck import com.tangem.feature.wallet.presentation.wallet.state2.WalletStateController import com.tangem.feature.wallet.presentation.wallet.subscribers.MultiWalletWarningsSubscriber import com.tangem.feature.wallet.presentation.wallet.subscribers.TokenListSubscriber +import com.tangem.feature.wallet.presentation.wallet.subscribers.WalletConnectNetworksSubscriber import com.tangem.feature.wallet.presentation.wallet.subscribers.WalletSubscriber import com.tangem.feature.wallet.presentation.wallet.viewmodels.intents.WalletClickIntentsV2 @@ -22,6 +24,7 @@ internal class MultiWalletContentLoader( private val walletWithFundsChecker: WalletWithFundsChecker, private val getTokenListUseCase: GetTokenListUseCase, private val getMultiWalletWarningsFactory: GetMultiWalletWarningsFactory, + private val reduxStateHolder: ReduxStateHolder, ) : WalletContentLoader(id = userWallet.walletId) { override fun create(): List { @@ -41,6 +44,11 @@ internal class MultiWalletContentLoader( clickIntents = clickIntents, getMultiWalletWarningsFactory = getMultiWalletWarningsFactory, ), + WalletConnectNetworksSubscriber( + userWallet = userWallet, + getTokenListUseCase = getTokenListUseCase, + reduxStateHolder = reduxStateHolder, + ), ) } } \ No newline at end of file diff --git a/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/loaders/implementors/MultiWalletContentLoaderFactory.kt b/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/loaders/implementors/MultiWalletContentLoaderFactory.kt index 05a888b92c..199162d4ed 100644 --- a/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/loaders/implementors/MultiWalletContentLoaderFactory.kt +++ b/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/loaders/implementors/MultiWalletContentLoaderFactory.kt @@ -1,6 +1,7 @@ package com.tangem.feature.wallet.presentation.wallet.loaders.implementors import com.tangem.domain.appcurrency.model.AppCurrency +import com.tangem.domain.redux.ReduxStateHolder import com.tangem.domain.tokens.GetTokenListUseCase import com.tangem.domain.wallets.models.UserWallet import com.tangem.feature.wallet.presentation.wallet.analytics.utils.TokenListAnalyticsSender @@ -18,6 +19,7 @@ internal class MultiWalletContentLoaderFactory @Inject constructor( private val tokenListAnalyticsSender: TokenListAnalyticsSender, private val walletWithFundsChecker: WalletWithFundsChecker, private val getMultiWalletWarningsFactory: GetMultiWalletWarningsFactory, + private val reduxStateHolder: ReduxStateHolder, ) { fun create( @@ -34,6 +36,7 @@ internal class MultiWalletContentLoaderFactory @Inject constructor( walletWithFundsChecker = walletWithFundsChecker, getTokenListUseCase = getTokenListUseCase, getMultiWalletWarningsFactory = getMultiWalletWarningsFactory, + reduxStateHolder = reduxStateHolder, ) } } \ No newline at end of file diff --git a/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/subscribers/WalletConnectNetworksSubscriber.kt b/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/subscribers/WalletConnectNetworksSubscriber.kt new file mode 100644 index 0000000000..420a93771a --- /dev/null +++ b/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/subscribers/WalletConnectNetworksSubscriber.kt @@ -0,0 +1,80 @@ +package com.tangem.feature.wallet.presentation.wallet.subscribers + +import arrow.core.Either +import com.tangem.domain.redux.ReduxStateHolder +import com.tangem.domain.tokens.GetTokenListUseCase +import com.tangem.domain.tokens.error.TokenListError +import com.tangem.domain.tokens.model.CryptoCurrencyStatus +import com.tangem.domain.tokens.model.NetworkGroup +import com.tangem.domain.tokens.model.TokenList +import com.tangem.domain.walletconnect.WalletConnectActions +import com.tangem.domain.wallets.models.UserWallet +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.flow.* +import kotlinx.coroutines.sync.Mutex +import kotlinx.coroutines.sync.withLock +import timber.log.Timber + +/** + * WalletConnect networks subscriber. Update WalletConnect networks for a specified [userWallet]. + * + * @property userWallet user wallet + * @property getTokenListUseCase use case for subscribing on token list changes + * @property reduxStateHolder redux state holder + * +[REDACTED_AUTHOR] + */ +internal class WalletConnectNetworksSubscriber( + private val userWallet: UserWallet, + private val getTokenListUseCase: GetTokenListUseCase, + private val reduxStateHolder: ReduxStateHolder, +) : WalletSubscriber() { + + private val mutex = Mutex() + + override fun create(coroutineScope: CoroutineScope): Flow> { + return getTokenListUseCase(userWalletId = userWallet.walletId) + .conflate() + .distinctUntilCurrenciesChanged() + .filterLoadedTokens() + .onEach { + mutex.withLock { + Timber.d("WalletConnect: ${userWallet.walletId} networks is updated") + + reduxStateHolder.dispatch( + action = WalletConnectActions.New.SetupUserChains(userWallet = userWallet), + ) + } + } + } + + private fun MaybeTokenListFlow.distinctUntilCurrenciesChanged(): MaybeTokenListFlow { + return distinctUntilChanged { old, new -> + val oldCurrencies = old.fold(ifLeft = { null }, ifRight = { it.getCryptoCurrencies() }) + val newCurrencies = new.fold(ifLeft = { null }, ifRight = { it.getCryptoCurrencies() }) + + oldCurrencies == newCurrencies + } + } + + private fun MaybeTokenListFlow.filterLoadedTokens(): MaybeTokenListFlow { + return filter { either -> + either.fold( + ifLeft = { false }, + ifRight = { it.getCryptoCurrencies().isAllCurrenciesLoaded() }, + ) + } + } + + private fun TokenList.getCryptoCurrencies(): List { + return when (this) { + is TokenList.Ungrouped -> currencies + is TokenList.GroupedByNetwork -> groups.flatMap(NetworkGroup::currencies) + else -> emptyList() + } + } + + private fun List.isAllCurrenciesLoaded(): Boolean { + return none { it.value is CryptoCurrencyStatus.Loading } + } +} \ No newline at end of file diff --git a/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/viewmodels/WalletViewModelV2.kt b/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/viewmodels/WalletViewModelV2.kt index d4051c9756..998d145ac0 100644 --- a/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/viewmodels/WalletViewModelV2.kt +++ b/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/viewmodels/WalletViewModelV2.kt @@ -34,6 +34,7 @@ import kotlinx.coroutines.delay import kotlinx.coroutines.flow.* import kotlinx.coroutines.launch import kotlinx.coroutines.withContext +import timber.log.Timber import javax.inject.Inject import kotlin.properties.Delegates @@ -142,9 +143,15 @@ internal class WalletViewModelV2 @Inject constructor( .distinctUntilChanged() .onEach { selectedWallet -> if (selectedWallet.isMultiCurrency) { + Timber.d("WalletConnect: initialize and setup networks for ${selectedWallet.walletId}") + reduxStateHolder.dispatch( action = WalletConnectActions.New.Initialize(userWallet = selectedWallet), ) + + reduxStateHolder.dispatch( + action = WalletConnectActions.New.SetupUserChains(userWallet = selectedWallet), + ) } } .flowOn(dispatchers.main) diff --git a/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/viewmodels/intents/WalletCurrencyActionsClickIntents.kt b/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/viewmodels/intents/WalletCurrencyActionsClickIntents.kt index 192ff6d7ab..995e0ab13e 100644 --- a/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/viewmodels/intents/WalletCurrencyActionsClickIntents.kt +++ b/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/viewmodels/intents/WalletCurrencyActionsClickIntents.kt @@ -23,7 +23,6 @@ import com.tangem.domain.tokens.model.CryptoCurrencyStatus import com.tangem.domain.tokens.model.NetworkAddress import com.tangem.domain.tokens.models.analytics.TokenReceiveAnalyticsEvent import com.tangem.domain.tokens.models.analytics.TokenScreenAnalyticsEvent -import com.tangem.domain.walletconnect.WalletConnectActions import com.tangem.domain.walletmanager.WalletManagersFacade import com.tangem.domain.wallets.models.UserWallet import com.tangem.domain.wallets.models.UserWalletId @@ -252,12 +251,6 @@ internal class WalletCurrencyActionsClickIntentsImplementor @Inject constructor( ) }, ifRight = { - getSelectedWalletSyncUseCase.unwrap()?.let { userWallet -> - reduxStateHolder.dispatch( - action = WalletConnectActions.New.SetupUserChains(userWallet = userWallet), - ) - } - stateHolder.update(CloseBottomSheetTransformer(userWalletId = userWalletId)) }, )