From d2cfcfa5e569d30291873687a7045d642d1929fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Igor=20Danil=C4=8Denko?= Date: Thu, 14 Nov 2024 11:28:37 +0100 Subject: [PATCH] Make TxRepository refresh the whole tx list every time I get a new tx, increase version to 0.29.0(317) --- .../walletManager/WalletCallbackListener.kt | 7 +- .../android/wallet/data/tx/TxRepository.kt | 123 ++---------------- build.gradle | 2 +- 3 files changed, 16 insertions(+), 116 deletions(-) diff --git a/app/src/main/java/com/tari/android/wallet/application/walletManager/WalletCallbackListener.kt b/app/src/main/java/com/tari/android/wallet/application/walletManager/WalletCallbackListener.kt index bd9cb09ba..e31920e71 100644 --- a/app/src/main/java/com/tari/android/wallet/application/walletManager/WalletCallbackListener.kt +++ b/app/src/main/java/com/tari/android/wallet/application/walletManager/WalletCallbackListener.kt @@ -74,15 +74,16 @@ class WalletCallbackListener @Inject constructor() { fun onTxBroadcast(contextPtr: ByteArray, completedTxPtr: FFIPointer) { val walletContextId = BigInteger(1, contextPtr).toInt() val tx = FFICompletedTx(completedTxPtr) - log(walletContextId, "Tx broadcast ${tx.getId()}") when (tx.getDirection()) { Tx.Direction.INBOUND -> { val pendingInboundTx = PendingInboundTx(tx) + log(walletContextId, "Tx inbound broadcast ${tx.getId()}") listeners[walletContextId]?.onInboundTxBroadcast(pendingInboundTx) } Tx.Direction.OUTBOUND -> { val pendingOutboundTx = PendingOutboundTx(tx) + log(walletContextId, "Tx outbound broadcast ${tx.getId()}") listeners[walletContextId]?.onOutboundTxBroadcast(pendingOutboundTx) } } @@ -99,7 +100,7 @@ class WalletCallbackListener @Inject constructor() { val walletContextId = BigInteger(1, contextPtr).toInt() val confirmationCount = BigInteger(1, confirmationCountBytes).toInt() val completed = CompletedTx(completedTxPtr) - log(walletContextId, "Tx mined & unconfirmed ${completed.id} $confirmationCount") + log(walletContextId, "Tx mined & unconfirmed ${completed.id} ($confirmationCount confirmations)") listeners[walletContextId]?.onTxMinedUnconfirmed(completed, confirmationCount) } @@ -114,7 +115,7 @@ class WalletCallbackListener @Inject constructor() { val walletContextId = BigInteger(1, contextPtr).toInt() val confirmationCount = BigInteger(1, confirmationCountBytes).toInt() val completed = CompletedTx(completedTxPtr) - log(walletContextId, "Tx faux unconfirmed ${completed.id}") + log(walletContextId, "Tx faux unconfirmed ${completed.id} ($confirmationCount confirmations)") listeners[walletContextId]?.onTxMinedUnconfirmed(completed, confirmationCount) } diff --git a/app/src/main/java/com/tari/android/wallet/data/tx/TxRepository.kt b/app/src/main/java/com/tari/android/wallet/data/tx/TxRepository.kt index d76e5c797..6dae77f45 100644 --- a/app/src/main/java/com/tari/android/wallet/data/tx/TxRepository.kt +++ b/app/src/main/java/com/tari/android/wallet/data/tx/TxRepository.kt @@ -5,24 +5,13 @@ import com.tari.android.wallet.application.walletManager.WalletManager import com.tari.android.wallet.application.walletManager.WalletManager.WalletEvent import com.tari.android.wallet.application.walletManager.doOnWalletRunning import com.tari.android.wallet.di.ApplicationScope -import com.tari.android.wallet.extension.replaceItem -import com.tari.android.wallet.extension.replaceOrAddItem -import com.tari.android.wallet.extension.withItem -import com.tari.android.wallet.extension.withoutItem -import com.tari.android.wallet.model.CancelledTx -import com.tari.android.wallet.model.CompletedTx -import com.tari.android.wallet.model.PendingInboundTx -import com.tari.android.wallet.model.PendingOutboundTx import com.tari.android.wallet.model.Tx -import com.tari.android.wallet.model.TxId -import com.tari.android.wallet.model.TxStatus import com.tari.android.wallet.ui.fragment.contactBook.data.ContactsRepository import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.asStateFlow import kotlinx.coroutines.flow.map -import kotlinx.coroutines.flow.update import kotlinx.coroutines.flow.zip import kotlinx.coroutines.launch import javax.inject.Inject @@ -66,21 +55,19 @@ class TxRepository @Inject constructor( applicationScope.launch(Dispatchers.IO) { walletManager.walletEvent.collect { event -> when (event) { - is WalletEvent.Tx.TxReceived -> onTxReceived(event.tx) - is WalletEvent.Tx.TxReplyReceived -> onTxReplyReceived(event.tx) - is WalletEvent.Tx.TxFinalized -> onTxFinalized(event.tx) - is WalletEvent.Tx.InboundTxBroadcast -> onInboundTxBroadcast(event.tx) - is WalletEvent.Tx.OutboundTxBroadcast -> onOutboundTxBroadcast(event.tx) - is WalletEvent.Tx.TxMinedUnconfirmed -> onTxMinedUnconfirmed(event.tx) - is WalletEvent.Tx.TxMined -> onTxMined(event.tx) - is WalletEvent.Tx.TxFauxMinedUnconfirmed -> onTxFauxMinedUnconfirmed(event.tx) - is WalletEvent.Tx.TxFauxConfirmed -> onFauxTxMined(event.tx) - is WalletEvent.Tx.TxCancelled -> onTxCancelled(event.tx) - + is WalletEvent.Tx.TxReceived, + is WalletEvent.Tx.TxReplyReceived, + is WalletEvent.Tx.TxFinalized, + is WalletEvent.Tx.InboundTxBroadcast, + is WalletEvent.Tx.OutboundTxBroadcast, + is WalletEvent.Tx.TxMinedUnconfirmed, + is WalletEvent.Tx.TxMined, + is WalletEvent.Tx.TxFauxMinedUnconfirmed, + is WalletEvent.Tx.TxFauxConfirmed, + is WalletEvent.Tx.TxCancelled, + is WalletEvent.TxSend.TxSendSuccessful, is WalletEvent.UtxosSplit -> refreshTxList() - is WalletEvent.TxSend.TxSendSuccessful -> onTxSendSuccessful(event.txId) - else -> Unit } } @@ -113,94 +100,6 @@ class TxRepository @Inject constructor( } } - private fun onTxReceived(tx: PendingInboundTx) { - _txs.update { it.copy(pendingInboundTxs = it.pendingInboundTxs.withItem(tx)) } - } - - private fun onTxReplyReceived(tx: PendingOutboundTx) { - _txs.update { txs -> - txs.copy(pendingOutboundTxs = txs.pendingOutboundTxs.replaceItem({ it.id == tx.id }, { it.copy(status = tx.status) })) - } - } - - private fun onTxFinalized(tx: PendingInboundTx) { - _txs.update { txs -> - txs.copy(pendingInboundTxs = txs.pendingInboundTxs.replaceItem({ it.id == tx.id }, { it.copy(status = tx.status) })) - } - } - - private fun onInboundTxBroadcast(tx: PendingInboundTx) { - _txs.update { txs -> - txs.copy(pendingInboundTxs = txs.pendingInboundTxs.replaceItem({ it.id == tx.id }, { it.copy(status = TxStatus.BROADCAST) })) - } - } - - private fun onOutboundTxBroadcast(tx: PendingOutboundTx) { - _txs.update { txs -> - txs.copy(pendingOutboundTxs = txs.pendingOutboundTxs.replaceItem({ it.id == tx.id }, { it.copy(status = TxStatus.BROADCAST) })) - } - } - - private fun onTxMinedUnconfirmed(tx: CompletedTx) { - _txs.update { txs -> - txs.copy( - completedTxs = txs.completedTxs.replaceOrAddItem({ it.id == tx.id }, tx), - pendingInboundTxs = if (tx.isInbound) txs.pendingInboundTxs.withoutItem { it.id == tx.id } else txs.pendingInboundTxs, - pendingOutboundTxs = if (tx.isOutbound) txs.pendingOutboundTxs.withoutItem { it.id == tx.id } else txs.pendingOutboundTxs, - ) - } - } - - private fun onTxMined(tx: CompletedTx) { - _txs.update { txs -> - txs.copy( - completedTxs = txs.completedTxs.replaceOrAddItem({ it.id == tx.id }, tx), - pendingInboundTxs = txs.pendingInboundTxs.withoutItem { it.id == tx.id }, - pendingOutboundTxs = txs.pendingOutboundTxs.withoutItem { it.id == tx.id }, - ) - } - } - - private fun onTxFauxMinedUnconfirmed(tx: CompletedTx) { - _txs.update { txs -> - txs.copy( - completedTxs = txs.completedTxs.replaceOrAddItem({ it.id == tx.id }, tx), - pendingInboundTxs = if (tx.isInbound) txs.pendingInboundTxs.withoutItem { it.id == tx.id } else txs.pendingInboundTxs, - pendingOutboundTxs = if (tx.isOutbound) txs.pendingOutboundTxs.withoutItem { it.id == tx.id } else txs.pendingOutboundTxs, - ) - } - } - - private fun onFauxTxMined(tx: CompletedTx) { - _txs.update { txs -> - txs.copy( - completedTxs = txs.completedTxs.replaceOrAddItem({ it.id == tx.id }, tx), - pendingInboundTxs = txs.pendingInboundTxs.withoutItem { it.id == tx.id }, - pendingOutboundTxs = txs.pendingOutboundTxs.withoutItem { it.id == tx.id }, - ) - } - } - - private fun onTxCancelled(tx: CancelledTx) { - _txs.update { txs -> - txs.copy( - cancelledTxs = txs.cancelledTxs.withItem(tx), - pendingInboundTxs = if (tx.isInbound) txs.pendingInboundTxs.withoutItem { it.id == tx.id } else txs.pendingInboundTxs, - pendingOutboundTxs = if (tx.isOutbound) txs.pendingOutboundTxs.withoutItem { it.id == tx.id } else txs.pendingOutboundTxs, - ) - } - } - - private fun onTxSendSuccessful(txId: TxId) { - try { - val tx = walletManager.requireWalletInstance.getPendingOutboundTxById(txId) - _txs.update { it.copy(pendingOutboundTxs = it.pendingOutboundTxs.withItem(tx)) } - } catch (e: Exception) { - logger.i("onTxSendSuccessful: error getting tx by id") - refreshTxList() - } - } - private fun Tx.toDto() = TxDto( tx = this, contact = contactsRepository.getContactForTx(this), diff --git a/build.gradle b/build.gradle index 64b9f1c5c..534322220 100644 --- a/build.gradle +++ b/build.gradle @@ -6,7 +6,7 @@ buildscript { ext.coroutines_version = '1.8.0' // build & version - ext.buildNumber = 316 + ext.buildNumber = 317 ext.versionNumber = "0.29.0" // JNI libs