package com.dmc.callcrm.ui import android.provider.CallLog 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.dmc.callcrm.R import com.dmc.callcrm.data.CallStats.CallItem import java.text.SimpleDateFormat import java.util.Date import java.util.Locale class CallAdapter( private var items: List, private val noteFor: (CallItem) -> String?, private val onCall: (CallItem) -> Unit, private val onWhatsapp: (CallItem) -> Unit, private val onPlay: (CallItem) -> Unit, private val onCopy: (CallItem) -> Unit, private val onMore: (CallItem, View) -> Unit, private val onNote: (CallItem) -> Unit ) : RecyclerView.Adapter() { private val tf = SimpleDateFormat("dd MMM, hh:mm a", Locale.getDefault()) class VH(v: View) : RecyclerView.ViewHolder(v) { val icon: TextView = v.findViewById(R.id.icon) val name: TextView = v.findViewById(R.id.name) val number: TextView = v.findViewById(R.id.number) val time: TextView = v.findViewById(R.id.time) val duration: TextView = v.findViewById(R.id.duration) val callBtn: ImageView = v.findViewById(R.id.callBtn) val whatsappBtn: ImageView = v.findViewById(R.id.whatsappBtn) val playBtn: ImageView = v.findViewById(R.id.playBtn) val copyBtn: ImageView = v.findViewById(R.id.copyBtn) val moreBtn: ImageView = v.findViewById(R.id.moreBtn) val notesRow: View = v.findViewById(R.id.notesRow) val notesText: TextView = v.findViewById(R.id.notesText) } fun submit(list: List) { items = list; notifyDataSetChanged() } /** Re-render just the notes row after a save, without reloading the whole list. */ fun refreshNotes() { notifyDataSetChanged() } override fun onCreateViewHolder(p: ViewGroup, vt: Int) = VH(LayoutInflater.from(p.context).inflate(R.layout.item_call, p, false)) override fun getItemCount() = items.size override fun onBindViewHolder(h: VH, pos: Int) { val it = items[pos] h.name.text = it.name ?: "Unknown" h.number.text = it.number h.time.text = tf.format(Date(it.dateMs)) h.duration.text = fmtDur(it.durationSec) val (glyph, color) = when (it.type) { CallLog.Calls.INCOMING_TYPE -> "↙" to 0xFF16A34A.toInt() CallLog.Calls.OUTGOING_TYPE -> "↗" to 0xFFF59E0B.toInt() CallLog.Calls.MISSED_TYPE -> "✕" to 0xFFDC2626.toInt() CallLog.Calls.REJECTED_TYPE -> "⊘" to 0xFF9333EA.toInt() else -> "•" to 0xFF64748B.toInt() } h.icon.text = glyph h.icon.setBackgroundColor(color) val note = noteFor(it) h.notesText.text = if (note.isNullOrBlank()) "Tap here to add notes" else note h.callBtn.setOnClickListener { onCall(it) } h.whatsappBtn.setOnClickListener { onWhatsapp(it) } h.playBtn.setOnClickListener { onPlay(it) } h.copyBtn.setOnClickListener { onCopy(it) } h.moreBtn.setOnClickListener { v -> onMore(it, v) } h.notesRow.setOnClickListener { onNote(it) } } private fun fmtDur(s: Int): String { val h = s / 3600; val m = (s % 3600) / 60; val sec = s % 60 return "%dh %dm %ds".format(h, m, sec) } }