diff --git a/app/src/main/java/to/bitkit/ui/ContentView.kt b/app/src/main/java/to/bitkit/ui/ContentView.kt index b623b0dba..07e7b76af 100644 --- a/app/src/main/java/to/bitkit/ui/ContentView.kt +++ b/app/src/main/java/to/bitkit/ui/ContentView.kt @@ -325,11 +325,6 @@ fun ContentView( appViewModel = appViewModel, walletViewModel = walletViewModel, startDestination = sheet.route, - onComplete = { txSheet -> - appViewModel.hideSheet() - appViewModel.clearClipboardForAutoRead() - txSheet?.let { appViewModel.showNewTransactionSheet(details = it, event = null) } - } ) } diff --git a/app/src/main/java/to/bitkit/ui/sheets/SendSheet.kt b/app/src/main/java/to/bitkit/ui/sheets/SendSheet.kt index 9f8d3f16c..544d76291 100644 --- a/app/src/main/java/to/bitkit/ui/sheets/SendSheet.kt +++ b/app/src/main/java/to/bitkit/ui/sheets/SendSheet.kt @@ -15,7 +15,6 @@ import androidx.navigation.compose.NavHost import androidx.navigation.compose.rememberNavController import androidx.navigation.toRoute import kotlinx.serialization.Serializable -import to.bitkit.models.NewTransactionSheetDetails import to.bitkit.ui.screens.scanner.QrScanningScreen import to.bitkit.ui.screens.wallets.send.AddTagScreen import to.bitkit.ui.screens.wallets.send.PIN_CHECK_RESULT_KEY @@ -46,7 +45,6 @@ fun SendSheet( appViewModel: AppViewModel, walletViewModel: WalletViewModel, startDestination: SendRoute = SendRoute.Recipient, - onComplete: (NewTransactionSheetDetails?) -> Unit, ) { LaunchedEffect(startDestination) { // always reset state on new user-initiated send @@ -72,7 +70,10 @@ fun SendSheet( is SendEffect.NavigateToCoinSelection -> navController.navigate(SendRoute.CoinSelection) is SendEffect.NavigateToConfirm -> navController.navigate(SendRoute.Confirm) is SendEffect.PopBack -> navController.popBackStack(it.route, inclusive = false) - is SendEffect.PaymentSuccess -> onComplete(it.sheet) + is SendEffect.PaymentSuccess -> { + appViewModel.clearClipboardForAutoRead() + navController.navigate(SendRoute.Success) + } is SendEffect.NavigateToQuickPay -> navController.navigate(SendRoute.QuickPay) is SendEffect.NavigateToWithdrawConfirm -> navController.navigate(SendRoute.WithdrawConfirm) is SendEffect.NavigateToWithdrawError -> navController.navigate(SendRoute.WithdrawError) @@ -162,6 +163,14 @@ fun SendSheet( onNavigateToPin = { navController.navigate(SendRoute.PinCheck) }, ) } + composableWithDefaultTransitions { + val sendDetail by appViewModel.successSendUiState.collectAsStateWithLifecycle() + NewTransactionSheetView( + details = sendDetail, + onCloseClick = { appViewModel.hideSheet() }, + onDetailClick = { appViewModel.onClickSendDetail() } + ) + } composableWithDefaultTransitions { val uiState by appViewModel.sendUiState.collectAsStateWithLifecycle() WithdrawConfirmScreen( @@ -210,7 +219,7 @@ fun SendSheet( SendQuickPayScreen( quickPayData = requireNotNull(quickPayData), onPaymentComplete = { - onComplete(null) + navController.navigate(SendRoute.Success) }, onShowError = { errorMessage -> navController.navigate(SendRoute.Error(errorMessage)) @@ -227,11 +236,11 @@ fun SendSheet( popUpTo { inclusive = true } } } else { - onComplete(null) + navController.navigate(SendRoute.Success) } }, onClose = { - onComplete(null) + appViewModel.hideSheet() } ) } @@ -285,6 +294,9 @@ sealed interface SendRoute { @Serializable data object Confirm : SendRoute + @Serializable + data object Success : SendRoute + @Serializable data class Error(val errorMessage: String) : SendRoute } diff --git a/app/src/main/java/to/bitkit/viewmodels/AppViewModel.kt b/app/src/main/java/to/bitkit/viewmodels/AppViewModel.kt index 667ff447b..0ca94c5fb 100644 --- a/app/src/main/java/to/bitkit/viewmodels/AppViewModel.kt +++ b/app/src/main/java/to/bitkit/viewmodels/AppViewModel.kt @@ -237,14 +237,13 @@ class AppViewModel @Inject constructor( is Event.PaymentSuccessful -> { // TODO: fee is not the sats sent. Need to get this amount from elsewhere like send flow or something. - showNewTransactionSheet( + handlePaymentSuccess( NewTransactionSheetDetails( type = NewTransactionSheetType.LIGHTNING, direction = NewTransactionSheetDirection.SENT, paymentHashOrTxId = event.paymentHash, sats = ((event.feePaidMsat ?: 0u) / 1000u).toLong(), ), - event = event ) } @@ -937,14 +936,12 @@ class AppViewModel @Inject constructor( tags = tags ) Logger.info("Onchain send result txid: $txId", context = TAG) - setSendEffect( - SendEffect.PaymentSuccess( - NewTransactionSheetDetails( - type = NewTransactionSheetType.ONCHAIN, - direction = NewTransactionSheetDirection.SENT, - paymentHashOrTxId = txId, - sats = amount.toLong(), - ) + handlePaymentSuccess( + NewTransactionSheetDetails( + type = NewTransactionSheetType.ONCHAIN, + direction = NewTransactionSheetDirection.SENT, + paymentHashOrTxId = txId, + sats = amount.toLong(), ) ) lightningRepo.sync() @@ -1059,6 +1056,30 @@ class AppViewModel @Inject constructor( } } + fun onClickSendDetail() { + val activityType = _successSendUiState.value.type.toActivityFilter() + val txType = _successSendUiState.value.direction.toTxType() + val paymentHashOrTxId = _successSendUiState.value.paymentHashOrTxId ?: return + _successSendUiState.update { it.copy(isLoadingDetails = true) } + viewModelScope.launch(bgDispatcher) { + activityRepo.findActivityByPaymentId( + paymentHashOrTxId = paymentHashOrTxId, + type = activityType, + txType = txType, + retry = true + ).onSuccess { activity -> + hideSheet() + _successSendUiState.update { it.copy(isLoadingDetails = false) } + val nextRoute = Routes.ActivityDetail(activity.rawId()) + mainScreenEffect(MainScreenEffect.Navigate(nextRoute)) + }.onFailure { e -> + Logger.error(msg = "Activity not found", context = TAG) + toast(e) + _successSendUiState.update { it.copy(isLoadingDetails = false) } + } + } + } + private suspend fun sendOnchain(address: String, amount: ULong): Result { return lightningRepo.sendOnChain( address = address, @@ -1225,6 +1246,17 @@ class AppViewModel @Inject constructor( val newTransaction = _newTransaction.asStateFlow() + private val _successSendUiState = MutableStateFlow( + NewTransactionSheetDetails( + type = NewTransactionSheetType.LIGHTNING, + direction = NewTransactionSheetDirection.SENT, + paymentHashOrTxId = null, + sats = 0 + ) + ) + + val successSendUiState = _successSendUiState.asStateFlow() + fun setNewTransactionSheetEnabled(enabled: Boolean) { isNewTransactionSheetEnabled = enabled } @@ -1408,6 +1440,11 @@ class AppViewModel @Inject constructor( } } + private fun handlePaymentSuccess(details: NewTransactionSheetDetails) { + _successSendUiState.update { details } + setSendEffect(SendEffect.PaymentSuccess(details)) + } + companion object { private const val TAG = "AppViewModel" private const val SEND_AMOUNT_WARNING_THRESHOLD = 100.0