Weight button, auto list update, better time formatting
This commit is contained in:
@ -1,6 +1,7 @@
|
||||
package it.danieleverducci.lunatracker.adapters
|
||||
|
||||
import android.content.Context
|
||||
import android.text.format.DateFormat
|
||||
import android.text.format.DateUtils
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
@ -10,7 +11,6 @@ import androidx.recyclerview.widget.RecyclerView
|
||||
import it.danieleverducci.lunatracker.entities.LunaEvent
|
||||
import it.danieleverducci.lunatracker.entities.LunaEventType
|
||||
import it.danieleverducci.lunatracker.R
|
||||
import java.text.DateFormat
|
||||
import java.util.Date
|
||||
|
||||
class LunaEventRecyclerAdapter: RecyclerView.Adapter<LunaEventRecyclerAdapter.LunaEventVH> {
|
||||
@ -38,6 +38,7 @@ class LunaEventRecyclerAdapter: RecyclerView.Adapter<LunaEventRecyclerAdapter.Lu
|
||||
holder.type.text = context.getString(
|
||||
when (item.type) {
|
||||
LunaEventType.BABY_BOTTLE -> R.string.event_bottle_type
|
||||
LunaEventType.WEIGHT -> R.string.event_scale_type
|
||||
LunaEventType.BREASTFEEDING_LEFT_NIPPLE -> R.string.event_breastfeeding_left_type
|
||||
LunaEventType.BREASTFEEDING_BOTH_NIPPLE -> R.string.event_breastfeeding_both_type
|
||||
LunaEventType.BREASTFEEDING_RIGHT_NIPPLE -> R.string.event_breastfeeding_right_type
|
||||
@ -49,6 +50,7 @@ class LunaEventRecyclerAdapter: RecyclerView.Adapter<LunaEventRecyclerAdapter.Lu
|
||||
holder.description.text = context.getString(
|
||||
when (item.type) {
|
||||
LunaEventType.BABY_BOTTLE -> R.string.event_bottle_desc
|
||||
LunaEventType.WEIGHT -> R.string.event_scale_desc
|
||||
LunaEventType.BREASTFEEDING_LEFT_NIPPLE -> R.string.event_breastfeeding_left_desc
|
||||
LunaEventType.BREASTFEEDING_BOTH_NIPPLE -> R.string.event_breastfeeding_both_desc
|
||||
LunaEventType.BREASTFEEDING_RIGHT_NIPPLE -> R.string.event_breastfeeding_right_desc
|
||||
@ -58,18 +60,50 @@ class LunaEventRecyclerAdapter: RecyclerView.Adapter<LunaEventRecyclerAdapter.Lu
|
||||
}
|
||||
)
|
||||
holder.quantity.text = if ((item.quantity ?: 0) > 0) item.quantity.toString() else ""
|
||||
// holder.time.text = DateUtils.getRelativeTimeSpanString(item.time * 1000)
|
||||
holder.time.text = DateUtils.getRelativeTimeSpanString(
|
||||
item.time * 1000,
|
||||
System.currentTimeMillis(),
|
||||
DateUtils.MINUTE_IN_MILLIS,
|
||||
DateUtils.FORMAT_SHOW_DATE or DateUtils.FORMAT_NO_YEAR or DateUtils.FORMAT_ABBREV_MONTH or DateUtils.FORMAT_SHOW_TIME)
|
||||
holder.time.text = formatTimeAgo(context, item.time)
|
||||
}
|
||||
|
||||
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 (formattedTime.isNotEmpty())
|
||||
formattedTime.append(", ")
|
||||
if (minutesAgo > 0) {
|
||||
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 type: TextView
|
||||
val description: TextView
|
||||
|
Reference in New Issue
Block a user