ProductAdapter.kt (4718B)
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.wallet.payment 18 19 import android.content.Context 20 import android.graphics.Bitmap 21 import android.view.LayoutInflater 22 import android.view.View 23 import android.view.View.GONE 24 import android.view.View.VISIBLE 25 import android.view.ViewGroup 26 import android.widget.ImageView 27 import android.widget.TextView 28 import androidx.recyclerview.widget.DiffUtil 29 import androidx.recyclerview.widget.RecyclerView 30 import androidx.recyclerview.widget.RecyclerView.ViewHolder 31 import net.taler.common.ContractProduct 32 import net.taler.common.base64Bitmap 33 import net.taler.wallet.R 34 import net.taler.wallet.payment.ProductAdapter.ProductViewHolder 35 36 internal interface ProductImageClickListener { 37 fun onImageClick(image: Bitmap) 38 } 39 40 internal class ProductAdapter(private val listener: ProductImageClickListener) : 41 RecyclerView.Adapter<ProductViewHolder>() { 42 43 private var items = emptyList<ContractProduct>() 44 45 override fun getItemCount() = items.size 46 47 override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ProductViewHolder { 48 val inflater = LayoutInflater.from(parent.context) 49 val view = inflater.inflate(R.layout.list_item_product, parent, false) 50 return ProductViewHolder(view) 51 } 52 53 override fun onBindViewHolder(holder: ProductViewHolder, position: Int) { 54 holder.bind(items[position]) 55 } 56 57 fun update(newItems: List<ContractProduct>) { 58 val oldItems = this.items 59 60 val diffCallback = ProductDiffCallback(oldItems, newItems) 61 val diffResult = DiffUtil.calculateDiff(diffCallback) 62 diffResult.dispatchUpdatesTo(this) 63 64 items = newItems 65 } 66 67 internal inner class ProductViewHolder(v: View) : ViewHolder(v) { 68 private val context: Context = v.context 69 70 private val quantity: TextView = v.findViewById(R.id.quantity) 71 private val image: ImageView = v.findViewById(R.id.image) 72 private val name: TextView = v.findViewById(R.id.name) 73 private val taxes: TextView = v.findViewById(R.id.taxes) 74 private val price: TextView = v.findViewById(R.id.price) 75 76 fun bind(product: ContractProduct) { 77 quantity.text = product.quantity.toString() 78 79 // base64 encoded image 80 val bitmap = product.image?.base64Bitmap 81 if (bitmap == null) { 82 image.visibility = GONE 83 } else { 84 image.visibility = VISIBLE 85 image.setImageBitmap(bitmap) 86 image.setOnClickListener { 87 listener.onImageClick(bitmap) 88 } 89 } 90 91 name.text = product.description 92 93 if (product.totalPrice != null) { 94 price.visibility = VISIBLE 95 price.text = product.totalPrice.toString() 96 } else { 97 price.visibility = GONE 98 } 99 100 if (product.taxes != null && product.taxes!!.isNotEmpty()) { 101 taxes.visibility = VISIBLE 102 taxes.text = product.taxes!!.filter { 103 !it.tax.isZero() 104 }.joinToString(separator = "\n") { 105 context.getString(R.string.payment_tax, it.name, it.tax) 106 } 107 } else { 108 taxes.visibility = GONE 109 } 110 } 111 } 112 } 113 114 internal class ProductDiffCallback( 115 private val oldList: List<ContractProduct>, 116 private val newList: List<ContractProduct>, 117 ): DiffUtil.Callback() { 118 override fun getOldListSize() = oldList.size 119 120 override fun getNewListSize() = newList.size 121 122 override fun areItemsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean { 123 val old = oldList[oldItemPosition] 124 val new = newList[newItemPosition] 125 126 return old.productId == new.productId 127 } 128 129 override fun areContentsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean { 130 val old = oldList[oldItemPosition] 131 val new = newList[newItemPosition] 132 133 return old == new 134 } 135 }