diff options
author | Torsten Grote <t@grobox.de> | 2020-02-12 16:53:03 -0300 |
---|---|---|
committer | Torsten Grote <t@grobox.de> | 2020-02-12 17:09:09 -0300 |
commit | ada382885e9c103fe0795817a8585270a3079302 (patch) | |
tree | 3da610aa39d2c767307d974d54f2f4eb32be9188 /app/src/main/java/net/taler/wallet/payment/PromptPaymentFragment.kt | |
parent | c20a7945f2b37863264c3b9bdcc85454018bd4cd (diff) | |
download | wallet-android-ada382885e9c103fe0795817a8585270a3079302.tar.gz wallet-android-ada382885e9c103fe0795817a8585270a3079302.tar.bz2 wallet-android-ada382885e9c103fe0795817a8585270a3079302.zip |
Refactor payment code to make it easier to extend
Diffstat (limited to 'app/src/main/java/net/taler/wallet/payment/PromptPaymentFragment.kt')
-rw-r--r-- | app/src/main/java/net/taler/wallet/payment/PromptPaymentFragment.kt | 136 |
1 files changed, 136 insertions, 0 deletions
diff --git a/app/src/main/java/net/taler/wallet/payment/PromptPaymentFragment.kt b/app/src/main/java/net/taler/wallet/payment/PromptPaymentFragment.kt new file mode 100644 index 0000000..4b4bf01 --- /dev/null +++ b/app/src/main/java/net/taler/wallet/payment/PromptPaymentFragment.kt @@ -0,0 +1,136 @@ +/* + This file is part of GNU Taler + (C) 2019 Taler Systems S.A. + + GNU Taler is free software; you can redistribute it and/or modify it under the + terms of the GNU General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along with + GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> + */ + +package net.taler.wallet.payment + +import android.annotation.SuppressLint +import android.os.Bundle +import android.view.LayoutInflater +import android.view.View +import android.view.View.INVISIBLE +import android.view.View.VISIBLE +import android.view.ViewGroup +import androidx.fragment.app.Fragment +import androidx.fragment.app.activityViewModels +import androidx.lifecycle.observe +import androidx.navigation.fragment.findNavController +import kotlinx.android.synthetic.main.fragment_prompt_payment.* +import net.taler.wallet.Amount +import net.taler.wallet.R +import net.taler.wallet.WalletViewModel + +/** + * Show a payment and ask the user to accept/decline. + */ +class PromptPaymentFragment : Fragment() { + + private val model: WalletViewModel by activityViewModels() + private val paymentManager by lazy { model.paymentManager } + + override fun onCreateView( + inflater: LayoutInflater, container: ViewGroup?, + savedInstanceState: Bundle? + ): View? { + return inflater.inflate(R.layout.fragment_prompt_payment, container, false) + } + + override fun onViewCreated(view: View, savedInstanceState: Bundle?) { + paymentManager.payStatus.observe(viewLifecycleOwner, this::onPaymentStatusChanged) + + button_abort_payment.setOnClickListener { + when (val ps = paymentManager.payStatus.value) { + is PayStatus.Prepared -> { + paymentManager.abortProposal(ps.proposalId) + } + } + paymentManager.resetPayStatus() + findNavController().navigateUp() + } + } + + private fun showLoading(show: Boolean) { + model.showProgressBar.value = show + } + + private fun onPaymentStatusChanged(payStatus: PayStatus) { + when (payStatus) { + is PayStatus.Prepared -> { + showLoading(false) + showOrder(payStatus.contractTerms, payStatus.totalFees) + button_confirm_payment.isEnabled = true + button_confirm_payment.setOnClickListener { + showLoading(true) + paymentManager.confirmPay(payStatus.proposalId) + button_confirm_payment.isEnabled = false + } + } + is PayStatus.InsufficientBalance -> { + showLoading(false) + showOrder(payStatus.contractTerms, null) + error_text.setText(R.string.payment_balance_insufficient) + fadeInView(error_text) + } + is PayStatus.Success -> { + showLoading(false) + paymentManager.resetPayStatus() + findNavController().navigate(R.id.action_promptPayment_to_paymentSuccessful) + } + is PayStatus.AlreadyPaid -> { + showLoading(false) + paymentManager.resetPayStatus() + findNavController().navigate(R.id.action_promptPayment_to_alreadyPaid) + } + is PayStatus.Error -> { + showLoading(false) + error_text.text = getString(R.string.payment_error, payStatus.error) + fadeInView(error_text) + } + is PayStatus.None -> { + // No payment active. + showLoading(false) + } + is PayStatus.Loading -> { + // Wait until loaded ... + showLoading(true) + } + } + } + + private fun showOrder(contractTerms: ContractTerms, totalFees: Amount?) { + order_summary.text = contractTerms.summary + val amount = contractTerms.amount + @SuppressLint("SetTextI18n") + order_amount.text = "${amount.amount} ${amount.currency}" + if (totalFees != null && !totalFees.isZero()) { + val fee = "${totalFees.amount} ${totalFees.currency}" + order_fees_amount.text = getString(R.string.payment_fee, fee) + fadeInView(order_fees_amount) + } else { + order_fees_amount.visibility = INVISIBLE + } + fadeInView(order_summary_label) + fadeInView(order_summary) + fadeInView(order_amount_label) + fadeInView(order_amount) + } + + private fun fadeInView(v: View) { + v.alpha = 0f + v.visibility = VISIBLE + v.animate().alpha(1f).start() + } + +} |