AccountAdapter.kt (3012B)
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/AccountAdapter.kt 23 */ 24 25 package org.gnunet.gnunetmessenger.ui.adapters 26 27 import android.view.LayoutInflater 28 import android.view.View 29 import android.view.ViewGroup 30 import android.widget.ImageView 31 import android.widget.TextView 32 import androidx.recyclerview.widget.DiffUtil 33 import androidx.recyclerview.widget.ListAdapter 34 import androidx.recyclerview.widget.RecyclerView 35 import org.gnunet.gnunetmessenger.R 36 import org.gnunet.gnunetmessenger.model.ChatAccount 37 import org.gnunet.gnunetmessenger.util.AvatarStorageUtil 38 39 class AccountAdapter( 40 private val onClick: (ChatAccount) -> Unit 41 ) : ListAdapter<ChatAccount, AccountAdapter.AccountViewHolder>(DIFF) { 42 43 companion object { 44 val DIFF = object : DiffUtil.ItemCallback<ChatAccount>() { 45 override fun areItemsTheSame(oldItem: ChatAccount, newItem: ChatAccount) = 46 oldItem.name == newItem.name 47 48 override fun areContentsTheSame(oldItem: ChatAccount, newItem: ChatAccount) = 49 oldItem == newItem 50 } 51 } 52 53 override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): AccountViewHolder { 54 val view = LayoutInflater.from(parent.context) 55 .inflate(R.layout.item_account, parent, false) 56 return AccountViewHolder(view) 57 } 58 59 override fun onBindViewHolder(holder: AccountViewHolder, position: Int) { 60 holder.bind(getItem(position)) 61 } 62 63 inner class AccountViewHolder(view: View) : RecyclerView.ViewHolder(view) { 64 private val nameView: TextView = view.findViewById(R.id.account_name) 65 private val avatarView: ImageView = view.findViewById(R.id.account_avatar) 66 67 68 fun bind(account: ChatAccount) { 69 nameView.text = account.name 70 val context = itemView.context 71 val avatarBitmap = AvatarStorageUtil.loadAvatar(context, account.key) 72 73 if (avatarBitmap != null) { 74 avatarView.setImageBitmap(avatarBitmap) 75 } else { 76 avatarView.setImageResource(R.drawable.ic_account_circle) 77 } 78 79 itemView.setOnClickListener { onClick(account) } 80 } 81 } 82 }