Skip to content

Commit 2ebce23

Browse files
authored
Merge branch 'master' into fix/wake-node-polish
2 parents 9e20060 + 9f7e7e6 commit 2ebce23

File tree

15 files changed

+294
-133
lines changed

15 files changed

+294
-133
lines changed

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

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,6 @@ class CacheStore @Inject constructor(
9898
}
9999
}
100100

101-
suspend fun setLastLightningPayment(paymentId: String) {
102-
store.updateData { it.copy(lastLightningPaymentId = paymentId) }
103-
}
104-
105101
suspend fun setBackgroundReceive(details: NewTransactionSheetDetails) = store.updateData {
106102
it.copy(backgroundReceive = details)
107103
}
@@ -130,7 +126,6 @@ data class AppCacheData(
130126
val balance: BalanceState? = null,
131127
val backupStatuses: Map<BackupCategory, BackupItemStatus> = mapOf(),
132128
val deletedActivities: List<String> = listOf(),
133-
val lastLightningPaymentId: String? = null,
134129
val pendingBoostActivities: List<PendingBoostActivity> = listOf(),
135130
val backgroundReceive: NewTransactionSheetDetails? = null,
136131
) {

app/src/main/java/to/bitkit/domain/commands/NotifyPaymentReceivedHandler.kt

Lines changed: 42 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -36,38 +36,13 @@ class NotifyPaymentReceivedHandler @Inject constructor(
3636
): Result<NotifyPaymentReceived.Result> = withContext(ioDispatcher) {
3737
runCatching {
3838
val shouldShow = when (command) {
39-
is NotifyPaymentReceived.Command.Lightning -> true
40-
is NotifyPaymentReceived.Command.Onchain -> {
41-
activityRepo.handleOnchainTransactionReceived(command.event.txid, command.event.details)
42-
if (command.event.details.amountSats > 0) {
43-
delay(DELAY_FOR_ACTIVITY_SYNC_MS)
44-
activityRepo.shouldShowReceivedSheet(
45-
command.event.txid,
46-
command.event.details.amountSats.toULong()
47-
)
48-
} else {
49-
false
50-
}
51-
}
39+
is NotifyPaymentReceived.Command.Lightning -> shouldShowLightning(command)
40+
is NotifyPaymentReceived.Command.Onchain -> shouldShowOnchain(command)
5241
}
5342

5443
if (!shouldShow) return@runCatching NotifyPaymentReceived.Result.Skip
5544

56-
val details = NewTransactionSheetDetails(
57-
type = when (command) {
58-
is NotifyPaymentReceived.Command.Lightning -> NewTransactionSheetType.LIGHTNING
59-
is NotifyPaymentReceived.Command.Onchain -> NewTransactionSheetType.ONCHAIN
60-
},
61-
direction = NewTransactionSheetDirection.RECEIVED,
62-
paymentHashOrTxId = when (command) {
63-
is NotifyPaymentReceived.Command.Lightning -> command.event.paymentHash
64-
is NotifyPaymentReceived.Command.Onchain -> command.event.txid
65-
},
66-
sats = when (command) {
67-
is NotifyPaymentReceived.Command.Lightning -> (command.event.amountMsat / 1000u).toLong()
68-
is NotifyPaymentReceived.Command.Onchain -> command.event.details.amountSats
69-
},
70-
)
45+
val details = buildSheetDetails(command)
7146

7247
if (command.includeNotification) {
7348
val notification = buildNotificationContent(details.sats)
@@ -80,6 +55,45 @@ class NotifyPaymentReceivedHandler @Inject constructor(
8055
}
8156
}
8257

58+
private suspend fun shouldShowLightning(command: NotifyPaymentReceived.Command.Lightning): Boolean {
59+
val paymentId = command.event.paymentId ?: return false
60+
delay(DELAY_FOR_ACTIVITY_SYNC_MS)
61+
if (activityRepo.isActivitySeen(paymentId)) return false
62+
activityRepo.markActivityAsSeen(paymentId)
63+
return true
64+
}
65+
66+
private suspend fun shouldShowOnchain(command: NotifyPaymentReceived.Command.Onchain): Boolean {
67+
activityRepo.handleOnchainTransactionReceived(command.event.txid, command.event.details)
68+
if (command.event.details.amountSats <= 0) return false
69+
70+
delay(DELAY_FOR_ACTIVITY_SYNC_MS)
71+
val shouldShowSheet = activityRepo.shouldShowReceivedSheet(
72+
command.event.txid,
73+
command.event.details.amountSats.toULong()
74+
)
75+
if (shouldShowSheet) {
76+
activityRepo.markOnchainActivityAsSeen(command.event.txid)
77+
}
78+
return shouldShowSheet
79+
}
80+
81+
private fun buildSheetDetails(command: NotifyPaymentReceived.Command) = NewTransactionSheetDetails(
82+
type = when (command) {
83+
is NotifyPaymentReceived.Command.Lightning -> NewTransactionSheetType.LIGHTNING
84+
is NotifyPaymentReceived.Command.Onchain -> NewTransactionSheetType.ONCHAIN
85+
},
86+
direction = NewTransactionSheetDirection.RECEIVED,
87+
paymentHashOrTxId = when (command) {
88+
is NotifyPaymentReceived.Command.Lightning -> command.event.paymentHash
89+
is NotifyPaymentReceived.Command.Onchain -> command.event.txid
90+
},
91+
sats = when (command) {
92+
is NotifyPaymentReceived.Command.Lightning -> (command.event.amountMsat / 1000u).toLong()
93+
is NotifyPaymentReceived.Command.Onchain -> command.event.details.amountSats
94+
},
95+
)
96+
8397
private suspend fun buildNotificationContent(sats: Long): NotificationDetails {
8498
val settings = settingsStore.data.first()
8599
val title = context.getString(R.string.notification_received_title)

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

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ import javax.inject.Inject
4141
import javax.inject.Singleton
4242
import kotlin.time.Clock
4343
import kotlin.time.ExperimentalTime
44+
import com.synonym.bitkitcore.TransactionDetails as BitkitCoreTransactionDetails
4445

4546
private const val SYNC_TIMEOUT_MS = 40_000L
4647

@@ -232,6 +233,24 @@ class ActivityRepo @Inject constructor(
232233
return coreService.activity.shouldShowReceivedSheet(txid, value)
233234
}
234235

236+
suspend fun isActivitySeen(activityId: String): Boolean {
237+
return coreService.activity.isActivitySeen(activityId)
238+
}
239+
240+
suspend fun markActivityAsSeen(activityId: String) {
241+
coreService.activity.markActivityAsSeen(activityId)
242+
notifyActivitiesChanged()
243+
}
244+
245+
suspend fun markOnchainActivityAsSeen(txid: String) {
246+
coreService.activity.markOnchainActivityAsSeen(txid)
247+
notifyActivitiesChanged()
248+
}
249+
250+
suspend fun getTransactionDetails(txid: String): Result<BitkitCoreTransactionDetails?> = runCatching {
251+
coreService.activity.getTransactionDetails(txid)
252+
}
253+
235254
suspend fun getBoostTxDoesExist(boostTxIds: List<String>): Map<String, Boolean> {
236255
return coreService.activity.getBoostTxDoesExist(boostTxIds)
237256
}
@@ -529,7 +548,7 @@ class ActivityRepo @Inject constructor(
529548
preimage = null,
530549
createdAt = now,
531550
updatedAt = null,
532-
seenAt = null, // TODO implement synonymdev/bitkit-ios#270 changes
551+
seenAt = null,
533552
)
534553
)
535554
)

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,12 @@ class LightningRepo @Inject constructor(
334334
Result.success(Unit)
335335
}
336336

337+
fun syncAsync() = scope.launch {
338+
sync().onFailure { error ->
339+
Logger.warn("Sync failed", e = error, context = TAG)
340+
}
341+
}
342+
337343
/** Clear pending sync flag. Called when manual pull-to-refresh takes priority. */
338344
fun clearPendingSync() {
339345
syncPending.set(false)
@@ -728,12 +734,6 @@ class LightningRepo @Inject constructor(
728734
Result.success(payments)
729735
}
730736

731-
suspend fun getTransactionDetails(txid: Txid): Result<TransactionDetails?> = executeWhenNodeRunning(
732-
"Get transaction details by txid"
733-
) {
734-
Result.success(lightningService.getTransactionDetails(txid))
735-
}
736-
737737
suspend fun getAddressBalance(address: String): Result<ULong> = executeWhenNodeRunning("Get address balance") {
738738
runCatching {
739739
lightningService.getAddressBalance(address)

0 commit comments

Comments
 (0)