summaryrefslogtreecommitdiff
path: root/wallet/src/main/java/net/taler/wallet/history/HistoryAdapter.kt
blob: b9770fbbf56971babfde1060b6c8fef50e881144 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
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 <http://www.gnu.org/licenses/>
 */

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<HistoryEventViewHolder>() {

    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)
        }

    }

}