@@ -95,8 +95,16 @@ class WalletRepo @Inject constructor(
9595 suspend fun refreshBip21 (): Result <Unit > = withContext(bgDispatcher) {
9696 Logger .debug(" Refreshing bip21" , context = TAG )
9797
98+ // Get old payment ID and tags before refreshing (which may change payment ID)
9899 val oldPaymentId = paymentId()
99- val currentTags = _walletState .value.selectedTags
100+ val tagsToMigrate = if (oldPaymentId != null && oldPaymentId.isNotEmpty()) {
101+ preActivityMetadataRepo
102+ .getPreActivityMetadata(oldPaymentId, searchByAddress = false )
103+ .getOrNull()
104+ ?.tags ? : emptyList()
105+ } else {
106+ emptyList()
107+ }
100108
101109 val (_, shouldBlockLightningReceive) = coreService.checkGeoBlock()
102110 _walletState .update {
@@ -109,56 +117,40 @@ class WalletRepo @Inject constructor(
109117 val newPaymentId = paymentId()
110118 val newBip21Url = _walletState .value.bip21
111119 if (newPaymentId != null && newPaymentId.isNotEmpty() && newBip21Url.isNotEmpty()) {
112- handlePreActivityMetadataForNewPaymentId (newPaymentId, oldPaymentId, currentTags , newBip21Url)
120+ persistPreActivityMetadata (newPaymentId, tagsToMigrate , newBip21Url)
113121 }
114122
115123 return @withContext Result .success(Unit )
116124 }
117125
118- private suspend fun handlePreActivityMetadataForNewPaymentId (
119- newPaymentId : String ,
120- oldPaymentId : String? ,
121- currentTags : List <String >,
122- newBip21Url : String ,
126+ private suspend fun persistPreActivityMetadata (
127+ paymentId : String ,
128+ tags : List <String >,
129+ bip21Url : String ,
123130 ) {
124131 val onChainAddress = getOnchainAddress()
125132 val paymentHash = runCatching {
126- when (val decoded = decode(newBip21Url )) {
133+ when (val decoded = decode(bip21Url )) {
127134 is Scanner .Lightning -> decoded.invoice.paymentHash.toHex()
128135 is Scanner .OnChain -> decoded.extractLightningHash()
129136 else -> null
130137 }
131138 }.getOrNull()
132139
133- val existingMetadata = preActivityMetadataRepo
134- .getPreActivityMetadata(newPaymentId, searchByAddress = false )
135- .getOrNull()
136-
137- if (existingMetadata == null ) {
138- // Create new metadata with current tags
139- val preActivityMetadata = PreActivityMetadata (
140- paymentId = newPaymentId,
141- createdAt = nowTimestamp().toEpochMilli().toULong(),
142- tags = currentTags,
143- paymentHash = paymentHash,
144- txId = null ,
145- address = onChainAddress,
146- isReceive = true ,
147- feeRate = 0u ,
148- isTransfer = false ,
149- channelId = " " ,
150- )
151- preActivityMetadataRepo.upsertPreActivityMetadata(listOf (preActivityMetadata))
152- Logger .debug(" Created pre-activity metadata: paymentId=$newPaymentId , tags=$currentTags " , context = TAG )
153- } else if (newPaymentId != oldPaymentId) {
154- if (currentTags.toSet() != existingMetadata.tags.toSet()) {
155- preActivityMetadataRepo.resetPreActivityMetadataTags(newPaymentId)
156- if (currentTags.isNotEmpty()) {
157- preActivityMetadataRepo.addPreActivityMetadataTags(newPaymentId, currentTags)
158- }
159- Logger .debug(" Updated tags for new payment ID: $newPaymentId , tags=$currentTags " , context = TAG )
160- }
161- }
140+ val preActivityMetadata = PreActivityMetadata (
141+ paymentId = paymentId,
142+ createdAt = nowTimestamp().toEpochMilli().toULong(),
143+ tags = tags,
144+ paymentHash = paymentHash,
145+ txId = null ,
146+ address = onChainAddress,
147+ isReceive = true ,
148+ feeRate = 0u ,
149+ isTransfer = false ,
150+ channelId = " " ,
151+ )
152+
153+ preActivityMetadataRepo.addPreActivityMetadata(preActivityMetadata)
162154 }
163155
164156 suspend fun observeLdkWallet () = withContext(bgDispatcher) {
@@ -511,7 +503,14 @@ class WalletRepo @Inject constructor(
511503 ): Result <Unit > = withContext(bgDispatcher) {
512504 return @withContext runCatching {
513505 val oldPaymentId = paymentId()
514- val currentTags = _walletState .value.selectedTags
506+ val tagsToMigrate = if (oldPaymentId != null && oldPaymentId.isNotEmpty()) {
507+ preActivityMetadataRepo
508+ .getPreActivityMetadata(oldPaymentId, searchByAddress = false )
509+ .getOrNull()
510+ ?.tags ? : emptyList()
511+ } else {
512+ emptyList()
513+ }
515514
516515 setBip21AmountSats(amountSats)
517516 setBip21Description(description)
@@ -527,10 +526,10 @@ class WalletRepo @Inject constructor(
527526 val newBip21Url = updateBip21Url(amountSats, description)
528527 setBip21(newBip21Url)
529528
530- // Update metadata with current tags if payment ID changed (e.g., new lightning invoice created)
529+ // Persist metadata with migrated tags
531530 val newPaymentId = paymentId()
532531 if (newPaymentId != null && newPaymentId.isNotEmpty() && newBip21Url.isNotEmpty()) {
533- handlePreActivityMetadataForNewPaymentId (newPaymentId, oldPaymentId, currentTags , newBip21Url)
532+ persistPreActivityMetadata (newPaymentId, tagsToMigrate , newBip21Url)
534533 }
535534 }.onFailure { e ->
536535 Logger .error(" Update BIP21 invoice error" , e, context = TAG )
0 commit comments