Skip to content

Commit 5643d43

Browse files
committed
feat: rename InvoiceTagEntity.kt to TagMetadataEntity.kt and add more parameters
1 parent e58873c commit 5643d43

File tree

7 files changed

+103
-63
lines changed

7 files changed

+103
-63
lines changed

app/src/main/java/to/bitkit/data/AppDb.kt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,22 @@ import dagger.assisted.AssistedInject
1919
import kotlinx.coroutines.coroutineScope
2020
import kotlinx.coroutines.flow.Flow
2121
import to.bitkit.BuildConfig
22-
import to.bitkit.data.dao.InvoiceTagDao
22+
import to.bitkit.data.dao.TagMetadataDao
2323
import to.bitkit.data.entities.ConfigEntity
24-
import to.bitkit.data.entities.InvoiceTagEntity
24+
import to.bitkit.data.entities.TagMetadataEntity
2525
import to.bitkit.data.typeConverters.StringListConverter
2626

2727
@Database(
2828
entities = [
2929
ConfigEntity::class,
30-
InvoiceTagEntity::class,
30+
TagMetadataEntity::class,
3131
],
32-
version = 2,
32+
version = 3,
3333
)
3434
@TypeConverters(StringListConverter::class)
3535
abstract class AppDb : RoomDatabase() {
3636
abstract fun configDao(): ConfigDao
37-
abstract fun invoiceTagDao(): InvoiceTagDao
37+
abstract fun tagMetadataDao(): TagMetadataDao
3838

3939
companion object {
4040
private const val DB_NAME = "${BuildConfig.APPLICATION_ID}.sqlite"

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

Lines changed: 0 additions & 33 deletions
This file was deleted.
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package to.bitkit.data.dao
2+
3+
import androidx.room.Dao
4+
import androidx.room.Delete
5+
import androidx.room.Insert
6+
import androidx.room.OnConflictStrategy
7+
import androidx.room.Query
8+
import to.bitkit.data.entities.TagMetadataEntity
9+
10+
@Dao
11+
interface TagMetadataDao {
12+
13+
@Insert(onConflict = OnConflictStrategy.REPLACE)
14+
suspend fun saveTagMetadata(tagMetadata: TagMetadataEntity)
15+
16+
@Query("SELECT * FROM tag_metadata")
17+
suspend fun getAll(): List<TagMetadataEntity>
18+
19+
// Search by payment hash (for invoices)
20+
@Query("SELECT * FROM tag_metadata WHERE paymentHash = :paymentHash LIMIT 1")
21+
suspend fun searchByPaymentHash(paymentHash: String): TagMetadataEntity?
22+
23+
// Search by transaction ID
24+
@Query("SELECT * FROM tag_metadata WHERE txId = :txId LIMIT 1")
25+
suspend fun searchByTxId(txId: String): TagMetadataEntity?
26+
27+
// Search by address
28+
@Query("SELECT * FROM tag_metadata WHERE address = :address")
29+
suspend fun searchByAddress(address: String): List<TagMetadataEntity>
30+
31+
// Search by primary key (id)
32+
@Query("SELECT * FROM tag_metadata WHERE id = :id LIMIT 1")
33+
suspend fun searchById(id: String): TagMetadataEntity?
34+
35+
// Get all receive transactions
36+
@Query("SELECT * FROM tag_metadata WHERE isReceive = 1")
37+
suspend fun getAllReceiveTransactions(): List<TagMetadataEntity>
38+
39+
// Get all send transactions
40+
@Query("SELECT * FROM tag_metadata WHERE isReceive = 0")
41+
suspend fun getAllSendTransactions(): List<TagMetadataEntity>
42+
43+
@Delete
44+
suspend fun deleteTagMetadata(tagMetadata: TagMetadataEntity)
45+
46+
@Query("DELETE FROM tag_metadata WHERE paymentHash = :paymentHash")
47+
suspend fun deleteByPaymentHash(paymentHash: String)
48+
49+
@Query("DELETE FROM tag_metadata WHERE txId = :txId")
50+
suspend fun deleteByTxId(txId: String)
51+
52+
@Query("DELETE FROM tag_metadata WHERE id = :id")
53+
suspend fun deleteById(id: String)
54+
55+
@Query("DELETE FROM tag_metadata")
56+
suspend fun deleteAll()
57+
58+
@Query("DELETE FROM tag_metadata WHERE createdAt < :expirationTimeStamp")
59+
suspend fun deleteExpired(expirationTimeStamp: Long)
60+
}

app/src/main/java/to/bitkit/data/entities/InvoiceTagEntity.kt

Lines changed: 0 additions & 11 deletions
This file was deleted.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package to.bitkit.data.entities
2+
3+
import androidx.room.Entity
4+
import androidx.room.PrimaryKey
5+
6+
@Entity(tableName = "tag_metadata")
7+
/**
8+
* @param id This will be paymentHash, txId, or address depending on context
9+
* @param txId on-chain transaction id
10+
* @param address on-chain address
11+
* @param isReceive true for receive, false for send
12+
* */
13+
data class TagMetadataEntity(
14+
@PrimaryKey val id: String,
15+
val paymentHash: String? = null,
16+
val txId: String? = null,
17+
val address: String,
18+
val isReceive: Boolean,
19+
val tags: List<String>,
20+
val createdAt: Long,
21+
)

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

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import org.lightningdevkit.ldknode.Txid
2020
import to.bitkit.data.AppDb
2121
import to.bitkit.data.CacheStore
2222
import to.bitkit.data.SettingsStore
23-
import to.bitkit.data.entities.InvoiceTagEntity
23+
import to.bitkit.data.entities.TagMetadataEntity
2424
import to.bitkit.data.keychain.Keychain
2525
import to.bitkit.di.BgDispatcher
2626
import to.bitkit.env.Env
@@ -440,10 +440,12 @@ class WalletRepo @Inject constructor(
440440
}
441441

442442
paymentHashOrAddress?.let {
443-
db.invoiceTagDao().saveInvoice(
444-
invoiceTag = InvoiceTagEntity(
445-
paymentHash = paymentHashOrAddress,
443+
db.tagMetadataDao().saveTagMetadata(
444+
tagMetadata = TagMetadataEntity(
445+
id = paymentHashOrAddress,
446446
tags = tags,
447+
address = "",
448+
isReceive = true,
447449
createdAt = System.currentTimeMillis()
448450
)
449451
)
@@ -453,17 +455,18 @@ class WalletRepo @Inject constructor(
453455
}
454456
}
455457

456-
suspend fun getAllInvoiceTags() : Result<List<InvoiceTagEntity>> = withContext(bgDispatcher) {
458+
suspend fun getAllInvoiceTags(): Result<List<TagMetadataEntity>> = withContext(bgDispatcher) {
457459
return@withContext runCatching {
458-
db.invoiceTagDao().getAll()
460+
db.tagMetadataDao().getAll()
459461
}
460462
}
461463

462-
suspend fun searchInvoice(txId: Txid): Result<InvoiceTagEntity> = withContext(bgDispatcher) {
464+
suspend fun searchInvoiceByPaymentHash(paymentHash: String): Result<TagMetadataEntity> = withContext(bgDispatcher) {
463465
return@withContext try {
464-
val invoiceTag = db.invoiceTagDao().searchInvoice(paymentHash = txId) ?: return@withContext Result.failure(
465-
Exception("Invoice not found")
466-
)
466+
val invoiceTag =
467+
db.tagMetadataDao().searchByPaymentHash(paymentHash = paymentHash) ?: return@withContext Result.failure(
468+
Exception("Invoice not found")
469+
)
467470
Result.success(invoiceTag)
468471
} catch (e: Throwable) {
469472
Logger.error("searchInvoice error", e, context = TAG)
@@ -473,15 +476,15 @@ class WalletRepo @Inject constructor(
473476

474477
suspend fun deleteInvoice(txId: Txid) = withContext(bgDispatcher) {
475478
try {
476-
db.invoiceTagDao().deleteInvoiceByPaymentHash(paymentHash = txId)
479+
db.tagMetadataDao().deleteByPaymentHash(paymentHash = txId)
477480
} catch (e: Throwable) {
478481
Logger.error("deleteInvoice error", e, context = TAG)
479482
}
480483
}
481484

482485
suspend fun deleteAllInvoices() = withContext(bgDispatcher) {
483486
try {
484-
db.invoiceTagDao().deleteAllInvoices()
487+
db.tagMetadataDao().deleteAll()
485488
} catch (e: Throwable) {
486489
Logger.error("deleteAllInvoices error", e, context = TAG)
487490
}
@@ -490,7 +493,7 @@ class WalletRepo @Inject constructor(
490493
suspend fun deleteExpiredInvoices() = withContext(bgDispatcher) {
491494
try {
492495
val twoDaysAgoMillis = Clock.System.now().minus(2.days).toEpochMilliseconds()
493-
db.invoiceTagDao().deleteExpiredInvoices(expirationTimeStamp = twoDaysAgoMillis)
496+
db.tagMetadataDao().deleteExpired(expirationTimeStamp = twoDaysAgoMillis)
494497
} catch (e: Throwable) {
495498
Logger.error("deleteExpiredInvoices error", e, context = TAG)
496499
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ class AppViewModel @Inject constructor(
261261
}
262262

263263
private suspend fun handleTags(event: Event.PaymentReceived) {
264-
val tags = walletRepo.searchInvoice(txId = event.paymentHash).getOrNull()?.tags.orEmpty()
264+
val tags = walletRepo.searchInvoiceByPaymentHash(paymentHash = event.paymentHash).getOrNull()?.tags.orEmpty()
265265
activityRepo.addTagsToTransaction(
266266
paymentHashOrTxId = event.paymentHash,
267267
type = ActivityFilter.LIGHTNING,

0 commit comments

Comments
 (0)