From ea3250845fb266a2ecd5ebeba561bc99101bf3de Mon Sep 17 00:00:00 2001 From: Torsten Grote Date: Wed, 15 Apr 2020 11:26:51 -0300 Subject: [wallet] rename history to transactions --- .../src/main/java/net/taler/wallet/MainFragment.kt | 4 +- .../main/java/net/taler/wallet/MainViewModel.kt | 4 +- .../net/taler/wallet/history/HistoryAdapter.kt | 199 --------- .../java/net/taler/wallet/history/HistoryEvent.kt | 459 -------------------- .../taler/wallet/history/HistoryEventFragment.kt | 127 ------ .../net/taler/wallet/history/HistoryFragment.kt | 132 ------ .../net/taler/wallet/history/HistoryManager.kt | 79 ---- .../net/taler/wallet/history/JsonDialogFragment.kt | 57 --- .../net/taler/wallet/history/ReserveTransaction.kt | 59 --- .../wallet/transactions/JsonDialogFragment.kt | 57 +++ .../wallet/transactions/ReserveTransaction.kt | 59 +++ .../net/taler/wallet/transactions/Transaction.kt | 459 ++++++++++++++++++++ .../wallet/transactions/TransactionAdapter.kt | 199 +++++++++ .../transactions/TransactionDetailFragment.kt | 127 ++++++ .../wallet/transactions/TransactionManager.kt | 83 ++++ .../wallet/transactions/TransactionsFragment.kt | 113 +++++ .../main/res/drawable/history_payment_aborted.xml | 25 -- wallet/src/main/res/drawable/history_refresh.xml | 28 -- wallet/src/main/res/drawable/history_refund.xml | 25 -- .../src/main/res/drawable/history_tip_accepted.xml | 25 -- .../src/main/res/drawable/history_tip_declined.xml | 25 -- wallet/src/main/res/drawable/history_withdrawn.xml | 25 -- .../res/drawable/transaction_payment_aborted.xml | 25 ++ .../src/main/res/drawable/transaction_refresh.xml | 28 ++ .../src/main/res/drawable/transaction_refund.xml | 25 ++ .../main/res/drawable/transaction_tip_accepted.xml | 25 ++ .../main/res/drawable/transaction_tip_declined.xml | 25 ++ .../main/res/drawable/transaction_withdrawal.xml | 25 ++ wallet/src/main/res/layout/fragment_event_paid.xml | 30 +- .../main/res/layout/fragment_event_withdraw.xml | 12 +- .../src/main/res/layout/fragment_show_history.xml | 46 -- .../src/main/res/layout/fragment_transactions.xml | 46 ++ wallet/src/main/res/layout/history_payment.xml | 87 ---- wallet/src/main/res/layout/history_receive.xml | 92 ---- wallet/src/main/res/layout/history_row.xml | 76 ---- wallet/src/main/res/layout/transaction_in.xml | 92 ++++ wallet/src/main/res/layout/transaction_out.xml | 87 ++++ wallet/src/main/res/layout/transaction_row.xml | 76 ++++ wallet/src/main/res/menu/balance.xml | 19 - wallet/src/main/res/menu/fragment_main.xml | 19 + wallet/src/main/res/menu/history.xml | 30 -- wallet/src/main/res/menu/history_event.xml | 23 - wallet/src/main/res/menu/transactions.xml | 19 + wallet/src/main/res/menu/transactions_detail.xml | 23 + wallet/src/main/res/navigation/nav_graph.xml | 21 +- wallet/src/main/res/values/strings.xml | 74 ++-- wallet/src/main/res/values/styles.xml | 8 +- wallet/src/main/res/xml/settings_main.xml | 2 +- .../net/taler/wallet/history/HistoryEventTest.kt | 470 --------------------- .../taler/wallet/history/ReserveTransactionTest.kt | 52 --- .../wallet/transactions/ReserveTransactionTest.kt | 52 +++ .../taler/wallet/transactions/TransactionTest.kt | 470 +++++++++++++++++++++ 52 files changed, 2207 insertions(+), 2242 deletions(-) delete mode 100644 wallet/src/main/java/net/taler/wallet/history/HistoryAdapter.kt delete mode 100644 wallet/src/main/java/net/taler/wallet/history/HistoryEvent.kt delete mode 100644 wallet/src/main/java/net/taler/wallet/history/HistoryEventFragment.kt delete mode 100644 wallet/src/main/java/net/taler/wallet/history/HistoryFragment.kt delete mode 100644 wallet/src/main/java/net/taler/wallet/history/HistoryManager.kt delete mode 100644 wallet/src/main/java/net/taler/wallet/history/JsonDialogFragment.kt delete mode 100644 wallet/src/main/java/net/taler/wallet/history/ReserveTransaction.kt create mode 100644 wallet/src/main/java/net/taler/wallet/transactions/JsonDialogFragment.kt create mode 100644 wallet/src/main/java/net/taler/wallet/transactions/ReserveTransaction.kt create mode 100644 wallet/src/main/java/net/taler/wallet/transactions/Transaction.kt create mode 100644 wallet/src/main/java/net/taler/wallet/transactions/TransactionAdapter.kt create mode 100644 wallet/src/main/java/net/taler/wallet/transactions/TransactionDetailFragment.kt create mode 100644 wallet/src/main/java/net/taler/wallet/transactions/TransactionManager.kt create mode 100644 wallet/src/main/java/net/taler/wallet/transactions/TransactionsFragment.kt delete mode 100644 wallet/src/main/res/drawable/history_payment_aborted.xml delete mode 100644 wallet/src/main/res/drawable/history_refresh.xml delete mode 100644 wallet/src/main/res/drawable/history_refund.xml delete mode 100644 wallet/src/main/res/drawable/history_tip_accepted.xml delete mode 100644 wallet/src/main/res/drawable/history_tip_declined.xml delete mode 100644 wallet/src/main/res/drawable/history_withdrawn.xml create mode 100644 wallet/src/main/res/drawable/transaction_payment_aborted.xml create mode 100644 wallet/src/main/res/drawable/transaction_refresh.xml create mode 100644 wallet/src/main/res/drawable/transaction_refund.xml create mode 100644 wallet/src/main/res/drawable/transaction_tip_accepted.xml create mode 100644 wallet/src/main/res/drawable/transaction_tip_declined.xml create mode 100644 wallet/src/main/res/drawable/transaction_withdrawal.xml delete mode 100644 wallet/src/main/res/layout/fragment_show_history.xml create mode 100644 wallet/src/main/res/layout/fragment_transactions.xml delete mode 100644 wallet/src/main/res/layout/history_payment.xml delete mode 100644 wallet/src/main/res/layout/history_receive.xml delete mode 100644 wallet/src/main/res/layout/history_row.xml create mode 100644 wallet/src/main/res/layout/transaction_in.xml create mode 100644 wallet/src/main/res/layout/transaction_out.xml create mode 100644 wallet/src/main/res/layout/transaction_row.xml delete mode 100644 wallet/src/main/res/menu/balance.xml create mode 100644 wallet/src/main/res/menu/fragment_main.xml delete mode 100644 wallet/src/main/res/menu/history.xml delete mode 100644 wallet/src/main/res/menu/history_event.xml create mode 100644 wallet/src/main/res/menu/transactions.xml create mode 100644 wallet/src/main/res/menu/transactions_detail.xml delete mode 100644 wallet/src/test/java/net/taler/wallet/history/HistoryEventTest.kt delete mode 100644 wallet/src/test/java/net/taler/wallet/history/ReserveTransactionTest.kt create mode 100644 wallet/src/test/java/net/taler/wallet/transactions/ReserveTransactionTest.kt create mode 100644 wallet/src/test/java/net/taler/wallet/transactions/TransactionTest.kt (limited to 'wallet') diff --git a/wallet/src/main/java/net/taler/wallet/MainFragment.kt b/wallet/src/main/java/net/taler/wallet/MainFragment.kt index f587aca..2145596 100644 --- a/wallet/src/main/java/net/taler/wallet/MainFragment.kt +++ b/wallet/src/main/java/net/taler/wallet/MainFragment.kt @@ -88,7 +88,7 @@ class MainFragment : Fragment(), BalanceClickListener { } override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) { - inflater.inflate(R.menu.balance, menu) + inflater.inflate(R.menu.fragment_main, menu) super.onCreateOptionsMenu(menu, inflater) } @@ -117,7 +117,7 @@ class MainFragment : Fragment(), BalanceClickListener { } override fun onBalanceClick(currency: String) { - findNavController().navigate(R.id.walletHistory) + findNavController().navigate(R.id.nav_transactions) } } diff --git a/wallet/src/main/java/net/taler/wallet/MainViewModel.kt b/wallet/src/main/java/net/taler/wallet/MainViewModel.kt index e5f385a..c1662c8 100644 --- a/wallet/src/main/java/net/taler/wallet/MainViewModel.kt +++ b/wallet/src/main/java/net/taler/wallet/MainViewModel.kt @@ -28,7 +28,7 @@ import com.fasterxml.jackson.databind.ObjectMapper import com.fasterxml.jackson.module.kotlin.KotlinModule import net.taler.common.Amount import net.taler.wallet.backend.WalletBackendApi -import net.taler.wallet.history.HistoryManager +import net.taler.wallet.transactions.TransactionManager import net.taler.wallet.payment.PaymentManager import net.taler.wallet.pending.PendingOperationsManager import net.taler.wallet.refund.RefundManager @@ -66,7 +66,7 @@ class MainViewModel(val app: Application) : AndroidViewModel(app) { val withdrawManager = WithdrawManager(walletBackendApi) val paymentManager = PaymentManager(walletBackendApi, mapper) val pendingOperationsManager = PendingOperationsManager(walletBackendApi) - val historyManager = HistoryManager(walletBackendApi, mapper) + val transactionManager = TransactionManager(walletBackendApi, mapper) val refundManager = RefundManager(walletBackendApi) override fun onCleared() { diff --git a/wallet/src/main/java/net/taler/wallet/history/HistoryAdapter.kt b/wallet/src/main/java/net/taler/wallet/history/HistoryAdapter.kt deleted file mode 100644 index b9770fb..0000000 --- a/wallet/src/main/java/net/taler/wallet/history/HistoryAdapter.kt +++ /dev/null @@ -1,199 +0,0 @@ -/* - * 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 - */ - -package net.taler.wallet.history - -import android.content.Context -import android.graphics.Paint.STRIKE_THRU_TEXT_FLAG -import android.view.LayoutInflater -import android.view.View -import android.view.ViewGroup -import android.widget.ImageView -import android.widget.TextView -import androidx.annotation.CallSuper -import androidx.recyclerview.widget.RecyclerView.Adapter -import androidx.recyclerview.widget.RecyclerView.ViewHolder -import net.taler.common.toRelativeTime -import net.taler.wallet.R -import net.taler.wallet.cleanExchange -import net.taler.wallet.history.HistoryAdapter.HistoryEventViewHolder - - -internal class HistoryAdapter( - private val devMode: Boolean, - private val listener: OnEventClickListener, - private var history: History = History() -) : Adapter() { - - init { - setHasStableIds(false) - } - - override fun getItemViewType(position: Int): Int = history[position].layout - - override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): HistoryEventViewHolder { - val view = LayoutInflater.from(parent.context).inflate(viewType, parent, false) - return when (viewType) { - R.layout.history_receive -> HistoryReceiveViewHolder(view) - R.layout.history_payment -> HistoryPaymentViewHolder(view) - else -> GenericHistoryEventViewHolder(view) - } - } - - override fun getItemCount(): Int = history.size - - override fun onBindViewHolder(holder: HistoryEventViewHolder, position: Int) { - val event = history[position] - holder.bind(event) - } - - fun update(updatedHistory: History) { - this.history = updatedHistory - this.notifyDataSetChanged() - } - - internal abstract inner class HistoryEventViewHolder(private val v: View) : ViewHolder(v) { - - protected val context: Context = v.context - private val icon: ImageView = v.findViewById(R.id.icon) - protected val title: TextView = v.findViewById(R.id.title) - private val time: TextView = v.findViewById(R.id.time) - private val selectableBackground = v.background - - @CallSuper - open fun bind(event: HistoryEvent) { - if (devMode || event.detailPageLayout != 0) { - v.background = selectableBackground - v.setOnClickListener { listener.onEventClicked(event) } - } else { - v.background = null - v.setOnClickListener(null) - } - icon.setImageResource(event.icon) - if (event.title == 0) title.text = event::class.java.simpleName - else title.setText(event.title) - time.text = event.timestamp.ms.toRelativeTime(context) - } - - } - - internal inner class GenericHistoryEventViewHolder(v: View) : HistoryEventViewHolder(v) { - - private val info: TextView = v.findViewById(R.id.info) - - override fun bind(event: HistoryEvent) { - super.bind(event) - info.text = when (event) { - is ExchangeAddedEvent -> cleanExchange(event.exchangeBaseUrl) - is ExchangeUpdatedEvent -> cleanExchange(event.exchangeBaseUrl) - is ReserveBalanceUpdatedEvent -> event.reserveBalance.toString() - is HistoryPaymentSentEvent -> event.orderShortInfo.summary - is HistoryOrderAcceptedEvent -> event.orderShortInfo.summary - is HistoryOrderRefusedEvent -> event.orderShortInfo.summary - is HistoryOrderRedirectedEvent -> event.newOrderShortInfo.summary - else -> "" - } - } - - } - - internal inner class HistoryReceiveViewHolder(v: View) : HistoryEventViewHolder(v) { - - private val summary: TextView = v.findViewById(R.id.summary) - private val amountWithdrawn: TextView = v.findViewById(R.id.amountWithdrawn) - private val paintFlags = amountWithdrawn.paintFlags - - override fun bind(event: HistoryEvent) { - super.bind(event) - when (event) { - is HistoryWithdrawnEvent -> bind(event) - is HistoryRefundedEvent -> bind(event) - is HistoryTipAcceptedEvent -> bind(event) - is HistoryTipDeclinedEvent -> bind(event) - } - } - - private fun bind(event: HistoryWithdrawnEvent) { - summary.text = cleanExchange(event.exchangeBaseUrl) - amountWithdrawn.text = - context.getString(R.string.amount_positive, event.amountWithdrawnEffective) - amountWithdrawn.paintFlags = paintFlags - } - - private fun bind(event: HistoryRefundedEvent) { - summary.text = event.orderShortInfo.summary - amountWithdrawn.text = - context.getString(R.string.amount_positive, event.amountRefundedEffective) - amountWithdrawn.paintFlags = paintFlags - } - - private fun bind(event: HistoryTipAcceptedEvent) { - summary.text = null - amountWithdrawn.text = context.getString(R.string.amount_positive, event.tipRaw) - amountWithdrawn.paintFlags = paintFlags - } - - private fun bind(event: HistoryTipDeclinedEvent) { - summary.text = null - amountWithdrawn.text = context.getString(R.string.amount_positive, event.tipAmount) - amountWithdrawn.paintFlags = amountWithdrawn.paintFlags or STRIKE_THRU_TEXT_FLAG - } - - } - - internal inner class HistoryPaymentViewHolder(v: View) : HistoryEventViewHolder(v) { - - private val summary: TextView = v.findViewById(R.id.summary) - private val amountPaidWithFees: TextView = v.findViewById(R.id.amountPaidWithFees) - - override fun bind(event: HistoryEvent) { - super.bind(event) - when (event) { - is HistoryPaymentSentEvent -> bind(event) - is HistoryPaymentAbortedEvent -> bind(event) - is HistoryRefreshedEvent -> bind(event) - } - } - - private fun bind(event: HistoryPaymentSentEvent) { - summary.text = event.orderShortInfo.summary - amountPaidWithFees.text = - context.getString(R.string.amount_negative, event.amountPaidWithFees) - } - - private fun bind(event: HistoryPaymentAbortedEvent) { - summary.text = event.orderShortInfo.summary - amountPaidWithFees.text = context.getString(R.string.amount_negative, event.amountLost) - } - - private fun bind(event: HistoryRefreshedEvent) { - val res = when (event.refreshReason) { - RefreshReason.MANUAL -> R.string.history_event_refresh_reason_manual - RefreshReason.PAY -> R.string.history_event_refresh_reason_pay - RefreshReason.REFUND -> R.string.history_event_refresh_reason_refund - RefreshReason.ABORT_PAY -> R.string.history_event_refresh_reason_abort_pay - RefreshReason.RECOUP -> R.string.history_event_refresh_reason_recoup - RefreshReason.BACKUP_RESTORED -> R.string.history_event_refresh_reason_backup_restored - } - summary.text = context.getString(res) - val fee = event.amountRefreshedRaw - event.amountRefreshedEffective - if (fee.isZero()) amountPaidWithFees.text = null - else amountPaidWithFees.text = context.getString(R.string.amount_negative, fee) - } - - } - -} diff --git a/wallet/src/main/java/net/taler/wallet/history/HistoryEvent.kt b/wallet/src/main/java/net/taler/wallet/history/HistoryEvent.kt deleted file mode 100644 index af017ed..0000000 --- a/wallet/src/main/java/net/taler/wallet/history/HistoryEvent.kt +++ /dev/null @@ -1,459 +0,0 @@ -/* - * 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 - */ - -package net.taler.wallet.history - -import androidx.annotation.DrawableRes -import androidx.annotation.LayoutRes -import androidx.annotation.StringRes -import com.fasterxml.jackson.annotation.JsonIgnoreProperties -import com.fasterxml.jackson.annotation.JsonInclude -import com.fasterxml.jackson.annotation.JsonInclude.Include.NON_EMPTY -import com.fasterxml.jackson.annotation.JsonProperty -import com.fasterxml.jackson.annotation.JsonSubTypes -import com.fasterxml.jackson.annotation.JsonSubTypes.Type -import com.fasterxml.jackson.annotation.JsonTypeInfo -import com.fasterxml.jackson.annotation.JsonTypeInfo.As.PROPERTY -import com.fasterxml.jackson.annotation.JsonTypeInfo.Id.NAME -import com.fasterxml.jackson.annotation.JsonTypeName -import net.taler.common.Amount -import net.taler.common.Timestamp -import net.taler.wallet.R -import org.json.JSONObject - -enum class ReserveType { - /** - * Manually created. - */ - @JsonProperty("manual") - MANUAL, - - /** - * Withdrawn from a bank that has "tight" Taler integration - */ - @JsonProperty("taler-bank-withdraw") - @Suppress("unused") - TALER_BANK_WITHDRAW, -} - -@JsonInclude(NON_EMPTY) -class ReserveCreationDetail(val type: ReserveType, val bankUrl: String?) - -enum class RefreshReason { - @JsonProperty("manual") - @Suppress("unused") - MANUAL, - - @JsonProperty("pay") - PAY, - - @JsonProperty("refund") - @Suppress("unused") - REFUND, - - @JsonProperty("abort-pay") - @Suppress("unused") - ABORT_PAY, - - @JsonProperty("recoup") - @Suppress("unused") - RECOUP, - - @JsonProperty("backup-restored") - @Suppress("unused") - BACKUP_RESTORED -} - -@JsonInclude(NON_EMPTY) -class ReserveShortInfo( - /** - * The exchange that the reserve will be at. - */ - val exchangeBaseUrl: String, - /** - * Key to query more details - */ - val reservePub: String, - /** - * Detail about how the reserve has been created. - */ - val reserveCreationDetail: ReserveCreationDetail -) - -typealias History = ArrayList - -@JsonTypeInfo( - use = NAME, - include = PROPERTY, - property = "type", - defaultImpl = HistoryUnknownEvent::class -) -/** missing: -AuditorComplaintSent = "auditor-complained-sent", -AuditorComplaintProcessed = "auditor-complaint-processed", -AuditorTrustAdded = "auditor-trust-added", -AuditorTrustRemoved = "auditor-trust-removed", -ExchangeTermsAccepted = "exchange-terms-accepted", -ExchangePolicyChanged = "exchange-policy-changed", -ExchangeTrustAdded = "exchange-trust-added", -ExchangeTrustRemoved = "exchange-trust-removed", -FundsDepositedToSelf = "funds-deposited-to-self", -FundsRecouped = "funds-recouped", -ReserveCreated = "reserve-created", - */ -@JsonSubTypes( - Type(value = ExchangeAddedEvent::class, name = "exchange-added"), - Type(value = ExchangeUpdatedEvent::class, name = "exchange-updated"), - Type(value = ReserveBalanceUpdatedEvent::class, name = "reserve-balance-updated"), - Type(value = HistoryWithdrawnEvent::class, name = "withdrawn"), - Type(value = HistoryOrderAcceptedEvent::class, name = "order-accepted"), - Type(value = HistoryOrderRefusedEvent::class, name = "order-refused"), - Type(value = HistoryOrderRedirectedEvent::class, name = "order-redirected"), - Type(value = HistoryPaymentSentEvent::class, name = "payment-sent"), - Type(value = HistoryPaymentAbortedEvent::class, name = "payment-aborted"), - Type(value = HistoryTipAcceptedEvent::class, name = "tip-accepted"), - Type(value = HistoryTipDeclinedEvent::class, name = "tip-declined"), - Type(value = HistoryRefundedEvent::class, name = "refund"), - Type(value = HistoryRefreshedEvent::class, name = "refreshed") -) -@JsonIgnoreProperties( - value = [ - "eventId" - ] -) -abstract class HistoryEvent( - val timestamp: Timestamp, - @get:LayoutRes - open val layout: Int = R.layout.history_row, - @get:LayoutRes - open val detailPageLayout: Int = 0, - @get:StringRes - open val title: Int = 0, - @get:DrawableRes - open val icon: Int = R.drawable.ic_account_balance, - open val showToUser: Boolean = false -) { - open lateinit var json: JSONObject -} - - -class HistoryUnknownEvent(timestamp: Timestamp) : HistoryEvent(timestamp) { - override val title = R.string.history_event_unknown -} - -@JsonTypeName("exchange-added") -class ExchangeAddedEvent( - timestamp: Timestamp, - val exchangeBaseUrl: String, - val builtIn: Boolean -) : HistoryEvent(timestamp) { - override val title = R.string.history_event_exchange_added -} - -@JsonTypeName("exchange-updated") -class ExchangeUpdatedEvent( - timestamp: Timestamp, - val exchangeBaseUrl: String -) : HistoryEvent(timestamp) { - override val title = R.string.history_event_exchange_updated -} - - -@JsonTypeName("reserve-balance-updated") -class ReserveBalanceUpdatedEvent( - timestamp: Timestamp, - /** - * Condensed information about the reserve. - */ - val reserveShortInfo: ReserveShortInfo, - /** - * Amount currently left in the reserve. - */ - val reserveBalance: Amount, - /** - * Amount we expected to be in the reserve at that time, - * considering ongoing withdrawals from that reserve. - */ - val reserveAwaitedAmount: Amount, - /** - * Amount that hasn't been withdrawn yet. - */ - val reserveUnclaimedAmount: Amount -) : HistoryEvent(timestamp) { - override val title = R.string.history_event_reserve_balance_updated -} - -@JsonTypeName("withdrawn") -class HistoryWithdrawnEvent( - timestamp: Timestamp, - /** - * Exchange that was withdrawn from. - */ - val exchangeBaseUrl: String, - /** - * Unique identifier for the withdrawal session, can be used to - * query more detailed information from the wallet. - */ - val withdrawalGroupId: String, - val withdrawalSource: WithdrawalSource, - /** - * Amount that has been subtracted from the reserve's balance - * for this withdrawal. - */ - val amountWithdrawnRaw: Amount, - /** - * Amount that actually was added to the wallet's balance. - */ - val amountWithdrawnEffective: Amount -) : HistoryEvent(timestamp) { - override val layout = R.layout.history_receive - override val detailPageLayout = R.layout.fragment_event_withdraw - override val title = R.string.history_event_withdrawn - override val icon = R.drawable.history_withdrawn - override val showToUser = true -} - -@JsonTypeName("order-accepted") -class HistoryOrderAcceptedEvent( - timestamp: Timestamp, - /** - * Condensed info about the order. - */ - val orderShortInfo: OrderShortInfo -) : HistoryEvent(timestamp) { - override val icon = R.drawable.ic_add_circle - override val title = R.string.history_event_order_accepted -} - -@JsonTypeName("order-refused") -class HistoryOrderRefusedEvent( - timestamp: Timestamp, - /** - * Condensed info about the order. - */ - val orderShortInfo: OrderShortInfo -) : HistoryEvent(timestamp) { - override val icon = R.drawable.ic_cancel - override val title = R.string.history_event_order_refused -} - -@JsonTypeName("payment-sent") -class HistoryPaymentSentEvent( - timestamp: Timestamp, - /** - * Condensed info about the order that we already paid for. - */ - val orderShortInfo: OrderShortInfo, - /** - * Set to true if the payment has been previously sent - * to the merchant successfully, possibly with a different session ID. - */ - val replay: Boolean, - /** - * Number of coins that were involved in the payment. - */ - val numCoins: Int, - /** - * Amount that was paid, including deposit and wire fees. - */ - val amountPaidWithFees: Amount, - /** - * Session ID that the payment was (re-)submitted under. - */ - val sessionId: String? -) : HistoryEvent(timestamp) { - override val layout = R.layout.history_payment - override val detailPageLayout = R.layout.fragment_event_paid - override val title = R.string.history_event_payment_sent - override val icon = R.drawable.ic_cash_usd_outline - override val showToUser = true -} - -@JsonTypeName("payment-aborted") -class HistoryPaymentAbortedEvent( - timestamp: Timestamp, - /** - * Condensed info about the order that we already paid for. - */ - val orderShortInfo: OrderShortInfo, - /** - * Amount that was lost due to refund and refreshing fees. - */ - val amountLost: Amount -) : HistoryEvent(timestamp) { - override val layout = R.layout.history_payment - override val title = R.string.history_event_payment_aborted - override val icon = R.drawable.history_payment_aborted - override val showToUser = true -} - -@JsonTypeName("refreshed") -class HistoryRefreshedEvent( - timestamp: Timestamp, - /** - * Amount that is now available again because it has - * been refreshed. - */ - val amountRefreshedEffective: Amount, - /** - * Amount that we spent for refreshing. - */ - val amountRefreshedRaw: Amount, - /** - * Why was the refreshing done? - */ - val refreshReason: RefreshReason, - val numInputCoins: Int, - val numRefreshedInputCoins: Int, - val numOutputCoins: Int, - /** - * Identifier for a refresh group, contains one or - * more refresh session IDs. - */ - val refreshGroupId: String -) : HistoryEvent(timestamp) { - override val layout = R.layout.history_payment - override val icon = R.drawable.history_refresh - override val title = R.string.history_event_refreshed - override val showToUser = !(amountRefreshedRaw - amountRefreshedEffective).isZero() -} - -@JsonTypeName("order-redirected") -class HistoryOrderRedirectedEvent( - timestamp: Timestamp, - /** - * Condensed info about the new order that contains a - * product (identified by the fulfillment URL) that we've already paid for. - */ - val newOrderShortInfo: OrderShortInfo, - /** - * Condensed info about the order that we already paid for. - */ - val alreadyPaidOrderShortInfo: OrderShortInfo -) : HistoryEvent(timestamp) { - override val icon = R.drawable.ic_directions - override val title = R.string.history_event_order_redirected -} - -@JsonTypeName("tip-accepted") -class HistoryTipAcceptedEvent( - timestamp: Timestamp, - /** - * Unique identifier for the tip to query more information. - */ - val tipId: String, - /** - * Raw amount of the tip, without extra fees that apply. - */ - val tipRaw: Amount -) : HistoryEvent(timestamp) { - override val icon = R.drawable.history_tip_accepted - override val title = R.string.history_event_tip_accepted - override val layout = R.layout.history_receive - override val showToUser = true -} - -@JsonTypeName("tip-declined") -class HistoryTipDeclinedEvent( - timestamp: Timestamp, - /** - * Unique identifier for the tip to query more information. - */ - val tipId: String, - /** - * Raw amount of the tip, without extra fees that apply. - */ - val tipAmount: Amount -) : HistoryEvent(timestamp) { - override val icon = R.drawable.history_tip_declined - override val title = R.string.history_event_tip_declined - override val layout = R.layout.history_receive - override val showToUser = true -} - -@JsonTypeName("refund") -class HistoryRefundedEvent( - timestamp: Timestamp, - val orderShortInfo: OrderShortInfo, - /** - * Unique identifier for this refund. - * (Identifies multiple refund permissions that were obtained at once.) - */ - val refundGroupId: String, - /** - * Part of the refund that couldn't be applied because - * the refund permissions were expired. - */ - val amountRefundedInvalid: Amount, - /** - * Amount that has been refunded by the merchant. - */ - val amountRefundedRaw: Amount, - /** - * Amount will be added to the wallet's balance after fees and refreshing. - */ - val amountRefundedEffective: Amount -) : HistoryEvent(timestamp) { - override val icon = R.drawable.history_refund - override val title = R.string.history_event_refund - override val layout = R.layout.history_receive - override val detailPageLayout = R.layout.fragment_event_paid - override val showToUser = true -} - -@JsonTypeInfo( - use = NAME, - include = PROPERTY, - property = "type" -) -@JsonSubTypes( - Type(value = WithdrawalSourceReserve::class, name = "reserve") -) -abstract class WithdrawalSource - -@Suppress("unused") -@JsonTypeName("tip") -class WithdrawalSourceTip( - val tipId: String -) : WithdrawalSource() - -@JsonTypeName("reserve") -class WithdrawalSourceReserve( - val reservePub: String -) : WithdrawalSource() - -data class OrderShortInfo( - /** - * Wallet-internal identifier of the proposal. - */ - val proposalId: String, - /** - * Order ID, uniquely identifies the order within a merchant instance. - */ - val orderId: String, - /** - * Base URL of the merchant. - */ - val merchantBaseUrl: String, - /** - * Amount that must be paid for the contract. - */ - val amount: Amount, - /** - * Summary of the proposal, given by the merchant. - */ - val summary: String -) diff --git a/wallet/src/main/java/net/taler/wallet/history/HistoryEventFragment.kt b/wallet/src/main/java/net/taler/wallet/history/HistoryEventFragment.kt deleted file mode 100644 index b6a2a33..0000000 --- a/wallet/src/main/java/net/taler/wallet/history/HistoryEventFragment.kt +++ /dev/null @@ -1,127 +0,0 @@ -/* - * 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 - */ - -package net.taler.wallet.history - -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.ViewGroup -import android.widget.Toast -import android.widget.Toast.LENGTH_LONG -import androidx.core.content.ContextCompat.getColor -import androidx.fragment.app.Fragment -import androidx.fragment.app.activityViewModels -import kotlinx.android.synthetic.main.fragment_event_paid.* -import kotlinx.android.synthetic.main.fragment_event_withdraw.* -import kotlinx.android.synthetic.main.fragment_event_withdraw.feeView -import kotlinx.android.synthetic.main.fragment_event_withdraw.timeView -import net.taler.common.Amount -import net.taler.common.toAbsoluteTime -import net.taler.wallet.R -import net.taler.wallet.MainViewModel -import net.taler.wallet.cleanExchange - -class HistoryEventFragment : Fragment() { - - private val model: MainViewModel by activityViewModels() - private val historyManager by lazy { model.historyManager } - private val event by lazy { requireNotNull(historyManager.selectedEvent) } - - 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(event.detailPageLayout, container, false) - } - - override fun onActivityCreated(savedInstanceState: Bundle?) { - super.onActivityCreated(savedInstanceState) - requireActivity().title = - getString(if (event.title != 0) event.title else R.string.history_detail_title) - } - - override fun onViewCreated(view: View, savedInstanceState: Bundle?) { - timeView.text = event.timestamp.ms.toAbsoluteTime(requireContext()) - when (val e = event) { - is HistoryWithdrawnEvent -> bind(e) - is HistoryPaymentSentEvent -> bind(e) - is HistoryRefundedEvent -> bind(e) - else -> Toast.makeText( - requireContext(), - "event ${e.javaClass} not implement", - LENGTH_LONG - ).show() - } - } - - override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) { - inflater.inflate(R.menu.history_event, menu) - } - - override fun onOptionsItemSelected(item: MenuItem): Boolean { - return when (item.itemId) { - R.id.show_json -> { - JsonDialogFragment.new(event.json.toString(2)).show(parentFragmentManager, null) - true - } - else -> super.onOptionsItemSelected(item) - } - } - - private fun bind(event: HistoryWithdrawnEvent) { - effectiveAmountLabel.text = getString(R.string.withdraw_total) - effectiveAmountView.text = event.amountWithdrawnEffective.toString() - chosenAmountLabel.text = getString(R.string.amount_chosen) - chosenAmountView.text = - getString(R.string.amount_positive, event.amountWithdrawnRaw.toString()) - val fee = event.amountWithdrawnRaw - event.amountWithdrawnEffective - feeView.text = getString(R.string.amount_negative, fee.toString()) - exchangeView.text = cleanExchange(event.exchangeBaseUrl) - } - - private fun bind(event: HistoryPaymentSentEvent) { - amountPaidWithFeesView.text = event.amountPaidWithFees.toString() - val fee = event.amountPaidWithFees - event.orderShortInfo.amount - bindOrderAndFee(event.orderShortInfo, fee) - } - - private fun bind(event: HistoryRefundedEvent) { - amountPaidWithFeesLabel.text = getString(R.string.history_event_refund) - amountPaidWithFeesView.setTextColor(getColor(requireContext(), R.color.green)) - amountPaidWithFeesView.text = - getString(R.string.amount_positive, event.amountRefundedEffective.toString()) - val fee = event.orderShortInfo.amount - event.amountRefundedEffective - bindOrderAndFee(event.orderShortInfo, fee) - } - - private fun bindOrderAndFee(orderShortInfo: OrderShortInfo, fee: Amount) { - orderAmountView.text = orderShortInfo.amount.toString() - feeView.text = getString(R.string.amount_negative, fee.toString()) - orderSummaryView.text = orderShortInfo.summary - orderIdView.text = - getString(R.string.history_event_payment_sent_order_id, orderShortInfo.orderId) - } - -} diff --git a/wallet/src/main/java/net/taler/wallet/history/HistoryFragment.kt b/wallet/src/main/java/net/taler/wallet/history/HistoryFragment.kt deleted file mode 100644 index 9f83d5a..0000000 --- a/wallet/src/main/java/net/taler/wallet/history/HistoryFragment.kt +++ /dev/null @@ -1,132 +0,0 @@ -/* - * 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 - */ - -package net.taler.wallet.history - -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.INVISIBLE -import android.view.View.VISIBLE -import android.view.ViewGroup -import androidx.fragment.app.Fragment -import androidx.fragment.app.activityViewModels -import androidx.lifecycle.Observer -import androidx.navigation.fragment.findNavController -import androidx.recyclerview.widget.DividerItemDecoration -import androidx.recyclerview.widget.LinearLayoutManager -import androidx.recyclerview.widget.LinearLayoutManager.VERTICAL -import kotlinx.android.synthetic.main.fragment_show_history.* -import net.taler.common.fadeIn -import net.taler.common.fadeOut -import net.taler.wallet.R -import net.taler.wallet.MainViewModel - -interface OnEventClickListener { - fun onEventClicked(event: HistoryEvent) -} - -class HistoryFragment : Fragment(), OnEventClickListener { - - private val model: MainViewModel by activityViewModels() - private val historyManager by lazy { model.historyManager } - private lateinit var showAllItem: MenuItem - private var reloadHistoryItem: MenuItem? = null - private val historyAdapter by lazy { HistoryAdapter(model.devMode.value == true, this) } - - override fun onCreate(savedInstanceState: Bundle?) { - super.onCreate(savedInstanceState) - setHasOptionsMenu(true) - } - - override fun onCreateView( - inflater: LayoutInflater, container: ViewGroup?, - savedInstanceState: Bundle? - ): View? { - return inflater.inflate(R.layout.fragment_show_history, container, false) - } - - override fun onViewCreated(view: View, savedInstanceState: Bundle?) { - historyList.apply { - layoutManager = LinearLayoutManager(context) - adapter = historyAdapter - addItemDecoration(DividerItemDecoration(context, VERTICAL)) - } - - model.devMode.observe(viewLifecycleOwner, Observer { enabled -> - reloadHistoryItem?.isVisible = enabled - }) - historyManager.progress.observe(viewLifecycleOwner, Observer { show -> - historyProgressBar.visibility = if (show) VISIBLE else INVISIBLE - }) - historyManager.history.observe(viewLifecycleOwner, Observer { history -> - onHistoryResult(history) - }) - - // kicks off initial load, needs to be adapted if showAll state is ever saved - if (savedInstanceState == null) historyManager.showAll.value = model.devMode.value - } - - override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) { - inflater.inflate(R.menu.history, menu) - showAllItem = menu.findItem(R.id.show_all_history) - showAllItem.isChecked = historyManager.showAll.value == true - reloadHistoryItem = menu.findItem(R.id.reload_history).apply { - isVisible = model.devMode.value!! - } - } - - override fun onOptionsItemSelected(item: MenuItem): Boolean { - return when (item.itemId) { - R.id.show_all_history -> { - item.isChecked = !item.isChecked - historyManager.showAll.value = item.isChecked - true - } - R.id.reload_history -> { - historyManager.showAll.value = showAllItem.isChecked - true - } - else -> super.onOptionsItemSelected(item) - } - } - - override fun onEventClicked(event: HistoryEvent) { - if (event.detailPageLayout != 0) { - historyManager.selectedEvent = event - findNavController().navigate(R.id.action_walletHistory_to_historyEventFragment) - } else if (model.devMode.value == true) { - JsonDialogFragment.new(event.json.toString(2)) - .show(parentFragmentManager, null) - } - } - - private fun onHistoryResult(result: HistoryResult) = when (result) { - HistoryResult.Error -> { - historyList.fadeOut() - historyEmptyState.text = getString(R.string.history_error) - historyEmptyState.fadeIn() - } - is HistoryResult.Success -> { - historyEmptyState.visibility = if (result.history.isEmpty()) VISIBLE else INVISIBLE - historyAdapter.update(result.history) - } - } - -} diff --git a/wallet/src/main/java/net/taler/wallet/history/HistoryManager.kt b/wallet/src/main/java/net/taler/wallet/history/HistoryManager.kt deleted file mode 100644 index 7ce4f5b..0000000 --- a/wallet/src/main/java/net/taler/wallet/history/HistoryManager.kt +++ /dev/null @@ -1,79 +0,0 @@ -/* - * 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 - */ - -package net.taler.wallet.history - -import androidx.lifecycle.LiveData -import androidx.lifecycle.MutableLiveData -import androidx.lifecycle.asLiveData -import androidx.lifecycle.switchMap -import com.fasterxml.jackson.databind.ObjectMapper -import com.fasterxml.jackson.module.kotlin.readValue -import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.channels.awaitClose -import kotlinx.coroutines.flow.callbackFlow -import kotlinx.coroutines.flow.onCompletion -import kotlinx.coroutines.flow.onStart -import net.taler.wallet.backend.WalletBackendApi - -sealed class HistoryResult { - object Error : HistoryResult() - class Success(val history: History) : HistoryResult() -} - -@Suppress("EXPERIMENTAL_API_USAGE") -class HistoryManager( - private val walletBackendApi: WalletBackendApi, - private val mapper: ObjectMapper -) { - - private val mProgress = MutableLiveData() - val progress: LiveData = mProgress - - val showAll = MutableLiveData() - - var selectedEvent: HistoryEvent? = null - - val history: LiveData = showAll.switchMap { showAll -> - loadHistory(showAll) - .onStart { mProgress.postValue(true) } - .onCompletion { mProgress.postValue(false) } - .asLiveData(Dispatchers.IO) - } - - private fun loadHistory(showAll: Boolean) = callbackFlow { - walletBackendApi.sendRequest("getHistory", null) { isError, result -> - if (isError) { - offer(HistoryResult.Error) - close() - return@sendRequest - } - val history = History() - val json = result.getJSONArray("history") - for (i in 0 until json.length()) { - val event: HistoryEvent = mapper.readValue(json.getString(i)) - event.json = json.getJSONObject(i) - history.add(event) - } - history.reverse() // show latest first - val filtered = if (showAll) history else history.filter { it.showToUser } as History - offer(HistoryResult.Success(filtered)) - close() - } - awaitClose() - } - -} diff --git a/wallet/src/main/java/net/taler/wallet/history/JsonDialogFragment.kt b/wallet/src/main/java/net/taler/wallet/history/JsonDialogFragment.kt deleted file mode 100644 index 5421db3..0000000 --- a/wallet/src/main/java/net/taler/wallet/history/JsonDialogFragment.kt +++ /dev/null @@ -1,57 +0,0 @@ -/* - * 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 - */ - -package net.taler.wallet.history - -import android.os.Bundle -import android.view.LayoutInflater -import android.view.View -import android.view.ViewGroup -import android.view.ViewGroup.LayoutParams.MATCH_PARENT -import android.view.ViewGroup.LayoutParams.WRAP_CONTENT -import androidx.fragment.app.DialogFragment -import kotlinx.android.synthetic.main.fragment_json.* -import net.taler.wallet.R - -class JsonDialogFragment : DialogFragment() { - - companion object { - fun new(json: String): JsonDialogFragment { - return JsonDialogFragment().apply { - arguments = Bundle().apply { putString("json", json) } - } - } - } - - override fun onCreateView( - inflater: LayoutInflater, - container: ViewGroup?, - savedInstanceState: Bundle? - ): View? { - return inflater.inflate(R.layout.fragment_json, container, false) - } - - override fun onViewCreated(view: View, savedInstanceState: Bundle?) { - val json = arguments!!.getString("json") - jsonView.text = json - } - - override fun onStart() { - super.onStart() - dialog?.window?.setLayout(MATCH_PARENT, WRAP_CONTENT) - } - -} diff --git a/wallet/src/main/java/net/taler/wallet/history/ReserveTransaction.kt b/wallet/src/main/java/net/taler/wallet/history/ReserveTransaction.kt deleted file mode 100644 index 6c8fdaa..0000000 --- a/wallet/src/main/java/net/taler/wallet/history/ReserveTransaction.kt +++ /dev/null @@ -1,59 +0,0 @@ -/* - * 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 - */ - -package net.taler.wallet.history - -import com.fasterxml.jackson.annotation.JsonProperty -import com.fasterxml.jackson.annotation.JsonSubTypes -import com.fasterxml.jackson.annotation.JsonTypeInfo -import com.fasterxml.jackson.annotation.JsonTypeInfo.As.PROPERTY -import com.fasterxml.jackson.annotation.JsonTypeInfo.Id.NAME -import com.fasterxml.jackson.annotation.JsonTypeName -import net.taler.common.Timestamp - - -@JsonTypeInfo( - use = NAME, - include = PROPERTY, - property = "type" -) -@JsonSubTypes( - JsonSubTypes.Type(value = ReserveDepositTransaction::class, name = "DEPOSIT") -) -abstract class ReserveTransaction - - -@JsonTypeName("DEPOSIT") -class ReserveDepositTransaction( - /** - * Amount withdrawn. - */ - val amount: String, - /** - * Sender account payto://-URL - */ - @JsonProperty("sender_account_url") - val senderAccountUrl: String, - /** - * Transfer details uniquely identifying the transfer. - */ - @JsonProperty("wire_reference") - val wireReference: String, - /** - * Timestamp of the incoming wire transfer. - */ - val timestamp: Timestamp -) : ReserveTransaction() diff --git a/wallet/src/main/java/net/taler/wallet/transactions/JsonDialogFragment.kt b/wallet/src/main/java/net/taler/wallet/transactions/JsonDialogFragment.kt new file mode 100644 index 0000000..4139da8 --- /dev/null +++ b/wallet/src/main/java/net/taler/wallet/transactions/JsonDialogFragment.kt @@ -0,0 +1,57 @@ +/* + * 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 + */ + +package net.taler.wallet.transactions + +import android.os.Bundle +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import android.view.ViewGroup.LayoutParams.MATCH_PARENT +import android.view.ViewGroup.LayoutParams.WRAP_CONTENT +import androidx.fragment.app.DialogFragment +import kotlinx.android.synthetic.main.fragment_json.* +import net.taler.wallet.R + +class JsonDialogFragment : DialogFragment() { + + companion object { + fun new(json: String): JsonDialogFragment { + return JsonDialogFragment().apply { + arguments = Bundle().apply { putString("json", json) } + } + } + } + + override fun onCreateView( + inflater: LayoutInflater, + container: ViewGroup?, + savedInstanceState: Bundle? + ): View? { + return inflater.inflate(R.layout.fragment_json, container, false) + } + + override fun onViewCreated(view: View, savedInstanceState: Bundle?) { + val json = arguments!!.getString("json") + jsonView.text = json + } + + override fun onStart() { + super.onStart() + dialog?.window?.setLayout(MATCH_PARENT, WRAP_CONTENT) + } + +} diff --git a/wallet/src/main/java/net/taler/wallet/transactions/ReserveTransaction.kt b/wallet/src/main/java/net/taler/wallet/transactions/ReserveTransaction.kt new file mode 100644 index 0000000..e497e9a --- /dev/null +++ b/wallet/src/main/java/net/taler/wallet/transactions/ReserveTransaction.kt @@ -0,0 +1,59 @@ +/* + * 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 + */ + +package net.taler.wallet.transactions + +import com.fasterxml.jackson.annotation.JsonProperty +import com.fasterxml.jackson.annotation.JsonSubTypes +import com.fasterxml.jackson.annotation.JsonTypeInfo +import com.fasterxml.jackson.annotation.JsonTypeInfo.As.PROPERTY +import com.fasterxml.jackson.annotation.JsonTypeInfo.Id.NAME +import com.fasterxml.jackson.annotation.JsonTypeName +import net.taler.common.Timestamp + + +@JsonTypeInfo( + use = NAME, + include = PROPERTY, + property = "type" +) +@JsonSubTypes( + JsonSubTypes.Type(value = ReserveDepositTransaction::class, name = "DEPOSIT") +) +abstract class ReserveTransaction + + +@JsonTypeName("DEPOSIT") +class ReserveDepositTransaction( + /** + * Amount withdrawn. + */ + val amount: String, + /** + * Sender account payto://-URL + */ + @JsonProperty("sender_account_url") + val senderAccountUrl: String, + /** + * Transfer details uniquely identifying the transfer. + */ + @JsonProperty("wire_reference") + val wireReference: String, + /** + * Timestamp of the incoming wire transfer. + */ + val timestamp: Timestamp +) : ReserveTransaction() diff --git a/wallet/src/main/java/net/taler/wallet/transactions/Transaction.kt b/wallet/src/main/java/net/taler/wallet/transactions/Transaction.kt new file mode 100644 index 0000000..5fcabe7 --- /dev/null +++ b/wallet/src/main/java/net/taler/wallet/transactions/Transaction.kt @@ -0,0 +1,459 @@ +/* + * 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 + */ + +package net.taler.wallet.transactions + +import androidx.annotation.DrawableRes +import androidx.annotation.LayoutRes +import androidx.annotation.StringRes +import com.fasterxml.jackson.annotation.JsonIgnoreProperties +import com.fasterxml.jackson.annotation.JsonInclude +import com.fasterxml.jackson.annotation.JsonInclude.Include.NON_EMPTY +import com.fasterxml.jackson.annotation.JsonProperty +import com.fasterxml.jackson.annotation.JsonSubTypes +import com.fasterxml.jackson.annotation.JsonSubTypes.Type +import com.fasterxml.jackson.annotation.JsonTypeInfo +import com.fasterxml.jackson.annotation.JsonTypeInfo.As.PROPERTY +import com.fasterxml.jackson.annotation.JsonTypeInfo.Id.NAME +import com.fasterxml.jackson.annotation.JsonTypeName +import net.taler.common.Amount +import net.taler.common.Timestamp +import net.taler.wallet.R +import org.json.JSONObject + +enum class ReserveType { + /** + * Manually created. + */ + @JsonProperty("manual") + MANUAL, + + /** + * Withdrawn from a bank that has "tight" Taler integration + */ + @JsonProperty("taler-bank-withdraw") + @Suppress("unused") + TALER_BANK_WITHDRAW, +} + +@JsonInclude(NON_EMPTY) +class ReserveCreationDetail(val type: ReserveType, val bankUrl: String?) + +enum class RefreshReason { + @JsonProperty("manual") + @Suppress("unused") + MANUAL, + + @JsonProperty("pay") + PAY, + + @JsonProperty("refund") + @Suppress("unused") + REFUND, + + @JsonProperty("abort-pay") + @Suppress("unused") + ABORT_PAY, + + @JsonProperty("recoup") + @Suppress("unused") + RECOUP, + + @JsonProperty("backup-restored") + @Suppress("unused") + BACKUP_RESTORED +} + +@JsonInclude(NON_EMPTY) +class ReserveShortInfo( + /** + * The exchange that the reserve will be at. + */ + val exchangeBaseUrl: String, + /** + * Key to query more details + */ + val reservePub: String, + /** + * Detail about how the reserve has been created. + */ + val reserveCreationDetail: ReserveCreationDetail +) + +typealias Transactions = ArrayList + +@JsonTypeInfo( + use = NAME, + include = PROPERTY, + property = "type", + defaultImpl = UnknownTransaction::class +) +/** missing: +AuditorComplaintSent = "auditor-complained-sent", +AuditorComplaintProcessed = "auditor-complaint-processed", +AuditorTrustAdded = "auditor-trust-added", +AuditorTrustRemoved = "auditor-trust-removed", +ExchangeTermsAccepted = "exchange-terms-accepted", +ExchangePolicyChanged = "exchange-policy-changed", +ExchangeTrustAdded = "exchange-trust-added", +ExchangeTrustRemoved = "exchange-trust-removed", +FundsDepositedToSelf = "funds-deposited-to-self", +FundsRecouped = "funds-recouped", +ReserveCreated = "reserve-created", + */ +@JsonSubTypes( + Type(value = ExchangeAddedEvent::class, name = "exchange-added"), + Type(value = ExchangeUpdatedEvent::class, name = "exchange-updated"), + Type(value = ReserveBalanceUpdatedTransaction::class, name = "reserve-balance-updated"), + Type(value = WithdrawTransaction::class, name = "withdrawn"), + Type(value = OrderAcceptedTransaction::class, name = "order-accepted"), + Type(value = OrderRefusedTransaction::class, name = "order-refused"), + Type(value = OrderRedirectedTransaction::class, name = "order-redirected"), + Type(value = PaymentTransaction::class, name = "payment-sent"), + Type(value = PaymentAbortedTransaction::class, name = "payment-aborted"), + Type(value = TipAcceptedTransaction::class, name = "tip-accepted"), + Type(value = TipDeclinedTransaction::class, name = "tip-declined"), + Type(value = RefundTransaction::class, name = "refund"), + Type(value = RefreshTransaction::class, name = "refreshed") +) +@JsonIgnoreProperties( + value = [ + "eventId" + ] +) +abstract class Transaction( + val timestamp: Timestamp, + @get:LayoutRes + open val layout: Int = R.layout.transaction_row, + @get:LayoutRes + open val detailPageLayout: Int = 0, + @get:StringRes + open val title: Int = 0, + @get:DrawableRes + open val icon: Int = R.drawable.ic_account_balance, + open val showToUser: Boolean = false +) { + open lateinit var json: JSONObject +} + + +class UnknownTransaction(timestamp: Timestamp) : Transaction(timestamp) { + override val title = R.string.transaction_unknown +} + +@JsonTypeName("exchange-added") +class ExchangeAddedEvent( + timestamp: Timestamp, + val exchangeBaseUrl: String, + val builtIn: Boolean +) : Transaction(timestamp) { + override val title = R.string.history_event_exchange_added +} + +@JsonTypeName("exchange-updated") +class ExchangeUpdatedEvent( + timestamp: Timestamp, + val exchangeBaseUrl: String +) : Transaction(timestamp) { + override val title = R.string.history_event_exchange_updated +} + + +@JsonTypeName("reserve-balance-updated") +class ReserveBalanceUpdatedTransaction( + timestamp: Timestamp, + /** + * Condensed information about the reserve. + */ + val reserveShortInfo: ReserveShortInfo, + /** + * Amount currently left in the reserve. + */ + val reserveBalance: Amount, + /** + * Amount we expected to be in the reserve at that time, + * considering ongoing withdrawals from that reserve. + */ + val reserveAwaitedAmount: Amount, + /** + * Amount that hasn't been withdrawn yet. + */ + val reserveUnclaimedAmount: Amount +) : Transaction(timestamp) { + override val title = R.string.transaction_reserve_balance_updated +} + +@JsonTypeName("withdrawn") +class WithdrawTransaction( + timestamp: Timestamp, + /** + * Exchange that was withdrawn from. + */ + val exchangeBaseUrl: String, + /** + * Unique identifier for the withdrawal session, can be used to + * query more detailed information from the wallet. + */ + val withdrawalGroupId: String, + val withdrawalSource: WithdrawalSource, + /** + * Amount that has been subtracted from the reserve's balance + * for this withdrawal. + */ + val amountWithdrawnRaw: Amount, + /** + * Amount that actually was added to the wallet's balance. + */ + val amountWithdrawnEffective: Amount +) : Transaction(timestamp) { + override val layout = R.layout.transaction_in + override val detailPageLayout = R.layout.fragment_event_withdraw + override val title = R.string.transaction_withdrawal + override val icon = R.drawable.transaction_withdrawal + override val showToUser = true +} + +@JsonTypeName("order-accepted") +class OrderAcceptedTransaction( + timestamp: Timestamp, + /** + * Condensed info about the order. + */ + val orderShortInfo: OrderShortInfo +) : Transaction(timestamp) { + override val icon = R.drawable.ic_add_circle + override val title = R.string.transaction_order_accepted +} + +@JsonTypeName("order-refused") +class OrderRefusedTransaction( + timestamp: Timestamp, + /** + * Condensed info about the order. + */ + val orderShortInfo: OrderShortInfo +) : Transaction(timestamp) { + override val icon = R.drawable.ic_cancel + override val title = R.string.transaction_order_refused +} + +@JsonTypeName("payment-sent") +class PaymentTransaction( + timestamp: Timestamp, + /** + * Condensed info about the order that we already paid for. + */ + val orderShortInfo: OrderShortInfo, + /** + * Set to true if the payment has been previously sent + * to the merchant successfully, possibly with a different session ID. + */ + val replay: Boolean, + /** + * Number of coins that were involved in the payment. + */ + val numCoins: Int, + /** + * Amount that was paid, including deposit and wire fees. + */ + val amountPaidWithFees: Amount, + /** + * Session ID that the payment was (re-)submitted under. + */ + val sessionId: String? +) : Transaction(timestamp) { + override val layout = R.layout.transaction_out + override val detailPageLayout = R.layout.fragment_event_paid + override val title = R.string.transaction_payment + override val icon = R.drawable.ic_cash_usd_outline + override val showToUser = true +} + +@JsonTypeName("payment-aborted") +class PaymentAbortedTransaction( + timestamp: Timestamp, + /** + * Condensed info about the order that we already paid for. + */ + val orderShortInfo: OrderShortInfo, + /** + * Amount that was lost due to refund and refreshing fees. + */ + val amountLost: Amount +) : Transaction(timestamp) { + override val layout = R.layout.transaction_out + override val title = R.string.transaction_payment_aborted + override val icon = R.drawable.transaction_payment_aborted + override val showToUser = true +} + +@JsonTypeName("refreshed") +class RefreshTransaction( + timestamp: Timestamp, + /** + * Amount that is now available again because it has + * been refreshed. + */ + val amountRefreshedEffective: Amount, + /** + * Amount that we spent for refreshing. + */ + val amountRefreshedRaw: Amount, + /** + * Why was the refreshing done? + */ + val refreshReason: RefreshReason, + val numInputCoins: Int, + val numRefreshedInputCoins: Int, + val numOutputCoins: Int, + /** + * Identifier for a refresh group, contains one or + * more refresh session IDs. + */ + val refreshGroupId: String +) : Transaction(timestamp) { + override val layout = R.layout.transaction_out + override val icon = R.drawable.transaction_refresh + override val title = R.string.transaction_refresh + override val showToUser = !(amountRefreshedRaw - amountRefreshedEffective).isZero() +} + +@JsonTypeName("order-redirected") +class OrderRedirectedTransaction( + timestamp: Timestamp, + /** + * Condensed info about the new order that contains a + * product (identified by the fulfillment URL) that we've already paid for. + */ + val newOrderShortInfo: OrderShortInfo, + /** + * Condensed info about the order that we already paid for. + */ + val alreadyPaidOrderShortInfo: OrderShortInfo +) : Transaction(timestamp) { + override val icon = R.drawable.ic_directions + override val title = R.string.transaction_order_redirected +} + +@JsonTypeName("tip-accepted") +class TipAcceptedTransaction( + timestamp: Timestamp, + /** + * Unique identifier for the tip to query more information. + */ + val tipId: String, + /** + * Raw amount of the tip, without extra fees that apply. + */ + val tipRaw: Amount +) : Transaction(timestamp) { + override val icon = R.drawable.transaction_tip_accepted + override val title = R.string.transaction_tip_accepted + override val layout = R.layout.transaction_in + override val showToUser = true +} + +@JsonTypeName("tip-declined") +class TipDeclinedTransaction( + timestamp: Timestamp, + /** + * Unique identifier for the tip to query more information. + */ + val tipId: String, + /** + * Raw amount of the tip, without extra fees that apply. + */ + val tipAmount: Amount +) : Transaction(timestamp) { + override val icon = R.drawable.transaction_tip_declined + override val title = R.string.transaction_tip_declined + override val layout = R.layout.transaction_in + override val showToUser = true +} + +@JsonTypeName("refund") +class RefundTransaction( + timestamp: Timestamp, + val orderShortInfo: OrderShortInfo, + /** + * Unique identifier for this refund. + * (Identifies multiple refund permissions that were obtained at once.) + */ + val refundGroupId: String, + /** + * Part of the refund that couldn't be applied because + * the refund permissions were expired. + */ + val amountRefundedInvalid: Amount, + /** + * Amount that has been refunded by the merchant. + */ + val amountRefundedRaw: Amount, + /** + * Amount will be added to the wallet's balance after fees and refreshing. + */ + val amountRefundedEffective: Amount +) : Transaction(timestamp) { + override val icon = R.drawable.transaction_refund + override val title = R.string.transaction_refund + override val layout = R.layout.transaction_in + override val detailPageLayout = R.layout.fragment_event_paid + override val showToUser = true +} + +@JsonTypeInfo( + use = NAME, + include = PROPERTY, + property = "type" +) +@JsonSubTypes( + Type(value = WithdrawalSourceReserve::class, name = "reserve") +) +abstract class WithdrawalSource + +@Suppress("unused") +@JsonTypeName("tip") +class WithdrawalSourceTip( + val tipId: String +) : WithdrawalSource() + +@JsonTypeName("reserve") +class WithdrawalSourceReserve( + val reservePub: String +) : WithdrawalSource() + +data class OrderShortInfo( + /** + * Wallet-internal identifier of the proposal. + */ + val proposalId: String, + /** + * Order ID, uniquely identifies the order within a merchant instance. + */ + val orderId: String, + /** + * Base URL of the merchant. + */ + val merchantBaseUrl: String, + /** + * Amount that must be paid for the contract. + */ + val amount: Amount, + /** + * Summary of the proposal, given by the merchant. + */ + val summary: String +) diff --git a/wallet/src/main/java/net/taler/wallet/transactions/TransactionAdapter.kt b/wallet/src/main/java/net/taler/wallet/transactions/TransactionAdapter.kt new file mode 100644 index 0000000..beebcda --- /dev/null +++ b/wallet/src/main/java/net/taler/wallet/transactions/TransactionAdapter.kt @@ -0,0 +1,199 @@ +/* + * 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 + */ + +package net.taler.wallet.transactions + +import android.content.Context +import android.graphics.Paint.STRIKE_THRU_TEXT_FLAG +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import android.widget.ImageView +import android.widget.TextView +import androidx.annotation.CallSuper +import androidx.recyclerview.widget.RecyclerView.Adapter +import androidx.recyclerview.widget.RecyclerView.ViewHolder +import net.taler.common.toRelativeTime +import net.taler.wallet.R +import net.taler.wallet.cleanExchange +import net.taler.wallet.transactions.TransactionAdapter.TransactionViewHolder + + +internal class TransactionAdapter( + private val devMode: Boolean, + private val listener: OnEventClickListener, + private var transactions: Transactions = Transactions() +) : Adapter() { + + init { + setHasStableIds(false) + } + + override fun getItemViewType(position: Int): Int = transactions[position].layout + + override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): TransactionViewHolder { + val view = LayoutInflater.from(parent.context).inflate(viewType, parent, false) + return when (viewType) { + R.layout.transaction_in -> TransactionInViewHolder(view) + R.layout.transaction_out -> TransactionOutViewHolder(view) + else -> GenericTransactionViewHolder(view) + } + } + + override fun getItemCount(): Int = transactions.size + + override fun onBindViewHolder(holder: TransactionViewHolder, position: Int) { + val event = transactions[position] + holder.bind(event) + } + + fun update(updatedTransactions: Transactions) { + this.transactions = updatedTransactions + this.notifyDataSetChanged() + } + + internal abstract inner class TransactionViewHolder(private val v: View) : ViewHolder(v) { + + protected val context: Context = v.context + private val icon: ImageView = v.findViewById(R.id.icon) + protected val title: TextView = v.findViewById(R.id.title) + private val time: TextView = v.findViewById(R.id.time) + private val selectableBackground = v.background + + @CallSuper + open fun bind(event: Transaction) { + if (devMode || event.detailPageLayout != 0) { + v.background = selectableBackground + v.setOnClickListener { listener.onEventClicked(event) } + } else { + v.background = null + v.setOnClickListener(null) + } + icon.setImageResource(event.icon) + if (event.title == 0) title.text = event::class.java.simpleName + else title.setText(event.title) + time.text = event.timestamp.ms.toRelativeTime(context) + } + + } + + internal inner class GenericTransactionViewHolder(v: View) : TransactionViewHolder(v) { + + private val info: TextView = v.findViewById(R.id.info) + + override fun bind(transaction: Transaction) { + super.bind(transaction) + info.text = when (transaction) { + is ExchangeAddedEvent -> cleanExchange(transaction.exchangeBaseUrl) + is ExchangeUpdatedEvent -> cleanExchange(transaction.exchangeBaseUrl) + is ReserveBalanceUpdatedTransaction -> transaction.reserveBalance.toString() + is PaymentTransaction -> transaction.orderShortInfo.summary + is OrderAcceptedTransaction -> transaction.orderShortInfo.summary + is OrderRefusedTransaction -> transaction.orderShortInfo.summary + is OrderRedirectedTransaction -> transaction.newOrderShortInfo.summary + else -> "" + } + } + + } + + internal inner class TransactionInViewHolder(v: View) : TransactionViewHolder(v) { + + private val summary: TextView = v.findViewById(R.id.summary) + private val amountWithdrawn: TextView = v.findViewById(R.id.amountWithdrawn) + private val paintFlags = amountWithdrawn.paintFlags + + override fun bind(event: Transaction) { + super.bind(event) + when (event) { + is WithdrawTransaction -> bind(event) + is RefundTransaction -> bind(event) + is TipAcceptedTransaction -> bind(event) + is TipDeclinedTransaction -> bind(event) + } + } + + private fun bind(event: WithdrawTransaction) { + summary.text = cleanExchange(event.exchangeBaseUrl) + amountWithdrawn.text = + context.getString(R.string.amount_positive, event.amountWithdrawnEffective) + amountWithdrawn.paintFlags = paintFlags + } + + private fun bind(event: RefundTransaction) { + summary.text = event.orderShortInfo.summary + amountWithdrawn.text = + context.getString(R.string.amount_positive, event.amountRefundedEffective) + amountWithdrawn.paintFlags = paintFlags + } + + private fun bind(transaction: TipAcceptedTransaction) { + summary.text = null + amountWithdrawn.text = context.getString(R.string.amount_positive, transaction.tipRaw) + amountWithdrawn.paintFlags = paintFlags + } + + private fun bind(transaction: TipDeclinedTransaction) { + summary.text = null + amountWithdrawn.text = context.getString(R.string.amount_positive, transaction.tipAmount) + amountWithdrawn.paintFlags = amountWithdrawn.paintFlags or STRIKE_THRU_TEXT_FLAG + } + + } + + internal inner class TransactionOutViewHolder(v: View) : TransactionViewHolder(v) { + + private val summary: TextView = v.findViewById(R.id.summary) + private val amountPaidWithFees: TextView = v.findViewById(R.id.amountPaidWithFees) + + override fun bind(event: Transaction) { + super.bind(event) + when (event) { + is PaymentTransaction -> bind(event) + is PaymentAbortedTransaction -> bind(event) + is RefreshTransaction -> bind(event) + } + } + + private fun bind(event: PaymentTransaction) { + summary.text = event.orderShortInfo.summary + amountPaidWithFees.text = + context.getString(R.string.amount_negative, event.amountPaidWithFees) + } + + private fun bind(transaction: PaymentAbortedTransaction) { + summary.text = transaction.orderShortInfo.summary + amountPaidWithFees.text = context.getString(R.string.amount_negative, transaction.amountLost) + } + + private fun bind(event: RefreshTransaction) { + val res = when (event.refreshReason) { + RefreshReason.MANUAL -> R.string.transaction_refresh_reason_manual + RefreshReason.PAY -> R.string.transaction_refresh_reason_pay + RefreshReason.REFUND -> R.string.transaction_refresh_reason_refund + RefreshReason.ABORT_PAY -> R.string.transaction_refresh_reason_abort_pay + RefreshReason.RECOUP -> R.string.transaction_refresh_reason_recoup + RefreshReason.BACKUP_RESTORED -> R.string.transaction_refresh_reason_backup_restored + } + summary.text = context.getString(res) + val fee = event.amountRefreshedRaw - event.amountRefreshedEffective + if (fee.isZero()) amountPaidWithFees.text = null + else amountPaidWithFees.text = context.getString(R.string.amount_negative, fee) + } + + } + +} diff --git a/wallet/src/main/java/net/taler/wallet/transactions/TransactionDetailFragment.kt b/wallet/src/main/java/net/taler/wallet/transactions/TransactionDetailFragment.kt new file mode 100644 index 0000000..f198215 --- /dev/null +++ b/wallet/src/main/java/net/taler/wallet/transactions/TransactionDetailFragment.kt @@ -0,0 +1,127 @@ +/* + * 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 + */ + +package net.taler.wallet.transactions + +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.ViewGroup +import android.widget.Toast +import android.widget.Toast.LENGTH_LONG +import androidx.core.content.ContextCompat.getColor +import androidx.fragment.app.Fragment +import androidx.fragment.app.activityViewModels +import kotlinx.android.synthetic.main.fragment_event_paid.* +import kotlinx.android.synthetic.main.fragment_event_withdraw.* +import kotlinx.android.synthetic.main.fragment_event_withdraw.feeView +import kotlinx.android.synthetic.main.fragment_event_withdraw.timeView +import net.taler.common.Amount +import net.taler.common.toAbsoluteTime +import net.taler.wallet.MainViewModel +import net.taler.wallet.R +import net.taler.wallet.cleanExchange + +class TransactionDetailFragment : Fragment() { + + private val model: MainViewModel by activityViewModels() + private val transactionManager by lazy { model.transactionManager } + private val event by lazy { requireNotNull(transactionManager.selectedEvent) } + + 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(event.detailPageLayout, container, false) + } + + override fun onActivityCreated(savedInstanceState: Bundle?) { + super.onActivityCreated(savedInstanceState) + requireActivity().title = + getString(if (event.title != 0) event.title else R.string.transactions_detail_title) + } + + override fun onViewCreated(view: View, savedInstanceState: Bundle?) { + timeView.text = event.timestamp.ms.toAbsoluteTime(requireContext()) + when (val e = event) { + is WithdrawTransaction -> bind(e) + is PaymentTransaction -> bind(e) + is RefundTransaction -> bind(e) + else -> Toast.makeText( + requireContext(), + "event ${e.javaClass} not implement", + LENGTH_LONG + ).show() + } + } + + override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) { + inflater.inflate(R.menu.transactions_detail, menu) + } + + override fun onOptionsItemSelected(item: MenuItem): Boolean { + return when (item.itemId) { + R.id.show_json -> { + JsonDialogFragment.new(event.json.toString(2)).show(parentFragmentManager, null) + true + } + else -> super.onOptionsItemSelected(item) + } + } + + private fun bind(event: WithdrawTransaction) { + effectiveAmountLabel.text = getString(R.string.withdraw_total) + effectiveAmountView.text = event.amountWithdrawnEffective.toString() + chosenAmountLabel.text = getString(R.string.amount_chosen) + chosenAmountView.text = + getString(R.string.amount_positive, event.amountWithdrawnRaw.toString()) + val fee = event.amountWithdrawnRaw - event.amountWithdrawnEffective + feeView.text = getString(R.string.amount_negative, fee.toString()) + exchangeView.text = cleanExchange(event.exchangeBaseUrl) + } + + private fun bind(event: PaymentTransaction) { + amountPaidWithFeesView.text = event.amountPaidWithFees.toString() + val fee = event.amountPaidWithFees - event.orderShortInfo.amount + bindOrderAndFee(event.orderShortInfo, fee) + } + + private fun bind(event: RefundTransaction) { + amountPaidWithFeesLabel.text = getString(R.string.transaction_refund) + amountPaidWithFeesView.setTextColor(getColor(requireContext(), R.color.green)) + amountPaidWithFeesView.text = + getString(R.string.amount_positive, event.amountRefundedEffective.toString()) + val fee = event.orderShortInfo.amount - event.amountRefundedEffective + bindOrderAndFee(event.orderShortInfo, fee) + } + + private fun bindOrderAndFee(orderShortInfo: OrderShortInfo, fee: Amount) { + orderAmountView.text = orderShortInfo.amount.toString() + feeView.text = getString(R.string.amount_negative, fee.toString()) + orderSummaryView.text = orderShortInfo.summary + orderIdView.text = + getString(R.string.transaction_order_id, orderShortInfo.orderId) + } + +} diff --git a/wallet/src/main/java/net/taler/wallet/transactions/TransactionManager.kt b/wallet/src/main/java/net/taler/wallet/transactions/TransactionManager.kt new file mode 100644 index 0000000..c4ab785 --- /dev/null +++ b/wallet/src/main/java/net/taler/wallet/transactions/TransactionManager.kt @@ -0,0 +1,83 @@ +/* + * 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 + */ + +package net.taler.wallet.transactions + +import androidx.lifecycle.LiveData +import androidx.lifecycle.MutableLiveData +import androidx.lifecycle.asLiveData +import androidx.lifecycle.switchMap +import com.fasterxml.jackson.databind.ObjectMapper +import com.fasterxml.jackson.module.kotlin.readValue +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.channels.awaitClose +import kotlinx.coroutines.flow.callbackFlow +import kotlinx.coroutines.flow.onCompletion +import kotlinx.coroutines.flow.onStart +import kotlinx.coroutines.launch +import net.taler.wallet.backend.WalletBackendApi + +sealed class TransactionsResult { + object Error : TransactionsResult() + class Success(val transactions: Transactions) : TransactionsResult() +} + +@Suppress("EXPERIMENTAL_API_USAGE") +class TransactionManager( + private val walletBackendApi: WalletBackendApi, + private val mapper: ObjectMapper +) { + + private val mProgress = MutableLiveData() + val progress: LiveData = mProgress + + val showAll = MutableLiveData() + + var selectedEvent: Transaction? = null + + val transactions: LiveData = showAll.switchMap { showAll -> + loadTransactions(showAll) + .onStart { mProgress.postValue(true) } + .onCompletion { mProgress.postValue(false) } + .asLiveData(Dispatchers.IO) + } + + private fun loadTransactions(showAll: Boolean) = callbackFlow { + walletBackendApi.sendRequest("getHistory", null) { isError, result -> + launch(Dispatchers.Default) { + if (isError) { + offer(TransactionsResult.Error) + close() + return@launch + } + val transactions = Transactions() + val json = result.getJSONArray("history") + for (i in 0 until json.length()) { + val event: Transaction = mapper.readValue(json.getString(i)) + event.json = json.getJSONObject(i) + transactions.add(event) + } + transactions.reverse() // show latest first + val filtered = + if (showAll) transactions else transactions.filter { it.showToUser } as Transactions + offer(TransactionsResult.Success(filtered)) + close() + } + } + awaitClose() + } + +} diff --git a/wallet/src/main/java/net/taler/wallet/transactions/TransactionsFragment.kt b/wallet/src/main/java/net/taler/wallet/transactions/TransactionsFragment.kt new file mode 100644 index 0000000..663a5aa --- /dev/null +++ b/wallet/src/main/java/net/taler/wallet/transactions/TransactionsFragment.kt @@ -0,0 +1,113 @@ +/* + * 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 + */ + +package net.taler.wallet.transactions + +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.INVISIBLE +import android.view.View.VISIBLE +import android.view.ViewGroup +import androidx.fragment.app.Fragment +import androidx.fragment.app.activityViewModels +import androidx.lifecycle.Observer +import androidx.navigation.fragment.findNavController +import androidx.recyclerview.widget.DividerItemDecoration +import androidx.recyclerview.widget.LinearLayoutManager +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 + +interface OnEventClickListener { + fun onEventClicked(event: Transaction) +} + +class TransactionsFragment : Fragment(), OnEventClickListener { + + private val model: MainViewModel by activityViewModels() + private val transactionManager by lazy { model.transactionManager } + private val transactionAdapter by lazy { TransactionAdapter(model.devMode.value == true, this) } + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + setHasOptionsMenu(true) + } + + override fun onCreateView( + inflater: LayoutInflater, container: ViewGroup?, + savedInstanceState: Bundle? + ): View? { + return inflater.inflate(R.layout.fragment_transactions, container, false) + } + + override fun onViewCreated(view: View, savedInstanceState: Bundle?) { + list.apply { + layoutManager = LinearLayoutManager(context) + adapter = transactionAdapter + addItemDecoration(DividerItemDecoration(context, VERTICAL)) + } + + transactionManager.progress.observe(viewLifecycleOwner, Observer { show -> + progressBar.visibility = if (show) VISIBLE else INVISIBLE + }) + transactionManager.transactions.observe(viewLifecycleOwner, Observer { result -> + onTransactionsResult(result) + }) + + // kicks off initial load, needs to be adapted if showAll state is ever saved + if (savedInstanceState == null) transactionManager.showAll.value = model.devMode.value + } + + override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) { + inflater.inflate(R.menu.transactions, menu) + } + + override fun onOptionsItemSelected(item: MenuItem): Boolean { + return when (item.itemId) { + else -> super.onOptionsItemSelected(item) + } + } + + override fun onEventClicked(event: Transaction) { + if (event.detailPageLayout != 0) { + transactionManager.selectedEvent = event + findNavController().navigate(R.id.action_nav_transactions_to_nav_transaction_detail) + } else if (model.devMode.value == true) { + JsonDialogFragment.new(event.json.toString(2)) + .show(parentFragmentManager, null) + } + } + + private fun onTransactionsResult(result: TransactionsResult) = when (result) { + TransactionsResult.Error -> { + list.fadeOut() + emptyState.text = getString(R.string.transactions_error) + emptyState.fadeIn() + } + is TransactionsResult.Success -> { + emptyState.visibility = if (result.transactions.isEmpty()) VISIBLE else INVISIBLE + transactionAdapter.update(result.transactions) + } + } + +} diff --git a/wallet/src/main/res/drawable/history_payment_aborted.xml b/wallet/src/main/res/drawable/history_payment_aborted.xml deleted file mode 100644 index 8d47c26..0000000 --- a/wallet/src/main/res/drawable/history_payment_aborted.xml +++ /dev/null @@ -1,25 +0,0 @@ - - - - - diff --git a/wallet/src/main/res/drawable/history_refresh.xml b/wallet/src/main/res/drawable/history_refresh.xml deleted file mode 100644 index 219b891..0000000 --- a/wallet/src/main/res/drawable/history_refresh.xml +++ /dev/null @@ -1,28 +0,0 @@ - - - - - - diff --git a/wallet/src/main/res/drawable/history_refund.xml b/wallet/src/main/res/drawable/history_refund.xml deleted file mode 100644 index 6c3d0a7..0000000 --- a/wallet/src/main/res/drawable/history_refund.xml +++ /dev/null @@ -1,25 +0,0 @@ - - - - - diff --git a/wallet/src/main/res/drawable/history_tip_accepted.xml b/wallet/src/main/res/drawable/history_tip_accepted.xml deleted file mode 100644 index b945b53..0000000 --- a/wallet/src/main/res/drawable/history_tip_accepted.xml +++ /dev/null @@ -1,25 +0,0 @@ - - - - - diff --git a/wallet/src/main/res/drawable/history_tip_declined.xml b/wallet/src/main/res/drawable/history_tip_declined.xml deleted file mode 100644 index 4bd1633..0000000 --- a/wallet/src/main/res/drawable/history_tip_declined.xml +++ /dev/null @@ -1,25 +0,0 @@ - - - - - diff --git a/wallet/src/main/res/drawable/history_withdrawn.xml b/wallet/src/main/res/drawable/history_withdrawn.xml deleted file mode 100644 index 4fd64f5..0000000 --- a/wallet/src/main/res/drawable/history_withdrawn.xml +++ /dev/null @@ -1,25 +0,0 @@ - - - - - diff --git a/wallet/src/main/res/drawable/transaction_payment_aborted.xml b/wallet/src/main/res/drawable/transaction_payment_aborted.xml new file mode 100644 index 0000000..8d47c26 --- /dev/null +++ b/wallet/src/main/res/drawable/transaction_payment_aborted.xml @@ -0,0 +1,25 @@ + + + + + diff --git a/wallet/src/main/res/drawable/transaction_refresh.xml b/wallet/src/main/res/drawable/transaction_refresh.xml new file mode 100644 index 0000000..219b891 --- /dev/null +++ b/wallet/src/main/res/drawable/transaction_refresh.xml @@ -0,0 +1,28 @@ + + + + + + diff --git a/wallet/src/main/res/drawable/transaction_refund.xml b/wallet/src/main/res/drawable/transaction_refund.xml new file mode 100644 index 0000000..6c3d0a7 --- /dev/null +++ b/wallet/src/main/res/drawable/transaction_refund.xml @@ -0,0 +1,25 @@ + + + + + diff --git a/wallet/src/main/res/drawable/transaction_tip_accepted.xml b/wallet/src/main/res/drawable/transaction_tip_accepted.xml new file mode 100644 index 0000000..b945b53 --- /dev/null +++ b/wallet/src/main/res/drawable/transaction_tip_accepted.xml @@ -0,0 +1,25 @@ + + + + + diff --git a/wallet/src/main/res/drawable/transaction_tip_declined.xml b/wallet/src/main/res/drawable/transaction_tip_declined.xml new file mode 100644 index 0000000..4bd1633 --- /dev/null +++ b/wallet/src/main/res/drawable/transaction_tip_declined.xml @@ -0,0 +1,25 @@ + + + + + diff --git a/wallet/src/main/res/drawable/transaction_withdrawal.xml b/wallet/src/main/res/drawable/transaction_withdrawal.xml new file mode 100644 index 0000000..4fd64f5 --- /dev/null +++ b/wallet/src/main/res/drawable/transaction_withdrawal.xml @@ -0,0 +1,25 @@ + + + + + diff --git a/wallet/src/main/res/layout/fragment_event_paid.xml b/wallet/src/main/res/layout/fragment_event_paid.xml index 4485744..3f17464 100644 --- a/wallet/src/main/res/layout/fragment_event_paid.xml +++ b/wallet/src/main/res/layout/fragment_event_paid.xml @@ -20,7 +20,7 @@ android:layout_width="match_parent" android:layout_height="match_parent" android:fillViewport="true" - tools:context=".history.HistoryEventFragment"> + tools:context=".transactions.TransactionDetailFragment"> + tools:context=".transactions.TransactionDetailFragment"> - - - - - - - - - - diff --git a/wallet/src/main/res/layout/fragment_transactions.xml b/wallet/src/main/res/layout/fragment_transactions.xml new file mode 100644 index 0000000..a2aebcd --- /dev/null +++ b/wallet/src/main/res/layout/fragment_transactions.xml @@ -0,0 +1,46 @@ + + + + + + + + + + + diff --git a/wallet/src/main/res/layout/history_payment.xml b/wallet/src/main/res/layout/history_payment.xml deleted file mode 100644 index 33cb676..0000000 --- a/wallet/src/main/res/layout/history_payment.xml +++ /dev/null @@ -1,87 +0,0 @@ - - - - - - - - - - - - - - - diff --git a/wallet/src/main/res/layout/history_receive.xml b/wallet/src/main/res/layout/history_receive.xml deleted file mode 100644 index 5f386a2..0000000 --- a/wallet/src/main/res/layout/history_receive.xml +++ /dev/null @@ -1,92 +0,0 @@ - - - - - - - - - - - - - - - diff --git a/wallet/src/main/res/layout/history_row.xml b/wallet/src/main/res/layout/history_row.xml deleted file mode 100644 index 5eac44b..0000000 --- a/wallet/src/main/res/layout/history_row.xml +++ /dev/null @@ -1,76 +0,0 @@ - - - - - - - - - - - - - \ No newline at end of file diff --git a/wallet/src/main/res/layout/transaction_in.xml b/wallet/src/main/res/layout/transaction_in.xml new file mode 100644 index 0000000..f116240 --- /dev/null +++ b/wallet/src/main/res/layout/transaction_in.xml @@ -0,0 +1,92 @@ + + + + + + + + + + + + + + + diff --git a/wallet/src/main/res/layout/transaction_out.xml b/wallet/src/main/res/layout/transaction_out.xml new file mode 100644 index 0000000..49f76b7 --- /dev/null +++ b/wallet/src/main/res/layout/transaction_out.xml @@ -0,0 +1,87 @@ + + + + + + + + + + + + + + + diff --git a/wallet/src/main/res/layout/transaction_row.xml b/wallet/src/main/res/layout/transaction_row.xml new file mode 100644 index 0000000..ac6bdb7 --- /dev/null +++ b/wallet/src/main/res/layout/transaction_row.xml @@ -0,0 +1,76 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/wallet/src/main/res/menu/balance.xml b/wallet/src/main/res/menu/balance.xml deleted file mode 100644 index d4568d4..0000000 --- a/wallet/src/main/res/menu/balance.xml +++ /dev/null @@ -1,19 +0,0 @@ - - - - diff --git a/wallet/src/main/res/menu/fragment_main.xml b/wallet/src/main/res/menu/fragment_main.xml new file mode 100644 index 0000000..d4568d4 --- /dev/null +++ b/wallet/src/main/res/menu/fragment_main.xml @@ -0,0 +1,19 @@ + + + + diff --git a/wallet/src/main/res/menu/history.xml b/wallet/src/main/res/menu/history.xml deleted file mode 100644 index bd50f5d..0000000 --- a/wallet/src/main/res/menu/history.xml +++ /dev/null @@ -1,30 +0,0 @@ - - - - - - diff --git a/wallet/src/main/res/menu/history_event.xml b/wallet/src/main/res/menu/history_event.xml deleted file mode 100644 index 45a1e0e..0000000 --- a/wallet/src/main/res/menu/history_event.xml +++ /dev/null @@ -1,23 +0,0 @@ - - - - - diff --git a/wallet/src/main/res/menu/transactions.xml b/wallet/src/main/res/menu/transactions.xml new file mode 100644 index 0000000..d4568d4 --- /dev/null +++ b/wallet/src/main/res/menu/transactions.xml @@ -0,0 +1,19 @@ + + + + diff --git a/wallet/src/main/res/menu/transactions_detail.xml b/wallet/src/main/res/menu/transactions_detail.xml new file mode 100644 index 0000000..388e3c4 --- /dev/null +++ b/wallet/src/main/res/menu/transactions_detail.xml @@ -0,0 +1,23 @@ + + + + + diff --git a/wallet/src/main/res/navigation/nav_graph.xml b/wallet/src/main/res/navigation/nav_graph.xml index d4257b9..f8927a6 100644 --- a/wallet/src/main/res/navigation/nav_graph.xml +++ b/wallet/src/main/res/navigation/nav_graph.xml @@ -55,23 +55,22 @@ + android:label="Settings" /> + android:id="@+id/nav_transactions" + android:name="net.taler.wallet.transactions.TransactionsFragment" + android:label="@string/transactions_title" + tools:layout="@layout/fragment_transactions"> + android:id="@+id/action_nav_transactions_to_nav_transaction_detail" + app:destination="@id/nav_transactions_detail" /> Continue Scan Taler QR Code - History Settings - Reload balances - Developer Mode Retry Pending Operations my service my aid Balances - +%s - -%s + + %s + - %s Chosen Amount inbound There is no digital cash in your wallet.\n\nYou can get test money from the demo bank:\n\nhttps://bank.demo.taler.net - History - Show All - Reload History - The wallet history is empty - Could not load history - Transaction - Show JSON + Transactions + You don\'t have any transactions + Could not load transactions + Transaction + Show JSON - + Exchange Added Exchange Updated - Reserve Balance Updated - Payment - Paid - Order Total - Order - Order Reference: %1$s - Payment Aborted - Withdraw - Purchase Confirmed - Purchase Cancelled - Tip Accepted - Tip Declined - Purchase Redirected - Refund - Obtained change - because of manual request - for payment - for refund - to abort payment - to recoup funds - because of restoring from backup - Unknown Event + Reserve Balance Updated + Payment + Paid + Order Total + Order + Order Reference: %1$s + Payment Aborted + Withdraw + Purchase Confirmed + Purchase Cancelled + Tip Accepted + Tip Declined + Purchase Redirected + Refund + Obtained change + because of manual request + for payment + for refund + to abort payment + to recoup funds + because of restoring from backup + Unknown Transaction +%s payment fee Confirm Payment @@ -118,21 +113,18 @@ Timespan: %1$s - %2$s Wire Fee: %s Closing Fee: %s + Accept Terms of Service Pending Operations Refuse Proposal (no action) - Version Information - Accept Terms of Service - Backups - Export wallet to file - Import from file + Developer Mode + Shows more information intended for debugging Withdraw TESTKUDOS Get money for testing Reset Wallet (dangerous!) Throws away your money - Shows more information intended for debugging Error processing refund Refund received diff --git a/wallet/src/main/res/values/styles.xml b/wallet/src/main/res/values/styles.xml index 9ebcae5..c8a2c3b 100644 --- a/wallet/src/main/res/values/styles.xml +++ b/wallet/src/main/res/values/styles.xml @@ -33,12 +33,12 @@ - - -