Updated on 2026-08-14
This commit is contained in:
parent
bb9d49e2e6
commit
400d625df1
3 changed files with 126 additions and 73 deletions
|
|
@ -2,12 +2,14 @@ package com.tangem.tap.common.redux.global
|
|||
|
||||
import com.tangem.blockchain.common.WalletManager
|
||||
import com.tangem.commands.Card
|
||||
import com.tangem.tap.domain.TapWalletManager
|
||||
import org.rekotlin.StateType
|
||||
import java.math.BigDecimal
|
||||
|
||||
data class GlobalState(
|
||||
val card: Card? = null,
|
||||
val walletManager: WalletManager? = null,
|
||||
val tapWalletManager: TapWalletManager = TapWalletManager(),
|
||||
val fiatRates: FiatRates = FiatRates(emptyMap()),
|
||||
) : StateType
|
||||
|
||||
|
|
|
|||
117
app/src/main/java/com/tangem/tap/domain/TapWalletManager.kt
Normal file
117
app/src/main/java/com/tangem/tap/domain/TapWalletManager.kt
Normal file
|
|
@ -0,0 +1,117 @@
|
|||
package com.tangem.tap.domain
|
||||
|
||||
import com.tangem.blockchain.common.Wallet
|
||||
import com.tangem.blockchain.common.WalletManager
|
||||
import com.tangem.commands.common.network.Result
|
||||
import com.tangem.common.extensions.toHexString
|
||||
import com.tangem.tap.TapConfig
|
||||
import com.tangem.tap.common.redux.global.GlobalAction
|
||||
import com.tangem.tap.features.wallet.redux.PayIdState
|
||||
import com.tangem.tap.features.wallet.redux.WalletAction
|
||||
import com.tangem.tap.network.coinmarketcap.CoinMarketCapService
|
||||
import com.tangem.tap.store
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.withContext
|
||||
import java.math.BigDecimal
|
||||
|
||||
|
||||
class TapWalletManager {
|
||||
private val payIdManager = PayIdManager()
|
||||
private val coinMarketCapService = CoinMarketCapService()
|
||||
|
||||
suspend fun loadWalletData() {
|
||||
val walletManager = store.state.globalState.walletManager
|
||||
if (walletManager == null) {
|
||||
store.dispatch(WalletAction.LoadWallet.Failure)
|
||||
return
|
||||
}
|
||||
updateWallet(walletManager)
|
||||
}
|
||||
|
||||
suspend fun loadPayId() {
|
||||
val result = loadPayIdIfNeeded()
|
||||
result?.let { handlePayIdResult(it) }
|
||||
}
|
||||
|
||||
suspend fun loadFiatRate() {
|
||||
val blockchainCurrency = store.state.globalState.walletManager?.wallet?.blockchain?.currency
|
||||
val tokenCurrency = store.state.globalState.walletManager?.wallet?.token?.symbol
|
||||
|
||||
val blockchainRate = blockchainCurrency?.let { coinMarketCapService.getRate(it) }
|
||||
val tokenRate = tokenCurrency?.let { coinMarketCapService.getRate(it) }
|
||||
|
||||
val results = mutableListOf<Pair<String, Result<BigDecimal>?>>()
|
||||
if (blockchainCurrency != null) results.add(blockchainCurrency to blockchainRate)
|
||||
if (tokenCurrency != null) results.add(tokenCurrency to tokenRate)
|
||||
|
||||
handleFiatRatesResult(results)
|
||||
}
|
||||
|
||||
private suspend fun updateWallet(walletManager: WalletManager) {
|
||||
val result = try {
|
||||
walletManager.update()
|
||||
Result.Success(walletManager.wallet)
|
||||
} catch (exeption: Exception) {
|
||||
Result.Failure(exeption)
|
||||
}
|
||||
handleUpdateWalletResult(result)
|
||||
}
|
||||
|
||||
private suspend fun handleUpdateWalletResult(result: Result<Wallet>) {
|
||||
withContext(Dispatchers.Main) {
|
||||
when (result) {
|
||||
is Result.Success -> store.dispatch(WalletAction.LoadWallet.Success(result.data))
|
||||
is Result.Failure -> store.dispatch(WalletAction.LoadWallet.Failure)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
private suspend fun loadPayIdIfNeeded(): Result<String?>? {
|
||||
if (!TapConfig.usePayId ||
|
||||
store.state.walletState.payIdData.payIdState == PayIdState.Disabled ||
|
||||
store.state.globalState.walletManager?.wallet?.blockchain?.isPayIdSupported() == false) {
|
||||
return null
|
||||
}
|
||||
val cardId = store.state.globalState.card?.cardId
|
||||
val publicKey = store.state.globalState.card?.cardPublicKey
|
||||
if (cardId == null || publicKey == null) {
|
||||
return null
|
||||
}
|
||||
return payIdManager.getPayId(cardId, publicKey.toHexString())
|
||||
}
|
||||
|
||||
private suspend fun handlePayIdResult(result: Result<String?>) {
|
||||
withContext(Dispatchers.Main) {
|
||||
when (result) {
|
||||
is Result.Success -> {
|
||||
val payId = result.data
|
||||
if (payId == null) {
|
||||
store.dispatch(WalletAction.LoadPayId.NotCreated)
|
||||
} else {
|
||||
store.dispatch(WalletAction.LoadPayId.Success(payId))
|
||||
}
|
||||
}
|
||||
is Result.Failure -> store.dispatch(WalletAction.LoadPayId.Failure)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun handleFiatRatesResult(results: List<Pair<String, Result<BigDecimal>?>>) {
|
||||
withContext(Dispatchers.Main) {
|
||||
results.map {
|
||||
when (it.second) {
|
||||
is Result.Success -> {
|
||||
val rate = it.first to (it.second as Result.Success<BigDecimal>).data
|
||||
store.dispatch(GlobalAction.SetFiatRate(rate))
|
||||
store.dispatch(WalletAction.LoadFiatRate.Success(rate))
|
||||
}
|
||||
is Result.Failure -> store.dispatch(WalletAction.LoadFiatRate.Failure)
|
||||
null -> {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -6,14 +6,11 @@ import androidx.core.content.ContextCompat
|
|||
import com.tangem.commands.common.network.Result
|
||||
import com.tangem.common.CompletionResult
|
||||
import com.tangem.common.extensions.toHexString
|
||||
import com.tangem.tap.TapConfig
|
||||
import com.tangem.tap.common.extensions.copyToClipboard
|
||||
import com.tangem.tap.common.redux.AppState
|
||||
import com.tangem.tap.common.redux.global.GlobalAction
|
||||
import com.tangem.tap.domain.PayIdManager
|
||||
import com.tangem.tap.domain.TapError
|
||||
import com.tangem.tap.domain.isPayIdSupported
|
||||
import com.tangem.tap.network.coinmarketcap.CoinMarketCapService
|
||||
import com.tangem.tap.scope
|
||||
import com.tangem.tap.store
|
||||
import com.tangem.tap.tangemSdkManager
|
||||
|
|
@ -29,80 +26,17 @@ val walletMiddleware: Middleware<AppState> = { dispatch, state ->
|
|||
when (action) {
|
||||
is WalletAction.LoadWallet -> {
|
||||
scope.launch {
|
||||
val walletManager = store.state.globalState.walletManager
|
||||
if (walletManager == null) {
|
||||
store.dispatch(WalletAction.LoadWallet.Failure)
|
||||
return@launch
|
||||
}
|
||||
try {
|
||||
walletManager.update()
|
||||
} catch (ex: Exception) {
|
||||
withContext(Dispatchers.Main) {
|
||||
store.dispatch(WalletAction.LoadWallet.Failure)
|
||||
// callback(CompletionResult.Failure(BlockchainInternalErrorConverter.convert(ex)))
|
||||
next(action)
|
||||
}
|
||||
}
|
||||
withContext(Dispatchers.Main) {
|
||||
store.dispatch(WalletAction.LoadWallet.Success(walletManager.wallet))
|
||||
}
|
||||
store.state.globalState.tapWalletManager.loadWalletData()
|
||||
}
|
||||
}
|
||||
is WalletAction.LoadPayId -> {
|
||||
scope.launch {
|
||||
store.state.globalState.tapWalletManager.loadPayId()
|
||||
}
|
||||
}
|
||||
is WalletAction.LoadFiatRate -> {
|
||||
scope.launch {
|
||||
val blockchainCurrency = store.state.globalState.walletManager?.wallet?.blockchain?.currency
|
||||
val tokenCurrency = store.state.globalState.walletManager?.wallet?.token?.symbol
|
||||
|
||||
val blockchainRate = blockchainCurrency?.let { CoinMarketCapService().getRate(it) }
|
||||
val tokenRate = tokenCurrency?.let { CoinMarketCapService().getRate(it) }
|
||||
|
||||
withContext(Dispatchers.Main) {
|
||||
when (blockchainRate) {
|
||||
is Result.Success -> {
|
||||
store.dispatch(GlobalAction.SetFiatRate(blockchainCurrency to blockchainRate.data)
|
||||
)
|
||||
store.dispatch(WalletAction.LoadFiatRate.Success(blockchainCurrency to blockchainRate.data)
|
||||
)
|
||||
}
|
||||
is Result.Failure, null -> store.dispatch(WalletAction.LoadFiatRate.Failure)
|
||||
}
|
||||
when (tokenRate) {
|
||||
is Result.Success -> {
|
||||
store.dispatch(GlobalAction.SetFiatRate(tokenCurrency to tokenRate.data)
|
||||
)
|
||||
store.dispatch(WalletAction.LoadFiatRate.Success(tokenCurrency to tokenRate.data)
|
||||
)
|
||||
}
|
||||
is Result.Failure, null -> store.dispatch(WalletAction.LoadFiatRate.Failure)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
is WalletAction.LoadPayId -> {
|
||||
if (!TapConfig.usePayId ||
|
||||
store.state.walletState.payIdData.payIdState == PayIdState.Disabled ||
|
||||
store.state.globalState.walletManager?.wallet?.blockchain?.isPayIdSupported() == false) {
|
||||
next(action)
|
||||
}
|
||||
scope.launch {
|
||||
val cardId = store.state.globalState.card?.cardId
|
||||
val publicKey = store.state.globalState.card?.cardPublicKey
|
||||
if (cardId != null && publicKey != null) {
|
||||
val result = PayIdManager().getPayId(cardId, publicKey.toHexString())
|
||||
withContext(Dispatchers.Main) {
|
||||
when (result) {
|
||||
is Result.Success -> {
|
||||
val payId = result.data
|
||||
if (payId == null) {
|
||||
store.dispatch(WalletAction.LoadPayId.NotCreated)
|
||||
} else {
|
||||
store.dispatch(WalletAction.LoadPayId.Success(payId))
|
||||
}
|
||||
}
|
||||
is Result.Failure -> store.dispatch(WalletAction.LoadPayId.Failure)
|
||||
}
|
||||
}
|
||||
}
|
||||
store.state.globalState.tapWalletManager.loadFiatRate()
|
||||
}
|
||||
}
|
||||
is WalletAction.CreatePayId.CompleteCreatingPayId -> {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue