Skip to content

Commit 3160b9a

Browse files
authored
Merge pull request #65 from synonymdev/feat/save-tags
Attach tags to Activity (Send)
2 parents 5f2dbd6 + df12c8c commit 3160b9a

File tree

2 files changed

+64
-3
lines changed

2 files changed

+64
-3
lines changed

app/src/main/java/to/bitkit/services/CoreService.kt

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -240,9 +240,14 @@ class ActivityService(
240240

241241
// MARK: - Tag Methods
242242

243-
suspend fun appendTags(toActivityId: String, tags: List<String>) {
244-
ServiceQueue.CORE.background {
245-
addTags(toActivityId, tags)
243+
suspend fun appendTags(toActivityId: String, tags: List<String>) : Result<Unit>{
244+
return try {
245+
ServiceQueue.CORE.background {
246+
addTags(toActivityId, tags)
247+
}
248+
Result.success(Unit)
249+
} catch (e: Exception) {
250+
Result.failure(e)
246251
}
247252
}
248253

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

Lines changed: 56 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,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

Comments
 (0)