45 lines
No EOL
1.6 KiB
Kotlin
45 lines
No EOL
1.6 KiB
Kotlin
package com.mistral.chat.ui
|
|
|
|
import android.view.LayoutInflater
|
|
import android.view.View
|
|
import android.view.ViewGroup
|
|
import android.widget.ImageView
|
|
import android.widget.TextView
|
|
import androidx.recyclerview.widget.RecyclerView
|
|
import com.mistral.chat.R
|
|
import com.mistral.chat.data.Session
|
|
|
|
class SessionsAdapter(
|
|
private val sessions: List<Session>,
|
|
private val getCurrentSessionId: () -> Long?,
|
|
private val onSessionClick: (Session) -> Unit,
|
|
private val onSessionLongClick: (Session) -> Unit
|
|
) : RecyclerView.Adapter<SessionsAdapter.ViewHolder>() {
|
|
|
|
class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
|
|
val title: TextView = view.findViewById(R.id.sessionTitle)
|
|
val checkmark: ImageView = view.findViewById(R.id.sessionCheckmark)
|
|
}
|
|
|
|
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
|
|
val view = LayoutInflater.from(parent.context)
|
|
.inflate(R.layout.item_session, parent, false)
|
|
return ViewHolder(view)
|
|
}
|
|
|
|
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
|
|
val session = sessions[position]
|
|
val currentId = getCurrentSessionId()
|
|
holder.title.text = session.title
|
|
holder.title.alpha = if (session.id == currentId) 1.0f else 0.7f
|
|
holder.checkmark.visibility = if (session.id == currentId) View.VISIBLE else View.GONE
|
|
|
|
holder.itemView.setOnClickListener { onSessionClick(session) }
|
|
holder.itemView.setOnLongClickListener {
|
|
onSessionLongClick(session)
|
|
true
|
|
}
|
|
}
|
|
|
|
override fun getItemCount(): Int = sessions.size
|
|
} |