Skip to content
Merged
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
5 changes: 0 additions & 5 deletions app/src/main/java/to/bitkit/ui/ContentView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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) }
}
)
}

Expand Down
24 changes: 18 additions & 6 deletions app/src/main/java/to/bitkit/ui/sheets/SendSheet.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -162,6 +163,14 @@ fun SendSheet(
onNavigateToPin = { navController.navigate(SendRoute.PinCheck) },
)
}
composableWithDefaultTransitions<SendRoute.Success> {
val sendDetail by appViewModel.successSendUiState.collectAsStateWithLifecycle()
NewTransactionSheetView(
details = sendDetail,
onCloseClick = { appViewModel.hideSheet() },
onDetailClick = { appViewModel.onClickSendDetail() }
)
}
composableWithDefaultTransitions<SendRoute.WithdrawConfirm> {
val uiState by appViewModel.sendUiState.collectAsStateWithLifecycle()
WithdrawConfirmScreen(
Expand Down Expand Up @@ -210,7 +219,7 @@ fun SendSheet(
SendQuickPayScreen(
quickPayData = requireNotNull(quickPayData),
onPaymentComplete = {
onComplete(null)
navController.navigate(SendRoute.Success)
},
onShowError = { errorMessage ->
navController.navigate(SendRoute.Error(errorMessage))
Expand All @@ -227,11 +236,11 @@ fun SendSheet(
popUpTo<SendRoute.Recipient> { inclusive = true }
}
} else {
onComplete(null)
navController.navigate(SendRoute.Success)
}
},
onClose = {
onComplete(null)
appViewModel.hideSheet()
}
)
}
Expand Down Expand Up @@ -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
}
57 changes: 47 additions & 10 deletions app/src/main/java/to/bitkit/viewmodels/AppViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
}

Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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<Txid> {
return lightningRepo.sendOnChain(
address = address,
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
Expand Down