From bcb4921e965498e8dcb03ae57a158c75abad8c2f Mon Sep 17 00:00:00 2001 From: Tangem Date: Thu, 3 Sep 2020 10:45:37 +0300 Subject: [PATCH] Updated on 2026-08-14 --- .../tangem/tap/domain/BlockchainExtensions.kt | 23 +++++++++++++ .../tangem/tap/domain/BlockchainManager.kt | 7 ---- .../com/tangem/tap/domain/TapWalletManager.kt | 20 +++++++++--- .../tap/features/wallet/redux/WalletAction.kt | 4 ++- .../features/wallet/redux/WalletReducer.kt | 23 ++++++++++--- .../tap/features/wallet/ui/BalanceWidget.kt | 17 ++++++++-- app/src/main/res/layout/layout_balance.xml | 32 +++++++++---------- app/src/main/res/values/strings.xml | 2 ++ 8 files changed, 93 insertions(+), 35 deletions(-) create mode 100644 app/src/main/java/com/tangem/tap/domain/BlockchainExtensions.kt delete mode 100644 app/src/main/java/com/tangem/tap/domain/BlockchainManager.kt diff --git a/app/src/main/java/com/tangem/tap/domain/BlockchainExtensions.kt b/app/src/main/java/com/tangem/tap/domain/BlockchainExtensions.kt new file mode 100644 index 0000000000..c9d4ffdebd --- /dev/null +++ b/app/src/main/java/com/tangem/tap/domain/BlockchainExtensions.kt @@ -0,0 +1,23 @@ +package com.tangem.tap.domain + +import com.tangem.blockchain.common.Blockchain +import org.stellar.sdk.requests.ErrorResponse + + +fun Blockchain.isNoAccountError(exception: Throwable): Boolean { + return when (this) { + Blockchain.Stellar -> { + (exception is ErrorResponse) && (exception.code == 404) + } + Blockchain.XRP -> exception.message?.contains("Account not found") == true + else -> false + } +} + +fun Blockchain.amountToCreateAccount(): Int? { + return when (this) { + Blockchain.Stellar -> 1 + Blockchain.XRP -> 20 + else -> null + } +} \ No newline at end of file diff --git a/app/src/main/java/com/tangem/tap/domain/BlockchainManager.kt b/app/src/main/java/com/tangem/tap/domain/BlockchainManager.kt deleted file mode 100644 index 845635dbe2..0000000000 --- a/app/src/main/java/com/tangem/tap/domain/BlockchainManager.kt +++ /dev/null @@ -1,7 +0,0 @@ -package com.tangem.tap.domain - -class BlockchainManager { - - - -} \ No newline at end of file diff --git a/app/src/main/java/com/tangem/tap/domain/TapWalletManager.kt b/app/src/main/java/com/tangem/tap/domain/TapWalletManager.kt index 042f947b77..92f240d8b4 100644 --- a/app/src/main/java/com/tangem/tap/domain/TapWalletManager.kt +++ b/app/src/main/java/com/tangem/tap/domain/TapWalletManager.kt @@ -23,7 +23,7 @@ class TapWalletManager { suspend fun loadWalletData() { val walletManager = store.state.globalState.walletManager if (walletManager == null) { - store.dispatch(WalletAction.LoadWallet.Failure) + store.dispatch(WalletAction.LoadWallet.Failure()) return } updateWallet(walletManager) @@ -69,20 +69,30 @@ class TapWalletManager { } catch (exception: Exception) { Result.Failure(exception) } - handleUpdateWalletResult(result) + handleUpdateWalletResult(result, walletManager) } - private suspend fun handleUpdateWalletResult(result: Result) { + private suspend fun handleUpdateWalletResult(result: Result, walletManager: WalletManager) { withContext(Dispatchers.Main) { when (result) { is Result.Success -> store.dispatch(WalletAction.LoadWallet.Success(result.data)) - is Result.Failure -> store.dispatch(WalletAction.LoadWallet.Failure) + is Result.Failure -> { + val error = result.error + val blockchain = walletManager.wallet.blockchain + if (error != null && blockchain.isNoAccountError(error)) { + val amountToCreateAccount = blockchain.amountToCreateAccount() + if (amountToCreateAccount != null) { + store.dispatch(WalletAction.LoadWallet.NoAccount(amountToCreateAccount)) + return@withContext + } + } + store.dispatch(WalletAction.LoadWallet.Failure(result.error?.localizedMessage)) + } } } } - private suspend fun loadPayIdIfNeeded(): Result? { if (!TapConfig.usePayId || store.state.walletState.payIdData.payIdState == PayIdState.Disabled || diff --git a/app/src/main/java/com/tangem/tap/features/wallet/redux/WalletAction.kt b/app/src/main/java/com/tangem/tap/features/wallet/redux/WalletAction.kt index e047de3201..4f080062f1 100644 --- a/app/src/main/java/com/tangem/tap/features/wallet/redux/WalletAction.kt +++ b/app/src/main/java/com/tangem/tap/features/wallet/redux/WalletAction.kt @@ -10,9 +10,11 @@ import org.rekotlin.Action import java.math.BigDecimal sealed class WalletAction : Action { + object LoadWallet : WalletAction() { data class Success(val wallet: Wallet): WalletAction() - object Failure: WalletAction() + data class NoAccount(val amountToCreateAccount: Int): WalletAction() + data class Failure(val errorMessage: String? = null): WalletAction() } object LoadFiatRate : WalletAction() { data class Success(val fiatRates: Pair) : WalletAction() diff --git a/app/src/main/java/com/tangem/tap/features/wallet/redux/WalletReducer.kt b/app/src/main/java/com/tangem/tap/features/wallet/redux/WalletReducer.kt index d943eff64c..245d057845 100644 --- a/app/src/main/java/com/tangem/tap/features/wallet/redux/WalletReducer.kt +++ b/app/src/main/java/com/tangem/tap/features/wallet/redux/WalletReducer.kt @@ -52,6 +52,7 @@ fun walletReducer(action: Action, state: AppState): WalletState { state = ProgressState.Done, wallet = action.wallet, currencyData = BalanceWidgetData( BalanceStatus.VerifiedOnline, action.wallet.blockchain.fullName, + currencySymbol = action.wallet.blockchain.currency, amount?.toFormattedString(action.wallet.blockchain), token = tokenData, fiatAmount = fiatAmount @@ -59,6 +60,24 @@ fun walletReducer(action: Action, state: AppState): WalletState { mainButton = WalletMainButton.SendButton(sendButtonEnabled) ) } + is WalletAction.LoadWallet.NoAccount -> { + val wallet = state.globalState.walletManager?.wallet + newState = newState.copy( + state = ProgressState.Done, wallet = wallet, + currencyData = BalanceWidgetData( + BalanceStatus.NoAccount, wallet?.blockchain?.fullName, + currencySymbol = wallet?.blockchain?.currency, + amountToCreateAccount = action.amountToCreateAccount + ) + ) + } + is WalletAction.LoadWallet.Failure -> newState = newState.copy( + state = ProgressState.Done, + currencyData = newState.currencyData.copy( + status = BalanceStatus.Unreachable, + errorMessage = action.errorMessage + ) + ) is WalletAction.LoadFiatRate.Success -> { val rate = action.fiatRates.second val currency = action.fiatRates.first @@ -77,10 +96,6 @@ fun walletReducer(action: Action, state: AppState): WalletState { token = newState.currencyData.token?.copy(fiatAmount = tokenFiatAmount) )) } - is WalletAction.LoadWallet.Failure -> newState = newState.copy( - state = ProgressState.Done, - currencyData = newState.currencyData.copy(status = BalanceStatus.Unreachable) - ) is WalletAction.ShowQrCode -> { newState = newState.copy(qrCode = newState.wallet?.shareUrl?.toQrCode()) } diff --git a/app/src/main/java/com/tangem/tap/features/wallet/ui/BalanceWidget.kt b/app/src/main/java/com/tangem/tap/features/wallet/ui/BalanceWidget.kt index 92e3dca6e1..c3431b742a 100644 --- a/app/src/main/java/com/tangem/tap/features/wallet/ui/BalanceWidget.kt +++ b/app/src/main/java/com/tangem/tap/features/wallet/ui/BalanceWidget.kt @@ -21,15 +21,19 @@ enum class BalanceStatus { VerifiedOnline, Unreachable, Loading, + NoAccount, EmptyCard } data class BalanceWidgetData( val status: BalanceStatus? = null, val currency: String? = null, + val currencySymbol: String? = null, val amount: String? = null, val fiatAmount: String? = null, - val token: TokenData? = null + val token: TokenData? = null, + val amountToCreateAccount: Int? = null, + val errorMessage: String? = null ) data class TokenData( @@ -87,7 +91,16 @@ class BalanceWidget( fragment.l_balance_error.show() fragment.tv_error_title.text = fragment.getText(R.string.wallet_empty_card) fragment.tv_error_descriptions.text = fragment.getText(R.string.wallet_empty_card_description) - + } + BalanceStatus.NoAccount -> { + fragment.l_balance.hide() + fragment.l_balance_error.show() + fragment.tv_error_title.text = fragment.getText(R.string.wallet_no_account) + fragment.tv_error_descriptions.text = + fragment.getString( + R.string.wallet_no_account_description, + data.amountToCreateAccount?.toString(), data.currencySymbol + ) } } } diff --git a/app/src/main/res/layout/layout_balance.xml b/app/src/main/res/layout/layout_balance.xml index 63ad2853df..4d4e77f50d 100644 --- a/app/src/main/res/layout/layout_balance.xml +++ b/app/src/main/res/layout/layout_balance.xml @@ -1,10 +1,10 @@ - + app:layout_constraintTop_toBottomOf="@id/tv_currency" /> + app:layout_constraintTop_toBottomOf="@id/tv_currency" /> + app:layout_constraintTop_toBottomOf="@id/tv_currency" /> + android:visibility="gone" + app:layout_constraintTop_toBottomOf="@+id/tv_currency" /> \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index a2fe09cfc5..c157743673 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -21,6 +21,8 @@ Account is not created Empty Card Create wallet to start using Tangem card + Account is not created + Load %1s+ %2s to create account Tokens Create PayID