@@ -7,6 +7,7 @@ import androidx.lifecycle.ViewModel
77import androidx.lifecycle.viewModelScope
88import dagger.hilt.android.lifecycle.HiltViewModel
99import kotlinx.coroutines.CompletableDeferred
10+ import kotlinx.coroutines.Dispatchers
1011import kotlinx.coroutines.delay
1112import kotlinx.coroutines.flow.MutableSharedFlow
1213import kotlinx.coroutines.flow.MutableStateFlow
@@ -39,8 +40,12 @@ import to.bitkit.ui.components.BottomSheetType
3940import to.bitkit.ui.screens.wallets.send.SendRoute
4041import to.bitkit.ui.shared.toast.ToastEventBus
4142import to.bitkit.utils.Logger
43+ import uniffi.bitkitcore.Activity
44+ import uniffi.bitkitcore.ActivityFilter
45+ import uniffi.bitkitcore.ActivityType
4246import uniffi.bitkitcore.LightningInvoice
4347import uniffi.bitkitcore.OnChainInvoice
48+ import uniffi.bitkitcore.PaymentType
4449import uniffi.bitkitcore.Scanner
4550import javax.inject.Inject
4651
@@ -455,6 +460,7 @@ class AppViewModel @Inject constructor(
455460 val result = sendOnchain(validatedAddress.address, amount)
456461 if (result.isSuccess) {
457462 val txId = result.getOrNull()
463+ attachTagsToActivity(paymentHashOrTxId = txId, type = ActivityFilter .ONCHAIN )
458464 Logger .info(" Onchain send result txid: $txId " )
459465 setSendEffect(
460466 SendEffect .PaymentSuccess (
@@ -482,6 +488,7 @@ class AppViewModel @Inject constructor(
482488 if (result.isSuccess) {
483489 val paymentHash = result.getOrNull()
484490 Logger .info(" Lightning send result payment hash: $paymentHash " )
491+ attachTagsToActivity(paymentHashOrTxId = paymentHash, type = ActivityFilter .LIGHTNING )
485492 setSendEffect(SendEffect .PaymentSuccess ())
486493 resetSendState()
487494 } else {
@@ -493,6 +500,55 @@ class AppViewModel @Inject constructor(
493500 }
494501 }
495502
503+ private fun attachTagsToActivity (paymentHashOrTxId : String? , type : ActivityFilter ) {
504+ val tags = _sendUiState .value.selectedTags
505+ Logger .debug(" attachTagsToActivity $tags " )
506+ if (tags.isEmpty()) {
507+ Logger .debug(" selectedTags empty" )
508+ return
509+ }
510+
511+ if (paymentHashOrTxId == null ) {
512+ Logger .error(msg = " null paymentHashOrTxId" )
513+ return
514+ }
515+
516+ viewModelScope.launch(Dispatchers .IO ) {
517+ val activity = coreService.activity.get(filter = type, txType = PaymentType .SENT , limit = 1u ).firstOrNull()
518+
519+ if (activity == null ) {
520+ Logger .error(msg = " Activity not found" )
521+ return @launch
522+ }
523+
524+ when (activity) {
525+ is Activity .Lightning -> {
526+ if (paymentHashOrTxId == activity.v1.id) {
527+ coreService.activity.appendTags(
528+ toActivityId = activity.v1.id,
529+ tags = tags
530+ ).onFailure {
531+ Logger .error(" Error attaching tags $tags " )
532+ }
533+ } else {
534+ Logger .error(" Different activity id. Expected: $paymentHashOrTxId found: ${activity.v1.id} " )
535+ }
536+ }
537+
538+ is Activity .Onchain -> {
539+ if (paymentHashOrTxId == activity.v1.txId) {
540+ coreService.activity.appendTags(
541+ toActivityId = activity.v1.id,
542+ tags = tags
543+ )
544+ } else {
545+ Logger .error(" Different txId. Expected: $paymentHashOrTxId found: ${activity.v1.txId} " )
546+ }
547+ }
548+ }
549+ }
550+ }
551+
496552 private suspend fun sendOnchain (address : String , amount : ULong ): Result <Txid > {
497553 return runCatching { lightningService.send(address = address, amount) }
498554 .onFailure {
0 commit comments