WIP detail view

This commit is contained in:
2024-11-24 10:19:47 +01:00
parent a4b43ffb10
commit de75ed584b
5 changed files with 143 additions and 41 deletions

View File

@ -3,7 +3,6 @@ package it.danieleverducci.lunatracker.adapters
import android.content.Context
import android.icu.util.LocaleData
import android.icu.util.ULocale
import android.text.format.DateFormat
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
@ -11,7 +10,7 @@ import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import it.danieleverducci.lunatracker.entities.LunaEvent
import it.danieleverducci.lunatracker.R
import java.util.Date
import utils.DateUtils
class LunaEventRecyclerAdapter: RecyclerView.Adapter<LunaEventRecyclerAdapter.LunaEventVH> {
private val context: Context
@ -19,6 +18,7 @@ class LunaEventRecyclerAdapter: RecyclerView.Adapter<LunaEventRecyclerAdapter.Lu
val measurement_unit_liquid_base: String
val measurement_unit_weight_base: String
val measurement_unit_weight_tiny: String
var onItemClickListener: OnItemClickListener? = null
constructor(context: Context) {
this.context = context
@ -57,9 +57,11 @@ class LunaEventRecyclerAdapter: RecyclerView.Adapter<LunaEventRecyclerAdapter.Lu
position: Int
) {
val item = items.get(position)
// Colors
holder.root.setBackgroundResource(
if (position % 2 == 0) R.color.list_background_even else R.color.list_background_odd
)
// Contents
holder.type.text = item.getTypeEmoji(context)
holder.description.text = when(item.type) {
LunaEvent.TYPE_MEDICINE -> item.notes
@ -67,7 +69,7 @@ class LunaEventRecyclerAdapter: RecyclerView.Adapter<LunaEventRecyclerAdapter.Lu
LunaEvent.TYPE_CUSTOM -> item.notes
else -> item.getTypeDescription(context)
}
holder.time.text = formatTimeAgo(context, item.time)
holder.time.text = DateUtils.formatTimeAgo(context, item.time)
val qtyText = if ((item.quantity ?: 0) > 0) {
item.quantity.toString() + " " + when (item.type) {
LunaEvent.TYPE_BABY_BOTTLE -> measurement_unit_liquid_base
@ -79,49 +81,18 @@ class LunaEventRecyclerAdapter: RecyclerView.Adapter<LunaEventRecyclerAdapter.Lu
""
}
holder.quantity.text = qtyText
// Listeners
if (onItemClickListener != null) {
holder.root.setOnClickListener({
onItemClickListener?.onItemClick(item)
})
}
}
override fun getItemCount(): Int {
return items.size
}
/**
* Formats the provided unix timestamp in a string like "3 hours, 26 minutes ago)
*/
fun formatTimeAgo(context: Context, unixTime: Long): String {
val secondsDiff = (System.currentTimeMillis() / 1000) - unixTime
val minutesDiff = secondsDiff / 60
if (minutesDiff < 1)
return context.getString(R.string.now)
val hoursAgo = (secondsDiff / (60 * 60)).toInt()
val minutesAgo = (minutesDiff % 60).toInt()
if (hoursAgo > 24)
return DateFormat.getDateFormat(context).format(Date(unixTime*1000)) + "\n" +
DateFormat.getTimeFormat(context).format(Date(unixTime*1000))
var formattedTime = StringBuilder()
if (hoursAgo > 0) {
formattedTime.append(hoursAgo).append(" ")
if (hoursAgo.toInt() == 1)
formattedTime.append(context.getString(R.string.hour_ago))
else
formattedTime.append(context.getString(R.string.hours_ago))
}
if (minutesAgo > 0) {
if (formattedTime.isNotEmpty())
formattedTime.append(", ")
formattedTime.append(minutesAgo).append(" ")
if (minutesAgo.toInt() == 1)
formattedTime.append(context.getString(R.string.minute_ago))
else
formattedTime.append(context.getString(R.string.minutes_ago))
}
return formattedTime.toString()
}
class LunaEventVH: RecyclerView.ViewHolder {
val root: View
val type: TextView
@ -137,4 +108,8 @@ class LunaEventRecyclerAdapter: RecyclerView.Adapter<LunaEventRecyclerAdapter.Lu
time = v.findViewById<TextView>(R.id.time)
}
}
interface OnItemClickListener {
fun onItemClick(event: LunaEvent)
}
}