Skip to content
9 changes: 9 additions & 0 deletions app/src/main/java/to/bitkit/repositories/LightningRepo.kt
Original file line number Diff line number Diff line change
Expand Up @@ -685,6 +685,15 @@ class LightningRepo @Inject constructor(
fun canSend(amountSats: ULong): Boolean =
_lightningState.value.nodeLifecycleState.isRunning() && lightningService.canSend(amountSats)

suspend fun canSendAsync(amountSats: ULong): Result<Boolean> = executeWhenNodeRunning("canSendAsync") {
withTimeoutOrNull(3.seconds) {
while (_lightningState.value.channels.isEmpty()) {
delay(1.seconds)
}
}
Result.success(lightningService.canSend(amountSats))
}

fun getSyncFlow(): Flow<Unit> = lightningService.syncFlow()

fun getNodeId(): String? =
Expand Down
21 changes: 14 additions & 7 deletions app/src/main/java/to/bitkit/viewmodels/AppViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -538,13 +538,6 @@
runCatching { decode(bolt11) }.getOrNull()
?.let { it as? Scanner.Lightning }
?.invoice
?.takeIf { invoice ->
val canSend = lightningRepo.canSend(invoice.amountSatoshis.coerceAtLeast(1u))
if (!canSend) {
Logger.debug("Cannot pay unified invoice using LN, defaulting to onchain-only", context = TAG)
}
return@takeIf canSend
}
}
_sendUiState.update {
it.copy(
Expand Down Expand Up @@ -583,9 +576,23 @@
} else {
setSendEffect(SendEffect.NavigateToAmount)
}

lightningRepo.canSendAsync(invoice.amountSatoshis.coerceAtLeast(1u)).onSuccess { canSend ->
if (!canSend) {
Logger.debug("Cannot pay unified invoice using LN, defaulting to onchain-only", context = TAG)
_sendUiState.update {
it.copy(
isUnified = false,
decodedInvoice = null,
payMethod = SendMethod.ONCHAIN,
)
}
}
}
}

private suspend fun onScanLightning(invoice: LightningInvoice, scanResult: String) {
//TODO HANDLE NODE STILL RUNNING
if (invoice.isExpired) {
toast(
type = Toast.ToastType.ERROR,
Expand Down