diff --git a/app/src/main/java/com/tangem/tap/domain/walletStores/repository/implementation/utils/WalletDataOperations.kt b/app/src/main/java/com/tangem/tap/domain/walletStores/repository/implementation/utils/WalletDataOperations.kt index 2b77d3ba85..2d1ab24a3b 100644 --- a/app/src/main/java/com/tangem/tap/domain/walletStores/repository/implementation/utils/WalletDataOperations.kt +++ b/app/src/main/java/com/tangem/tap/domain/walletStores/repository/implementation/utils/WalletDataOperations.kt @@ -205,16 +205,7 @@ internal fun List.updateSelectedAddress( if (index == -1) return this val oldWalletData = this[index] - val addresses = oldWalletData.walletAddresses - ?: return this - val selectedAddress = addresses.list - .firstOrNull { it.type == addressType } - ?: addresses.selectedAddress - val updatedWalletData = oldWalletData.copy( - walletAddresses = addresses.copy( - selectedAddress = selectedAddress, - ), - ) + val updatedWalletData = oldWalletData.updateSelectedAddress(addressType) if (oldWalletData == updatedWalletData) return this return this.toMutableList().apply { @@ -222,6 +213,21 @@ internal fun List.updateSelectedAddress( } } +internal fun WalletDataModel.updateSelectedAddress( + addressType: AddressType, +): WalletDataModel { + val addresses = walletAddresses ?: return this + val selectedAddress = addresses.list + .firstOrNull { it.type == addressType } + ?: addresses.selectedAddress + + return this.copy( + walletAddresses = addresses.copy( + selectedAddress = selectedAddress, + ), + ) +} + internal fun WalletDataModel.isSameWalletData(other: WalletDataModel): Boolean { return this.currency == other.currency } \ No newline at end of file diff --git a/app/src/main/java/com/tangem/tap/features/send/redux/middlewares/SendMiddleware.kt b/app/src/main/java/com/tangem/tap/features/send/redux/middlewares/SendMiddleware.kt index 0a528bd856..551d1b67b9 100644 --- a/app/src/main/java/com/tangem/tap/features/send/redux/middlewares/SendMiddleware.kt +++ b/app/src/main/java/com/tangem/tap/features/send/redux/middlewares/SendMiddleware.kt @@ -176,18 +176,29 @@ private fun sendTransaction( scope.launch { val updateWalletResult = walletManager.safeUpdate() if (updateWalletResult is Result.Failure) { - when (val error = updateWalletResult.error) { - is TapError -> store.dispatchErrorNotification(error) - else -> { - val tapError = if (error.message == null) { - TapError.UnknownError - } else { - TapError.CustomError(error.message!!) + withMainContext { + when (val error = updateWalletResult.error) { + is TapError -> store.dispatchErrorNotification(error) + is BlockchainSdkError -> { + updateFeedbackManagerInfo( + walletManager = walletManager, + amountToSend = amountToSend, + feeAmount = feeAmount, + destinationAddress = destinationAddress, + ) + dispatch(SendAction.Dialog.SendTransactionFails.BlockchainSdkError(error = error)) + } + else -> { + val tapError = if (error.message == null) { + TapError.UnknownError + } else { + TapError.CustomError(error.message!!) + } + store.dispatchErrorNotification(tapError) } - store.dispatchErrorNotification(tapError) } + dispatch(SendAction.ChangeSendButtonState(ButtonState.ENABLED)) } - withMainContext { dispatch(SendAction.ChangeSendButtonState(ButtonState.ENABLED)) } return@launch } @@ -250,7 +261,7 @@ private fun sendTransaction( } } is SimpleResult.Failure -> { - store.state.globalState.feedbackManager?.infoHolder?.updateOnSendError( + updateFeedbackManagerInfo( walletManager = walletManager, amountToSend = amountToSend, feeAmount = feeAmount, @@ -306,6 +317,20 @@ private fun sendTransaction( } } +private fun updateFeedbackManagerInfo( + walletManager: WalletManager, + amountToSend: Amount, + feeAmount: Amount, + destinationAddress: String, +) { + store.state.globalState.feedbackManager?.infoHolder?.updateOnSendError( + walletManager = walletManager, + amountToSend = amountToSend, + feeAmount = feeAmount, + destinationAddress = destinationAddress, + ) +} + fun createValidateTransactionError( errorList: EnumSet, walletManager: WalletManager, diff --git a/app/src/main/java/com/tangem/tap/features/wallet/ui/wallet/SingleWalletView.kt b/app/src/main/java/com/tangem/tap/features/wallet/ui/wallet/SingleWalletView.kt index 744fc34601..b66b7d3038 100644 --- a/app/src/main/java/com/tangem/tap/features/wallet/ui/wallet/SingleWalletView.kt +++ b/app/src/main/java/com/tangem/tap/features/wallet/ui/wallet/SingleWalletView.kt @@ -19,15 +19,15 @@ import com.tangem.tap.features.wallet.models.PendingTransactionType import com.tangem.tap.features.wallet.redux.WalletAction import com.tangem.tap.features.wallet.redux.WalletMainButton import com.tangem.tap.features.wallet.redux.WalletState +import com.tangem.tap.features.wallet.ui.BalanceWidget +import com.tangem.tap.features.wallet.ui.MultipleAddressUiHelper +import com.tangem.tap.features.wallet.ui.WalletFragment +import com.tangem.tap.features.wallet.ui.adapters.PendingTransactionsAdapter import com.tangem.tap.features.wallet.ui.utils.getAvailableActions import com.tangem.tap.features.wallet.ui.utils.isAvailableToBuy import com.tangem.tap.features.wallet.ui.utils.isAvailableToSell import com.tangem.tap.features.wallet.ui.utils.mainButton import com.tangem.tap.features.wallet.ui.utils.shouldShowMultipleAddress -import com.tangem.tap.features.wallet.ui.BalanceWidget -import com.tangem.tap.features.wallet.ui.MultipleAddressUiHelper -import com.tangem.tap.features.wallet.ui.WalletFragment -import com.tangem.tap.features.wallet.ui.adapters.PendingTransactionsAdapter import com.tangem.tap.features.wallet.ui.view.WalletDetailsButtonsRow import com.tangem.tap.store import com.tangem.wallet.R @@ -88,7 +88,12 @@ class SingleWalletView : WalletView() { binding?.rvPendingTransaction?.show(knownTransactions.isNotEmpty()) } + // FIXME: Move to model watcher + private var watchedPrimaryWallet: WalletDataModel? = null private fun setupBalance(state: WalletState, primaryWallet: WalletDataModel) { + if (watchedPrimaryWallet == primaryWallet) return + watchedPrimaryWallet = primaryWallet + val fragment = fragment ?: return binding?.apply { lCardBalance.lBalance.root.show() @@ -178,8 +183,13 @@ class SingleWalletView : WalletView() { } } + // FIXME: Move to model watcher + private var watchedPrimaryWallet: WalletDataModel? = null private fun setupAddressCard(state: WalletState, binding: FragmentWalletBinding) = with(binding.lAddress) { val primaryWallet = state.primaryWalletData + if (primaryWallet == watchedPrimaryWallet) return@with + watchedPrimaryWallet = primaryWallet + if (primaryWallet?.walletAddresses != null && primaryWallet.currency is Currency.Blockchain) { binding.lAddress.root.show() if (primaryWallet.shouldShowMultipleAddress()) { @@ -206,16 +216,16 @@ class SingleWalletView : WalletView() { ), ) } - setupCardInfo(state) + setupCardInfo(primaryWallet) } else { binding.lAddress.root.hide() } } - private fun setupCardInfo(state: WalletState) { + private fun setupCardInfo(walletData: WalletDataModel) { val textView = binding?.lAddress?.tvInfo - val blockchain = state.primaryWalletData?.currency?.blockchain - if (textView != null && blockchain != null) { + val blockchain = walletData.currency.blockchain + if (textView != null) { textView.text = textView.getString( id = R.string.address_qr_code_message_format, blockchain.fullName,