First working version

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

View File

@ -50,6 +50,7 @@ dependencies {
implementation(libs.androidx.ui.tooling.preview)
implementation(libs.androidx.material3)
implementation(libs.androidx.appcompat)
implementation(libs.androidx.recyclerview)
testImplementation(libs.junit)
androidTestImplementation(libs.androidx.junit)
androidTestImplementation(libs.androidx.espresso.core)

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)
}
}
}

View File

@ -26,7 +26,7 @@
android:background="@drawable/button_background"
android:gravity="center_horizontal"
android:textSize="50sp"
android:text="🍼"/>
android:text="@string/event_bottle_type"/>
<LinearLayout
android:layout_width="match_parent"
@ -101,7 +101,7 @@
android:text="Diario di bordo"
android:layout_marginTop="30dp"/>
<ListView
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/list_events"
android:layout_width="match_parent"
android:layout_height="0dp"

View File

@ -0,0 +1,38 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical">
<TextView
android:id="@+id/type"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:textSize="28sp"
android:text="Type"/>
<TextView
android:id="@+id/description"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="Description"/>
<TextView
android:id="@+id/quantity"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Qty"/>
<TextView
android:id="@+id/time"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:textStyle="bold"
android:text="Time"/>
</LinearLayout>

View File

@ -5,4 +5,21 @@
<string name="log_bottle_dialog_title">Biberon</string>
<string name="log_bottle_dialog_description">Inserisci la quantità contenuta nel biberon</string>
<string name="event_bottle_type">🍼</string>
<string name="event_breastfeeding_left_type">🤱 ←</string>
<string name="event_breastfeeding_both_type">🤱 ↔</string>
<string name="event_breastfeeding_right_type">🤱 →</string>
<string name="event_diaperchange_poo_type">🚼 💩</string>
<string name="event_diaperchange_pee_type">🚼 💧</string>
<string name="event_unknown_type">-</string>
<string name="event_bottle_desc">Biberon</string>
<string name="event_breastfeeding_left_desc">Allatt. al seno (sx)</string>
<string name="event_breastfeeding_both_desc">Allatt. al seno</string>
<string name="event_breastfeeding_right_desc">Allatt. al seno (dx)</string>
<string name="event_diaperchange_poo_desc">Cambio (con cacca)</string>
<string name="event_diaperchange_pee_desc">Cambio (con pipì)</string>
<string name="event_unknown_desc"></string>
</resources>

View File

@ -9,9 +9,11 @@ lifecycleRuntimeKtx = "2.6.1"
activityCompose = "1.8.0"
composeBom = "2024.04.01"
appcompat = "1.7.0"
recyclerview = "1.3.2"
[libraries]
androidx-core-ktx = { group = "androidx.core", name = "core-ktx", version.ref = "coreKtx" }
androidx-recyclerview = { module = "androidx.recyclerview:recyclerview", version.ref = "recyclerview" }
junit = { group = "junit", name = "junit", version.ref = "junit" }
androidx-junit = { group = "androidx.test.ext", name = "junit", version.ref = "junitVersion" }
androidx-espresso-core = { group = "androidx.test.espresso", name = "espresso-core", version.ref = "espressoCore" }