From 905c63242ba6d80caece6c18b2b867cb300bbe7b Mon Sep 17 00:00:00 2001 From: Torsten Grote Date: Fri, 6 Jan 2023 11:43:10 -0300 Subject: [wallet] Allow to restrict coins to age when withdrawing #0007352 --- .../wallet/withdraw/PromptWithdrawFragment.kt | 20 ++++++++++-- .../net/taler/wallet/withdraw/WithdrawManager.kt | 32 +++++++++++++------ .../main/res/layout/fragment_prompt_withdraw.xml | 36 +++++++++++++++++++++- wallet/src/main/res/layout/list_item_age.xml | 26 ++++++++++++++++ wallet/src/main/res/values/strings.xml | 2 ++ 5 files changed, 104 insertions(+), 12 deletions(-) create mode 100644 wallet/src/main/res/layout/list_item_age.xml (limited to 'wallet/src') diff --git a/wallet/src/main/java/net/taler/wallet/withdraw/PromptWithdrawFragment.kt b/wallet/src/main/java/net/taler/wallet/withdraw/PromptWithdrawFragment.kt index 3cd2f49..dbf901a 100644 --- a/wallet/src/main/java/net/taler/wallet/withdraw/PromptWithdrawFragment.kt +++ b/wallet/src/main/java/net/taler/wallet/withdraw/PromptWithdrawFragment.kt @@ -20,6 +20,7 @@ import android.os.Bundle import android.view.LayoutInflater import android.view.View import android.view.ViewGroup +import android.widget.ArrayAdapter import androidx.fragment.app.Fragment import androidx.fragment.app.activityViewModels import androidx.navigation.fragment.findNavController @@ -114,13 +115,18 @@ class PromptWithdrawFragment : Fragment() { } private fun onReceivedDetails(s: ReceivedDetails) { - showContent(s.amountRaw, s.amountEffective, s.exchangeBaseUrl, s.talerWithdrawUri) + showContent(s.amountRaw, s.amountEffective, s.exchangeBaseUrl, s.talerWithdrawUri, + s.ageRestrictionOptions) ui.confirmWithdrawButton.apply { text = getString(R.string.withdraw_button_confirm) setOnClickListener { it.fadeOut() ui.confirmProgressBar.fadeIn() - withdrawManager.acceptWithdrawal() + val ageRestrict = (ui.ageSelector.selectedItem as String?)?.let { age -> + if (age == context.getString(R.string.withdraw_restrict_age_unrestricted)) null + else age.toIntOrNull() + } + withdrawManager.acceptWithdrawal(ageRestrict) } isEnabled = true } @@ -131,6 +137,7 @@ class PromptWithdrawFragment : Fragment() { amountEffective: Amount, exchange: String, uri: String?, + ageRestrictionOptions: List? = null, ) { model.showProgressBar.value = false ui.progressBar.fadeOut() @@ -160,6 +167,15 @@ class PromptWithdrawFragment : Fragment() { } } + if (ageRestrictionOptions != null) { + ui.ageLabel.fadeIn() + val context = requireContext() + val items = listOf(context.getString(R.string.withdraw_restrict_age_unrestricted)) + + ageRestrictionOptions.map { it.toString() } + ui.ageSelector.adapter = ArrayAdapter(context, R.layout.list_item_age, items) + ui.ageSelector.fadeIn() + } + ui.withdrawCard.fadeIn() } diff --git a/wallet/src/main/java/net/taler/wallet/withdraw/WithdrawManager.kt b/wallet/src/main/java/net/taler/wallet/withdraw/WithdrawManager.kt index 6fdc916..1698a10 100644 --- a/wallet/src/main/java/net/taler/wallet/withdraw/WithdrawManager.kt +++ b/wallet/src/main/java/net/taler/wallet/withdraw/WithdrawManager.kt @@ -44,6 +44,7 @@ sealed class WithdrawStatus { val exchangeBaseUrl: String, val amountRaw: Amount, val amountEffective: Amount, + val ageRestrictionOptions: List? = null, val tosText: String, val tosEtag: String, val showImmediately: Event, @@ -54,11 +55,12 @@ sealed class WithdrawStatus { val exchangeBaseUrl: String, val amountRaw: Amount, val amountEffective: Amount, + val ageRestrictionOptions: List? = null, ) : WithdrawStatus() object Withdrawing : WithdrawStatus() data class Success(val currency: String) : WithdrawStatus() - sealed class ManualTransferRequired: WithdrawStatus() { + sealed class ManualTransferRequired : WithdrawStatus() { abstract val uri: Uri abstract val transactionId: String? } @@ -103,6 +105,7 @@ data class WithdrawalDetails( val tosAccepted: Boolean, val amountRaw: Amount, val amountEffective: Amount, + val ageRestrictionOptions: List? = null, ) @Serializable @@ -152,9 +155,12 @@ class WithdrawManager( if (details.defaultExchangeBaseUrl == null) { val exchangeSelection = ExchangeSelection(details.amount, uri) withdrawStatus.value = WithdrawStatus.NeedsExchange(exchangeSelection.toEvent()) - } else { - getWithdrawalDetails(details.defaultExchangeBaseUrl, details.amount, false, uri) - } + } else getWithdrawalDetails( + exchangeBaseUrl = details.defaultExchangeBaseUrl, + amount = details.amount, + showTosImmediately = false, + uri = uri, + ) } } @@ -177,6 +183,7 @@ class WithdrawManager( exchangeBaseUrl = exchangeBaseUrl, amountRaw = details.amountRaw, amountEffective = details.amountEffective, + ageRestrictionOptions = details.ageRestrictionOptions, ) } else getExchangeTos(exchangeBaseUrl, details, showTosImmediately, uri) } @@ -198,6 +205,7 @@ class WithdrawManager( exchangeBaseUrl = exchangeBaseUrl, amountRaw = details.amountRaw, amountEffective = details.amountEffective, + ageRestrictionOptions = details.ageRestrictionOptions, tosText = it.content, tosEtag = it.currentEtag, showImmediately = showImmediately.toEvent(), @@ -221,23 +229,28 @@ class WithdrawManager( exchangeBaseUrl = s.exchangeBaseUrl, amountRaw = s.amountRaw, amountEffective = s.amountEffective, + ageRestrictionOptions = s.ageRestrictionOptions, ) } } @UiThread - fun acceptWithdrawal() = scope.launch { + fun acceptWithdrawal(restrictAge: Int? = null) = scope.launch { val status = withdrawStatus.value as ReceivedDetails withdrawStatus.value = WithdrawStatus.Withdrawing if (status.talerWithdrawUri == null) { - acceptManualWithdrawal(status) + acceptManualWithdrawal(status, restrictAge) } else { - acceptBankIntegratedWithdrawal(status) + acceptBankIntegratedWithdrawal(status, restrictAge) } } - private suspend fun acceptBankIntegratedWithdrawal(status: ReceivedDetails) { + private suspend fun acceptBankIntegratedWithdrawal( + status: ReceivedDetails, + restrictAge: Int? = null, + ) { api.request("acceptBankIntegratedWithdrawal") { + restrictAge?.let { put("restrictAge", restrictAge) } put("exchangeBaseUrl", status.exchangeBaseUrl) put("talerWithdrawUri", status.talerWithdrawUri) }.onError { @@ -247,8 +260,9 @@ class WithdrawManager( } } - private suspend fun acceptManualWithdrawal(status: ReceivedDetails) { + private suspend fun acceptManualWithdrawal(status: ReceivedDetails, restrictAge: Int? = null) { api.request("acceptManualWithdrawal", AcceptManualWithdrawalResponse.serializer()) { + restrictAge?.let { put("restrictAge", restrictAge) } put("exchangeBaseUrl", status.exchangeBaseUrl) put("amount", status.amountRaw.toJSONString()) }.onError { diff --git a/wallet/src/main/res/layout/fragment_prompt_withdraw.xml b/wallet/src/main/res/layout/fragment_prompt_withdraw.xml index 372163b..03e7d1a 100644 --- a/wallet/src/main/res/layout/fragment_prompt_withdraw.xml +++ b/wallet/src/main/res/layout/fragment_prompt_withdraw.xml @@ -152,7 +152,7 @@ android:textSize="24sp" android:visibility="invisible" app:layout_constrainedWidth="true" - app:layout_constraintBottom_toTopOf="@+id/withdrawCard" + app:layout_constraintBottom_toTopOf="@+id/ageLabel" app:layout_constraintEnd_toStartOf="@+id/selectExchangeButton" app:layout_constraintHorizontal_chainStyle="packed" app:layout_constraintStart_toStartOf="parent" @@ -176,6 +176,40 @@ app:tint="?attr/colorOnPrimary" tools:visibility="visible" /> + + + + + + diff --git a/wallet/src/main/res/values/strings.xml b/wallet/src/main/res/values/strings.xml index eb15021..0595fa0 100644 --- a/wallet/src/main/res/values/strings.xml +++ b/wallet/src/main/res/values/strings.xml @@ -156,6 +156,8 @@ GNU Taler is immune against many types of fraud, such as phishing of credit card Withdrawal Withdraw Fee + Restrict Usage to Age + Unrestricted Exchange Confirm Withdraw Confirm with bank -- cgit v1.2.3