diff options
author | Torsten Grote <t@grobox.de> | 2020-09-03 11:50:13 -0300 |
---|---|---|
committer | Torsten Grote <t@grobox.de> | 2020-09-03 11:50:13 -0300 |
commit | 85c344b358e13d5b78647d191d6129dd11f8bdaf (patch) | |
tree | 0aa870edbd76e43075d3c701626a2b41ce78682d /wallet/src/main/java/net/taler/wallet/transactions | |
parent | 126b0710dda4546dc3337df1bee886f5f4657674 (diff) | |
download | taler-android-85c344b358e13d5b78647d191d6129dd11f8bdaf.tar.gz taler-android-85c344b358e13d5b78647d191d6129dd11f8bdaf.tar.bz2 taler-android-85c344b358e13d5b78647d191d6129dd11f8bdaf.zip |
[wallet] migrate away from kotlin view extensions
Diffstat (limited to 'wallet/src/main/java/net/taler/wallet/transactions')
7 files changed, 288 insertions, 118 deletions
diff --git a/wallet/src/main/java/net/taler/wallet/transactions/TransactionDetailFragment.kt b/wallet/src/main/java/net/taler/wallet/transactions/TransactionDetailFragment.kt index f15e34f..302e684 100644 --- a/wallet/src/main/java/net/taler/wallet/transactions/TransactionDetailFragment.kt +++ b/wallet/src/main/java/net/taler/wallet/transactions/TransactionDetailFragment.kt @@ -19,68 +19,34 @@ package net.taler.wallet.transactions import android.content.Intent import android.net.Uri import android.os.Bundle -import android.view.LayoutInflater import android.view.Menu import android.view.MenuInflater import android.view.MenuItem -import android.view.View -import android.view.View.GONE -import android.view.ViewGroup -import android.widget.Toast -import android.widget.Toast.LENGTH_LONG -import androidx.core.content.ContextCompat.getColor +import android.widget.TextView import androidx.fragment.app.Fragment import androidx.fragment.app.activityViewModels -import kotlinx.android.synthetic.main.fragment_transaction_payment.* -import kotlinx.android.synthetic.main.fragment_transaction_withdrawal.* -import kotlinx.android.synthetic.main.fragment_transaction_withdrawal.feeView -import kotlinx.android.synthetic.main.fragment_transaction_withdrawal.timeView import net.taler.common.isSafe -import net.taler.common.toAbsoluteTime import net.taler.lib.common.Amount import net.taler.wallet.MainViewModel import net.taler.wallet.R -import net.taler.wallet.cleanExchange -import net.taler.wallet.transactions.WithdrawalDetails.TalerBankIntegrationApi -class TransactionDetailFragment : Fragment() { +abstract class TransactionDetailFragment : Fragment() { private val model: MainViewModel by activityViewModels() private val transactionManager by lazy { model.transactionManager } - private val transaction by lazy { requireNotNull(transactionManager.selectedTransaction) } + protected val transaction: Transaction? get() = transactionManager.selectedTransaction override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setHasOptionsMenu(model.devMode.value == true) } - override fun onCreateView( - inflater: LayoutInflater, - container: ViewGroup?, - savedInstanceState: Bundle? - ): View? { - return inflater.inflate(transaction.detailPageLayout, container, false) - } - override fun onActivityCreated(savedInstanceState: Bundle?) { super.onActivityCreated(savedInstanceState) requireActivity().apply { - title = getString(transaction.generalTitleRes) - } - } - - override fun onViewCreated(view: View, savedInstanceState: Bundle?) { - timeView.text = transaction.timestamp.ms.toAbsoluteTime(requireContext()) - when (val e = transaction) { - is TransactionWithdrawal -> bind(e) - is TransactionPayment -> bind(e) - is TransactionRefund -> bind(e) - is TransactionRefresh -> bind(e) - else -> Toast.makeText( - requireContext(), - "Transaction ${e.javaClass.simpleName} not implemented.", - LENGTH_LONG - ).show() + transaction?.generalTitleRes?.let { + title = getString(it) + } } } @@ -94,54 +60,15 @@ class TransactionDetailFragment : Fragment() { } } - private fun bind(t: TransactionWithdrawal) { - effectiveAmountLabel.text = getString(R.string.withdraw_total) - effectiveAmountView.text = t.amountEffective.toString() - if (t.pending && t.withdrawalDetails is TalerBankIntegrationApi && - !t.confirmed && t.withdrawalDetails.bankConfirmationUrl != null - ) { - val i = Intent().apply { - data = Uri.parse(t.withdrawalDetails.bankConfirmationUrl) - } - if (i.isSafe(requireContext())) { - confirmWithdrawalButton.setOnClickListener { startActivity(i) } - } - } else confirmWithdrawalButton.visibility = GONE - chosenAmountLabel.text = getString(R.string.amount_chosen) - chosenAmountView.text = - getString(R.string.amount_positive, t.amountRaw.toString()) - val fee = t.amountRaw - t.amountEffective - feeView.text = getString(R.string.amount_negative, fee.toString()) - exchangeView.text = cleanExchange(t.exchangeBaseUrl) - } - - private fun bind(t: TransactionPayment) { - amountPaidWithFeesView.text = t.amountEffective.toString() - val fee = t.amountEffective - t.amountRaw - bindOrderAndFee(t.info, t.amountRaw, fee) - } - - private fun bind(t: TransactionRefund) { - amountPaidWithFeesLabel.text = getString(R.string.transaction_refund) - amountPaidWithFeesView.setTextColor(getColor(requireContext(), R.color.green)) - amountPaidWithFeesView.text = - getString(R.string.amount_positive, t.amountEffective.toString()) - val fee = t.amountRaw - t.amountEffective - bindOrderAndFee(t.info, t.amountRaw, fee) - } - - private fun bind(t: TransactionRefresh) { - effectiveAmountLabel.visibility = GONE - effectiveAmountView.visibility = GONE - confirmWithdrawalButton.visibility = GONE - chosenAmountLabel.visibility = GONE - chosenAmountView.visibility = GONE - val fee = t.amountEffective - feeView.text = getString(R.string.amount_negative, fee.toString()) - exchangeView.text = cleanExchange(t.exchangeBaseUrl) - } - - private fun bindOrderAndFee(info: TransactionInfo, raw: Amount, fee: Amount) { + protected fun bindOrderAndFee( + orderSummaryView: TextView, + orderAmountView: TextView, + orderIdView: TextView, + feeView: TextView, + info: TransactionInfo, + raw: Amount, + fee: Amount + ) { orderAmountView.text = raw.toString() feeView.text = getString(R.string.amount_negative, fee.toString()) orderSummaryView.text = if (info.fulfillmentMessage == null) { diff --git a/wallet/src/main/java/net/taler/wallet/transactions/TransactionPaymentFragment.kt b/wallet/src/main/java/net/taler/wallet/transactions/TransactionPaymentFragment.kt new file mode 100644 index 0000000..84c5c77 --- /dev/null +++ b/wallet/src/main/java/net/taler/wallet/transactions/TransactionPaymentFragment.kt @@ -0,0 +1,56 @@ +/* + * This file is part of GNU Taler + * (C) 2020 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.transactions + +import android.os.Bundle +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import net.taler.common.toAbsoluteTime +import net.taler.wallet.databinding.FragmentTransactionPaymentBinding + +class TransactionPaymentFragment : TransactionDetailFragment() { + + private lateinit var ui: FragmentTransactionPaymentBinding + + override fun onCreateView( + inflater: LayoutInflater, + container: ViewGroup?, + savedInstanceState: Bundle? + ): View? { + ui = FragmentTransactionPaymentBinding.inflate(inflater, container, false) + return ui.root + } + + override fun onViewCreated(view: View, savedInstanceState: Bundle?) { + val t = transaction as TransactionPayment + ui.timeView.text = t.timestamp.ms.toAbsoluteTime(requireContext()) + + ui.amountPaidWithFeesView.text = t.amountEffective.toString() + val fee = t.amountEffective - t.amountRaw + bindOrderAndFee( + ui.orderSummaryView, + ui.orderAmountView, + ui.orderIdView, + ui.feeView, + t.info, + t.amountRaw, + fee + ) + } + +} diff --git a/wallet/src/main/java/net/taler/wallet/transactions/TransactionRefreshFragment.kt b/wallet/src/main/java/net/taler/wallet/transactions/TransactionRefreshFragment.kt new file mode 100644 index 0000000..717dd33 --- /dev/null +++ b/wallet/src/main/java/net/taler/wallet/transactions/TransactionRefreshFragment.kt @@ -0,0 +1,56 @@ +/* + * This file is part of GNU Taler + * (C) 2020 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.transactions + +import android.os.Bundle +import android.view.LayoutInflater +import android.view.View +import android.view.View.GONE +import android.view.ViewGroup +import net.taler.common.toAbsoluteTime +import net.taler.wallet.R +import net.taler.wallet.cleanExchange +import net.taler.wallet.databinding.FragmentTransactionWithdrawalBinding + +class TransactionRefreshFragment : TransactionDetailFragment() { + + private lateinit var ui: FragmentTransactionWithdrawalBinding + + override fun onCreateView( + inflater: LayoutInflater, + container: ViewGroup?, + savedInstanceState: Bundle? + ): View? { + ui = FragmentTransactionWithdrawalBinding.inflate(inflater, container, false) + return ui.root + } + + override fun onViewCreated(view: View, savedInstanceState: Bundle?) { + val t = transaction as TransactionRefresh + ui.timeView.text = t.timestamp.ms.toAbsoluteTime(requireContext()) + + ui.effectiveAmountLabel.visibility = GONE + ui.effectiveAmountView.visibility = GONE + ui.confirmWithdrawalButton.visibility = GONE + ui.chosenAmountLabel.visibility = GONE + ui.chosenAmountView.visibility = GONE + val fee = t.amountEffective + ui.feeView.text = getString(R.string.amount_negative, fee.toString()) + ui. exchangeView.text = cleanExchange(t.exchangeBaseUrl) + } + +} diff --git a/wallet/src/main/java/net/taler/wallet/transactions/TransactionRefundFragment.kt b/wallet/src/main/java/net/taler/wallet/transactions/TransactionRefundFragment.kt new file mode 100644 index 0000000..6628d6c --- /dev/null +++ b/wallet/src/main/java/net/taler/wallet/transactions/TransactionRefundFragment.kt @@ -0,0 +1,61 @@ +/* + * This file is part of GNU Taler + * (C) 2020 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.transactions + +import android.os.Bundle +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import androidx.core.content.ContextCompat.getColor +import net.taler.common.toAbsoluteTime +import net.taler.wallet.R +import net.taler.wallet.databinding.FragmentTransactionPaymentBinding + +class TransactionRefundFragment : TransactionDetailFragment() { + + private lateinit var ui: FragmentTransactionPaymentBinding + + override fun onCreateView( + inflater: LayoutInflater, + container: ViewGroup?, + savedInstanceState: Bundle? + ): View? { + ui = FragmentTransactionPaymentBinding.inflate(inflater, container, false) + return ui.root + } + + override fun onViewCreated(view: View, savedInstanceState: Bundle?) { + val t = transaction as TransactionRefund + ui.timeView.text = t.timestamp.ms.toAbsoluteTime(requireContext()) + + ui.amountPaidWithFeesLabel.text = getString(R.string.transaction_refund) + ui.amountPaidWithFeesView.setTextColor(getColor(requireContext(), R.color.green)) + ui.amountPaidWithFeesView.text = + getString(R.string.amount_positive, t.amountEffective.toString()) + val fee = t.amountRaw - t.amountEffective + bindOrderAndFee( + ui.orderSummaryView, + ui.orderAmountView, + ui.orderIdView, + ui.feeView, + t.info, + t.amountRaw, + fee + ) + } + +} diff --git a/wallet/src/main/java/net/taler/wallet/transactions/TransactionWithdrawalFragment.kt b/wallet/src/main/java/net/taler/wallet/transactions/TransactionWithdrawalFragment.kt new file mode 100644 index 0000000..26965ef --- /dev/null +++ b/wallet/src/main/java/net/taler/wallet/transactions/TransactionWithdrawalFragment.kt @@ -0,0 +1,68 @@ +/* + * This file is part of GNU Taler + * (C) 2020 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.transactions + +import android.content.Intent +import android.net.Uri +import android.os.Bundle +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import net.taler.common.isSafe +import net.taler.common.toAbsoluteTime +import net.taler.wallet.R +import net.taler.wallet.cleanExchange +import net.taler.wallet.databinding.FragmentTransactionWithdrawalBinding + +class TransactionWithdrawalFragment : TransactionDetailFragment() { + + private lateinit var ui: FragmentTransactionWithdrawalBinding + + override fun onCreateView( + inflater: LayoutInflater, + container: ViewGroup?, + savedInstanceState: Bundle? + ): View? { + ui = FragmentTransactionWithdrawalBinding.inflate(inflater, container, false) + return ui.root + } + + override fun onViewCreated(view: View, savedInstanceState: Bundle?) { + val t = transaction as TransactionWithdrawal + ui.timeView.text = t.timestamp.ms.toAbsoluteTime(requireContext()) + + ui.effectiveAmountLabel.text = getString(R.string.withdraw_total) + ui.effectiveAmountView.text = t.amountEffective.toString() + if (t.pending && t.withdrawalDetails is WithdrawalDetails.TalerBankIntegrationApi && + !t.confirmed && t.withdrawalDetails.bankConfirmationUrl != null + ) { + val i = Intent().apply { + data = Uri.parse(t.withdrawalDetails.bankConfirmationUrl) + } + if (i.isSafe(requireContext())) { + ui.confirmWithdrawalButton.setOnClickListener { startActivity(i) } + } + } else ui.confirmWithdrawalButton.visibility = View.GONE + ui.chosenAmountLabel.text = getString(R.string.amount_chosen) + ui.chosenAmountView.text = + getString(R.string.amount_positive, t.amountRaw.toString()) + val fee = t.amountRaw - t.amountEffective + ui.feeView.text = getString(R.string.amount_negative, fee.toString()) + ui.exchangeView.text = cleanExchange(t.exchangeBaseUrl) + } + +} diff --git a/wallet/src/main/java/net/taler/wallet/transactions/Transactions.kt b/wallet/src/main/java/net/taler/wallet/transactions/Transactions.kt index 0817c36..50181c5 100644 --- a/wallet/src/main/java/net/taler/wallet/transactions/Transactions.kt +++ b/wallet/src/main/java/net/taler/wallet/transactions/Transactions.kt @@ -18,7 +18,7 @@ package net.taler.wallet.transactions import android.content.Context import androidx.annotation.DrawableRes -import androidx.annotation.LayoutRes +import androidx.annotation.IdRes import androidx.annotation.StringRes import kotlinx.serialization.SerialName import kotlinx.serialization.Serializable @@ -48,8 +48,8 @@ sealed class Transaction { @get:DrawableRes abstract val icon: Int - @get:LayoutRes - abstract val detailPageLayout: Int + @get:IdRes + abstract val detailPageNav: Int abstract val amountType: AmountType @@ -78,7 +78,8 @@ class TransactionWithdrawal( override val amountEffective: Amount ) : Transaction() { override val icon = R.drawable.transaction_withdrawal - override val detailPageLayout = R.layout.fragment_transaction_withdrawal + + override val detailPageNav = R.id.action_nav_transactions_detail_withdrawal @Transient override val amountType = AmountType.Positive @@ -135,7 +136,7 @@ class TransactionPayment( override val amountEffective: Amount ) : Transaction() { override val icon = R.drawable.ic_cash_usd_outline - override val detailPageLayout = R.layout.fragment_transaction_payment + override val detailPageNav = R.id.action_nav_transactions_detail_payment @Transient override val amountType = AmountType.Negative @@ -194,7 +195,7 @@ class TransactionRefund( override val amountEffective: Amount ) : Transaction() { override val icon = R.drawable.transaction_refund - override val detailPageLayout = R.layout.fragment_transaction_payment + override val detailPageNav = R.id.action_nav_transactions_detail_refund @Transient override val amountType = AmountType.Positive @@ -219,7 +220,7 @@ class TransactionTip( override val amountEffective: Amount ) : Transaction() { override val icon = R.drawable.transaction_tip_accepted // TODO different when declined - override val detailPageLayout = R.layout.fragment_transaction_payment + override val detailPageNav = 0 @Transient override val amountType = AmountType.Positive @@ -242,7 +243,7 @@ class TransactionRefresh( override val amountEffective: Amount ) : Transaction() { override val icon = R.drawable.transaction_refresh - override val detailPageLayout = R.layout.fragment_transaction_withdrawal + override val detailPageNav = R.id.action_nav_transactions_detail_refresh @Transient override val amountType = AmountType.Negative diff --git a/wallet/src/main/java/net/taler/wallet/transactions/TransactionsFragment.kt b/wallet/src/main/java/net/taler/wallet/transactions/TransactionsFragment.kt index 8d47a3f..90510e6 100644 --- a/wallet/src/main/java/net/taler/wallet/transactions/TransactionsFragment.kt +++ b/wallet/src/main/java/net/taler/wallet/transactions/TransactionsFragment.kt @@ -30,18 +30,17 @@ import androidx.appcompat.widget.SearchView import androidx.appcompat.widget.SearchView.OnQueryTextListener import androidx.fragment.app.Fragment import androidx.fragment.app.activityViewModels -import androidx.lifecycle.Observer import androidx.navigation.fragment.findNavController import androidx.recyclerview.selection.SelectionPredicates import androidx.recyclerview.selection.SelectionTracker import androidx.recyclerview.selection.StorageStrategy import androidx.recyclerview.widget.DividerItemDecoration import androidx.recyclerview.widget.LinearLayoutManager.VERTICAL -import kotlinx.android.synthetic.main.fragment_transactions.* import net.taler.common.fadeIn import net.taler.common.fadeOut import net.taler.wallet.MainViewModel import net.taler.wallet.R +import net.taler.wallet.databinding.FragmentTransactionsBinding interface OnTransactionClickListener { fun onTransactionClicked(transaction: Transaction) @@ -52,6 +51,7 @@ class TransactionsFragment : Fragment(), OnTransactionClickListener, ActionMode. private val model: MainViewModel by activityViewModels() private val transactionManager by lazy { model.transactionManager } + private lateinit var ui: FragmentTransactionsBinding private val transactionAdapter by lazy { TransactionAdapter(this) } private val currency by lazy { transactionManager.selectedCurrency!! } private var tracker: SelectionTracker<String>? = null @@ -66,19 +66,20 @@ class TransactionsFragment : Fragment(), OnTransactionClickListener, ActionMode. inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? ): View? { - return inflater.inflate(R.layout.fragment_transactions, container, false) + ui = FragmentTransactionsBinding.inflate(inflater, container, false) + return ui.root } override fun onViewCreated(view: View, savedInstanceState: Bundle?) { - list.apply { + ui.list.apply { adapter = transactionAdapter addItemDecoration(DividerItemDecoration(context, VERTICAL)) } val tracker = SelectionTracker.Builder( "transaction-selection-id", - list, + ui.list, transactionAdapter.keyProvider, - TransactionLookup(list, transactionAdapter), + TransactionLookup(ui.list, transactionAdapter), StorageStrategy.createStringStorage() ).withSelectionPredicate( SelectionPredicates.createSelectAnything() @@ -101,17 +102,17 @@ class TransactionsFragment : Fragment(), OnTransactionClickListener, ActionMode. } }) - transactionManager.progress.observe(viewLifecycleOwner, Observer { show -> - if (show) progressBar.fadeIn() else progressBar.fadeOut() + transactionManager.progress.observe(viewLifecycleOwner, { show -> + if (show) ui.progressBar.fadeIn() else ui.progressBar.fadeOut() }) - transactionManager.transactions.observe(viewLifecycleOwner, Observer { result -> + transactionManager.transactions.observe(viewLifecycleOwner, { result -> onTransactionsResult(result) }) } override fun onActivityCreated(savedInstanceState: Bundle?) { super.onActivityCreated(savedInstanceState) - model.balances.observe(viewLifecycleOwner, Observer { balances -> + model.balances.observe(viewLifecycleOwner, { balances -> balances.find { it.currency == currency }?.available?.let { amount -> requireActivity().title = getString(R.string.transactions_detail_title_balance, amount) @@ -153,35 +154,35 @@ class TransactionsFragment : Fragment(), OnTransactionClickListener, ActionMode. override fun onTransactionClicked(transaction: Transaction) { if (actionMode != null) return // don't react on clicks while in action mode - if (transaction.detailPageLayout != 0) { + if (transaction.detailPageNav != 0) { transactionManager.selectedTransaction = transaction - findNavController().navigate(R.id.action_nav_transaction_detail) + findNavController().navigate(transaction.detailPageNav) } } private fun onTransactionsResult(result: TransactionsResult) = when (result) { is TransactionsResult.Error -> { - list.fadeOut() - emptyState.text = getString(R.string.transactions_error, result.msg) - emptyState.fadeIn() + ui.list.fadeOut() + ui.emptyState.text = getString(R.string.transactions_error, result.msg) + ui.emptyState.fadeIn() } is TransactionsResult.Success -> { if (result.transactions.isEmpty()) { val isSearch = transactionManager.searchQuery.value != null - emptyState.setText(if (isSearch) R.string.transactions_empty_search else R.string.transactions_empty) - emptyState.fadeIn() - list.fadeOut() + ui.emptyState.setText(if (isSearch) R.string.transactions_empty_search else R.string.transactions_empty) + ui.emptyState.fadeIn() + ui.list.fadeOut() } else { - emptyState.fadeOut() + ui.emptyState.fadeOut() transactionAdapter.update(result.transactions) - list.fadeIn() + ui.list.fadeIn() } } } private fun onSearch(query: String) { - list.fadeOut() - progressBar.fadeIn() + ui.list.fadeOut() + ui.progressBar.fadeIn() transactionManager.searchQuery.value = query } |