package sushi.hardcore.aira.adapters import android.content.Context import android.graphics.Color import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import android.widget.* import androidx.core.content.ContextCompat import sushi.hardcore.aira.R import sushi.hardcore.aira.widgets.TextAvatar class SessionAdapter(private val context: Context): BaseAdapter() { private val sessions = mutableListOf() private val inflater: LayoutInflater = LayoutInflater.from(context) val selectedItems = mutableListOf() override fun getCount(): Int { return sessions.size } override fun getItem(position: Int): Session { return sessions[position] } override fun getItemId(position: Int): Long { return 0 } override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View { val view: View = convertView ?: inflater.inflate(R.layout.adapter_session, parent, false) val currentSession = getItem(position) view.findViewById(R.id.text_name).apply { view.findViewById(R.id.text_avatar).setLetterFrom(if (currentSession.name == null) { text = currentSession.ip setTextColor(Color.RED) "?" } else { text = currentSession.name setTextColor(Color.WHITE) currentSession.name!! }) } view.findViewById(R.id.image_trust_level).apply { if (currentSession.isVerified) { setImageResource(R.drawable.ic_verified) } else if (!currentSession.isContact) { setImageResource(R.drawable.ic_warning) } else { setImageDrawable(null) } } view.findViewById(R.id.marker_not_seen).visibility = if (currentSession.seen) { View.GONE } else { View.VISIBLE } view.findViewById(R.id.image_arrow).setColorFilter(ContextCompat.getColor(context, if (currentSession.seen) { R.color.sessionArrow } else { R.color.secondary })) view.setBackgroundColor(ContextCompat.getColor(context, if (selectedItems.contains(position)) { R.color.itemSelected } else { R.color.sessionBackground })) return view } fun add(session: Session) { sessions.add(session) notifyDataSetChanged() } private fun getSessionById(sessionId: Int): Session? { for (session in sessions){ if (session.sessionId == sessionId) { return session } } return null } fun remove(sessionId: Int): Session? { getSessionById(sessionId)?.let { sessions.remove(it) notifyDataSetChanged() return it } return null } fun setName(sessionId: Int, name: String) { getSessionById(sessionId)?.let { it.name = name notifyDataSetChanged() } } fun setSeen(sessionId: Int, seen: Boolean) { getSessionById(sessionId)?.let { it.seen = seen notifyDataSetChanged() } } fun reset() { sessions.clear() notifyDataSetChanged() } fun onSelectionChanged(position: Int) { if (!selectedItems.contains(position)) { selectedItems.add(position) } else { selectedItems.remove(position) } notifyDataSetChanged() } fun unSelectAll() { selectedItems.clear() notifyDataSetChanged() } fun getSelectedSessionIds(): List { return selectedItems.map { position -> sessions[position].sessionId } } }