diff --git a/app/src/main/java/to/bitkit/services/CoreService.kt b/app/src/main/java/to/bitkit/services/CoreService.kt index e5719a47c..a501a1650 100644 --- a/app/src/main/java/to/bitkit/services/CoreService.kt +++ b/app/src/main/java/to/bitkit/services/CoreService.kt @@ -240,9 +240,14 @@ class ActivityService( // MARK: - Tag Methods - suspend fun appendTags(toActivityId: String, tags: List) { - ServiceQueue.CORE.background { - addTags(toActivityId, tags) + suspend fun appendTags(toActivityId: String, tags: List) : Result{ + return try { + ServiceQueue.CORE.background { + addTags(toActivityId, tags) + } + Result.success(Unit) + } catch (e: Exception) { + Result.failure(e) } } diff --git a/app/src/main/java/to/bitkit/viewmodels/AppViewModel.kt b/app/src/main/java/to/bitkit/viewmodels/AppViewModel.kt index 0c00de5c6..36d03f750 100644 --- a/app/src/main/java/to/bitkit/viewmodels/AppViewModel.kt +++ b/app/src/main/java/to/bitkit/viewmodels/AppViewModel.kt @@ -7,6 +7,7 @@ import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope import dagger.hilt.android.lifecycle.HiltViewModel import kotlinx.coroutines.CompletableDeferred +import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.delay import kotlinx.coroutines.flow.MutableSharedFlow import kotlinx.coroutines.flow.MutableStateFlow @@ -39,8 +40,12 @@ import to.bitkit.ui.components.BottomSheetType import to.bitkit.ui.screens.wallets.send.SendRoute import to.bitkit.ui.shared.toast.ToastEventBus import to.bitkit.utils.Logger +import uniffi.bitkitcore.Activity +import uniffi.bitkitcore.ActivityFilter +import uniffi.bitkitcore.ActivityType import uniffi.bitkitcore.LightningInvoice import uniffi.bitkitcore.OnChainInvoice +import uniffi.bitkitcore.PaymentType import uniffi.bitkitcore.Scanner import javax.inject.Inject @@ -455,6 +460,7 @@ class AppViewModel @Inject constructor( val result = sendOnchain(validatedAddress.address, amount) if (result.isSuccess) { val txId = result.getOrNull() + attachTagsToActivity(paymentHashOrTxId = txId, type = ActivityFilter.ONCHAIN) Logger.info("Onchain send result txid: $txId") setSendEffect( SendEffect.PaymentSuccess( @@ -482,6 +488,7 @@ class AppViewModel @Inject constructor( if (result.isSuccess) { val paymentHash = result.getOrNull() Logger.info("Lightning send result payment hash: $paymentHash") + attachTagsToActivity(paymentHashOrTxId = paymentHash, type = ActivityFilter.LIGHTNING) setSendEffect(SendEffect.PaymentSuccess()) resetSendState() } else { @@ -493,6 +500,55 @@ class AppViewModel @Inject constructor( } } + private fun attachTagsToActivity(paymentHashOrTxId: String?, type: ActivityFilter) { + val tags = _sendUiState.value.selectedTags + Logger.debug("attachTagsToActivity $tags") + if (tags.isEmpty()) { + Logger.debug("selectedTags empty") + return + } + + if (paymentHashOrTxId == null) { + Logger.error(msg = "null paymentHashOrTxId") + return + } + + viewModelScope.launch(Dispatchers.IO) { + val activity = coreService.activity.get(filter = type, txType = PaymentType.SENT, limit = 1u).firstOrNull() + + if (activity == null) { + Logger.error(msg = "Activity not found") + return@launch + } + + when (activity) { + is Activity.Lightning -> { + if (paymentHashOrTxId == activity.v1.id) { + coreService.activity.appendTags( + toActivityId = activity.v1.id, + tags = tags + ).onFailure { + Logger.error("Error attaching tags $tags") + } + } else { + Logger.error("Different activity id. Expected: $paymentHashOrTxId found: ${activity.v1.id}") + } + } + + is Activity.Onchain -> { + if (paymentHashOrTxId == activity.v1.txId) { + coreService.activity.appendTags( + toActivityId = activity.v1.id, + tags = tags + ) + } else { + Logger.error("Different txId. Expected: $paymentHashOrTxId found: ${activity.v1.txId}") + } + } + } + } + } + private suspend fun sendOnchain(address: String, amount: ULong): Result { return runCatching { lightningService.send(address = address, amount) } .onFailure {