Updated on 2026-08-14

This commit is contained in:
Tangem 2020-09-03 10:45:37 +03:00
parent 3b6bbc5ba2
commit bcb4921e96
8 changed files with 93 additions and 35 deletions

View file

@ -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
}
}

View file

@ -1,7 +0,0 @@
package com.tangem.tap.domain
class BlockchainManager {
}

View file

@ -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<Wallet>) {
private suspend fun handleUpdateWalletResult(result: Result<Wallet>, 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<String?>? {
if (!TapConfig.usePayId ||
store.state.walletState.payIdData.payIdState == PayIdState.Disabled ||

View file

@ -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<String, BigDecimal>) : WalletAction()

View file

@ -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())
}

View file

@ -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
)
}
}
}