diff options
Diffstat (limited to 'wallee-c2ec/app/src/main/java/ch/bfh/habej2/wallee_c2ec/withdrawal/WithdrawalViewModel.kt')
-rw-r--r-- | wallee-c2ec/app/src/main/java/ch/bfh/habej2/wallee_c2ec/withdrawal/WithdrawalViewModel.kt | 35 |
1 files changed, 33 insertions, 2 deletions
diff --git a/wallee-c2ec/app/src/main/java/ch/bfh/habej2/wallee_c2ec/withdrawal/WithdrawalViewModel.kt b/wallee-c2ec/app/src/main/java/ch/bfh/habej2/wallee_c2ec/withdrawal/WithdrawalViewModel.kt index 46490e0..8cfc7f9 100644 --- a/wallee-c2ec/app/src/main/java/ch/bfh/habej2/wallee_c2ec/withdrawal/WithdrawalViewModel.kt +++ b/wallee-c2ec/app/src/main/java/ch/bfh/habej2/wallee_c2ec/withdrawal/WithdrawalViewModel.kt @@ -17,6 +17,7 @@ import kotlinx.coroutines.launch import java.io.Closeable import java.math.BigDecimal import java.security.SecureRandom +import java.util.Optional data class Amount( val value: Int, @@ -66,8 +67,8 @@ class WithdrawalViewModel( _uiState.value.encodedWopid = Base32Encode(wopid()) } - fun updateAmount(amount: Amount) { - _uiState.value.amount = amount + fun updateAmount(amount: String) { + _uiState.value.amount = parseAmount(amount).orElse(null) } fun updateCurrency(currency: String) { @@ -115,4 +116,34 @@ class WithdrawalViewModel( rand.nextBytes(wopid) // will seed automatically return wopid } + + /** + * Format expected X[.X], X an integer + */ + private fun parseAmount(inp: String): Optional<Amount> { + + val points = inp.count { it == '.' } + if (points > 1) { + return Optional.empty() + } + + if (points == 1) { + val valueStr = inp.split(".")[0] + val fracStr = inp.split(".")[1] + return try { + val value = valueStr.toInt() + val frac = fracStr.toInt() + Optional.of(Amount(value, frac)) + } catch (ex: NumberFormatException) { + Optional.empty() + } + } + + return try { + val value = inp.toInt() + Optional.of(Amount(value, 0)) + } catch (ex: NumberFormatException) { + Optional.empty() + } + } }
\ No newline at end of file |