package com.dmc.callcrm.ui import android.provider.CallLog import android.view.LayoutInflater import android.view.View import android.view.ViewGroup 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 onCall: (String) -> 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: TextView = v.findViewById(R.id.callBtn) } fun submit(list: List) { items = list; 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) h.callBtn.setOnClickListener { onCall(it.number) } } 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) } }