Skip to content

Commit

Permalink
(android) Add 1 hour/1 day expiry options
Browse files Browse the repository at this point in the history
Some services require short expiry for BOLT11 invoices.

Fixes #592
  • Loading branch information
dpad85 committed Jul 19, 2024
1 parent 7c5218b commit e73fb0e
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 87 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,10 @@
package fr.acinq.phoenix.android.settings

import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Slider
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
Expand All @@ -34,7 +31,6 @@ import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import fr.acinq.phoenix.android.R
import fr.acinq.phoenix.android.components.*
Expand All @@ -48,6 +44,8 @@ import fr.acinq.phoenix.android.utils.datastore.SwapAddressFormat
import fr.acinq.phoenix.data.lnurl.LnurlAuth
import kotlinx.coroutines.launch
import java.text.NumberFormat
import kotlin.time.Duration.Companion.days
import kotlin.time.Duration.Companion.hours

@Composable
fun PaymentSettingsView(
Expand All @@ -58,7 +56,6 @@ fun PaymentSettingsView(
val userPrefs = userPrefs

var showDescriptionDialog by rememberSaveable { mutableStateOf(false) }
var showExpiryDialog by rememberSaveable { mutableStateOf(false) }

val invoiceDefaultDesc by userPrefs.getInvoiceDefaultDesc.collectAsState(initial = "")
val invoiceDefaultExpiry by userPrefs.getInvoiceDefaultExpiry.collectAsState(null)
Expand All @@ -77,18 +74,7 @@ fun PaymentSettingsView(
description = invoiceDefaultDesc.ifEmpty { stringResource(id = R.string.paymentsettings_defaultdesc_none) },
onClick = { showDescriptionDialog = true }
)
Setting(
title = stringResource(id = R.string.paymentsettings_expiry_title),
description = when (invoiceDefaultExpiry) {
null -> stringResource(id = R.string.utils_unknown)
1 * 604800L -> stringResource(id = R.string.paymentsettings_expiry_one_week)
2 * 604800L -> stringResource(id = R.string.paymentsettings_expiry_two_weeks)
3 * 604800L -> stringResource(id = R.string.paymentsettings_expiry_three_weeks)
else -> stringResource(id = R.string.paymentsettings_expiry_value, NumberFormat.getInstance().format(invoiceDefaultExpiry))
},
onClick = { showExpiryDialog = true }
)

Bolt11ExpiryPreference()
val swapAddressFormat = swapAddressFormatState.value
if (swapAddressFormat != null) {
val schemes = listOf(
Expand Down Expand Up @@ -192,70 +178,42 @@ fun PaymentSettingsView(
}
)
}

if (showExpiryDialog) {
invoiceDefaultExpiry?.let {
DefaultExpiryInvoiceDialog(
expiry = it,
onDismiss = { showExpiryDialog = false },
onConfirm = {
scope.launch { userPrefs.saveInvoiceDefaultExpiry(it.toLong()) }
showExpiryDialog = false
}
)
}
}
}

@Composable
private fun DefaultExpiryInvoiceDialog(
expiry: (Long),
onDismiss: () -> Unit,
onConfirm: (Float) -> Unit,
) {
var paymentExpiry by rememberSaveable { mutableStateOf(expiry.toFloat()) }

Dialog(
onDismiss = onDismiss,
title = stringResource(id = R.string.paymentsettings_expiry_dialog_title),
buttons = {
Button(onClick = onDismiss, text = stringResource(id = R.string.btn_cancel))
Button(
onClick = { onConfirm(paymentExpiry) },
text = stringResource(id = R.string.btn_ok)
)
}
) {
Column(Modifier.padding(horizontal = 24.dp)) {
Text(text = stringResource(id = R.string.paymentsettings_expiry_dialog_description))
Spacer(Modifier.height(16.dp))
Slider(
value = paymentExpiry,
onValueChange = { paymentExpiry = it },
valueRange = 604800f..1814400f,
steps = 1,
)
Row {
Text(
text = stringResource(id = R.string.paymentsettings_expiry_one_week),
style = MaterialTheme.typography.caption,
textAlign = TextAlign.Start,
modifier = Modifier.weight(1f)
)
Text(
text = stringResource(id = R.string.paymentsettings_expiry_two_weeks),
style = MaterialTheme.typography.caption,
textAlign = TextAlign.Center,
modifier = Modifier.weight(1f)
)
Text(
text = stringResource(id = R.string.paymentsettings_expiry_three_weeks),
style = MaterialTheme.typography.caption,
textAlign = TextAlign.End,
modifier = Modifier.weight(1f)
)
private fun Bolt11ExpiryPreference() {
val scope = rememberCoroutineScope()
val userPrefs = userPrefs
val preferences = listOf(
PreferenceItem(item = 1.hours.inWholeSeconds, title = stringResource(id = R.string.paymentsettings_expiry_one_hour)),
PreferenceItem(item = 1.days.inWholeSeconds, title = stringResource(id = R.string.paymentsettings_expiry_one_day)),
PreferenceItem(item = 7.days.inWholeSeconds, title = stringResource(id = R.string.paymentsettings_expiry_one_week)),
PreferenceItem(item = 14.days.inWholeSeconds, title = stringResource(id = R.string.paymentsettings_expiry_two_weeks)),
PreferenceItem(item = 21.days.inWholeSeconds, title = stringResource(id = R.string.paymentsettings_expiry_three_weeks)),
)
val expiry = userPrefs.getInvoiceDefaultExpiry.collectAsState(initial = null)
expiry.value?.let { expiry ->
ListPreferenceButton(
title = stringResource(id = R.string.paymentsettings_expiry_title),
subtitle = {
Text(text = when (expiry) {
1.hours.inWholeSeconds -> stringResource(id = R.string.paymentsettings_expiry_one_hour)
1.days.inWholeSeconds -> stringResource(id = R.string.paymentsettings_expiry_one_day)
7.days.inWholeSeconds -> stringResource(id = R.string.paymentsettings_expiry_one_week)
14.days.inWholeSeconds -> stringResource(id = R.string.paymentsettings_expiry_two_weeks)
21.days.inWholeSeconds -> stringResource(id = R.string.paymentsettings_expiry_three_weeks)
else -> stringResource(id = R.string.paymentsettings_expiry_value, NumberFormat.getInstance().format(expiry))
})
},
selectedItem = expiry,
preferences = preferences,
enabled = true,
onPreferenceSubmit = {
scope.launch {
userPrefs.saveInvoiceDefaultExpiry(it.item)
}
}
}
)
}
}

Expand Down
4 changes: 3 additions & 1 deletion phoenix-android/src/main/res/values-b+es+419/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -597,7 +597,9 @@
<string name="paymentsettings_expiry_title">Vencimiento de la factura</string>
<string name="paymentsettings_expiry_dialog_title">Vencimiento de la factura</string>
<string name="paymentsettings_expiry_dialog_description">Las facturas que crees caducan después de este plazo. El valor predeterminado es 1 semana.</string>
<string name="paymentsettings_expiry_one_week">1 semana</string>
<string name="paymentsettings_expiry_one_hour">1 hora</string>
<string name="paymentsettings_expiry_one_day">1 día</string>
<string name="paymentsettings_expiry_one_week">1 semana (por defecto)</string>
<string name="paymentsettings_expiry_two_weeks">2 semanas</string>
<string name="paymentsettings_expiry_three_weeks">3 semanas</string>
<string name="paymentsettings_expiry_value">%1$s segundos</string>
Expand Down
4 changes: 3 additions & 1 deletion phoenix-android/src/main/res/values-cs/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -604,7 +604,9 @@
<string name="paymentsettings_expiry_title">Platnost faktury</string>
<string name="paymentsettings_expiry_dialog_title">Platnost faktury</string>
<string name="paymentsettings_expiry_dialog_description">Vytvořené faktury jsou po této prodlevě neaktuální. Výchozí hodnota je 1 týden.</string>
<string name="paymentsettings_expiry_one_week">1 týden</string>
<string name="paymentsettings_expiry_one_hour">1 hodina</string>
<string name="paymentsettings_expiry_one_day">1 den</string>
<string name="paymentsettings_expiry_one_week">1 týden (výchozí)</string>
<string name="paymentsettings_expiry_two_weeks">2 týdny</string>
<string name="paymentsettings_expiry_three_weeks">3 týdny</string>
<string name="paymentsettings_expiry_value">%1$s sekund</string>
Expand Down
4 changes: 3 additions & 1 deletion phoenix-android/src/main/res/values-de/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -612,7 +612,9 @@
<string name="paymentsettings_expiry_title">Verfallsfrist von Rechnungen</string>
<string name="paymentsettings_expiry_dialog_title">Verfallsfrist von Rechnungen</string>
<string name="paymentsettings_expiry_dialog_description">Rechnungen, die Sie erstellen, sind nach dieser Frist ungültig. Standardwert ist 1 Woche.</string>
<string name="paymentsettings_expiry_one_week">1 Woche</string>
<string name="paymentsettings_expiry_one_hour">1 Stunde</string>
<string name="paymentsettings_expiry_one_day">1 Tag</string>
<string name="paymentsettings_expiry_one_week">1 Woche (Standard)</string>
<string name="paymentsettings_expiry_two_weeks">2 Wochen</string>
<string name="paymentsettings_expiry_three_weeks">3 Wochen</string>
<string name="paymentsettings_expiry_value">%1$s Sekunden</string>
Expand Down
10 changes: 6 additions & 4 deletions phoenix-android/src/main/res/values-fr/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -627,10 +627,12 @@
<string name="paymentsettings_expiry_title">Expiration des requêtes de paiement</string>
<string name="paymentsettings_expiry_dialog_title">Expiration des requêtes</string>
<string name="paymentsettings_expiry_dialog_description">Vos requêtes de paiements seront inactives une fois ce délai passé. Par défaut, 1 semaine.</string>
<string name="paymentsettings_expiry_one_week">1 week</string>
<string name="paymentsettings_expiry_two_weeks">2 weeks</string>
<string name="paymentsettings_expiry_three_weeks">3 weeks</string>
<string name="paymentsettings_expiry_value">%1$s seconds</string>
<string name="paymentsettings_expiry_one_hour">1 heure</string>
<string name="paymentsettings_expiry_one_day">1 jour</string>
<string name="paymentsettings_expiry_one_week">1 semaine (défaut)</string>
<string name="paymentsettings_expiry_two_weeks">2 semaines</string>
<string name="paymentsettings_expiry_three_weeks">3 semaines</string>
<string name="paymentsettings_expiry_value">%1$s secondes</string>

<string name="paymentsettings_lnurlauth_scheme_title">Schéma d\'authentification LNURL</string>

Expand Down
4 changes: 3 additions & 1 deletion phoenix-android/src/main/res/values-sk/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -654,7 +654,9 @@
<string name="paymentsettings_expiry_title">Platnosť faktúry</string>
<string name="paymentsettings_expiry_dialog_title">Platnosť faktúry</string>
<string name="paymentsettings_expiry_dialog_description">Vytvorené faktúry sú po tejto lehote neaktuálne. Predvolená hodnota je 1 týždeň.</string>
<string name="paymentsettings_expiry_one_week">1 týždeň</string>
<string name="paymentsettings_expiry_one_hour">1 hodina</string>
<string name="paymentsettings_expiry_one_day">1 deň</string>
<string name="paymentsettings_expiry_one_week">1 týždeň (predvolená)</string>
<string name="paymentsettings_expiry_two_weeks">2 týždne</string>
<string name="paymentsettings_expiry_three_weeks">3 týždne</string>
<string name="paymentsettings_expiry_value">%1$s sekúnd</string>
Expand Down
4 changes: 3 additions & 1 deletion phoenix-android/src/main/res/values-vi/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -612,7 +612,9 @@
<string name="paymentsettings_expiry_title">Hóa đơn hết hạn</string>
<string name="paymentsettings_expiry_dialog_title">Hóa đơn hết hạn</string>
<string name="paymentsettings_expiry_dialog_description">Hóa đơn bạn tạo đã hết hạn sau khoảng trì hoãn này. Thời hạn mặc định là 1 tuần.</string>
<string name="paymentsettings_expiry_one_week">1 tuần</string>
<string name="paymentsettings_expiry_one_hour">1 giờ</string>
<string name="paymentsettings_expiry_one_day">1 ngày</string>
<string name="paymentsettings_expiry_one_week">1 tuần (mặc định)</string>
<string name="paymentsettings_expiry_two_weeks">2 tuần</string>
<string name="paymentsettings_expiry_three_weeks">3 tuần</string>
<string name="paymentsettings_expiry_value">%1$s giây</string>
Expand Down
4 changes: 3 additions & 1 deletion phoenix-android/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -678,7 +678,9 @@
<string name="paymentsettings_expiry_title">Invoice expiry</string>
<string name="paymentsettings_expiry_dialog_title">Invoice expiry</string>
<string name="paymentsettings_expiry_dialog_description">Invoices that you create are stale after this delay. Default value is 1 week.</string>
<string name="paymentsettings_expiry_one_week">1 week</string>
<string name="paymentsettings_expiry_one_hour">1 hour</string>
<string name="paymentsettings_expiry_one_day">1 day</string>
<string name="paymentsettings_expiry_one_week">1 week (default)</string>
<string name="paymentsettings_expiry_two_weeks">2 weeks</string>
<string name="paymentsettings_expiry_three_weeks">3 weeks</string>
<string name="paymentsettings_expiry_value">%1$s seconds</string>
Expand Down

0 comments on commit e73fb0e

Please sign in to comment.