messenger-android

Android graphical user interfaces for GNUnet Messenger
Log | Files | Refs | README | LICENSE

ContactListAdapter.kt (3442B)


      1 /*
      2    This file is part of GNUnet.
      3    Copyright (C) 2021--2025 GNUnet e.V.
      4 
      5    GNUnet is free software: you can redistribute it and/or modify it
      6    under the terms of the GNU Affero General Public License as published
      7    by the Free Software Foundation, either version 3 of the License,
      8    or (at your option) any later version.
      9 
     10    GNUnet is distributed in the hope that it will be useful, but
     11    WITHOUT ANY WARRANTY; without even the implied warranty of
     12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     13    Affero General Public License for more details.
     14 
     15    You should have received a copy of the GNU Affero General Public License
     16    along with this program.  If not, see <http://www.gnu.org/licenses/>.
     17 
     18    SPDX-License-Identifier: AGPL3.0-or-later
     19  */
     20 /*
     21  * @author t3sserakt
     22  * @file GNUnetMessenger/app/src/main/java/org/gnunet/gnunetmessenger/ui/adapters/ContactListAdapter.kt
     23  */
     24 
     25 
     26 package org.gnunet.gnunetmessenger.ui.adapters
     27 
     28 import android.graphics.Color
     29 import android.view.LayoutInflater
     30 import android.view.View
     31 import android.view.ViewGroup
     32 import android.widget.TextView
     33 import androidx.recyclerview.widget.RecyclerView
     34 import org.gnunet.gnunetmessenger.R
     35 import org.gnunet.gnunetmessenger.model.ChatContact
     36 
     37 class ContactListAdapter(
     38     private val onClick: ((ChatContact) -> Unit)? = null,
     39     private val multiSelect: Boolean = true
     40 ) : RecyclerView.Adapter<ContactListAdapter.ViewHolder>() {
     41 
     42     private val contacts = mutableListOf<ChatContact>()
     43     private val selectedContacts = mutableSetOf<ChatContact>()
     44 
     45     fun submitList(list: List<ChatContact>) {
     46         contacts.clear()
     47         contacts.addAll(list)
     48         selectedContacts.clear()
     49         notifyDataSetChanged()
     50     }
     51 
     52     fun getSelectedContacts(): List<ChatContact> = selectedContacts.toList()
     53 
     54     inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
     55         val nameText: TextView = view.findViewById(R.id.contactName)
     56 
     57         init {
     58             view.setOnClickListener {
     59                 val contact = contacts[adapterPosition]
     60                 toggleSelection(contact)
     61                 notifyDataSetChanged()
     62                 onClick?.invoke(contact)
     63             }
     64         }
     65 
     66         fun bind(contact: ChatContact, isSelected: Boolean) {
     67             nameText.text = contact.name
     68             itemView.setBackgroundColor(
     69                 if (isSelected) Color.LTGRAY else Color.TRANSPARENT
     70             )
     71         }
     72     }
     73 
     74     override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
     75         val view = LayoutInflater.from(parent.context)
     76             .inflate(R.layout.item_contact, parent, false)
     77         return ViewHolder(view)
     78     }
     79 
     80     override fun getItemCount(): Int = contacts.size
     81 
     82     override fun onBindViewHolder(holder: ViewHolder, position: Int) {
     83         val contact = contacts[position]
     84         holder.bind(contact, selectedContacts.contains(contact))
     85     }
     86 
     87     private fun toggleSelection(contact: ChatContact) {
     88         if (multiSelect) {
     89             if (selectedContacts.contains(contact)) {
     90                 selectedContacts.remove(contact)
     91             } else {
     92                 selectedContacts.add(contact)
     93             }
     94         } else {
     95             if (!selectedContacts.contains(contact)) {
     96                 selectedContacts.clear()
     97                 selectedContacts.add(contact)
     98             } else {
     99                 selectedContacts.clear()
    100             }
    101         }
    102     }
    103 }