4 Commits

Author SHA1 Message Date
8e4a5b89c0 NumericUtils: remove possible trailing whitespace 2025-12-12 15:27:13 +01:00
469666cd0f MainActivity: use System.currentTimeMillis
A little better way to get the ms since epoch.
2025-12-12 15:27:13 +01:00
0e980a8c68 MainActivity: do not switch logbook on reload 2025-12-12 15:27:13 +01:00
b1fc3ce890 LunaEvent: reorganize event text getters
Use method names that better reflect
the use of the returned text.
2025-12-12 15:27:08 +01:00
4 changed files with 33 additions and 26 deletions

View File

@@ -108,9 +108,13 @@ class MainActivity : AppCompatActivity() {
loadLogbookList() loadLogbookList()
} }
findViewById<View>(R.id.button_sync).setOnClickListener { findViewById<View>(R.id.button_sync).setOnClickListener {
if (logbook != null) {
loadLogbook(logbook!!.name)
} else {
loadLogbookList() loadLogbookList()
} }
} }
}
private fun setListAdapter(items: ArrayList<LunaEvent>) { private fun setListAdapter(items: ArrayList<LunaEvent>) {
val adapter = LunaEventRecyclerAdapter(this, items) val adapter = LunaEventRecyclerAdapter(this, items)
@@ -183,7 +187,7 @@ class MainActivity : AppCompatActivity() {
fun show(vararg tvs: TextView) { fun show(vararg tvs: TextView) {
for (tv in tvs) { for (tv in tvs) {
val type = sortedEventTypes[showCounter] val type = sortedEventTypes[showCounter]
tv.text = LunaEvent.getTypeEmoji(applicationContext, type) tv.text = LunaEvent.getHeaderEmoji(applicationContext, type)
tv.setOnClickListener { showCreateDialog(type) } tv.setOnClickListener { showCreateDialog(type) }
tv.visibility = View.VISIBLE tv.visibility = View.VISIBLE
// show parent row // show parent row
@@ -260,7 +264,7 @@ class MainActivity : AppCompatActivity() {
fun askBabyBottleContent(event: LunaEvent, showTime: Boolean, onPositive: () -> Unit) { fun askBabyBottleContent(event: LunaEvent, showTime: Boolean, onPositive: () -> Unit) {
val d = AlertDialog.Builder(this) val d = AlertDialog.Builder(this)
val dialogView = layoutInflater.inflate(R.layout.dialog_edit_bottle, null) val dialogView = layoutInflater.inflate(R.layout.dialog_edit_bottle, null)
d.setTitle(event.getTypeDescription(this)) d.setTitle(event.getDialogTitle(this))
d.setMessage(event.getDialogMessage(this)) d.setMessage(event.getDialogMessage(this))
d.setView(dialogView) d.setView(dialogView)
@@ -302,7 +306,7 @@ class MainActivity : AppCompatActivity() {
// Show number picker dialog // Show number picker dialog
val d = AlertDialog.Builder(this) val d = AlertDialog.Builder(this)
val dialogView = layoutInflater.inflate(R.layout.dialog_edit_weight, null) val dialogView = layoutInflater.inflate(R.layout.dialog_edit_weight, null)
d.setTitle(event.getTypeDescription(this)) d.setTitle(event.getDialogTitle(this))
d.setMessage(event.getDialogMessage(this)) d.setMessage(event.getDialogMessage(this))
d.setView(dialogView) d.setView(dialogView)
@@ -346,7 +350,7 @@ class MainActivity : AppCompatActivity() {
// Show number picker dialog // Show number picker dialog
val d = AlertDialog.Builder(this) val d = AlertDialog.Builder(this)
val dialogView = layoutInflater.inflate(R.layout.dialog_edit_temperature, null) val dialogView = layoutInflater.inflate(R.layout.dialog_edit_temperature, null)
d.setTitle(event.getTypeDescription(this)) d.setTitle(event.getDialogTitle(this))
d.setMessage(event.getDialogMessage(this)) d.setMessage(event.getDialogMessage(this))
d.setView(dialogView) d.setView(dialogView)
@@ -434,7 +438,7 @@ class MainActivity : AppCompatActivity() {
fun askSleepValue(event: LunaEvent, onPositive: () -> Unit) { fun askSleepValue(event: LunaEvent, onPositive: () -> Unit) {
val d = AlertDialog.Builder(this) val d = AlertDialog.Builder(this)
val dialogView = layoutInflater.inflate(R.layout.dialog_edit_duration, null) val dialogView = layoutInflater.inflate(R.layout.dialog_edit_duration, null)
d.setTitle(event.getTypeDescription(this)) d.setTitle(event.getDialogTitle(this))
d.setMessage(event.getDialogMessage(this)) d.setMessage(event.getDialogMessage(this))
d.setView(dialogView) d.setView(dialogView)
@@ -450,7 +454,7 @@ class MainActivity : AppCompatActivity() {
var duration = event.quantity var duration = event.quantity
fun isValidTime(timeSeconds: Long, durationSeconds: Int): Boolean { fun isValidTime(timeSeconds: Long, durationSeconds: Int): Boolean {
val now = Calendar.getInstance().time.time / 1000 val now = System.currentTimeMillis() / 1000
return (timeSeconds + durationSeconds) <= now && durationSeconds < (12 * 60 * 60) return (timeSeconds + durationSeconds) <= now && durationSeconds < (12 * 60 * 60)
} }
@@ -484,7 +488,7 @@ class MainActivity : AppCompatActivity() {
durationPlus5Button.setOnClickListener { adjust(5) } durationPlus5Button.setOnClickListener { adjust(5) }
durationNowButton.setOnClickListener { durationNowButton.setOnClickListener {
val now = Calendar.getInstance().time.time / 1000 val now = System.currentTimeMillis() / 1000
val start = pickedDateTime.time.time / 1000 val start = pickedDateTime.time.time / 1000
if (now > start) { if (now > start) {
duration = (now - start).toInt() duration = (now - start).toInt()
@@ -521,7 +525,7 @@ class MainActivity : AppCompatActivity() {
fun askAmountValue(event: LunaEvent, showTime: Boolean, onPositive: () -> Unit) { fun askAmountValue(event: LunaEvent, showTime: Boolean, onPositive: () -> Unit) {
val d = AlertDialog.Builder(this) val d = AlertDialog.Builder(this)
val dialogView = layoutInflater.inflate(R.layout.dialog_edit_amount, null) val dialogView = layoutInflater.inflate(R.layout.dialog_edit_amount, null)
d.setTitle(event.getTypeDescription(this)) d.setTitle(event.getDialogTitle(this))
d.setMessage(event.getDialogMessage(this)) d.setMessage(event.getDialogMessage(this))
d.setView(dialogView) d.setView(dialogView)
@@ -563,7 +567,7 @@ class MainActivity : AppCompatActivity() {
fun askDateValue(event: LunaEvent, showTime: Boolean, onPositive: () -> Unit) { fun askDateValue(event: LunaEvent, showTime: Boolean, onPositive: () -> Unit) {
val d = AlertDialog.Builder(this) val d = AlertDialog.Builder(this)
val dialogView = layoutInflater.inflate(R.layout.dialog_edit_plain, null) val dialogView = layoutInflater.inflate(R.layout.dialog_edit_plain, null)
d.setTitle(event.getTypeDescription(this)) d.setTitle(event.getDialogTitle(this))
d.setMessage(event.getDialogMessage(this)) d.setMessage(event.getDialogMessage(this))
d.setView(dialogView) d.setView(dialogView)
@@ -596,7 +600,7 @@ class MainActivity : AppCompatActivity() {
val d = AlertDialog.Builder(this) val d = AlertDialog.Builder(this)
val dialogView = layoutInflater.inflate(R.layout.dialog_edit_notes, null) val dialogView = layoutInflater.inflate(R.layout.dialog_edit_notes, null)
d.setTitle(event.getTypeDescription(this)) d.setTitle(event.getDialogTitle(this))
d.setMessage(event.getDialogMessage(this)) d.setMessage(event.getDialogMessage(this))
d.setView(dialogView) d.setView(dialogView)
val notesET = dialogView.findViewById<EditText>(R.id.notes_edittext) val notesET = dialogView.findViewById<EditText>(R.id.notes_edittext)
@@ -774,8 +778,8 @@ class MainActivity : AppCompatActivity() {
val quantityTextView = dialogView.findViewById<TextView>(R.id.dialog_event_detail_type_quantity) val quantityTextView = dialogView.findViewById<TextView>(R.id.dialog_event_detail_type_quantity)
val notesTextView = dialogView.findViewById<TextView>(R.id.dialog_event_detail_type_notes) val notesTextView = dialogView.findViewById<TextView>(R.id.dialog_event_detail_type_notes)
emojiTextView.text = event.getTypeEmoji(this) emojiTextView.text = event.getHeaderEmoji(this)
descriptionTextView.text = event.getTypeDescription(this) descriptionTextView.text = event.getDialogTitle(this)
d.setView(dialogView) d.setView(dialogView)
@@ -872,7 +876,7 @@ class MainActivity : AppCompatActivity() {
val previousTextView = dialogView.findViewById<TextView>(R.id.dialog_event_previous) val previousTextView = dialogView.findViewById<TextView>(R.id.dialog_event_previous)
val previousEvent = getPreviousSameEvent(event, allEvents) val previousEvent = getPreviousSameEvent(event, allEvents)
if (previousEvent != null) { if (previousEvent != null) {
val emoji = previousEvent.getTypeEmoji(applicationContext) val emoji = previousEvent.getHeaderEmoji(applicationContext)
val time = DateUtils.formatTimeDuration(applicationContext, event.time - previousEvent.time) val time = DateUtils.formatTimeDuration(applicationContext, event.time - previousEvent.time)
previousTextView.text = String.format("⬅️ %s %s", emoji, time) previousTextView.text = String.format("⬅️ %s %s", emoji, time)
previousTextView.setOnClickListener { previousTextView.setOnClickListener {
@@ -887,7 +891,7 @@ class MainActivity : AppCompatActivity() {
val nextTextView = dialogView.findViewById<TextView>(R.id.dialog_event_next) val nextTextView = dialogView.findViewById<TextView>(R.id.dialog_event_next)
val nextEvent = getNextSameEvent(event, allEvents) val nextEvent = getNextSameEvent(event, allEvents)
if (nextEvent != null) { if (nextEvent != null) {
val emoji = nextEvent.getTypeEmoji(applicationContext) val emoji = nextEvent.getHeaderEmoji(applicationContext)
val time = DateUtils.formatTimeDuration(applicationContext, nextEvent.time - event.time) val time = DateUtils.formatTimeDuration(applicationContext, nextEvent.time - event.time)
nextTextView.text = String.format("%s %s ➡️", time, emoji) nextTextView.text = String.format("%s %s ➡️", time, emoji)
nextTextView.setOnClickListener { nextTextView.setOnClickListener {
@@ -1058,7 +1062,7 @@ class MainActivity : AppCompatActivity() {
if (DEBUG_CHECK_LOGBOOK_CONSISTENCY) { if (DEBUG_CHECK_LOGBOOK_CONSISTENCY) {
for (e in logbook?.logs ?: listOf()) { for (e in logbook?.logs ?: listOf()) {
val em = e.getTypeEmoji(this@MainActivity) val em = e.getHeaderEmoji(this@MainActivity)
if (em == getString(R.string.event_unknown_type)) { if (em == getString(R.string.event_unknown_type)) {
Log.e(TAG, "UNKNOWN: ${e.type}") Log.e(TAG, "UNKNOWN: ${e.type}")
} }

View File

@@ -52,11 +52,11 @@ class LunaEventRecyclerAdapter: RecyclerView.Adapter<LunaEventRecyclerAdapter.Lu
) )
holder.quantity.setTextColor(ContextCompat.getColor(context, R.color.textColor)) holder.quantity.setTextColor(ContextCompat.getColor(context, R.color.textColor))
// Contents // Contents
holder.type.text = item.getTypeEmoji(context) holder.type.text = item.getHeaderEmoji(context)
holder.description.text = when (item.type) { holder.description.text = when (item.type) {
LunaEvent.Type.MEDICINE -> item.notes LunaEvent.Type.MEDICINE -> item.notes
LunaEvent.Type.NOTE -> item.notes LunaEvent.Type.NOTE -> item.notes
else -> item.getTypeDescription(context) else -> item.getRowItemTitle(context)
} }
val endTime = if (item.type == LunaEvent.Type.SLEEP) { val endTime = if (item.type == LunaEvent.Type.SLEEP) {
item.quantity + item.time item.quantity + item.time

View File

@@ -12,7 +12,6 @@ import java.util.Date
* release, it is simply ignored by previous ones). * release, it is simply ignored by previous ones).
*/ */
class LunaEvent: Comparable<LunaEvent> { class LunaEvent: Comparable<LunaEvent> {
enum class Type { enum class Type {
BABY_BOTTLE, BABY_BOTTLE,
FOOD, FOOD,
@@ -100,12 +99,16 @@ class LunaEvent: Comparable<LunaEvent> {
this.quantity = quantity this.quantity = quantity
} }
fun getTypeEmoji(context: Context): String { fun getHeaderEmoji(context: Context): String {
return getTypeEmoji(context, type) return getHeaderEmoji(context, type)
} }
fun getTypeDescription(context: Context): String { fun getDialogTitle(context: Context): String {
return getTypeDescription(context, type) return getDialogTitle(context, type)
}
fun getRowItemTitle(context: Context): String {
return getPopupItemTitle(context, type).split(" ", limit = 2).last() // remove emoji
} }
fun getDialogMessage(context: Context): String { fun getDialogMessage(context: Context): String {
@@ -125,7 +128,7 @@ class LunaEvent: Comparable<LunaEvent> {
} }
companion object { companion object {
fun getTypeEmoji(context: Context, type: Type): String { fun getHeaderEmoji(context: Context, type: Type): String {
return context.getString( return context.getString(
when (type) { when (type) {
Type.BABY_BOTTLE -> R.string.event_bottle_type Type.BABY_BOTTLE -> R.string.event_bottle_type
@@ -165,7 +168,7 @@ class LunaEvent: Comparable<LunaEvent> {
) )
} }
fun getTypeDescription(context: Context, type: Type): String { fun getDialogTitle(context: Context, type: Type): String {
return context.getString( return context.getString(
when (type) { when (type) {
Type.BABY_BOTTLE -> R.string.event_bottle_desc Type.BABY_BOTTLE -> R.string.event_bottle_desc

View File

@@ -101,7 +101,7 @@ class NumericUtils (val context: Context) {
else -> "" else -> ""
}) })
} }
return formatted.toString() return formatted.toString().trim()
} }
/** /**