From b1e26f8a96eec9fdd927c426e642099d9a31bbb7 Mon Sep 17 00:00:00 2001 From: Daniele Verducci Date: Thu, 9 Jan 2025 08:27:36 +0100 Subject: [PATCH] Added Temperature and Gaseous colic events, removed custom event stub --- .../lunatracker/MainActivity.kt | 35 +++++++++++- .../lunatracker/entities/LunaEvent.kt | 6 ++ app/src/main/java/utils/NumericUtils.kt | 57 ++++++++++++++++--- app/src/main/res/layout/more_events_popup.xml | 22 +++---- .../main/res/layout/temperature_dialog.xml | 26 +++++++++ app/src/main/res/values-it/strings.xml | 8 ++- app/src/main/res/values/strings.xml | 12 +++- app/src/main/res/values/themes.xml | 5 ++ app/src/main/res/values/values.xml | 9 +++ 9 files changed, 158 insertions(+), 22 deletions(-) create mode 100644 app/src/main/res/layout/temperature_dialog.xml create mode 100644 app/src/main/res/values/values.xml diff --git a/app/src/main/java/it/danieleverducci/lunatracker/MainActivity.kt b/app/src/main/java/it/danieleverducci/lunatracker/MainActivity.kt index 4a5a26b..8b47feb 100644 --- a/app/src/main/java/it/danieleverducci/lunatracker/MainActivity.kt +++ b/app/src/main/java/it/danieleverducci/lunatracker/MainActivity.kt @@ -17,6 +17,7 @@ import androidx.appcompat.app.AppCompatActivity import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.RecyclerView import com.google.android.material.progressindicator.LinearProgressIndicator +import com.google.android.material.slider.Slider import com.thegrizzlylabs.sardineandroid.impl.SardineException import it.danieleverducci.lunatracker.adapters.LunaEventRecyclerAdapter import it.danieleverducci.lunatracker.entities.Logbook @@ -207,6 +208,30 @@ class MainActivity : AppCompatActivity() { alertDialog.show() } + fun askTemperatureValue() { + // Show number picker dialog + val d = AlertDialog.Builder(this) + val dialogView = layoutInflater.inflate(R.layout.temperature_dialog, null) + d.setTitle(R.string.log_temperature_dialog_title) + d.setMessage(R.string.log_temperature_dialog_description) + d.setView(dialogView) + val tempSlider = dialogView.findViewById(R.id.dialog_temperature_value) + val range = NumericUtils(this).getValidEventQuantityRange(LunaEvent.TYPE_TEMPERATURE)!! + tempSlider.valueFrom = range.first.toFloat() + tempSlider.valueTo = range.second.toFloat() + tempSlider.value = range.third.toFloat() + val tempTextView = dialogView.findViewById(R.id.dialog_temperature_display) + tempTextView.text = range.third.toString() + tempSlider.addOnChangeListener({s, v, b -> tempTextView.text = v.toString()}) + d.setPositiveButton(android.R.string.ok) { dialogInterface, i -> + val temperature = (tempSlider.value * 10).toInt() // In tenth of a grade + logEvent(LunaEvent(LunaEvent.TYPE_TEMPERATURE, temperature)) + } + d.setNegativeButton(android.R.string.cancel) { dialogInterface, i -> dialogInterface.dismiss() } + val alertDialog = d.create() + alertDialog.show() + } + fun askNotes(lunaEvent: LunaEvent) { val d = AlertDialog.Builder(this) val dialogView = layoutInflater.inflate(R.layout.dialog_notes, null) @@ -486,8 +511,14 @@ class MainActivity : AppCompatActivity() { askNotes(LunaEvent(LunaEvent.TYPE_NOTE)) dismiss() }) - contentView.findViewById(R.id.button_custom).setOnClickListener({ - Toast.makeText(anchor.context, "TODO: Implement custom events", Toast.LENGTH_SHORT).show() + contentView.findViewById(R.id.button_temperature).setOnClickListener({ + askTemperatureValue() + dismiss() + }) + contentView.findViewById(R.id.button_colic).setOnClickListener({ + logEvent( + LunaEvent(LunaEvent.TYPE_COLIC) + ) dismiss() }) }.also { popupWindow -> diff --git a/app/src/main/java/it/danieleverducci/lunatracker/entities/LunaEvent.kt b/app/src/main/java/it/danieleverducci/lunatracker/entities/LunaEvent.kt index 475104d..d62891e 100644 --- a/app/src/main/java/it/danieleverducci/lunatracker/entities/LunaEvent.kt +++ b/app/src/main/java/it/danieleverducci/lunatracker/entities/LunaEvent.kt @@ -25,6 +25,8 @@ class LunaEvent { val TYPE_ENEMA = "ENEMA" val TYPE_NOTE = "NOTE" val TYPE_CUSTOM = "CUSTOM" + val TYPE_COLIC = "COLIC" + val TYPE_TEMPERATURE = "TEMPERATURE" } private val jo: JSONObject @@ -84,6 +86,8 @@ class LunaEvent { TYPE_MEDICINE -> R.string.event_medicine_type TYPE_ENEMA -> R.string.event_enema_type TYPE_NOTE -> R.string.event_note_type + TYPE_TEMPERATURE -> R.string.event_temperature_type + TYPE_COLIC -> R.string.event_colic_type else -> R.string.event_unknown_type } ) @@ -102,6 +106,8 @@ class LunaEvent { TYPE_MEDICINE -> R.string.event_medicine_desc TYPE_ENEMA -> R.string.event_enema_desc TYPE_NOTE -> R.string.event_note_desc + TYPE_TEMPERATURE -> R.string.event_temperature_desc + TYPE_COLIC -> R.string.event_colic_desc else -> R.string.event_unknown_desc } ) diff --git a/app/src/main/java/utils/NumericUtils.kt b/app/src/main/java/utils/NumericUtils.kt index 81669c6..49b8552 100644 --- a/app/src/main/java/utils/NumericUtils.kt +++ b/app/src/main/java/utils/NumericUtils.kt @@ -12,6 +12,7 @@ class NumericUtils (val context: Context) { val measurement_unit_liquid_base: String val measurement_unit_weight_base: String val measurement_unit_weight_tiny: String + val measurement_unit_temperature_base: String init { this.numberFormat = NumberFormat.getInstance() @@ -34,18 +35,58 @@ class NumericUtils (val context: Context) { else R.string.measurement_unit_weight_tiny_imperial ) + this.measurement_unit_temperature_base = context.getString( + if (measurementSystem == LocaleData. MeasurementSystem.SI) + R.string.measurement_unit_temperature_base_metric + else + R.string.measurement_unit_temperature_base_imperial + ) } fun formatEventQuantity(item: LunaEvent): String { - return if ((item.quantity ?: 0) > 0) { - numberFormat.format(item.quantity) + " " + when (item.type) { - LunaEvent.TYPE_BABY_BOTTLE -> measurement_unit_liquid_base - LunaEvent.TYPE_WEIGHT -> measurement_unit_weight_base - LunaEvent.TYPE_MEDICINE -> measurement_unit_weight_tiny - else -> "" + val formatted = StringBuilder() + if ((item.quantity ?: 0) > 0) { + if (item.type == LunaEvent.TYPE_TEMPERATURE) + formatted.append((item.quantity / 10.0f).toString()) + else + formatted.append(item.quantity) + + formatted.append(" ") + formatted.append( + when (item.type) { + LunaEvent.TYPE_BABY_BOTTLE -> measurement_unit_liquid_base + LunaEvent.TYPE_WEIGHT -> measurement_unit_weight_base + LunaEvent.TYPE_MEDICINE -> measurement_unit_weight_tiny + LunaEvent.TYPE_TEMPERATURE -> measurement_unit_temperature_base + else -> "" + } + ) + } + return formatted.toString() + } + + /** + * Returns a valid quantity range for the event type. + * @return min, max, normal + */ + fun getValidEventQuantityRange(lunaEventType: String): Triple? { + val measurementSystem = LocaleData.getMeasurementSystem(ULocale.getDefault()) + return when (lunaEventType) { + LunaEvent.TYPE_TEMPERATURE -> { + if (measurementSystem == LocaleData. MeasurementSystem.SI) + Triple( + context.resources.getInteger(R.integer.human_body_temp_min_metric), + context.resources.getInteger(R.integer.human_body_temp_max_metric), + context.resources.getInteger(R.integer.human_body_temp_normal_metric) + ) + else + Triple( + context.resources.getInteger(R.integer.human_body_temp_min_imperial), + context.resources.getInteger(R.integer.human_body_temp_max_imperial), + context.resources.getInteger(R.integer.human_body_temp_normal_imperial) + ) } - } else { - "" + else -> null } } } \ No newline at end of file diff --git a/app/src/main/res/layout/more_events_popup.xml b/app/src/main/res/layout/more_events_popup.xml index 5775004..62b973a 100644 --- a/app/src/main/res/layout/more_events_popup.xml +++ b/app/src/main/res/layout/more_events_popup.xml @@ -39,23 +39,25 @@ style="@style/OverflowMenuText" android:text="@string/overflow_event_note"/> - - - - + android:text="@string/overflow_event_temperature"/> + + diff --git a/app/src/main/res/layout/temperature_dialog.xml b/app/src/main/res/layout/temperature_dialog.xml new file mode 100644 index 0000000..292ed19 --- /dev/null +++ b/app/src/main/res/layout/temperature_dialog.xml @@ -0,0 +1,26 @@ + + + + + + + diff --git a/app/src/main/res/values-it/strings.xml b/app/src/main/res/values-it/strings.xml index 8fb3456..efccd63 100644 --- a/app/src/main/res/values-it/strings.xml +++ b/app/src/main/res/values-it/strings.xml @@ -10,10 +10,14 @@ Pesata Inserisci il peso rilevato + Temperatura + Inserisci la temperatura + 💊 Medicina 🪠 Clistere 📝 Nota - ➕ Personalizzato + 🌡️ Temperatura + 💨 Colichette Biberon Pesata @@ -25,6 +29,8 @@ Medicina Clistere Nota + Temperatura + Colichette Evento aggiunto diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 64b64eb..74c1bf6 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -10,6 +10,9 @@ Weight Insert the weight + Temperature + Insert the temperature + 🍼 ⚖️ 🤱 ← @@ -20,6 +23,8 @@ 💊 🪠 📝 + 🌡️ + 💨 \? Baby bottle @@ -32,12 +37,15 @@ Medicine Enema Note + Temperature + Gaseous colic 💊 Medicine 🪠 Enema 📝 Note - ➕ Add custom + 🌡️ Temperature + 💨 Gaseous colic Event logged Logbook saved @@ -90,6 +98,8 @@ fl oz. oz gr + °F + °C Event detail diff --git a/app/src/main/res/values/themes.xml b/app/src/main/res/values/themes.xml index c68ef34..9a5c676 100644 --- a/app/src/main/res/values/themes.xml +++ b/app/src/main/res/values/themes.xml @@ -9,4 +9,9 @@ + + + \ No newline at end of file diff --git a/app/src/main/res/values/values.xml b/app/src/main/res/values/values.xml new file mode 100644 index 0000000..5afe0eb --- /dev/null +++ b/app/src/main/res/values/values.xml @@ -0,0 +1,9 @@ + + + 91 + 33 + 109 + 43 + 98 + 37 + \ No newline at end of file