aboutsummaryrefslogtreecommitdiff
path: root/wallet/src
diff options
context:
space:
mode:
authorTorsten Grote <t@grobox.de>2022-10-27 10:37:54 -0300
committerTorsten Grote <t@grobox.de>2022-10-27 10:37:54 -0300
commit431e326b1b3212bdcb975957306e45958e2e98e5 (patch)
treebdbe81c46bb91386a5b86d40d1c19a8759949dff /wallet/src
parent725562a48a23ed4d0153f05a5c36f4d17875c90c (diff)
downloadtaler-android-431e326b1b3212bdcb975957306e45958e2e98e5.tar.gz
taler-android-431e326b1b3212bdcb975957306e45958e2e98e5.tar.bz2
taler-android-431e326b1b3212bdcb975957306e45958e2e98e5.zip
[wallet] Check for sufficient balance when sending funds
Diffstat (limited to 'wallet/src')
-rw-r--r--wallet/src/main/java/net/taler/wallet/MainViewModel.kt11
-rw-r--r--wallet/src/main/java/net/taler/wallet/SendFundsFragment.kt27
2 files changed, 30 insertions, 8 deletions
diff --git a/wallet/src/main/java/net/taler/wallet/MainViewModel.kt b/wallet/src/main/java/net/taler/wallet/MainViewModel.kt
index 50438c4..aa9b0f1 100644
--- a/wallet/src/main/java/net/taler/wallet/MainViewModel.kt
+++ b/wallet/src/main/java/net/taler/wallet/MainViewModel.kt
@@ -26,6 +26,7 @@ import androidx.lifecycle.distinctUntilChanged
import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.Job
import kotlinx.coroutines.launch
+import net.taler.common.Amount
import net.taler.common.Event
import net.taler.common.assertUiThread
import net.taler.common.toEvent
@@ -141,6 +142,16 @@ class MainViewModel(val app: Application) : AndroidViewModel(app) {
}
@UiThread
+ fun hasSufficientBalance(amount: Amount): Boolean {
+ balances.value?.forEach { balanceItem ->
+ if (balanceItem.currency == amount.currency) {
+ return balanceItem.available >= amount
+ }
+ }
+ return false
+ }
+
+ @UiThread
fun dangerouslyReset() {
api.sendRequest("reset")
withdrawManager.testWithdrawalStatus.value = null
diff --git a/wallet/src/main/java/net/taler/wallet/SendFundsFragment.kt b/wallet/src/main/java/net/taler/wallet/SendFundsFragment.kt
index 60d98a5..d6f7280 100644
--- a/wallet/src/main/java/net/taler/wallet/SendFundsFragment.kt
+++ b/wallet/src/main/java/net/taler/wallet/SendFundsFragment.kt
@@ -67,6 +67,7 @@ class SendFundsFragment : Fragment() {
Surface {
SendFundsIntro(
model.transactionManager.selectedCurrency ?: error("No currency selected"),
+ model::hasSufficientBalance,
this@SendFundsFragment::onDeposit,
this@SendFundsFragment::onPeerPush,
)
@@ -99,6 +100,7 @@ class SendFundsFragment : Fragment() {
@Composable
private fun SendFundsIntro(
currency: String,
+ hasSufficientBalance: (Amount) -> Boolean,
onDeposit: (Amount) -> Unit,
onPeerPush: (Amount) -> Unit,
) {
@@ -110,6 +112,7 @@ private fun SendFundsIntro(
) {
var text by rememberSaveable { mutableStateOf("") }
var isError by rememberSaveable { mutableStateOf(false) }
+ var insufficientBalance by rememberSaveable { mutableStateOf(false) }
Row(
verticalAlignment = Alignment.CenterVertically,
modifier = Modifier
@@ -123,15 +126,21 @@ private fun SendFundsIntro(
keyboardOptions = KeyboardOptions.Default.copy(keyboardType = KeyboardType.Decimal),
onValueChange = { input ->
isError = false
+ insufficientBalance = false
text = input.filter { it.isDigit() || it == '.' }
},
- isError = isError,
+ isError = isError || insufficientBalance,
label = {
if (isError) {
Text(
stringResource(R.string.receive_amount_invalid),
color = Color.Red,
)
+ } else if (insufficientBalance) {
+ Text(
+ stringResource(R.string.payment_balance_insufficient),
+ color = Color.Red,
+ )
} else {
Text(stringResource(R.string.send_amount))
}
@@ -150,14 +159,18 @@ private fun SendFundsIntro(
style = MaterialTheme.typography.h6,
)
Row(modifier = Modifier.padding(16.dp)) {
+ fun onClickButton(block: (Amount) -> Unit) {
+ val amount = getAmount(currency, text)
+ if (amount == null) isError = true
+ else if (!hasSufficientBalance(amount)) insufficientBalance = true
+ else block(amount)
+ }
Button(
modifier = Modifier
.padding(end = 16.dp)
.weight(1f),
onClick = {
- val amount = getAmount(currency, text)
- if (amount == null) isError = true
- else onDeposit(amount)
+ onClickButton { amount -> onDeposit(amount) }
}) {
Text(text = stringResource(R.string.send_deposit))
}
@@ -166,9 +179,7 @@ private fun SendFundsIntro(
.height(IntrinsicSize.Max)
.weight(1f),
onClick = {
- val amount = getAmount(currency, text)
- if (amount == null) isError = true
- else onPeerPush(amount)
+ onClickButton { amount -> onPeerPush(amount) }
},
) {
Text(text = stringResource(R.string.send_peer))
@@ -181,6 +192,6 @@ private fun SendFundsIntro(
@Composable
fun PreviewSendFundsIntro() {
Surface {
- SendFundsIntro("TESTKUDOS", {}) {}
+ SendFundsIntro("TESTKUDOS", { true }, {}) {}
}
}