Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions app/src/main/java/to/bitkit/services/CoreService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -240,9 +240,14 @@ class ActivityService(

// MARK: - Tag Methods

suspend fun appendTags(toActivityId: String, tags: List<String>) {
ServiceQueue.CORE.background {
addTags(toActivityId, tags)
suspend fun appendTags(toActivityId: String, tags: List<String>) : Result<Unit>{
return try {
ServiceQueue.CORE.background {
addTags(toActivityId, tags)
}
Result.success(Unit)
} catch (e: Exception) {
Result.failure(e)
}
}

Expand Down
56 changes: 56 additions & 0 deletions app/src/main/java/to/bitkit/viewmodels/AppViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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 {
Expand All @@ -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<Txid> {
return runCatching { lightningService.send(address = address, amount) }
.onFailure {
Expand Down