diff --git a/app/src/main/java/to/bitkit/repositories/LightningRepo.kt b/app/src/main/java/to/bitkit/repositories/LightningRepo.kt index 94f15e352..aeb5cdf21 100644 --- a/app/src/main/java/to/bitkit/repositories/LightningRepo.kt +++ b/app/src/main/java/to/bitkit/repositories/LightningRepo.kt @@ -789,6 +789,15 @@ class LightningRepo @Inject constructor( fun canSend(amountSats: ULong): Boolean = _lightningState.value.nodeLifecycleState.isRunning() && lightningService.canSend(amountSats) + suspend fun canSendAsync(amountSats: ULong): Result = executeWhenNodeRunning("canSendAsync") { + withTimeoutOrNull(3.seconds) { + while (_lightningState.value.channels.isEmpty()) { + delay(1.seconds) + } + } + Result.success(lightningService.canSend(amountSats)) + } + fun getSyncFlow(): Flow = lightningService.syncFlow() fun getNodeId(): String? = diff --git a/app/src/main/java/to/bitkit/viewmodels/AppViewModel.kt b/app/src/main/java/to/bitkit/viewmodels/AppViewModel.kt index ff9c8804c..9920234bc 100644 --- a/app/src/main/java/to/bitkit/viewmodels/AppViewModel.kt +++ b/app/src/main/java/to/bitkit/viewmodels/AppViewModel.kt @@ -538,13 +538,6 @@ class AppViewModel @Inject constructor( 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( @@ -583,9 +576,23 @@ class AppViewModel @Inject constructor( } 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,