Skip to content

Commit a597e2a

Browse files
committed
feat: attachTagsToActivity in success send flow
1 parent 90e34ee commit a597e2a

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

app/src/main/java/to/bitkit/viewmodels/AppViewModel.kt

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import androidx.lifecycle.ViewModel
77
import androidx.lifecycle.viewModelScope
88
import dagger.hilt.android.lifecycle.HiltViewModel
99
import kotlinx.coroutines.CompletableDeferred
10+
import kotlinx.coroutines.Dispatchers
1011
import kotlinx.coroutines.delay
1112
import kotlinx.coroutines.flow.MutableSharedFlow
1213
import kotlinx.coroutines.flow.MutableStateFlow
@@ -39,8 +40,12 @@ import to.bitkit.ui.components.BottomSheetType
3940
import to.bitkit.ui.screens.wallets.send.SendRoute
4041
import to.bitkit.ui.shared.toast.ToastEventBus
4142
import to.bitkit.utils.Logger
43+
import uniffi.bitkitcore.Activity
44+
import uniffi.bitkitcore.ActivityFilter
45+
import uniffi.bitkitcore.ActivityType
4246
import uniffi.bitkitcore.LightningInvoice
4347
import uniffi.bitkitcore.OnChainInvoice
48+
import uniffi.bitkitcore.PaymentType
4449
import uniffi.bitkitcore.Scanner
4550
import 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,34 @@ class AppViewModel @Inject constructor(
493500
}
494501
}
495502

503+
private fun attachTagsToActivity(paymentHashOrTxId: String?, type: ActivityFilter) {
504+
if (_sendUiState.value.selectedTags.isEmpty()) return
505+
if (paymentHashOrTxId == null) return
506+
507+
viewModelScope.launch(Dispatchers.IO) {
508+
val activity = coreService.activity.get(filter = type, txType = PaymentType.SENT, limit = 1u).firstOrNull()
509+
510+
if (activity == null) {
511+
Logger.error(msg = "Activity not found")
512+
return@launch
513+
}
514+
515+
//TODO COMPARE PAYMENT HASH AND TX ID
516+
517+
when (activity) {
518+
is Activity.Lightning -> coreService.activity.appendTags(
519+
toActivityId = activity.v1.id,
520+
tags = _sendUiState.value.selectedTags
521+
)
522+
523+
is Activity.Onchain -> coreService.activity.appendTags(
524+
toActivityId = activity.v1.id,
525+
tags = _sendUiState.value.selectedTags
526+
)
527+
}
528+
}
529+
}
530+
496531
private suspend fun sendOnchain(address: String, amount: ULong): Result<Txid> {
497532
return runCatching { lightningService.send(address = address, amount) }
498533
.onFailure {

0 commit comments

Comments
 (0)