diff --git a/app/src/main/java/com/tangem/tap/MainActivity.kt b/app/src/main/java/com/tangem/tap/MainActivity.kt index 52e9ff1262..efa00b067f 100644 --- a/app/src/main/java/com/tangem/tap/MainActivity.kt +++ b/app/src/main/java/com/tangem/tap/MainActivity.kt @@ -129,6 +129,8 @@ class MainActivity : AppCompatActivity(), SnackbarHandler, ActivityResultCallbac super.onCreate(savedInstanceState) + cardSdkLifecycleObserver.onCreate(context = this) + bootstrapMainStateUpdates() splashScreen.setKeepOnScreenCondition { isInitializing } @@ -138,8 +140,6 @@ class MainActivity : AppCompatActivity(), SnackbarHandler, ActivityResultCallbac store.dispatch(NavigationAction.ActivityCreated(WeakReference(this))) - cardSdkLifecycleObserver.onCreate(context = this) - tangemSdkManager = injectedTangemSdkManager appStateHolder.tangemSdkManager = tangemSdkManager backupService = BackupService.init(cardSdkConfigRepository.sdk, this) diff --git a/app/src/main/java/com/tangem/tap/features/customtoken/impl/domain/DefaultCustomTokenInteractor.kt b/app/src/main/java/com/tangem/tap/features/customtoken/impl/domain/DefaultCustomTokenInteractor.kt index fe1bb63d2a..7071f8c24d 100644 --- a/app/src/main/java/com/tangem/tap/features/customtoken/impl/domain/DefaultCustomTokenInteractor.kt +++ b/app/src/main/java/com/tangem/tap/features/customtoken/impl/domain/DefaultCustomTokenInteractor.kt @@ -71,9 +71,9 @@ class DefaultCustomTokenInteractor( onSuccess: suspend (ScanResponse) -> Unit, ) { val config = CardConfig.createConfig(scanResponse.card) - val derivationDataList = currencyList.mapNotNull { - val curve = config.primaryCurve(it.blockchain) - curve?.let { getDerivations(curve, scanResponse, currencyList) } + val derivationDataList = currencyList.mapNotNull { currency -> + val curve = config.primaryCurve(currency.blockchain) + curve?.let { getDerivations(curve, scanResponse, currency) } } val derivations = derivationDataList.associate(TokensMiddleware.DerivationData::derivations) @@ -110,24 +110,25 @@ class DefaultCustomTokenInteractor( private fun getDerivations( curve: EllipticCurve, scanResponse: ScanResponse, - currencyList: List, + currency: Currency, ): TokensMiddleware.DerivationData? { val wallet = scanResponse.card.wallets.firstOrNull { it.curve == curve } ?: return null - val manageTokensCandidates = currencyList.map { it.blockchain }.distinct().filter { - it.getSupportedCurves().contains(curve) - }.mapNotNull { - it.derivationPath(scanResponse.derivationStyleProvider.getDerivationStyle()) - } + val supportedCurves = currency.blockchain.getSupportedCurves() + val path = currency.derivationPath + ?.let { + currency.blockchain.derivationPath(scanResponse.derivationStyleProvider.getDerivationStyle()) + } + .takeIf { supportedCurves.contains(curve) } - val customTokensCandidates = currencyList.filter { - it.blockchain.getSupportedCurves().contains(curve) - }.mapNotNull { it.derivationPath }.map { DerivationPath(it) } + val customPath = currency.derivationPath?.let { + DerivationPath(it) + }.takeIf { supportedCurves.contains(curve) } - val bothCandidates = (manageTokensCandidates + customTokensCandidates).distinct().toMutableList() + val bothCandidates = listOfNotNull(path, customPath).distinct().toMutableList() if (bothCandidates.isEmpty()) return null - currencyList.find { it is Currency.Blockchain && it.blockchain == Blockchain.Cardano }?.let { currency -> + if (currency is Currency.Blockchain && currency.blockchain == Blockchain.Cardano) { currency.derivationPath?.let { bothCandidates.add(CardanoUtils.extendedDerivationPath(DerivationPath(it))) } diff --git a/app/src/main/java/com/tangem/tap/features/tokens/impl/domain/DefaultTokensListInteractor.kt b/app/src/main/java/com/tangem/tap/features/tokens/impl/domain/DefaultTokensListInteractor.kt index 9610727483..a70d6379c1 100644 --- a/app/src/main/java/com/tangem/tap/features/tokens/impl/domain/DefaultTokensListInteractor.kt +++ b/app/src/main/java/com/tangem/tap/features/tokens/impl/domain/DefaultTokensListInteractor.kt @@ -136,9 +136,9 @@ internal class DefaultTokensListInteractor( private suspend fun deriveMissingBlockchains(scanResponse: ScanResponse, currencies: List) { val config = CardConfig.createConfig(scanResponse.card) - val derivations = currencies.mapNotNull { - val curve = config.primaryCurve(it.blockchain) - curve?.let { getDerivations(curve, scanResponse, currencies) } + val derivations = currencies.mapNotNull { currency -> + val curve = config.primaryCurve(currency.blockchain) + curve?.let { getDerivations(curve, scanResponse, currency) } }.associate(transform = TokensMiddleware.DerivationData::derivations) if (derivations.isEmpty()) { @@ -176,35 +176,36 @@ internal class DefaultTokensListInteractor( private fun getDerivations( curve: EllipticCurve, scanResponse: ScanResponse, - currencyList: List, + currency: Currency, ): TokensMiddleware.DerivationData? { val wallet = scanResponse.card.wallets.firstOrNull { it.curve == curve } ?: return null - val manageTokensCandidates = currencyList - .map(Currency::blockchain) - .distinct() - .filter { it.getSupportedCurves().contains(curve) } - .mapNotNull { it.derivationPath(scanResponse.derivationStyleProvider.getDerivationStyle()) } + val supportedCurves = currency.blockchain.getSupportedCurves() + val path = currency.derivationPath + ?.let { + currency.blockchain.derivationPath(scanResponse.derivationStyleProvider.getDerivationStyle()) + } + .takeIf { supportedCurves.contains(curve) } - val customTokensCandidates = currencyList - .filter { it.blockchain.getSupportedCurves().contains(curve) } - .mapNotNull(Currency::derivationPath) - .map(::DerivationPath) + val customPath = currency.derivationPath?.let { + DerivationPath(it) + }.takeIf { supportedCurves.contains(curve) } - val bothCandidates = (manageTokensCandidates + customTokensCandidates).distinct().toMutableList() + val bothCandidates = listOfNotNull(path, customPath).distinct().toMutableList() if (bothCandidates.isEmpty()) return null - currencyList.find { it is Currency.Blockchain && it.blockchain == Blockchain.Cardano }?.let { currency -> + if (currency is Currency.Blockchain && currency.blockchain == Blockchain.Cardano) { currency.derivationPath?.let { bothCandidates.add(CardanoUtils.extendedDerivationPath(DerivationPath(it))) } } val mapKeyOfWalletPublicKey = wallet.publicKey.toMapKey() - val alreadyDerivedKeys = scanResponse.derivedKeys[mapKeyOfWalletPublicKey] ?: ExtendedPublicKeysMap(emptyMap()) + val alreadyDerivedKeys: ExtendedPublicKeysMap = + scanResponse.derivedKeys[mapKeyOfWalletPublicKey] ?: ExtendedPublicKeysMap(emptyMap()) val alreadyDerivedPaths = alreadyDerivedKeys.keys.toList() - val toDerive = bothCandidates.filterNot(alreadyDerivedPaths::contains) + val toDerive = bothCandidates.filterNot { alreadyDerivedPaths.contains(it) } if (toDerive.isEmpty()) return null return TokensMiddleware.DerivationData(derivations = mapKeyOfWalletPublicKey to toDerive) 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 1a5eb1fd45..70ac55b12f 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 @@ -121,9 +121,9 @@ object TokensMiddleware { onSuccess: (ScanResponse) -> Unit, ) { val config = CardConfig.createConfig(scanResponse.card) - val derivationDataList = currencyList.mapNotNull { - val curve = config.primaryCurve(it.blockchain) - curve?.let { getDerivations(curve, scanResponse, currencyList) } + val derivationDataList = currencyList.mapNotNull { currency -> + val curve = config.primaryCurve(currency.blockchain) + curve?.let { getDerivations(curve, scanResponse, currency) } } val derivations = derivationDataList.associate { it.derivations } if (derivations.isEmpty()) { @@ -163,27 +163,24 @@ object TokensMiddleware { } } - private fun getDerivations( - curve: EllipticCurve, - scanResponse: ScanResponse, - currencyList: List, - ): DerivationData? { + private fun getDerivations(curve: EllipticCurve, scanResponse: ScanResponse, currency: Currency): DerivationData? { val wallet = scanResponse.card.wallets.firstOrNull { it.curve == curve } ?: return null - val manageTokensCandidates = currencyList.map { it.blockchain }.distinct().filter { - it.getSupportedCurves().contains(curve) - }.mapNotNull { - it.derivationPath(scanResponse.derivationStyleProvider.getDerivationStyle()) - } + val supportedCurves = currency.blockchain.getSupportedCurves() + val path = currency.derivationPath + ?.let { + currency.blockchain.derivationPath(scanResponse.derivationStyleProvider.getDerivationStyle()) + } + .takeIf { supportedCurves.contains(curve) } - val customTokensCandidates = currencyList.filter { - it.blockchain.getSupportedCurves().contains(curve) - }.mapNotNull { it.derivationPath }.map { DerivationPath(it) } + val customPath = currency.derivationPath?.let { + DerivationPath(it) + }.takeIf { supportedCurves.contains(curve) } - val bothCandidates = (manageTokensCandidates + customTokensCandidates).distinct().toMutableList() + val bothCandidates = listOfNotNull(path, customPath).distinct().toMutableList() if (bothCandidates.isEmpty()) return null - currencyList.find { it is Currency.Blockchain && it.blockchain == Blockchain.Cardano }?.let { currency -> + if (currency is Currency.Blockchain && currency.blockchain == Blockchain.Cardano) { currency.derivationPath?.let { bothCandidates.add(CardanoUtils.extendedDerivationPath(DerivationPath(it))) } diff --git a/app/src/main/java/com/tangem/tap/features/wallet/redux/WalletState.kt b/app/src/main/java/com/tangem/tap/features/wallet/redux/WalletState.kt index 0048c80636..f0bdd8f3f1 100644 --- a/app/src/main/java/com/tangem/tap/features/wallet/redux/WalletState.kt +++ b/app/src/main/java/com/tangem/tap/features/wallet/redux/WalletState.kt @@ -82,9 +82,6 @@ data class WalletState( get() = primaryWalletStore?.walletsData ?.firstOrNull { it.currency !is Currency.Blockchain } - val shouldShowDetails: Boolean = - primaryWalletData?.status !is WalletDataModel.Unreachable - fun getWalletManager(currency: Currency?): WalletManager? { if (currency?.blockchain == null) return null return getWalletStore(currency)?.walletManager diff --git a/app/src/main/java/com/tangem/tap/features/wallet/ui/WalletFragment.kt b/app/src/main/java/com/tangem/tap/features/wallet/ui/WalletFragment.kt index d049f046a3..cbb33a7dcd 100644 --- a/app/src/main/java/com/tangem/tap/features/wallet/ui/WalletFragment.kt +++ b/app/src/main/java/com/tangem/tap/features/wallet/ui/WalletFragment.kt @@ -190,9 +190,7 @@ class WalletFragment : Fragment(R.layout.fragment_wallet), SafeStoreSubscriber Unit, ) { val config = CardConfig.createConfig(scanResponse.card) - val derivationDataList = currencyList.mapNotNull { - val curve = config.primaryCurve(it.blockchain) - curve?.let { getDerivations(curve, scanResponse, currencyList) } + val derivationDataList = currencyList.mapNotNull { currency -> + val curve = config.primaryCurve(currency.blockchain) + curve?.let { getDerivations(curve, scanResponse, currency) } } val derivations = derivationDataList.associate { it.derivations } if (derivations.isEmpty()) { @@ -159,23 +160,32 @@ class DerivationManagerImpl( private fun getDerivations( curve: EllipticCurve, scanResponse: ScanResponse, - currencyList: List, - ): DerivationData? { + currency: com.tangem.tap.features.wallet.models.Currency, + ): TokensMiddleware.DerivationData? { val wallet = scanResponse.card.wallets.firstOrNull { it.curve == curve } ?: return null - val manageTokensCandidates = currencyList.map { it.blockchain }.distinct().filter { - it.getSupportedCurves().contains(curve) - }.mapNotNull { - it.derivationPath(scanResponse.derivationStyleProvider.getDerivationStyle()) - } + val supportedCurves = currency.blockchain.getSupportedCurves() + val path = currency.derivationPath + ?.let { + currency.blockchain.derivationPath(scanResponse.derivationStyleProvider.getDerivationStyle()) + } + .takeIf { supportedCurves.contains(curve) } - val customTokensCandidates = currencyList.filter { - it.blockchain.getSupportedCurves().contains(curve) - }.mapNotNull { it.derivationPath }.map { DerivationPath(it) } + val customPath = currency.derivationPath?.let { + DerivationPath(it) + }.takeIf { supportedCurves.contains(curve) } - val bothCandidates = (manageTokensCandidates + customTokensCandidates).distinct().toMutableList() + val bothCandidates = listOfNotNull(path, customPath).distinct().toMutableList() if (bothCandidates.isEmpty()) return null + if (currency is WalletModelCurrency.Blockchain && + currency.blockchain == Blockchain.Cardano + ) { + currency.derivationPath?.let { + bothCandidates.add(CardanoUtils.extendedDerivationPath(DerivationPath(it))) + } + } + val mapKeyOfWalletPublicKey = wallet.publicKey.toMapKey() val alreadyDerivedKeys: ExtendedPublicKeysMap = scanResponse.derivedKeys[mapKeyOfWalletPublicKey] ?: ExtendedPublicKeysMap(emptyMap()) @@ -184,16 +194,7 @@ class DerivationManagerImpl( val toDerive = bothCandidates.filterNot { alreadyDerivedPaths.contains(it) } if (toDerive.isEmpty()) return null - currencyList.find { it is WalletModelCurrency.Blockchain && it.blockchain == Blockchain.Cardano } - ?.let { currency -> - currency.derivationPath?.let { - bothCandidates.add(CardanoUtils.extendedDerivationPath(DerivationPath(it))) - } - } - - return DerivationData( - derivations = mapKeyOfWalletPublicKey to toDerive, - ) + return TokensMiddleware.DerivationData(derivations = mapKeyOfWalletPublicKey to toDerive) } /** diff --git a/core/res/src/main/res/values-ru/strings.xml b/core/res/src/main/res/values-ru/strings.xml index 2eb19248a6..1ed0453bfb 100644 --- a/core/res/src/main/res/values-ru/strings.xml +++ b/core/res/src/main/res/values-ru/strings.xml @@ -490,7 +490,7 @@ Разблокировать все с %s История транзакций Сеть недоступна - Блокчейн недоступен. Попробуй позже. + Блокчейн недоступен. Попробуйте позже. Баланс загружается… Отсканируйте карту Транзакция подтверждается… diff --git a/data/card/src/main/java/com/tangem/data/card/sdk/DefaultCardSdkProvider.kt b/data/card/src/main/java/com/tangem/data/card/sdk/DefaultCardSdkProvider.kt index c2c2a2dd19..1249de10d7 100644 --- a/data/card/src/main/java/com/tangem/data/card/sdk/DefaultCardSdkProvider.kt +++ b/data/card/src/main/java/com/tangem/data/card/sdk/DefaultCardSdkProvider.kt @@ -7,7 +7,6 @@ import com.tangem.common.CardFilter import com.tangem.common.card.FirmwareVersion import com.tangem.common.core.Config import com.tangem.sdk.extensions.initWithBiometrics -import java.lang.ref.WeakReference import javax.inject.Inject import javax.inject.Singleton @@ -20,17 +19,18 @@ import javax.inject.Singleton internal class DefaultCardSdkProvider @Inject constructor() : CardSdkProvider, CardSdkLifecycleObserver { override val sdk: TangemSdk - get() = requireNotNull(value = _sdk?.get()) { "Impossible to get the TangemSdk when activity is destroyed" } + get() = requireNotNull(value = _sdk) { "Impossible to get the TangemSdk when activity is destroyed" } - private var _sdk: WeakReference? = null + private var _sdk: TangemSdk? = null override fun onCreate(context: Context) { - _sdk = WeakReference(TangemSdk.initWithBiometrics(activity = context as FragmentActivity, config = config)) + _sdk = TangemSdk.initWithBiometrics(activity = context as FragmentActivity, config = config) } override fun onDestroy(context: Context) { // Commented out to prevent crash on getting sdk when it's null. // FIXME: We still should find the real cause and fix it properly. + // idea: pass everywhere DefaultCardSdkProvider instead sdk to reach lazy access to sdk property // _sdk = null } diff --git a/gradle/dependencies.toml b/gradle/dependencies.toml index 657e767378..d4c76f2594 100644 --- a/gradle/dependencies.toml +++ b/gradle/dependencies.toml @@ -32,6 +32,7 @@ compose-navigation = "2.5.3" compose-accompanist = "0.30.1" compose-paging = "3.2.0" compose-reorderable = "0.9.6" +compoese-lifecycle-runtime = "2.6.1" # endregion Compose # region Other libraries @@ -80,7 +81,7 @@ okHttp-prettyLogging = "3.1.0" # endregion Other libraries # region Tangem -tangemBlockchainSdk = "release-app_4.11-348" +tangemBlockchainSdk = "release-app_4.11-349" #tangemBlockchainSdk = "0.0.1" # Keep it! - used for local builds tangemCardSdk = "release-app_4.11-295" #tangemCardSdk = "0.0.1" # Keep it! - used for local builds @@ -132,7 +133,7 @@ androidx-paging-runtime = { module = "androidx.paging:paging-runtime", version.r lifecycle-common-java8 = { module = "androidx.lifecycle:lifecycle-common-java8", version.ref = "androidxLifecycle" } lifecycle-runtime-ktx = { module = "androidx.lifecycle:lifecycle-runtime-ktx", version.ref = "androidxLifecycle" } lifecycle-viewModel-ktx = { module = "androidx.lifecycle:lifecycle-viewmodel-ktx", version.ref = "androidxLifecycle" } -lifecycle-compose = { module = "androidx.lifecycle:lifecycle-runtime-compose", version.ref = "androidxLifecycle" } +lifecycle-compose = { module = "androidx.lifecycle:lifecycle-runtime-compose", version.ref = "compoese-lifecycle-runtime" } # region AndroidX # region Compose