From f920fa7fa12db5d6fd40844ffb8402426d0a2b07 Mon Sep 17 00:00:00 2001 From: Torsten Grote Date: Thu, 16 Apr 2020 15:06:06 -0300 Subject: [wallet] allow transactions to be selected by long tap --- .../wallet/transactions/TransactionAdapter.kt | 47 +++++++++++++++++++--- 1 file changed, 41 insertions(+), 6 deletions(-) (limited to 'wallet/src/main/java/net/taler/wallet/transactions/TransactionAdapter.kt') diff --git a/wallet/src/main/java/net/taler/wallet/transactions/TransactionAdapter.kt b/wallet/src/main/java/net/taler/wallet/transactions/TransactionAdapter.kt index 809f6a9..a72b8a8 100644 --- a/wallet/src/main/java/net/taler/wallet/transactions/TransactionAdapter.kt +++ b/wallet/src/main/java/net/taler/wallet/transactions/TransactionAdapter.kt @@ -18,13 +18,17 @@ package net.taler.wallet.transactions import android.content.Context import android.view.LayoutInflater +import android.view.MotionEvent import android.view.View import android.view.View.GONE import android.view.View.VISIBLE import android.view.ViewGroup import android.widget.ImageView import android.widget.TextView -import androidx.annotation.CallSuper +import androidx.recyclerview.selection.ItemDetailsLookup +import androidx.recyclerview.selection.ItemKeyProvider +import androidx.recyclerview.selection.SelectionTracker +import androidx.recyclerview.widget.RecyclerView import androidx.recyclerview.widget.RecyclerView.Adapter import androidx.recyclerview.widget.RecyclerView.ViewHolder import net.taler.common.exhaustive @@ -39,6 +43,9 @@ internal class TransactionAdapter( private var transactions: Transactions = Transactions() ) : Adapter() { + lateinit var tracker: SelectionTracker + val keyProvider = TransactionKeyProvider() + init { setHasStableIds(false) } @@ -52,8 +59,8 @@ internal class TransactionAdapter( override fun getItemCount(): Int = transactions.size override fun onBindViewHolder(holder: TransactionViewHolder, position: Int) { - val event = transactions[position] - holder.bind(event) + val transaction = transactions[position] + holder.bind(transaction, tracker.isSelected(transaction.eventId)) } fun update(updatedTransactions: Transactions) { @@ -61,6 +68,10 @@ internal class TransactionAdapter( this.notifyDataSetChanged() } + fun selectAll() = transactions.forEach { + tracker.select(it.eventId) + } + internal open inner class TransactionViewHolder(private val v: View) : ViewHolder(v) { protected val context: Context = v.context @@ -73,15 +84,15 @@ internal class TransactionAdapter( private val selectableBackground = v.background private val amountColor = amount.currentTextColor - @CallSuper - open fun bind(transaction: Transaction) { + open fun bind(transaction: Transaction, selected: Boolean) { if (devMode || transaction.detailPageLayout != 0) { v.background = selectableBackground - v.setOnClickListener { listener.onEventClicked(transaction) } + v.setOnClickListener { listener.onTransactionClicked(transaction) } } else { v.background = null v.setOnClickListener(null) } + v.isActivated = selected icon.setImageResource(transaction.icon) title.text = if (transaction.title == null) { @@ -140,4 +151,28 @@ internal class TransactionAdapter( } + internal inner class TransactionKeyProvider : ItemKeyProvider(SCOPE_MAPPED) { + override fun getKey(position: Int) = transactions[position].eventId + override fun getPosition(key: String): Int { + return transactions.indexOfFirst { it.eventId == key } + } + } + +} + +internal class TransactionLookup( + private val list: RecyclerView, + private val adapter: TransactionAdapter +) : ItemDetailsLookup() { + override fun getItemDetails(e: MotionEvent): ItemDetails? { + list.findChildViewUnder(e.x, e.y)?.let { view -> + val holder = list.getChildViewHolder(view) + val position = holder.adapterPosition + return object : ItemDetails() { + override fun getPosition(): Int = position + override fun getSelectionKey(): String = adapter.keyProvider.getKey(position) + } + } + return null + } } -- cgit v1.2.3