Weight button, auto list update, better time formatting

This commit is contained in:
2024-11-04 23:03:05 +01:00
parent aaa78139d6
commit 8e8966e906
8 changed files with 147 additions and 16 deletions

View File

@ -4,6 +4,7 @@ import android.os.Bundle
import android.os.Handler
import android.view.View
import android.widget.NumberPicker
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
@ -23,9 +24,15 @@ class MainActivity : AppCompatActivity() {
lateinit var logbook: Logbook
lateinit var adapter: LunaEventRecyclerAdapter
lateinit var recyclerView: RecyclerView
lateinit var handler: Handler
val updateListRunnable: Runnable = Runnable {
adapter.notifyDataSetChanged()
handler.postDelayed(updateListRunnable, 1000*30)
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
handler = Handler(mainLooper)
// Load data
logbook = Logbook.load(this)
@ -42,6 +49,7 @@ class MainActivity : AppCompatActivity() {
// Set listeners
findViewById<View>(R.id.button_bottle).setOnClickListener { askBabyBottleContent() }
findViewById<View>(R.id.button_scale).setOnClickListener { askWeightValue() }
findViewById<View>(R.id.button_nipple_left).setOnClickListener { logEvent(
LunaEvent(
LunaEventType.BREASTFEEDING_LEFT_NIPPLE
@ -74,6 +82,13 @@ class MainActivity : AppCompatActivity() {
// Update list dates
adapter.notifyDataSetChanged()
handler.postDelayed(updateListRunnable, 1000*30)
}
override fun onStop() {
handler.removeCallbacks(updateListRunnable)
super.onStop()
}
fun askBabyBottleContent() {
@ -96,6 +111,26 @@ class MainActivity : AppCompatActivity() {
alertDialog.show()
}
fun askWeightValue() {
// Show number picker dialog
val d = AlertDialog.Builder(this)
val dialogView = layoutInflater.inflate(R.layout.number_edit_dialog, null)
d.setTitle(R.string.log_weight_dialog_title)
d.setMessage(R.string.log_weight_dialog_description)
d.setView(dialogView)
val weightET = dialogView.findViewById<TextView>(R.id.dialog_number_edittext)
d.setPositiveButton(android.R.string.ok) { dialogInterface, i ->
val weight = weightET.text.toString().toIntOrNull()
if (weight != null)
logEvent(LunaEvent(LunaEventType.WEIGHT, weight))
else
Toast.makeText(this, R.string.toast_integer_error, Toast.LENGTH_SHORT).show()
}
d.setNegativeButton(android.R.string.cancel) { dialogInterface, i -> dialogInterface.dismiss() }
val alertDialog = d.create()
alertDialog.show()
}
fun logEvent(event: LunaEvent) {
adapter.items.add(0, event)
adapter.notifyItemInserted(0)

View File

@ -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

View File

@ -21,6 +21,7 @@ class LunaEvent(
val jo = JSONObject()
val type = when (type) {
LunaEventType.BABY_BOTTLE -> "BABY_BOTTLE"
LunaEventType.WEIGHT -> "SCALE"
LunaEventType.BREASTFEEDING_LEFT_NIPPLE -> "BREASTFEEDING_LEFT_NIPPLE"
LunaEventType.BREASTFEEDING_BOTH_NIPPLE -> "BREASTFEEDING_BOTH_NIPPLE"
LunaEventType.BREASTFEEDING_RIGHT_NIPPLE -> "BREASTFEEDING_RIGHT_NIPPLE"
@ -38,6 +39,7 @@ class LunaEvent(
fun fromJson(j: JSONObject): LunaEvent {
val type = when (j.getString("type")) {
"BABY_BOTTLE" -> LunaEventType.BABY_BOTTLE
"SCALE" -> LunaEventType.WEIGHT
"BREASTFEEDING_LEFT_NIPPLE" -> LunaEventType.BREASTFEEDING_LEFT_NIPPLE
"BREASTFEEDING_BOTH_NIPPLE" -> LunaEventType.BREASTFEEDING_BOTH_NIPPLE
"BREASTFEEDING_RIGHT_NIPPLE" -> LunaEventType.BREASTFEEDING_RIGHT_NIPPLE
@ -56,6 +58,7 @@ class LunaEvent(
enum class LunaEventType {
BABY_BOTTLE,
WEIGHT,
BREASTFEEDING_LEFT_NIPPLE,
BREASTFEEDING_BOTH_NIPPLE,
BREASTFEEDING_RIGHT_NIPPLE,