From fd19956fd52fbab0c922cd58fac66cad97a94805 Mon Sep 17 00:00:00 2001 From: Joel-Haeberli Date: Tue, 23 Apr 2024 22:17:54 +0200 Subject: app: update flow --- c2ec/main.go | 12 ++ .../wallee_c2ec/withdrawal/WithdrawalActivity.kt | 126 ++++++++------------- .../wallee_c2ec/withdrawal/WithdrawalViewModel.kt | 35 +++++- 3 files changed, 95 insertions(+), 78 deletions(-) diff --git a/c2ec/main.go b/c2ec/main.go index dcd551a..3eb8d98 100644 --- a/c2ec/main.go +++ b/c2ec/main.go @@ -82,6 +82,12 @@ func main() { RunAttestor(attestorCtx, attestorErrs) LogInfo("main", "attestor is running") + transferCtx, transferCancel := context.WithCancel(context.Background()) + defer transferCancel() + transferErrs := make(chan error) + RunRefunder(attestorCtx, attestorErrs) + LogInfo("main", "refunder is running") + router := http.NewServeMux() setupBankIntegrationRoutes(router) @@ -154,6 +160,12 @@ func main() { retryCancel() // first run old cancellation function retryCtx, retryCancel = context.WithCancel(context.Background()) RunRetrier(retryCtx, retryErrs) + case transferError := <-transferErrs: + LogError("main from refunder", transferError) + case <-transferCtx.Done(): + transferCancel() // first run old cancellation function + transferCtx, transferCancel = context.WithCancel(context.Background()) + RunRefunder(retryCtx, retryErrs) } } } 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() @@ -117,40 +130,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, @@ -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 { - - 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" 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 { + + 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 -- cgit v1.2.3