OrderAdapter.kt (5555B)
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.order 18 19 import android.view.LayoutInflater 20 import android.view.MotionEvent 21 import android.view.View 22 import android.view.View.GONE 23 import android.view.View.VISIBLE 24 import android.view.ViewGroup 25 import android.widget.ImageView 26 import android.widget.TextView 27 import androidx.recyclerview.selection.ItemDetailsLookup 28 import androidx.recyclerview.selection.ItemKeyProvider 29 import androidx.recyclerview.selection.SelectionTracker 30 import androidx.recyclerview.widget.AsyncListDiffer 31 import androidx.recyclerview.widget.DiffUtil.ItemCallback 32 import androidx.recyclerview.widget.RecyclerView 33 import androidx.recyclerview.widget.RecyclerView.Adapter 34 import net.taler.lib.android.base64Bitmap 35 import net.taler.merchantpos.R 36 import net.taler.merchantpos.config.ConfigProduct 37 import net.taler.merchantpos.order.OrderAdapter.OrderViewHolder 38 39 internal class OrderAdapter : Adapter<OrderViewHolder>() { 40 41 lateinit var tracker: SelectionTracker<String> 42 val keyProvider = OrderKeyProvider() 43 private val itemCallback = object : ItemCallback<ConfigProduct>() { 44 override fun areItemsTheSame(oldItem: ConfigProduct, newItem: ConfigProduct): Boolean { 45 return oldItem == newItem 46 } 47 48 override fun areContentsTheSame(oldItem: ConfigProduct, newItem: ConfigProduct): Boolean { 49 return oldItem.quantity == newItem.quantity 50 } 51 } 52 private val differ = AsyncListDiffer(this, itemCallback) 53 54 override fun getItemCount() = differ.currentList.size 55 56 override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): OrderViewHolder { 57 val view = 58 LayoutInflater.from(parent.context).inflate(R.layout.list_item_order, parent, false) 59 return OrderViewHolder(view) 60 } 61 62 override fun onBindViewHolder(holder: OrderViewHolder, position: Int) { 63 val item = getItem(position)!! 64 holder.bind(item, tracker.isSelected(item.id)) 65 } 66 67 fun setItems(items: List<ConfigProduct>, commitCallback: () -> Unit) { 68 // toMutableList() is needed for some reason, otherwise doesn't update adapter 69 differ.submitList(items.toMutableList(), commitCallback) 70 } 71 72 fun getItem(position: Int): ConfigProduct? = differ.currentList[position] 73 74 fun getItemByKey(key: String): ConfigProduct? { 75 return differ.currentList.find { it.id == key } 76 } 77 78 fun findPosition(product: ConfigProduct): Int { 79 return differ.currentList.indexOf(product) 80 } 81 82 internal inner class OrderViewHolder(private val v: View) : RecyclerView.ViewHolder(v) { 83 private val quantity: TextView = v.findViewById(R.id.quantity) 84 private val name: TextView = v.findViewById(R.id.name) 85 private val description: TextView = v.findViewById(R.id.description) 86 private val price: TextView = v.findViewById(R.id.price) 87 private val image: ImageView = v.findViewById(R.id.image) 88 89 fun bind(product: ConfigProduct, selected: Boolean) { 90 v.isActivated = selected 91 quantity.text = product.quantity.toString() 92 name.text = product.displayName 93 val productDescription = product.displayDescription 94 if (productDescription == null) { 95 description.visibility = GONE 96 } else { 97 description.visibility = VISIBLE 98 description.text = productDescription 99 } 100 price.text = product.totalPrice.toString() 101 102 // base64 encoded image 103 val bitmap = product.image?.base64Bitmap 104 if (bitmap == null) { 105 image.visibility = GONE 106 } else { 107 image.visibility = VISIBLE 108 image.setImageBitmap(bitmap) 109 } 110 } 111 } 112 113 internal inner class OrderKeyProvider : ItemKeyProvider<String>(SCOPE_MAPPED) { 114 override fun getKey(position: Int) = getItem(position)!!.id 115 override fun getPosition(key: String): Int { 116 return differ.currentList.indexOfFirst { it.id == key } 117 } 118 } 119 120 internal class OrderLineLookup(private val list: RecyclerView) : ItemDetailsLookup<String>() { 121 override fun getItemDetails(e: MotionEvent): ItemDetails<String>? { 122 list.findChildViewUnder(e.x, e.y)?.let { view -> 123 val holder = list.getChildViewHolder(view) 124 val adapter = list.adapter as OrderAdapter 125 val position = holder.bindingAdapterPosition 126 return object : ItemDetails<String>() { 127 override fun getPosition(): Int = position 128 override fun getSelectionKey(): String = adapter.keyProvider.getKey(position) 129 override fun inSelectionHotspot(e: MotionEvent) = true 130 } 131 } 132 return null 133 } 134 } 135 136 }