HistoryItemAdapter.kt (4208B)
1 /* 2 * This file is part of GNU Taler 3 * (C) 2020 Taler Systems S.A. 4 * 5 * GNU Taler is free software; you can redistribute it and/or modify it under the 6 * terms of the GNU General Public License as published by the Free Software 7 * Foundation; either version 3, or (at your option) any later version. 8 * 9 * GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY 10 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR 11 * A PARTICULAR PURPOSE. See the GNU General Public License for more details. 12 * 13 * You should have received a copy of the GNU General Public License along with 14 * GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> 15 */ 16 17 package net.taler.merchantpos.history 18 19 import android.view.LayoutInflater 20 import android.view.View 21 import android.view.ViewGroup 22 import android.widget.ImageButton 23 import android.widget.TextView 24 import androidx.core.content.ContextCompat.getColor 25 import androidx.recyclerview.widget.RecyclerView 26 import androidx.recyclerview.widget.RecyclerView.Adapter 27 import com.google.android.material.button.MaterialButton 28 import net.taler.merchantlib.OrderHistoryEntry 29 import net.taler.merchantpos.R 30 import net.taler.merchantpos.history.HistoryItemAdapter.HistoryItemViewHolder 31 import net.taler.lib.android.toRelativeTime 32 33 34 internal class HistoryItemAdapter(private val listener: HistoryActionListener) : 35 Adapter<HistoryItemViewHolder>() { 36 37 private val items = ArrayList<OrderHistoryEntry>() 38 39 override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): HistoryItemViewHolder { 40 val v = 41 LayoutInflater.from(parent.context).inflate(R.layout.list_item_history, parent, false) 42 return HistoryItemViewHolder(v) 43 } 44 45 override fun getItemCount() = items.size 46 47 override fun onBindViewHolder(holder: HistoryItemViewHolder, position: Int) { 48 holder.bind(items[position]) 49 } 50 51 fun setData(items: List<OrderHistoryEntry>) { 52 this.items.clear() 53 this.items.addAll(items) 54 this.notifyDataSetChanged() 55 } 56 57 internal inner class HistoryItemViewHolder(private val v: View) : RecyclerView.ViewHolder(v) { 58 59 private val orderSummaryView: TextView = v.findViewById(R.id.orderSummaryView) 60 private val orderAmountView: TextView = v.findViewById(R.id.orderAmountView) 61 private val orderTimeView: TextView = v.findViewById(R.id.orderTimeView) 62 private val orderIdView: TextView = v.findViewById(R.id.orderIdView) 63 private val refundButton: ImageButton = v.findViewById(R.id.refundButton) 64 private val deleteButton: MaterialButton = v.findViewById(R.id.deleteButton) 65 66 private val orderIdColor = orderIdView.currentTextColor 67 68 fun bind(item: OrderHistoryEntry) { 69 orderSummaryView.text = item.summary 70 val amount = item.amount 71 orderAmountView.text = amount.toString() 72 orderTimeView.text = item.timestamp.ms.toRelativeTime(v.context) 73 if (item.paid) { 74 orderIdView.text = v.context.getString(R.string.history_ref_no, item.orderId) 75 orderIdView.setTextColor(orderIdColor) 76 } else { 77 orderIdView.text = v.context.getString(R.string.history_unpaid) 78 orderIdView.setTextColor(getColor(v.context, R.color.colorError)) 79 } 80 if (item.refundable) { 81 refundButton.visibility = View.VISIBLE 82 deleteButton.visibility = View.GONE 83 refundButton.setOnClickListener { listener.onRefundClicked(item) } 84 deleteButton.setOnClickListener(null) 85 } else if (!item.paid) { 86 refundButton.visibility = View.GONE 87 deleteButton.visibility = View.VISIBLE 88 deleteButton.setOnClickListener { listener.onDeleteClicked(item) } 89 refundButton.setOnClickListener(null) 90 } else { 91 refundButton.visibility = View.GONE 92 deleteButton.visibility = View.GONE 93 refundButton.setOnClickListener(null) 94 deleteButton.setOnClickListener(null) 95 } 96 } 97 98 } 99 100 }