Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions app/src/main/java/to/bitkit/repositories/MoveToSparkRepo.kt
Original file line number Diff line number Diff line change
Expand Up @@ -765,6 +765,9 @@ sealed class MoveToSparkError(message: String, cause: Throwable? = null) : AppEr
val feeSats: ULong = 0uL,
) : MoveToSparkError("The move's fee rose after '$movedSats' sats moved")

/** The quote for the update did not finish in time, so nothing moved. */
class QuoteTimedOut : MoveToSparkError("The update quote did not finish in time")

/** The update moved the balance, but Spark cannot pay with it yet: the move or Spark's balance has not settled. */
class UpdateUnsettled : MoveToSparkError("The update to Spark has not settled yet")

Expand Down
3 changes: 3 additions & 0 deletions app/src/main/java/to/bitkit/ui/ContentView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -600,6 +600,7 @@ fun ContentView(
val isRetryingInitialSubscriptionPayment by
appViewModel.isRetryingInitialSubscriptionPayment.collectAsStateWithLifecycle()
var homeWalletPageRequest by remember { mutableIntStateOf(0) }
var isSendConfirmLocked by remember { mutableStateOf(false) }
var homeWidgetsPageRequest by remember { mutableIntStateOf(0) }
val navigateToHomeWallet = {
homeWalletPageRequest++
Expand Down Expand Up @@ -629,6 +630,7 @@ fun ContentView(
dismissEnabled = !isCreatingPaymentRequest &&
!isAcceptingSubscription &&
!isRetryingInitialSubscriptionPayment &&
!isSendConfirmLocked &&
canDismissSheet,
sheetDragHandle = { SheetDragHandle(topPadding = if (usesFlatSheetStyle) 8.dp else 12.dp) },
sheetHandlePlacement = when (currentSheet) {
Expand All @@ -654,6 +656,7 @@ fun ContentView(
hardwareWalletId = sheet.hardwareWalletId,
preparingRequest = sheet.preparingRequest,
hwSendViewModel = hwSendViewModel,
onConfirmLockChange = { isSendConfirmLocked = it },
)
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package to.bitkit.ui.screens.wallets.send

import androidx.activity.compose.BackHandler
import androidx.compose.animation.animateContentSize
import androidx.compose.animation.core.tween
import androidx.compose.foundation.background
Expand All @@ -25,6 +26,7 @@ import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.Icon
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableFloatStateOf
Expand Down Expand Up @@ -110,6 +112,9 @@ import kotlin.time.Duration.Companion.seconds

private val EXPIRY_REFRESH_INTERVAL = 60.seconds

/** How long the confirm stays locked after a swipe once nothing runs for it, before it frees the screen. */
internal val SWIPE_IDLE_RELEASE = 1.seconds

/** Characters of a swap provider's pubky shown on the confirm screen. */
private const val PROVIDER_KEY_LENGTH = 12
const val SEND_CONFIRM_RESET_RESULT_KEY = "SEND_CONFIRM_RESET_RESULT_KEY"
Expand Down Expand Up @@ -138,6 +143,8 @@ fun SendConfirmScreen(
onClickAddTag: () -> Unit,
onClickTag: (String) -> Unit,
onNavigateToPin: () -> Unit,
isSwipeRunning: Boolean = false,
onLockChange: (Boolean) -> Unit = {},
syncNodeVariant: SyncNodeVariant = SyncNodeVariant.LIGHTNING,
autoPayContent: (@Composable () -> Unit)? = null,
) {
Expand Down Expand Up @@ -199,12 +206,22 @@ fun SendConfirmScreen(
if (uiState.isPreparedFeeReview) isLoading = false
}

val isLocked = rememberConfirmLock(isLoading, isSwipeRunning, uiState, showBiometrics)

// A running swipe holds the sheet too, until it moves on or returns here
DisposableEffect(isLocked) {
onLockChange(isLocked)
onDispose { onLockChange(false) }
}
BackHandler(enabled = isLocked) {}

SendConfirmContent(
uiState = uiState,
isNodeRunning = isNodeRunning,
isLoading = isLoading,
showBiometrics = showBiometrics,
canGoBack = canGoBack,
isLocked = isLocked,
syncNodeVariant = syncNodeVariant,
initialShowDetails = uiState.isInitialSubscriptionPayment && !uiState.shouldAutomaticallyPay,
autoPayContent = autoPayContent,
Expand All @@ -225,13 +242,40 @@ fun SendConfirmScreen(
onEvent(SendEvent.PayConfirmed)
},
onBiometricsFailure = {
isLoading = false
// Loading stays on, as on the direct PIN path; the PIN result resets it.
showBiometrics = false
onNavigateToPin()
},
)
}

/**
* Whether the confirm screen is locked: from the swipe until it moves on or returns here. A swipe with nothing
* running for it for [SWIPE_IDLE_RELEASE], such as one stopped before paying, no longer holds it, and neither does
* the amount warning or the biometrics prompt, which the user answers. The review of a prepared fee clears the
* swipe's loading, so it stays leavable until the user swipes again to accept it.
*/
@Composable
internal fun rememberConfirmLock(
isLoading: Boolean,
isSwipeRunning: Boolean,
uiState: SendUiState,
showBiometrics: Boolean = false,
): Boolean {
val isAnswering = showBiometrics || uiState.showSanityWarningDialog != null
// Keyed so a new run, or a closed prompt, locks from its first composition, before the effect resets it.
var isSwipeIdle by remember(isLoading, isSwipeRunning, uiState.shouldConfirmPay, isAnswering) {
mutableStateOf(false)
}
LaunchedEffect(isLoading, isSwipeRunning, uiState.shouldConfirmPay, isAnswering) {
if (!isLoading || isSwipeRunning || uiState.shouldConfirmPay || isAnswering) return@LaunchedEffect
delay(SWIPE_IDLE_RELEASE)
isSwipeIdle = true
}
if (isAnswering) return false
return isLoading && !isSwipeIdle
}

@Composable
internal fun AutoPayOverlay(
isVisible: Boolean,
Expand All @@ -257,6 +301,7 @@ internal fun SendConfirmContent(
preparingRequest: PaykitPaymentRequest? = null,
preparingContact: PubkyProfile? = null,
canGoBack: Boolean = true,
isLocked: Boolean = false,
initialShowDetails: Boolean = false,
syncNodeVariant: SyncNodeVariant = SyncNodeVariant.LIGHTNING,
autoPayContent: (@Composable () -> Unit)? = null,
Expand Down Expand Up @@ -300,7 +345,7 @@ internal fun SendConfirmContent(
else -> stringResource(R.string.wallet__send_review)
},
contact = if (isPreparing) preparingContact else uiState.contactPaymentProfile,
onBack = onBack.takeIf { canGoBack },
onBack = onBack.takeIf { canGoBack && !isLocked },
)

Spacer(Modifier.height(16.dp))
Expand All @@ -316,9 +361,10 @@ internal fun SendConfirmContent(
preparingRequest = preparingRequest,
preparingContact = preparingContact,
initialShowDetails = initialShowDetails,
onEvent = onEvent,
onClickAddTag = onClickAddTag,
onClickTag = onClickTag,
// The payment's inputs stay as swiped while the swipe runs
onEvent = { if (!isLocked) onEvent(it) },
onClickAddTag = { if (!isLocked) onClickAddTag() },
onClickTag = { if (!isLocked) onClickTag(it) },
onSwipeToConfirm = onSwipeToConfirm,
)
} else {
Expand Down Expand Up @@ -448,7 +494,7 @@ private fun ContentRunning(
VerticalSpacer(if (isPreparing || uiState.isPaymentRequest) 24.dp else 44.dp)

if (isLnurlPay && !uiState.isPaymentRequest) {
LnurlPayDetails(uiState = uiState, onEvent = onEvent)
LnurlPayDetails(uiState = uiState, isLoading = isLoading, onEvent = onEvent)
} else if (showDetails && !isPreparing) {
when (uiState.payMethod) {
SendMethod.ONCHAIN -> {
Expand All @@ -475,7 +521,7 @@ private fun ContentRunning(
)
val lnurlPay = uiState.lnurl as? LnurlParams.LnurlPay
if (uiState.paysLnurl && lnurlPay?.data?.commentAllowed() == true) {
LnurlCommentSection(uiState, onEvent)
LnurlCommentSection(uiState, onEvent, enabled = !isLoading)
}
}
}
Expand Down Expand Up @@ -567,6 +613,7 @@ private fun SendUiState.canSwipeToPay() = isAmountInputValid &&
private fun LnurlCommentSection(
uiState: SendUiState,
onEvent: (SendEvent) -> Unit,
enabled: Boolean,
) {
Spacer(modifier = Modifier.height(16.dp))
Caption13Up(stringResource(R.string.wallet__lnurl_pay_confirm__comment), color = Colors.White64)
Expand All @@ -578,6 +625,7 @@ private fun LnurlCommentSection(
onValueChange = { onEvent(SendEvent.CommentChange(it)) },
minLines = 3,
maxLines = 3,
enabled = enabled,
modifier = Modifier
.fillMaxWidth()
.testTag("CommentInput")
Expand Down Expand Up @@ -965,6 +1013,7 @@ private val SendUiState.confirmationRequestNote: String?
@Composable
private fun LnurlPayDetails(
uiState: SendUiState,
isLoading: Boolean,
onEvent: (SendEvent) -> Unit,
modifier: Modifier = Modifier,
) {
Expand Down Expand Up @@ -1016,7 +1065,7 @@ private fun LnurlPayDetails(
UpdateFeeCell(uiState.sparkUpdateQuote?.feeSats)

if (lnurlPay.data.commentAllowed()) {
LnurlCommentSection(uiState, onEvent)
LnurlCommentSection(uiState, onEvent, enabled = !isLoading)
}
}
}
Expand Down
14 changes: 13 additions & 1 deletion app/src/main/java/to/bitkit/ui/sheets/SendSheet.kt
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,14 @@ internal fun NavController.navigateToCoinSelection() {
navigateTo(SendRoute.CoinSelection)
}

/** Shows [route] after a failed swipe, resetting the confirmation it covers so Back finds it ready to swipe. */
internal fun NavController.navigateToSendError(route: SendRoute.Error) {
currentBackStackEntry?.takeIf { it.destination.hasRoute<SendRoute.Confirm>() }
?.savedStateHandle
?.set(SEND_CONFIRM_RESET_RESULT_KEY, true)
navigateTo(route)
}

internal fun NavController.navigateToSendAmount(startDestination: SendRoute) {
if (popBackStack(SendRoute.Amount, inclusive = false)) return
navigateTo(SendRoute.Amount) {
Expand All @@ -172,6 +180,7 @@ fun SendSheet(
startDestination: SendRoute = SendRoute.Recipient,
hardwareWalletId: String? = null,
preparingRequest: PaykitPaymentRequest? = null,
onConfirmLockChange: (Boolean) -> Unit = {},
) {
val context = LocalContext.current
val connectivityState by appViewModel.isOnline.collectAsStateWithLifecycle()
Expand Down Expand Up @@ -376,7 +385,7 @@ fun SendSheet(
retryRoute = it.retryRoute,
)
)
is SendEffect.NavigateToError -> navController.navigateTo(
is SendEffect.NavigateToError -> navController.navigateToSendError(
SendRoute.errorFromFailure(
failure = it.failure,
retryRoute = it.retryRoute,
Expand Down Expand Up @@ -531,6 +540,7 @@ fun SendSheet(
}
val uiState by appViewModel.sendUiState.collectAsStateWithLifecycle()
val lightningState by walletViewModel.lightningState.collectAsStateWithLifecycle()
val isSwipeRunning by appViewModel.isSendSwipeRunning.collectAsStateWithLifecycle()
val initialSubscriptionId = uiState.incomingPaymentRequestId
?.takeIf { uiState.isInitialSubscriptionPayment }
?.let { id -> PaykitSubscriptionId(id.paymentRequestId, id.counterparty) }
Expand Down Expand Up @@ -565,6 +575,8 @@ fun SendSheet(
},
onClickTag = { tag -> appViewModel.removeTag(tag) },
onNavigateToPin = { navController.navigateTo(SendRoute.PinCheck) },
isSwipeRunning = isSwipeRunning,
onLockChange = onConfirmLockChange,
autoPayContent = initialSubscription?.let { subscription ->
{
SubscriptionFirstPaymentProgress(
Expand Down
Loading