Logbook event date edit

This commit is contained in:
Daniele Verducci 2025-04-23 08:58:32 +02:00
parent 4c5c7bcf1a
commit 0d3be20e1e
6 changed files with 47 additions and 37 deletions

View File

@ -21,7 +21,6 @@ import android.widget.Toast
import androidx.appcompat.app.AlertDialog import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat import androidx.core.content.ContextCompat
import androidx.core.view.children
import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView import androidx.recyclerview.widget.RecyclerView
import com.google.android.material.progressindicator.LinearProgressIndicator import com.google.android.material.progressindicator.LinearProgressIndicator
@ -53,14 +52,14 @@ class MainActivity : AppCompatActivity() {
} }
var logbook: Logbook? = null var logbook: Logbook? = null
lateinit var adapter: LunaEventRecyclerAdapter var pauseLogbookUpdate = false
lateinit var progressIndicator: LinearProgressIndicator lateinit var progressIndicator: LinearProgressIndicator
lateinit var buttonsContainer: ViewGroup lateinit var buttonsContainer: ViewGroup
lateinit var recyclerView: RecyclerView lateinit var recyclerView: RecyclerView
lateinit var handler: Handler lateinit var handler: Handler
var savingEvent = false var savingEvent = false
val updateListRunnable: Runnable = Runnable { val updateListRunnable: Runnable = Runnable {
if (logbook != null) if (logbook != null && !pauseLogbookUpdate)
loadLogbook(logbook!!.name) loadLogbook(logbook!!.name)
handler.postDelayed(updateListRunnable, 1000*60) handler.postDelayed(updateListRunnable, 1000*60)
} }
@ -71,12 +70,6 @@ class MainActivity : AppCompatActivity() {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
handler = Handler(mainLooper) handler = Handler(mainLooper)
adapter = LunaEventRecyclerAdapter(this)
adapter.onItemClickListener = object: LunaEventRecyclerAdapter.OnItemClickListener{
override fun onItemClick(event: LunaEvent) {
showEventDetailDialog(event)
}
}
// Show view // Show view
setContentView(R.layout.activity_main) setContentView(R.layout.activity_main)
@ -85,7 +78,6 @@ class MainActivity : AppCompatActivity() {
buttonsContainer = findViewById<ViewGroup>(R.id.buttons_container) buttonsContainer = findViewById<ViewGroup>(R.id.buttons_container)
recyclerView = findViewById<RecyclerView>(R.id.list_events) recyclerView = findViewById<RecyclerView>(R.id.list_events)
recyclerView.setLayoutManager(LinearLayoutManager(applicationContext)) recyclerView.setLayoutManager(LinearLayoutManager(applicationContext))
recyclerView.adapter = adapter
// Set listeners // Set listeners
findViewById<View>(R.id.logbooks_add_button).setOnClickListener { showAddLogbookDialog(true) } findViewById<View>(R.id.logbooks_add_button).setOnClickListener { showAddLogbookDialog(true) }
@ -135,6 +127,16 @@ class MainActivity : AppCompatActivity() {
}) })
} }
private fun setListAdapter(items: ArrayList<LunaEvent>) {
val adapter = LunaEventRecyclerAdapter(this, items)
adapter.onItemClickListener = object: LunaEventRecyclerAdapter.OnItemClickListener{
override fun onItemClick(event: LunaEvent) {
showEventDetailDialog(event)
}
}
recyclerView.adapter = adapter
}
fun showSettings() { fun showSettings() {
val i = Intent(this, SettingsActivity::class.java) val i = Intent(this, SettingsActivity::class.java)
startActivity(i) startActivity(i)
@ -145,9 +147,7 @@ class MainActivity : AppCompatActivity() {
if (logbook == null) if (logbook == null)
Log.w(TAG, "showLogbook(): logbook is null!") Log.w(TAG, "showLogbook(): logbook is null!")
adapter.items.clear() setListAdapter(logbook?.logs ?: arrayListOf())
adapter.items.addAll(logbook?.logs ?: listOf())
adapter.notifyDataSetChanged()
} }
override fun onStart() { override fun onStart() {
@ -169,7 +169,7 @@ class MainActivity : AppCompatActivity() {
} }
// Update list dates // Update list dates
adapter.notifyDataSetChanged() recyclerView.adapter?.notifyDataSetChanged()
if (logbook != null) { if (logbook != null) {
// Already running: reload data for currently selected logbook // Already running: reload data for currently selected logbook
@ -303,6 +303,8 @@ class MainActivity : AppCompatActivity() {
} }
fun showEventDetailDialog(event: LunaEvent) { fun showEventDetailDialog(event: LunaEvent) {
// Do not update list while the detail is shown, to avoid changing the object below while it is changed by the user
pauseLogbookUpdate = true
val dateFormat = DateFormat.getDateTimeInstance(); val dateFormat = DateFormat.getDateTimeInstance();
val d = AlertDialog.Builder(this) val d = AlertDialog.Builder(this)
d.setTitle(R.string.dialog_event_detail_title) d.setTitle(R.string.dialog_event_detail_title)
@ -331,22 +333,27 @@ class MainActivity : AppCompatActivity() {
val pickedDateTime = Calendar.getInstance() val pickedDateTime = Calendar.getInstance()
pickedDateTime.set(year, month, day, hour, minute) pickedDateTime.set(year, month, day, hour, minute)
currentDateTime.time = pickedDateTime.time currentDateTime.time = pickedDateTime.time
dateTextView.text = String.format(getString(R.string.dialog_event_detail_datetime_icon), dateFormat.format(currentDateTime.time))
// Save event and move it to the right position in the logbook
event.time = currentDateTime.time.time / 1000 // Seconds since epoch
logbook?.sort()
recyclerView.adapter?.notifyDataSetChanged()
saveLogbook()
}, startHour, startMinute, false).show() }, startHour, startMinute, false).show()
}, startYear, startMonth, startDay).show() }, startYear, startMonth, startDay).show()
}) })
d.setView(dialogView) d.setView(dialogView)
d.setPositiveButton(R.string.dialog_event_detail_save_button) { dialogInterface, i -> { d.setPositiveButton(R.string.dialog_event_detail_close_button) { dialogInterface, i -> dialogInterface.dismiss() }
// Save event
event.time = currentDateTime.time.time / 1000 // Seconds since epoch
// TODO: move event at the correct logbook position
saveLogbook()
} }
d.setNegativeButton(R.string.dialog_event_detail_cancel_button) { dialogInterface, i -> dialogInterface.dismiss() }
d.setNeutralButton(R.string.dialog_event_detail_delete_button) { dialogInterface, i -> deleteEvent(event) } d.setNeutralButton(R.string.dialog_event_detail_delete_button) { dialogInterface, i -> deleteEvent(event) }
val alertDialog = d.create() val alertDialog = d.create()
alertDialog.show() alertDialog.show()
alertDialog.getButton(DialogInterface.BUTTON_NEUTRAL).setTextColor(ContextCompat.getColor(this, R.color.danger)) alertDialog.getButton(DialogInterface.BUTTON_NEUTRAL).setTextColor(ContextCompat.getColor(this, R.color.danger))
alertDialog.setOnDismissListener({
// Resume logbook update
pauseLogbookUpdate = false
})
} }
fun showAddLogbookDialog(requestedByUser: Boolean) { fun showAddLogbookDialog(requestedByUser: Boolean) {
@ -397,8 +404,7 @@ class MainActivity : AppCompatActivity() {
id: Long id: Long
) { ) {
// Changed logbook: empty list // Changed logbook: empty list
adapter.items.clear() setListAdapter(arrayListOf())
adapter.notifyDataSetChanged()
// Load logbook // Load logbook
loadLogbook(logbooksNames.get(position)) loadLogbook(logbooksNames.get(position))
} }
@ -568,12 +574,11 @@ class MainActivity : AppCompatActivity() {
fun logEvent(event: LunaEvent) { fun logEvent(event: LunaEvent) {
savingEvent(true) savingEvent(true)
adapter.items.add(0, event)
adapter.notifyItemInserted(0)
recyclerView.smoothScrollToPosition(0)
setLoading(true) setLoading(true)
logbook?.logs?.add(0, event) logbook?.logs?.add(0, event)
recyclerView.adapter?.notifyItemInserted(0)
recyclerView.smoothScrollToPosition(0)
saveLogbook(event) saveLogbook(event)
// Check logbook size to avoid OOM errors // Check logbook size to avoid OOM errors
@ -585,12 +590,11 @@ class MainActivity : AppCompatActivity() {
fun deleteEvent(event: LunaEvent) { fun deleteEvent(event: LunaEvent) {
// Update view // Update view
savingEvent(true) savingEvent(true)
adapter.items.remove(event)
adapter.notifyDataSetChanged()
// Update data // Update data
setLoading(true) setLoading(true)
logbook?.logs?.remove(event) logbook?.logs?.remove(event)
recyclerView.adapter?.notifyDataSetChanged()
saveLogbook() saveLogbook()
} }
@ -673,8 +677,7 @@ class MainActivity : AppCompatActivity() {
setLoading(false) setLoading(false)
Toast.makeText(this@MainActivity, R.string.toast_event_add_error, Toast.LENGTH_SHORT).show() Toast.makeText(this@MainActivity, R.string.toast_event_add_error, Toast.LENGTH_SHORT).show()
adapter.items.remove(event) recyclerView.adapter?.notifyDataSetChanged()
adapter.notifyDataSetChanged()
savingEvent(false) savingEvent(false)
}) })
} }

View File

@ -14,13 +14,14 @@ import utils.NumericUtils
class LunaEventRecyclerAdapter: RecyclerView.Adapter<LunaEventRecyclerAdapter.LunaEventVH> { class LunaEventRecyclerAdapter: RecyclerView.Adapter<LunaEventRecyclerAdapter.LunaEventVH> {
private val context: Context private val context: Context
val items = ArrayList<LunaEvent>() private val items: ArrayList<LunaEvent>
val numericUtils: NumericUtils val numericUtils: NumericUtils
var onItemClickListener: OnItemClickListener? = null var onItemClickListener: OnItemClickListener? = null
val layoutRes: Int val layoutRes: Int
constructor(context: Context) { constructor(context: Context, items: ArrayList<LunaEvent>) {
this.context = context this.context = context
this.items = items
this.numericUtils = NumericUtils(context) this.numericUtils = NumericUtils(context)
val fontScale = context.resources.configuration.fontScale val fontScale = context.resources.configuration.fontScale

View File

@ -16,4 +16,8 @@ class Logbook(val name: String) {
fun trim() { fun trim() {
logs.subList(MAX_SAFE_LOGBOOK_SIZE/2, logs.size).clear() logs.subList(MAX_SAFE_LOGBOOK_SIZE/2, logs.size).clear()
} }
fun sort() {
logs.sortDescending()
}
} }

View File

@ -11,7 +11,7 @@ import java.util.Date
* allow expandability and backwards compatibility (if a field is added in a * allow expandability and backwards compatibility (if a field is added in a
* release, it is simply ignored by previous ones). * release, it is simply ignored by previous ones).
*/ */
class LunaEvent { class LunaEvent: Comparable<LunaEvent> {
companion object { companion object {
val TYPE_BABY_BOTTLE = "BABY_BOTTLE" val TYPE_BABY_BOTTLE = "BABY_BOTTLE"
@ -130,4 +130,8 @@ class LunaEvent {
override fun toString(): String { override fun toString(): String {
return "${type} qty: $quantity time: ${Date(time * 1000)}" return "${type} qty: $quantity time: ${Date(time * 1000)}"
} }
override fun compareTo(other: LunaEvent): Int {
return (this.time - other.time).toInt()
}
} }

View File

@ -83,8 +83,7 @@
<string name="log_notes_dialog_note_hint">Inserisci le note</string> <string name="log_notes_dialog_note_hint">Inserisci le note</string>
<string name="dialog_event_detail_title">Dettaglio evento</string> <string name="dialog_event_detail_title">Dettaglio evento</string>
<string name="dialog_event_detail_save_button">Salva</string> <string name="dialog_event_detail_close_button">OK</string>
<string name="dialog_event_detail_cancel_button">Annulla</string>
<string name="dialog_event_detail_delete_button">Elimina</string> <string name="dialog_event_detail_delete_button">Elimina</string>
<string name="dialog_add_logbook_title">Aggiungi diario</string> <string name="dialog_add_logbook_title">Aggiungi diario</string>

View File

@ -108,8 +108,7 @@
<string name="dialog_event_detail_title">Event detail</string> <string name="dialog_event_detail_title">Event detail</string>
<string name="dialog_event_detail_datetime_icon" translatable="false">🕒 %s1</string> <string name="dialog_event_detail_datetime_icon" translatable="false">🕒 %s1</string>
<string name="dialog_event_detail_save_button">Save</string> <string name="dialog_event_detail_close_button">OK</string>
<string name="dialog_event_detail_cancel_button">Cancel</string>
<string name="dialog_event_detail_delete_button">Delete</string> <string name="dialog_event_detail_delete_button">Delete</string>
<string name="dialog_add_logbook_title">Add logbook</string> <string name="dialog_add_logbook_title">Add logbook</string>