First working version

This commit is contained in:
2024-11-03 10:26:08 +01:00
parent 2948e059de
commit 596f76462f
7 changed files with 158 additions and 5 deletions

View File

@ -1,15 +1,16 @@
package it.danieleverducci.lunatracker
import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.NumberPicker
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import it.danieleverducci.lunatracker.adapters.LunaEventRecyclerAdapter
import it.danieleverducci.lunatracker.entities.Logbook
import it.danieleverducci.lunatracker.entities.LunaEvent
import it.danieleverducci.lunatracker.entities.LunaEventType
import java.io.File
class MainActivity : AppCompatActivity() {
companion object {
@ -17,14 +18,25 @@ class MainActivity : AppCompatActivity() {
}
lateinit var logbook: Logbook
lateinit var adapter: LunaEventRecyclerAdapter
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// Load data
logbook = Logbook.load(this)
// Show view
setContentView(R.layout.activity_main)
// Show logbook
val recyclerView = findViewById<RecyclerView>(R.id.list_events)
recyclerView.setLayoutManager(LinearLayoutManager(this))
adapter = LunaEventRecyclerAdapter(this)
adapter.items.addAll(logbook.logs)
recyclerView.adapter = adapter
// Set listeners
findViewById<View>(R.id.button_bottle).setOnClickListener { askBabyBottleContent() }
findViewById<View>(R.id.button_nipple_left).setOnClickListener { logEvent(
LunaEvent(
@ -74,7 +86,9 @@ class MainActivity : AppCompatActivity() {
}
fun logEvent(event: LunaEvent) {
logbook.logs.add(event)
adapter.items.add(0, event)
adapter.notifyItemInserted(0)
logbook.logs.add(0, event)
logbook.save(this)
}

View File

@ -0,0 +1,81 @@
package it.danieleverducci.lunatracker.adapters
import android.content.Context
import android.text.format.DateUtils
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
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> {
private val context: Context
val items = ArrayList<LunaEvent>()
constructor(context: Context) {
this.context = context
}
override fun onCreateViewHolder(
parent: ViewGroup,
viewType: Int
): LunaEventVH {
val view = LayoutInflater.from(context).inflate(R.layout.row_luna_event, parent, false)
return LunaEventVH(view)
}
override fun onBindViewHolder(
holder: LunaEventVH,
position: Int
) {
val item = items.get(position)
holder.type.text = context.getString(
when (item.type) {
LunaEventType.BABY_BOTTLE -> R.string.event_bottle_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
LunaEventType.DIAPERCHANGE_POO -> R.string.event_diaperchange_poo_type
LunaEventType.DIAPERCHANGE_PEE -> R.string.event_diaperchange_pee_type
else -> R.string.event_unknown_type
}
)
holder.description.text = context.getString(
when (item.type) {
LunaEventType.BABY_BOTTLE -> R.string.event_bottle_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
LunaEventType.DIAPERCHANGE_POO -> R.string.event_diaperchange_poo_desc
LunaEventType.DIAPERCHANGE_PEE -> R.string.event_diaperchange_pee_desc
else -> R.string.event_unknown_desc
}
)
holder.quantity.text = if ((item.quantity ?: 0) > 0) item.quantity.toString() else ""
holder.time.text = DateUtils.getRelativeTimeSpanString(item.time * 1000)
}
override fun getItemCount(): Int {
return items.size
}
class LunaEventVH: RecyclerView.ViewHolder {
val type: TextView
val description: TextView
val quantity: TextView
val time: TextView
constructor(v: View) : super(v) {
type = v.findViewById<TextView>(R.id.type)
description = v.findViewById<TextView>(R.id.description)
quantity = v.findViewById<TextView>(R.id.quantity)
time = v.findViewById<TextView>(R.id.time)
}
}
}