diff options
Diffstat (limited to 'cashier/src/main/java')
-rw-r--r-- | cashier/src/main/java/net/taler/cashier/BalanceFragment.kt | 20 | ||||
-rw-r--r-- | cashier/src/main/java/net/taler/cashier/withdraw/WithdrawManager.kt | 13 |
2 files changed, 24 insertions, 9 deletions
diff --git a/cashier/src/main/java/net/taler/cashier/BalanceFragment.kt b/cashier/src/main/java/net/taler/cashier/BalanceFragment.kt index fa9600b..c0e2aca 100644 --- a/cashier/src/main/java/net/taler/cashier/BalanceFragment.kt +++ b/cashier/src/main/java/net/taler/cashier/BalanceFragment.kt @@ -164,13 +164,19 @@ class BalanceFragment : Fragment() { private fun onAmountConfirmed(amount: Amount) { if (amount.isZero()) { ui.amountView.error = getString(R.string.withdraw_error_zero) - } else if (!withdrawManager.hasSufficientBalance(amount)) { - ui.amountView.error = getString(R.string.withdraw_error_insufficient_balance) - } else { - ui.amountView.error = null - withdrawManager.withdraw(amount) - actionBalanceFragmentToTransactionFragment().let { - findNavController().navigate(it) + } else when (withdrawManager.hasSufficientBalance(amount)) { + true -> { + ui.amountView.error = null + withdrawManager.withdraw(amount) + actionBalanceFragmentToTransactionFragment().let { + findNavController().navigate(it) + } + } + false -> { + ui.amountView.error = getString(R.string.withdraw_error_insufficient_balance) + } + null -> { + ui.amountView.error = getString(R.string.withdraw_error_currency_mismatch) } } } diff --git a/cashier/src/main/java/net/taler/cashier/withdraw/WithdrawManager.kt b/cashier/src/main/java/net/taler/cashier/withdraw/WithdrawManager.kt index 1b809bb..601b59a 100644 --- a/cashier/src/main/java/net/taler/cashier/withdraw/WithdrawManager.kt +++ b/cashier/src/main/java/net/taler/cashier/withdraw/WithdrawManager.kt @@ -73,11 +73,20 @@ class WithdrawManager( private val mLastTransaction = MutableLiveData<LastTransaction>() val lastTransaction: LiveData<LastTransaction> = mLastTransaction + /** + * Returns null if the given [amount] can't be compared to the balance + * e.g. due to mismatching currency. + */ @UiThread - fun hasSufficientBalance(amount: Amount): Boolean { + fun hasSufficientBalance(amount: Amount): Boolean? { val balanceResult = viewModel.balance.value if (balanceResult !is BalanceResult.Success) return false - return balanceResult.amount.positive && amount <= balanceResult.amount.amount + return try { + balanceResult.amount.positive && amount <= balanceResult.amount.amount + } catch (e : IllegalStateException) { + Log.e(TAG, "Error comparing amounts", e) + null + } } @UiThread |