summaryrefslogtreecommitdiff
path: root/wallee-c2ec/app/src/main/java/ch/bfh/habej2/wallee_c2ec/withdrawal/WithdrawalActivity.kt
diff options
context:
space:
mode:
Diffstat (limited to 'wallee-c2ec/app/src/main/java/ch/bfh/habej2/wallee_c2ec/withdrawal/WithdrawalActivity.kt')
-rw-r--r--wallee-c2ec/app/src/main/java/ch/bfh/habej2/wallee_c2ec/withdrawal/WithdrawalActivity.kt126
1 files changed, 50 insertions, 76 deletions
diff --git a/wallee-c2ec/app/src/main/java/ch/bfh/habej2/wallee_c2ec/withdrawal/WithdrawalActivity.kt b/wallee-c2ec/app/src/main/java/ch/bfh/habej2/wallee_c2ec/withdrawal/WithdrawalActivity.kt
index 94238e7..526924c 100644
--- a/wallee-c2ec/app/src/main/java/ch/bfh/habej2/wallee_c2ec/withdrawal/WithdrawalActivity.kt
+++ b/wallee-c2ec/app/src/main/java/ch/bfh/habej2/wallee_c2ec/withdrawal/WithdrawalActivity.kt
@@ -5,6 +5,7 @@ import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.Column
+import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material3.Button
import androidx.compose.material3.Text
import androidx.compose.material3.TextField
@@ -13,6 +14,7 @@ import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.platform.LocalContext
+import androidx.compose.ui.text.input.KeyboardType
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController
@@ -24,7 +26,6 @@ import com.wallee.android.till.sdk.data.Transaction
import com.wallee.android.till.sdk.data.TransactionProcessingBehavior
import java.math.BigDecimal
import java.util.Currency
-import java.util.Optional
class WithdrawalActivity : ComponentActivity() {
@@ -39,15 +40,22 @@ class WithdrawalActivity : ComponentActivity() {
NavHost(navController = navController, startDestination = "chooseExchangeScreen") {
composable("chooseExchangeScreen") {
ExchangeSelectionScreen(model) {
+ navController.navigate("amountScreen")
+ }
+ }
+ composable("amountScreen") {
+ AmountScreen(model) {
navController.navigate("registerWithdrawalScreen")
}
}
composable("registerWithdrawalScreen") {
RegisterWithdrawalScreen(model) {
- navController.navigate("paymentScreen")
+ navController.navigate("authorizePaymentScreen")
}
}
- composable("paymentScreen") { PaymentScreen(model) }
+ composable("authorizePaymentScreen") {
+ AuthorizePaymentScreen(model)
+ }
}
}
}
@@ -84,7 +92,7 @@ fun RegisterWithdrawalScreen(
}
@Composable
-fun PaymentScreen(model: WithdrawalViewModel) {
+fun AmountScreen(model: WithdrawalViewModel, navigateToWhenAmountEntered: () -> Unit) {
val activity = LocalContext.current as Activity
@@ -97,16 +105,21 @@ fun PaymentScreen(model: WithdrawalViewModel) {
TextField(
value = "",
onValueChange = {
- val optAmount = parseAmount(it)
- if (optAmount.isPresent) {
- model.updateAmount(optAmount.get())
- }
+ model.updateAmount(it)
},
label = { Text(text = "Enter amount") },
- placeholder = { Text(text = "amount") }
+ placeholder = { Text(text = "amount") },
+ keyboardOptions = KeyboardOptions(
+ autoCorrect = false,
+ keyboardType = KeyboardType.Number
+ )
)
- AuthorizePaymentButton(model = model)
+ Button(onClick = {
+ navigateToWhenAmountEntered()
+ }) {
+ Text(text = "pay")
+ }
Button(onClick = {
model.withdrawalOperationFailed()
@@ -118,40 +131,6 @@ fun PaymentScreen(model: WithdrawalViewModel) {
}
@Composable
-fun AuthorizePaymentButton(model: WithdrawalViewModel) {
-
- val uiState by model.uiState.collectAsState()
- val activity = LocalContext.current as Activity
- val client = ApiClient(WalleeResponseHandler())
-
- client.bind(activity)
-
- Button(enabled = false, onClick = {
- val withdrawalAmount = LineItem
- .ListBuilder(
- uiState.encodedWopid,
- BigDecimal("${uiState.amount.value}.${uiState.amount.frac}")
- )
- .build()
-
- val transaction = Transaction.Builder(withdrawalAmount)
- .setCurrency(Currency.getInstance(uiState.currency))
- .setInvoiceReference(uiState.encodedWopid)
- .setMerchantReference(uiState.encodedWopid)
- .setTransactionProcessingBehavior(TransactionProcessingBehavior.COMPLETE_IMMEDIATELY)
- .build()
-
- try {
- client.authorizeTransaction(transaction)
- } catch (e: Exception) {
- e.printStackTrace()
- }
- }) {
- Text(text = "")
- }
-}
-
-@Composable
fun ExchangeSelectionScreen(
model: WithdrawalViewModel,
onNavigateToWithdrawal: () -> Unit
@@ -171,7 +150,6 @@ fun ExchangeSelectionScreen(
val ctx = LocalContext.current
Button(onClick = {
- // TODO trigger model.exchangeUpdated(...)
model.exchangeUpdated(TalerBankIntegrationConfig("","","",""))
onNavigateToWithdrawal()
}) {
@@ -185,9 +163,35 @@ fun ExchangeSelectionScreen(
}
@Composable
-fun SummaryScreen(model: WithdrawalViewModel) {
+fun AuthorizePaymentScreen(model: WithdrawalViewModel) {
+ val uiState by model.uiState.collectAsState()
val activity = LocalContext.current as Activity
+ val client = ApiClient(WalleeResponseHandler())
+
+ client.bind(activity)
+
+ val withdrawalAmount = LineItem
+ .ListBuilder(
+ uiState.encodedWopid,
+ BigDecimal("${uiState.amount.value}.${uiState.amount.frac}")
+ )
+ .build()
+
+ val transaction = Transaction.Builder(withdrawalAmount)
+ .setCurrency(Currency.getInstance(uiState.currency))
+ .setInvoiceReference(uiState.encodedWopid)
+ .setMerchantReference(uiState.encodedWopid)
+ .setTransactionProcessingBehavior(TransactionProcessingBehavior.COMPLETE_IMMEDIATELY)
+ .build()
+
+ try {
+ client.authorizeTransaction(transaction)
+ } catch (e: Exception) {
+ e.printStackTrace()
+ }
+
+ client.unbind(activity)
Column(
horizontalAlignment = Alignment.CenterHorizontally
@@ -201,34 +205,4 @@ fun SummaryScreen(model: WithdrawalViewModel) {
}
}
-/**
- * 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()
- }
-}
-
private fun formatTalerUri(encodedWopid: String) = "taler://withdraw/$encodedWopid"