Removes the need for the payment popup for only one invoice.

This commit is contained in:
Vitor Pamplona
2023-09-21 14:03:38 -04:00
parent 18e81de26d
commit ff82b52a5b
4 changed files with 112 additions and 70 deletions
@@ -413,9 +413,20 @@ fun ZapVote(
} }
if (wantsToPay.isNotEmpty()) { if (wantsToPay.isNotEmpty()) {
PayViaIntentDialog(payingInvoices = wantsToPay, accountViewModel = accountViewModel) { PayViaIntentDialog(
wantsToPay = persistentListOf() payingInvoices = wantsToPay,
} accountViewModel = accountViewModel,
onClose = {
wantsToPay = persistentListOf()
},
onError = {
wantsToPay = persistentListOf()
scope.launch {
zappingProgress = 0f
showErrorMessageDialog = it
}
}
)
} }
if (showErrorMessageDialog != null) { if (showErrorMessageDialog != null) {
@@ -1056,9 +1056,20 @@ fun ZapReaction(
} }
if (wantsToPay.isNotEmpty()) { if (wantsToPay.isNotEmpty()) {
PayViaIntentDialog(payingInvoices = wantsToPay, accountViewModel = accountViewModel) { PayViaIntentDialog(
wantsToPay = persistentListOf() payingInvoices = wantsToPay,
} accountViewModel = accountViewModel,
onClose = {
wantsToPay = persistentListOf()
},
onError = {
wantsToPay = persistentListOf()
scope.launch {
zappingProgress = 0f
showErrorMessageDialog = it
}
}
)
} }
if (wantsToSetCustomZap) { if (wantsToSetCustomZap) {
@@ -1,5 +1,6 @@
package com.vitorpamplona.amethyst.ui.note package com.vitorpamplona.amethyst.ui.note
import android.content.Context
import android.content.Intent import android.content.Intent
import android.net.Uri import android.net.Uri
import androidx.compose.foundation.layout.* import androidx.compose.foundation.layout.*
@@ -144,6 +145,7 @@ fun ZapCustomDialog(
onPayViaIntent = onPayViaIntent, onPayViaIntent = onPayViaIntent,
zapType = selectedZapType zapType = selectedZapType
) )
onClose()
} }
} }
@@ -298,84 +300,84 @@ fun ErrorMessageDialog(
fun PayViaIntentDialog( fun PayViaIntentDialog(
payingInvoices: ImmutableList<ZapPaymentHandler.Payable>, payingInvoices: ImmutableList<ZapPaymentHandler.Payable>,
accountViewModel: AccountViewModel, accountViewModel: AccountViewModel,
onClose: () -> Unit onClose: () -> Unit,
onError: (String) -> Unit
) { ) {
val context = LocalContext.current val context = LocalContext.current
Dialog( if (payingInvoices.size == 1) {
onDismissRequest = onClose, payViaIntent(payingInvoices.first().invoice, context, onError)
properties = DialogProperties( } else {
dismissOnClickOutside = false, Dialog(
usePlatformDefaultWidth = false onDismissRequest = onClose,
) properties = DialogProperties(
) { dismissOnClickOutside = false,
Surface() { usePlatformDefaultWidth = false
Column(modifier = Modifier.padding(10.dp)) { )
Row( ) {
horizontalArrangement = Arrangement.SpaceBetween, Surface() {
verticalAlignment = Alignment.CenterVertically Column(modifier = Modifier.padding(10.dp)) {
) { Row(
CloseButton(onPress = onClose) horizontalArrangement = Arrangement.SpaceBetween,
} verticalAlignment = Alignment.CenterVertically
) {
Spacer(modifier = DoubleVertSpacer) CloseButton(onPress = onClose)
payingInvoices.forEachIndexed { index, it ->
val paid = remember {
mutableStateOf(false)
} }
Row(verticalAlignment = Alignment.CenterVertically, modifier = Modifier.padding(vertical = Size10dp)) { Spacer(modifier = DoubleVertSpacer)
if (it.user != null) {
BaseUserPicture(it.user, Size55dp, accountViewModel = accountViewModel) payingInvoices.forEachIndexed { index, it ->
} else { val paid = remember {
DisplayBlankAuthor(size = Size55dp) mutableStateOf(false)
} }
Spacer(modifier = DoubleHorzSpacer) Row(verticalAlignment = Alignment.CenterVertically, modifier = Modifier.padding(vertical = Size10dp)) {
Column(modifier = Modifier.weight(1f)) {
if (it.user != null) { if (it.user != null) {
UsernameDisplay(it.user, showPlayButton = false) BaseUserPicture(it.user, Size55dp, accountViewModel = accountViewModel)
} else { } else {
Text( DisplayBlankAuthor(size = Size55dp)
text = stringResource(id = R.string.wallet_number, index + 1),
maxLines = 1,
overflow = TextOverflow.Ellipsis,
fontWeight = FontWeight.Bold,
fontSize = 18.sp
)
} }
Row() {
Text( Spacer(modifier = DoubleHorzSpacer)
text = showAmount((it.amountMilliSats / 1000.0f).toBigDecimal()),
maxLines = 1, Column(modifier = Modifier.weight(1f)) {
overflow = TextOverflow.Ellipsis, if (it.user != null) {
fontWeight = FontWeight.Bold, UsernameDisplay(it.user, showPlayButton = false)
fontSize = 18.sp } else {
) Text(
Spacer(modifier = StdHorzSpacer) text = stringResource(id = R.string.wallet_number, index + 1),
Text( maxLines = 1,
text = stringResource(id = R.string.sats), overflow = TextOverflow.Ellipsis,
maxLines = 1, fontWeight = FontWeight.Bold,
overflow = TextOverflow.Ellipsis, fontSize = 18.sp
fontWeight = FontWeight.Bold, )
fontSize = 18.sp }
) Row() {
Text(
text = showAmount((it.amountMilliSats / 1000.0f).toBigDecimal()),
maxLines = 1,
overflow = TextOverflow.Ellipsis,
fontWeight = FontWeight.Bold,
fontSize = 18.sp
)
Spacer(modifier = StdHorzSpacer)
Text(
text = stringResource(id = R.string.sats),
maxLines = 1,
overflow = TextOverflow.Ellipsis,
fontWeight = FontWeight.Bold,
fontSize = 18.sp
)
}
} }
}
Spacer(modifier = DoubleHorzSpacer) Spacer(modifier = DoubleHorzSpacer)
PayButton(isActive = !paid.value) { PayButton(isActive = !paid.value) {
paid.value = true paid.value = true
val uri = "lightning:" + it.invoice payViaIntent(it.invoice, context, onError)
}
val intent = Intent(Intent.ACTION_VIEW, Uri.parse(uri))
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
ContextCompat.startActivity(context, intent, null)
} }
} }
} }
@@ -384,6 +386,21 @@ fun PayViaIntentDialog(
} }
} }
fun payViaIntent(invoice: String, context: Context, onError: (String) -> Unit) {
try {
val intent = Intent(Intent.ACTION_VIEW, Uri.parse("lightning:$invoice"))
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
ContextCompat.startActivity(context, intent, null)
} catch (e: Exception) {
if (e.message != null) {
onError(context.getString(R.string.no_wallet_found_with_error, e.message!!))
} else {
onError(context.getString(R.string.no_wallet_found))
}
}
}
@Composable @Composable
fun PayButton(isActive: Boolean, modifier: Modifier = Modifier, onPost: () -> Unit = {}) { fun PayButton(isActive: Boolean, modifier: Modifier = Modifier, onPost: () -> Unit = {}) {
Button( Button(
+3
View File
@@ -590,4 +590,7 @@
<string name="wallet_number">Wallet %1$s</string> <string name="wallet_number">Wallet %1$s</string>
<string name="error_opening_external_signer">Error opening signer app</string> <string name="error_opening_external_signer">Error opening signer app</string>
<string name="sign_request_rejected">Sign request rejected</string> <string name="sign_request_rejected">Sign request rejected</string>
<string name="no_wallet_found_with_error">No Wallets found to pay a lightning invoice (Error: %1$s). Please install a Lightning wallet to use zaps</string>
<string name="no_wallet_found">No Wallets found to pay a lightning invoice. Please install a Lightning wallet to use zaps</string>
</resources> </resources>