From a6e5a8d9f210618d779d43c859234b94827bf172 Mon Sep 17 00:00:00 2001 From: Torsten Grote Date: Thu, 2 Apr 2020 13:48:33 -0300 Subject: [wallet] Show withdraw summary before ToS and do one-click accept --- .../wallet/withdraw/PromptWithdrawFragment.kt | 62 ++++++++++++-------- .../wallet/withdraw/ReviewExchangeTosFragment.kt | 11 +--- .../net/taler/wallet/withdraw/WithdrawManager.kt | 68 ++++++++++------------ .../main/res/layout/fragment_prompt_withdraw.xml | 14 +++-- .../res/layout/fragment_review_exchange_tos.xml | 36 +++--------- wallet/src/main/res/navigation/nav_graph.xml | 7 +-- wallet/src/main/res/values/strings.xml | 7 ++- 7 files changed, 94 insertions(+), 111 deletions(-) (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 61aed4a..875a9c4 100644 --- a/wallet/src/main/java/net/taler/wallet/withdraw/PromptWithdrawFragment.kt +++ b/wallet/src/main/java/net/taler/wallet/withdraw/PromptWithdrawFragment.kt @@ -25,6 +25,7 @@ import androidx.fragment.app.activityViewModels import androidx.lifecycle.Observer import androidx.navigation.fragment.findNavController import kotlinx.android.synthetic.main.fragment_prompt_withdraw.* +import net.taler.common.Amount import net.taler.common.fadeIn import net.taler.common.fadeOut import net.taler.wallet.R @@ -48,34 +49,23 @@ class PromptWithdrawFragment : Fragment() { override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) - confirmWithdrawButton.setOnClickListener { - val status = withdrawManager.withdrawStatus.value - if (status !is WithdrawStatus.ReceivedDetails) throw AssertionError() - it.fadeOut() - confirmProgressBar.fadeIn() - withdrawManager.acceptWithdrawal(status.talerWithdrawUri, status.suggestedExchange) - } - withdrawManager.withdrawStatus.observe(viewLifecycleOwner, Observer { showWithdrawStatus(it) }) } - private fun showWithdrawStatus(status: WithdrawStatus?) = when (status) { + private fun showWithdrawStatus(status: WithdrawStatus?): Any = when (status) { is WithdrawStatus.ReceivedDetails -> { - model.showProgressBar.value = false - progressBar.fadeOut() - - introView.fadeIn() - withdrawAmountView.text = status.amount.toString() - withdrawAmountView.fadeIn() - feeView.fadeIn() - - exchangeIntroView.fadeIn() - withdrawExchangeUrl.text = status.suggestedExchange - withdrawExchangeUrl.fadeIn() - - confirmWithdrawButton.isEnabled = true + showContent(status.amount, status.suggestedExchange) + confirmWithdrawButton.apply { + text = getString(R.string.withdraw_button_confirm) + setOnClickListener { + it.fadeOut() + confirmProgressBar.fadeIn() + withdrawManager.acceptWithdrawal(status.talerWithdrawUri, status.suggestedExchange) + } + isEnabled = true + } } is WithdrawStatus.Success -> { model.showProgressBar.value = false @@ -89,8 +79,14 @@ class PromptWithdrawFragment : Fragment() { model.showProgressBar.value = true } is TermsOfServiceReviewRequired -> { - model.showProgressBar.value = false - findNavController().navigate(R.id.action_promptWithdraw_to_reviewExchangeTOS) + showContent(status.amount, status.suggestedExchange) + confirmWithdrawButton.apply { + text = getString(R.string.withdraw_button_tos) + setOnClickListener { + findNavController().navigate(R.id.action_promptWithdraw_to_reviewExchangeTOS) + } + isEnabled = true + } } is WithdrawStatus.Error -> { model.showProgressBar.value = false @@ -99,4 +95,22 @@ class PromptWithdrawFragment : Fragment() { null -> model.showProgressBar.value = false } + private fun showContent(amount: Amount, exchange: String) { + model.showProgressBar.value = false + progressBar.fadeOut() + + introView.fadeIn() + withdrawAmountView.text = amount.toString() + withdrawAmountView.fadeIn() + feeView.fadeIn() + + exchangeIntroView.fadeIn() + withdrawExchangeUrl.text = exchange.let { + if (it.startsWith("https://")) it.substring(8) else it + }.trimEnd('/') + withdrawExchangeUrl.fadeIn() + + withdrawCard.fadeIn() + } + } diff --git a/wallet/src/main/java/net/taler/wallet/withdraw/ReviewExchangeTosFragment.kt b/wallet/src/main/java/net/taler/wallet/withdraw/ReviewExchangeTosFragment.kt index d998e5d..af76971 100644 --- a/wallet/src/main/java/net/taler/wallet/withdraw/ReviewExchangeTosFragment.kt +++ b/wallet/src/main/java/net/taler/wallet/withdraw/ReviewExchangeTosFragment.kt @@ -47,14 +47,7 @@ class ReviewExchangeTosFragment : Fragment() { override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) acceptTosCheckBox.isChecked = false - acceptTosCheckBox.setOnCheckedChangeListener { _, isChecked -> - acceptTosButton.isEnabled = isChecked - } -// abortTosButton.setOnClickListener { -// withdrawManager.cancelCurrentWithdraw() -// findNavController().navigateUp() -// } - acceptTosButton.setOnClickListener { + acceptTosCheckBox.setOnCheckedChangeListener { _, _ -> withdrawManager.acceptCurrentTermsOfService() } withdrawManager.withdrawStatus.observe(viewLifecycleOwner, Observer { @@ -71,8 +64,6 @@ class ReviewExchangeTosFragment : Fragment() { is WithdrawStatus.ReceivedDetails -> { findNavController().navigate(R.id.action_reviewExchangeTOS_to_promptWithdraw) } - else -> { - } } }) } 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 d686465..e252627 100644 --- a/wallet/src/main/java/net/taler/wallet/withdraw/WithdrawManager.kt +++ b/wallet/src/main/java/net/taler/wallet/withdraw/WithdrawManager.kt @@ -21,6 +21,7 @@ import androidx.lifecycle.MutableLiveData import net.taler.common.Amount import net.taler.wallet.TAG import net.taler.wallet.backend.WalletBackendApi +import net.taler.wallet.withdraw.WithdrawStatus.ReceivedDetails import org.json.JSONObject sealed class WithdrawStatus { @@ -62,9 +63,9 @@ class WithdrawManager(private val walletBackendApi: WalletBackendApi) { } fun getWithdrawalInfo(talerWithdrawUri: String) { - val args = JSONObject() - args.put("talerWithdrawUri", talerWithdrawUri) - + val args = JSONObject().apply { + put("talerWithdrawUri", talerWithdrawUri) + } withdrawStatus.value = WithdrawStatus.Loading(talerWithdrawUri) this.currentWithdrawRequestId++ @@ -97,12 +98,13 @@ class WithdrawManager(private val walletBackendApi: WalletBackendApi) { } private fun getWithdrawalInfoWithExchange(talerWithdrawUri: String, selectedExchange: String) { - val args = JSONObject() - args.put("talerWithdrawUri", talerWithdrawUri) - args.put("selectedExchange", selectedExchange) + val args = JSONObject().apply { + put("talerWithdrawUri", talerWithdrawUri) + put("selectedExchange", selectedExchange) + } - this.currentWithdrawRequestId++ - val myWithdrawRequestId = this.currentWithdrawRequestId + currentWithdrawRequestId++ + val myWithdrawRequestId = currentWithdrawRequestId walletBackendApi.sendRequest("getWithdrawDetailsForUri", args) { isError, result -> if (isError) { @@ -111,15 +113,15 @@ class WithdrawManager(private val walletBackendApi: WalletBackendApi) { withdrawStatus.postValue(WithdrawStatus.Error(message)) return@sendRequest } - if (myWithdrawRequestId != this.currentWithdrawRequestId) { - val mismatch = "$myWithdrawRequestId != ${this.currentWithdrawRequestId}" + if (myWithdrawRequestId != currentWithdrawRequestId) { + val mismatch = "$myWithdrawRequestId != $currentWithdrawRequestId" Log.w(TAG, "Got withdraw result for different request id $mismatch") return@sendRequest } Log.v(TAG, "got getWithdrawDetailsForUri result (with exchange details)") val status = withdrawStatus.value if (status !is WithdrawStatus.Loading) { - Log.v(TAG, "ignoring withdrawal info result, not loading.") + Log.w(TAG, "ignoring withdrawal info result, not loading.") return@sendRequest } val wi = result.getJSONObject("bankWithdrawDetails") @@ -145,7 +147,7 @@ class WithdrawManager(private val walletBackendApi: WalletBackendApi) { ) } else { withdrawStatus.postValue( - WithdrawStatus.ReceivedDetails( + ReceivedDetails( status.talerWithdrawUri, amount, suggestedExchange @@ -162,15 +164,16 @@ class WithdrawManager(private val walletBackendApi: WalletBackendApi) { withdrawStatus.value = WithdrawStatus.Withdrawing(talerWithdrawUri) - walletBackendApi.sendRequest("acceptWithdrawal", args) { isError, _ -> + walletBackendApi.sendRequest("acceptWithdrawal", args) { isError, result -> if (isError) { - Log.v(TAG, "got acceptWithdrawal error result") + Log.v(TAG, "got acceptWithdrawal error result: ${result.toString(4)}") return@sendRequest } Log.v(TAG, "got acceptWithdrawal result") val status = withdrawStatus.value if (status !is WithdrawStatus.Withdrawing) { - Log.v(TAG, "ignoring acceptWithdrawal result, invalid state") + Log.w(TAG, "ignoring acceptWithdrawal result, invalid state: $status") + return@sendRequest } withdrawStatus.postValue(WithdrawStatus.Success) } @@ -180,30 +183,21 @@ class WithdrawManager(private val walletBackendApi: WalletBackendApi) { * Accept the currently displayed terms of service. */ fun acceptCurrentTermsOfService() { - when (val s = withdrawStatus.value) { - is WithdrawStatus.TermsOfServiceReviewRequired -> { - val args = JSONObject() - args.put("exchangeBaseUrl", s.exchangeBaseUrl) - args.put("etag", s.tosEtag) - walletBackendApi.sendRequest("acceptExchangeTermsOfService", args) { isError, _ -> - if (isError) { - return@sendRequest - } - withdrawStatus.postValue( - WithdrawStatus.ReceivedDetails( - s.talerWithdrawUri, - s.amount, - s.suggestedExchange - ) - ) - } + val s = withdrawStatus.value + check(s is WithdrawStatus.TermsOfServiceReviewRequired) + + val args = JSONObject().apply { + put("exchangeBaseUrl", s.exchangeBaseUrl) + put("etag", s.tosEtag) + } + walletBackendApi.sendRequest("acceptExchangeTermsOfService", args) { isError, result -> + if (isError) { + Log.e(TAG, "Error acceptExchangeTermsOfService ${result.toString(4)}") + return@sendRequest } + val status = ReceivedDetails(s.talerWithdrawUri, s.amount, s.suggestedExchange) + withdrawStatus.postValue(status) } } - fun cancelCurrentWithdraw() { - currentWithdrawRequestId++ - withdrawStatus.value = null - } - } diff --git a/wallet/src/main/res/layout/fragment_prompt_withdraw.xml b/wallet/src/main/res/layout/fragment_prompt_withdraw.xml index fe64bad..b03ee03 100644 --- a/wallet/src/main/res/layout/fragment_prompt_withdraw.xml +++ b/wallet/src/main/res/layout/fragment_prompt_withdraw.xml @@ -29,7 +29,7 @@ android:layout_marginEnd="16dp" android:layout_marginBottom="8dp" android:gravity="center" - android:text="@string/withdraw_do_you_want" + android:text="@string/withdraw_total" android:visibility="invisible" app:layout_constraintBottom_toTopOf="@+id/withdrawAmountView" app:layout_constraintEnd_toEndOf="parent" @@ -97,13 +97,13 @@ android:layout_marginStart="16dp" android:layout_marginEnd="16dp" android:gravity="center" - android:textSize="25sp" + android:textSize="24sp" android:visibility="invisible" app:layout_constraintBottom_toTopOf="@+id/withdrawCard" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/exchangeIntroView" - tools:text="(exchange base url)" + tools:text="long.exchange.demo.taler.net" tools:visibility="visible" /> + app:layout_constraintStart_toStartOf="parent" + tools:visibility="visible"> + app:layout_constraintStart_toStartOf="parent" + tools:enabled="true" + tools:text="@string/withdraw_button_tos" /> - - - - -