Skip to content

Commit 2c1f6af

Browse files
committed
feat: attach tags on activity sync
1 parent 05b4957 commit 2c1f6af

File tree

4 files changed

+36
-4
lines changed

4 files changed

+36
-4
lines changed

app/src/main/java/to/bitkit/data/dao/InvoiceTagDao.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ interface InvoiceTagDao {
1313
@Insert(onConflict = OnConflictStrategy.REPLACE)
1414
suspend fun saveInvoice(invoiceTag: InvoiceTagEntity)
1515

16+
@Query("SELECT * FROM invoice_tag")
17+
suspend fun getAll(): List<InvoiceTagEntity>
18+
1619
@Query("SELECT * FROM invoice_tag WHERE paymentHash = :paymentHash LIMIT 1")
1720
suspend fun searchInvoice(paymentHash: String): InvoiceTagEntity?
1821

app/src/main/java/to/bitkit/repositories/ActivityRepo.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ class ActivityRepo @Inject constructor(
105105
suspend fun findActivityByPaymentId(
106106
paymentHashOrTxId: String,
107107
type: ActivityFilter,
108-
txType: PaymentType,
108+
txType: PaymentType?,
109109
retry: Boolean = true,
110110
): Result<Activity> = withContext(bgDispatcher) {
111111
if (paymentHashOrTxId.isEmpty()) {

app/src/main/java/to/bitkit/repositories/WalletRepo.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,12 @@ class WalletRepo @Inject constructor(
453453
}
454454
}
455455

456+
suspend fun getAllInvoiceTags() : Result<List<InvoiceTagEntity>> = withContext(bgDispatcher) {
457+
return@withContext runCatching {
458+
db.invoiceTagDao().getAll()
459+
}
460+
}
461+
456462
suspend fun searchInvoice(txId: Txid): Result<InvoiceTagEntity> = withContext(bgDispatcher) {
457463
return@withContext try {
458464
val invoiceTag = db.invoiceTagDao().searchInvoice(paymentHash = txId) ?: return@withContext Result.failure(

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

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@ import kotlinx.coroutines.flow.debounce
1515
import kotlinx.coroutines.launch
1616
import kotlinx.coroutines.withContext
1717
import to.bitkit.di.BgDispatcher
18+
import to.bitkit.ext.rawId
1819
import to.bitkit.repositories.ActivityRepo
19-
import to.bitkit.repositories.LightningRepo
20+
import to.bitkit.repositories.WalletRepo
2021
import to.bitkit.services.CoreService
2122
import to.bitkit.services.LdkNodeEventBus
2223
import to.bitkit.ui.screens.wallets.activity.components.ActivityTab
@@ -27,9 +28,9 @@ import javax.inject.Inject
2728
class ActivityListViewModel @Inject constructor(
2829
@BgDispatcher private val bgDispatcher: CoroutineDispatcher,
2930
private val coreService: CoreService,
30-
private val lightningRepo: LightningRepo,
31+
private val walletRepo: WalletRepo,
3132
private val ldkNodeEventBus: LdkNodeEventBus,
32-
private val activityRepo: ActivityRepo
33+
private val activityRepo: ActivityRepo,
3334
) : ViewModel() {
3435
private val _filteredActivities = MutableStateFlow<List<Activity>?>(null)
3536
val filteredActivities = _filteredActivities.asStateFlow()
@@ -224,6 +225,7 @@ class ActivityListViewModel @Inject constructor(
224225
viewModelScope.launch {
225226
activityRepo.syncActivities().onSuccess {
226227
syncState()
228+
syncActivityTags()
227229
}.onFailure { e ->
228230
Logger.error("Failed to sync ldk-node payments", e)
229231
}
@@ -243,4 +245,25 @@ class ActivityListViewModel @Inject constructor(
243245
syncState()
244246
}
245247
}
248+
249+
private fun syncActivityTags() {
250+
viewModelScope.launch {
251+
walletRepo.getAllInvoiceTags().onSuccess { invoiceTags ->
252+
invoiceTags.forEach { tagEntity ->
253+
activityRepo.findActivityByPaymentId(
254+
paymentHashOrTxId = tagEntity.paymentHash,
255+
type = ActivityFilter.ALL,
256+
txType = null
257+
).onSuccess { activity ->
258+
activityRepo.addTagsToActivity(
259+
activityId = activity.rawId(),
260+
tags = tagEntity.tags
261+
).onSuccess {
262+
walletRepo.deleteInvoice(tagEntity.paymentHash)
263+
}
264+
}
265+
}
266+
}
267+
}
268+
}
246269
}

0 commit comments

Comments
 (0)