From 0be05a1c4c9f9c2838745c3b35f6a5d16f539aae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Honza=20Rychnovsk=C3=BD?= Date: Mon, 31 Jul 2023 13:34:41 +0200 Subject: [PATCH] [#1108] Getting memo for sent transactions --- .../z/ecc/android/sdk/internal/Backend.kt | 8 ++++++ .../screen/transactions/view/Transactions.kt | 14 +++------- .../cash/z/ecc/android/sdk/SdkSynchronizer.kt | 27 ++++++++++++------- .../cash/z/ecc/android/sdk/Synchronizer.kt | 8 +++++- 4 files changed, 36 insertions(+), 21 deletions(-) diff --git a/backend-lib/src/main/java/cash/z/ecc/android/sdk/internal/Backend.kt b/backend-lib/src/main/java/cash/z/ecc/android/sdk/internal/Backend.kt index be5ea7c18..295a76f61 100644 --- a/backend-lib/src/main/java/cash/z/ecc/android/sdk/internal/Backend.kt +++ b/backend-lib/src/main/java/cash/z/ecc/android/sdk/internal/Backend.kt @@ -75,8 +75,16 @@ interface Backend { fun getBranchIdForHeight(height: Long): Long + /** + * @throws RuntimeException as a common indicator of the operation failure + */ + @Throws(RuntimeException::class) suspend fun getReceivedMemoAsUtf8(idNote: Long): String? + /** + * @throws RuntimeException as a common indicator of the operation failure + */ + @Throws(RuntimeException::class) suspend fun getSentMemoAsUtf8(idNote: Long): String? suspend fun getVerifiedBalance(account: Int): Long diff --git a/demo-app/src/main/java/cash/z/ecc/android/sdk/demoapp/ui/screen/transactions/view/Transactions.kt b/demo-app/src/main/java/cash/z/ecc/android/sdk/demoapp/ui/screen/transactions/view/Transactions.kt index aa8a9dee4..82c93b867 100644 --- a/demo-app/src/main/java/cash/z/ecc/android/sdk/demoapp/ui/screen/transactions/view/Transactions.kt +++ b/demo-app/src/main/java/cash/z/ecc/android/sdk/demoapp/ui/screen/transactions/view/Transactions.kt @@ -106,18 +106,10 @@ private fun TransactionsMainContent( Button({ val memos = synchronizer.getMemos(it) queryScope.launch { - runCatching { - memos.toList().run { - Twig.debug { - "Transaction memos: count: $size, contains: " + - joinToString().ifEmpty { - "-" - } - } + memos.toList().run { + Twig.debug { + "Transaction memos: count: $size, contains: ${joinToString().ifEmpty { "-" }}" } - }.onFailure { - // https://github.com/zcash/librustzcash/issues/834 - Twig.error { "Failed to get memos with: $it" } } } }) { diff --git a/sdk-lib/src/main/java/cash/z/ecc/android/sdk/SdkSynchronizer.kt b/sdk-lib/src/main/java/cash/z/ecc/android/sdk/SdkSynchronizer.kt index 4dad23f1e..2adeaa4dc 100644 --- a/sdk-lib/src/main/java/cash/z/ecc/android/sdk/SdkSynchronizer.kt +++ b/sdk-lib/src/main/java/cash/z/ecc/android/sdk/SdkSynchronizer.kt @@ -64,7 +64,6 @@ import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.StateFlow import kotlinx.coroutines.flow.asStateFlow import kotlinx.coroutines.flow.combine -import kotlinx.coroutines.flow.filterNotNull import kotlinx.coroutines.flow.launchIn import kotlinx.coroutines.flow.map import kotlinx.coroutines.flow.onEach @@ -316,15 +315,25 @@ class SdkSynchronizer private constructor( override fun getMemos(transactionOverview: TransactionOverview): Flow { return storage.getNoteIds(transactionOverview.id).map { - when (transactionOverview.isSentTransaction) { - true -> { - backend.getSentMemoAsUtf8(it) - } - false -> { - backend.getReceivedMemoAsUtf8(it) + runCatching { + when (transactionOverview.isSentTransaction) { + true -> { + backend.getSentMemoAsUtf8(it) + } + false -> { + backend.getReceivedMemoAsUtf8(it) + } } - } - }.filterNotNull() + }.onFailure { + // https://github.com/zcash/librustzcash/issues/834 + Twig.error { "Failed to get memo with: $it" } + }.onSuccess { + Twig.debug { "Transaction memo queried: $it" } + }.fold( + onSuccess = { it ?: "" }, + onFailure = { "" } + ) + } } override fun getRecipients(transactionOverview: TransactionOverview): Flow { diff --git a/sdk-lib/src/main/java/cash/z/ecc/android/sdk/Synchronizer.kt b/sdk-lib/src/main/java/cash/z/ecc/android/sdk/Synchronizer.kt index 5a7c0e994..33507f970 100644 --- a/sdk-lib/src/main/java/cash/z/ecc/android/sdk/Synchronizer.kt +++ b/sdk-lib/src/main/java/cash/z/ecc/android/sdk/Synchronizer.kt @@ -281,7 +281,13 @@ interface Synchronizer { suspend fun quickRewind() /** - * Returns a list of memos for a transaction. + * Returns a stream of memos for a transaction. It works for both received and sent transaction. + * + * Note that this function internally resolves any error which comes, logs it, and then transforms it to an empty + * string. + * + * @param transactionOverview For which the memos will be queried + * @return Flow of memo strings */ fun getMemos(transactionOverview: TransactionOverview): Flow