Skip to content

Commit 187875d

Browse files
authored
Merge pull request #334 from synonymdev/feat/ln-sent-value-sheet
fix: Display LN amount in send success sheet
2 parents d50e308 + 27a927b commit 187875d

File tree

1 file changed

+40
-10
lines changed

1 file changed

+40
-10
lines changed

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

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ import to.bitkit.ext.minWithdrawableSat
5757
import to.bitkit.ext.rawId
5858
import to.bitkit.ext.removeSpaces
5959
import to.bitkit.ext.setClipboardText
60+
import to.bitkit.ext.totalValue
6061
import to.bitkit.ext.watchUntil
6162
import to.bitkit.models.FeeRate
6263
import to.bitkit.models.NewTransactionSheetDetails
@@ -137,6 +138,8 @@ class AppViewModel @Inject constructor(
137138
private val _showForgotPinSheet = MutableStateFlow(false)
138139
val showForgotPinSheet = _showForgotPinSheet.asStateFlow()
139140

141+
private val processedPayments = mutableSetOf<String>()
142+
140143
fun setShowForgotPin(value: Boolean) {
141144
_showForgotPinSheet.value = value
142145
}
@@ -236,15 +239,26 @@ class AppViewModel @Inject constructor(
236239
}
237240

238241
is Event.PaymentSuccessful -> {
239-
// TODO: fee is not the sats sent. Need to get this amount from elsewhere like send flow or something.
240-
handlePaymentSuccess(
241-
NewTransactionSheetDetails(
242-
type = NewTransactionSheetType.LIGHTNING,
243-
direction = NewTransactionSheetDirection.SENT,
244-
paymentHashOrTxId = event.paymentHash,
245-
sats = ((event.feePaidMsat ?: 0u) / 1000u).toLong(),
246-
),
247-
)
242+
val paymentHash = event.paymentHash
243+
// TODO Temporary solution while LDK node don't returns the sent value in the event
244+
activityRepo.findActivityByPaymentId(
245+
paymentHashOrTxId = paymentHash,
246+
type = ActivityFilter.LIGHTNING,
247+
txType = PaymentType.SENT,
248+
retry = true
249+
).onSuccess { activity ->
250+
handlePaymentSuccess(
251+
NewTransactionSheetDetails(
252+
type = NewTransactionSheetType.LIGHTNING,
253+
direction = NewTransactionSheetDirection.SENT,
254+
paymentHashOrTxId = event.paymentHash,
255+
sats = activity.totalValue().toLong(),
256+
),
257+
)
258+
}.onFailure { e ->
259+
Logger.warn("Failed displaying sheet for event: $Event", e)
260+
}
261+
248262
}
249263

250264
is Event.PaymentClaimable -> Unit
@@ -973,7 +987,14 @@ class AppViewModel @Inject constructor(
973987
isReceive = false,
974988
tags = tags
975989
)
976-
setSendEffect(SendEffect.PaymentSuccess())
990+
handlePaymentSuccess(
991+
NewTransactionSheetDetails(
992+
type = NewTransactionSheetType.LIGHTNING,
993+
direction = NewTransactionSheetDirection.SENT,
994+
paymentHashOrTxId = paymentHash,
995+
sats = paymentAmount.toLong(), // TODO Add fee when available
996+
),
997+
)
977998
}.onFailure { e ->
978999
Logger.error("Error sending lightning payment", e, context = TAG)
9791000
toast(e)
@@ -1225,6 +1246,8 @@ class AppViewModel @Inject constructor(
12251246
feeRates = rates,
12261247
)
12271248
}
1249+
1250+
processedPayments.clear()
12281251
}
12291252
// endregion
12301253

@@ -1441,6 +1464,13 @@ class AppViewModel @Inject constructor(
14411464
}
14421465

14431466
private fun handlePaymentSuccess(details: NewTransactionSheetDetails) {
1467+
details.paymentHashOrTxId?.let {
1468+
if (!processedPayments.add(it)) {
1469+
Logger.debug("Payment $it already processed, skipping duplicate", context = TAG)
1470+
return
1471+
}
1472+
}
1473+
14441474
_successSendUiState.update { details }
14451475
setSendEffect(SendEffect.PaymentSuccess(details))
14461476
}

0 commit comments

Comments
 (0)