Auto list update, scroll list on new element, toast on new element

This commit is contained in:
Daniele Verducci 2024-11-04 19:11:47 +01:00
parent 96e2410753
commit aaa78139d6
3 changed files with 24 additions and 2 deletions

View File

@ -1,8 +1,10 @@
package it.danieleverducci.lunatracker
import android.os.Bundle
import android.os.Handler
import android.view.View
import android.widget.NumberPicker
import android.widget.Toast
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
@ -11,6 +13,7 @@ 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 kotlinx.coroutines.Runnable
class MainActivity : AppCompatActivity() {
companion object {
@ -19,6 +22,7 @@ class MainActivity : AppCompatActivity() {
lateinit var logbook: Logbook
lateinit var adapter: LunaEventRecyclerAdapter
lateinit var recyclerView: RecyclerView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
@ -30,7 +34,7 @@ class MainActivity : AppCompatActivity() {
setContentView(R.layout.activity_main)
// Show logbook
val recyclerView = findViewById<RecyclerView>(R.id.list_events)
recyclerView = findViewById<RecyclerView>(R.id.list_events)
recyclerView.setLayoutManager(LinearLayoutManager(this))
adapter = LunaEventRecyclerAdapter(this)
adapter.items.addAll(logbook.logs)
@ -65,6 +69,13 @@ class MainActivity : AppCompatActivity() {
) }
}
override fun onStart() {
super.onStart()
// Update list dates
adapter.notifyDataSetChanged()
}
fun askBabyBottleContent() {
// Show number picker dialog
val d = AlertDialog.Builder(this)
@ -88,8 +99,12 @@ class MainActivity : AppCompatActivity() {
fun logEvent(event: LunaEvent) {
adapter.items.add(0, event)
adapter.notifyItemInserted(0)
recyclerView.smoothScrollToPosition(0)
logbook.logs.add(0, event)
logbook.save(this)
Toast.makeText(this, R.string.toast_event_added, Toast.LENGTH_SHORT).show()
}
}

View File

@ -58,7 +58,12 @@ 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)
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)
}
override fun getItemCount(): Int {

View File

@ -23,4 +23,6 @@
<string name="event_diaperchange_pee_desc">Cambio (con pipì)</string>
<string name="event_unknown_desc"></string>
<string name="toast_event_added">Evento aggiunto!</string>
</resources>