Added Temperature and Gaseous colic events, removed custom event stub

This commit is contained in:
Daniele Verducci 2025-01-09 08:27:36 +01:00
parent 48017f70f6
commit b1e26f8a96
9 changed files with 158 additions and 22 deletions

View File

@ -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<Slider>(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<TextView>(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<View>(R.id.button_custom).setOnClickListener({
Toast.makeText(anchor.context, "TODO: Implement custom events", Toast.LENGTH_SHORT).show()
contentView.findViewById<View>(R.id.button_temperature).setOnClickListener({
askTemperatureValue()
dismiss()
})
contentView.findViewById<View>(R.id.button_colic).setOnClickListener({
logEvent(
LunaEvent(LunaEvent.TYPE_COLIC)
)
dismiss()
})
}.also { popupWindow ->

View File

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

View File

@ -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) {
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 -> ""
}
} else {
""
)
}
return formatted.toString()
}
/**
* Returns a valid quantity range for the event type.
* @return min, max, normal
*/
fun getValidEventQuantityRange(lunaEventType: String): Triple<Int, Int, Int>? {
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 -> null
}
}
}

View File

@ -39,23 +39,25 @@
style="@style/OverflowMenuText"
android:text="@string/overflow_event_note"/>
<LinearLayout
android:id="@+id/overflow_event_custom_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
</LinearLayout>
<TextView
android:id="@+id/button_custom"
android:id="@+id/button_temperature"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="10dp"
android:padding="20dp"
android:background="@drawable/button_background"
style="@style/OverflowMenuText"
android:text="@string/overflow_event_custom"/>
android:text="@string/overflow_event_temperature"/>
<TextView
android:id="@+id/button_colic"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="10dp"
android:padding="20dp"
android:background="@drawable/button_background"
style="@style/OverflowMenuText"
android:text="@string/overflow_event_colic"/>
</LinearLayout>

View File

@ -0,0 +1,26 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:gravity="center"
android:orientation="vertical">
<com.google.android.material.slider.Slider
android:id="@+id/dialog_temperature_value"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:valueFrom="@integer/human_body_temp_min_metric"
android:valueTo="@integer/human_body_temp_max_metric"
android:stepSize="0.1"
android:value="@integer/human_body_temp_normal_metric"
android:theme="@style/LTSlider"/>
<TextView
android:id="@+id/dialog_temperature_display"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30sp"
android:textColor="@color/accent"/>
</LinearLayout>

View File

@ -10,10 +10,14 @@
<string name="log_weight_dialog_title">Pesata</string>
<string name="log_weight_dialog_description">Inserisci il peso rilevato</string>
<string name="log_temperature_dialog_title">Temperatura</string>
<string name="log_temperature_dialog_description">Inserisci la temperatura</string>
<string name="overflow_event_medicine">💊 Medicina</string>
<string name="overflow_event_enema">🪠 Clistere</string>
<string name="overflow_event_note">📝 Nota</string>
<string name="overflow_event_custom"> Personalizzato</string>
<string name="overflow_event_temperature">🌡️ Temperatura</string>
<string name="overflow_event_colic">💨 Colichette</string>
<string name="event_bottle_desc">Biberon</string>
<string name="event_scale_desc">Pesata</string>
@ -25,6 +29,8 @@
<string name="event_medicine_desc">Medicina</string>
<string name="event_enema_desc">Clistere</string>
<string name="event_note_desc">Nota</string>
<string name="event_temperature_desc">Temperatura</string>
<string name="event_colic_desc">Colichette</string>
<string name="event_unknown_desc"></string>
<string name="toast_event_added">Evento aggiunto</string>

View File

@ -10,6 +10,9 @@
<string name="log_weight_dialog_title">Weight</string>
<string name="log_weight_dialog_description">Insert the weight</string>
<string name="log_temperature_dialog_title">Temperature</string>
<string name="log_temperature_dialog_description">Insert the temperature</string>
<string name="event_bottle_type" translatable="false">🍼</string>
<string name="event_scale_type" translatable="false">⚖️</string>
<string name="event_breastfeeding_left_type" translatable="false">🤱 ←</string>
@ -20,6 +23,8 @@
<string name="event_medicine_type" translatable="false">💊</string>
<string name="event_enema_type" translatable="false">🪠</string>
<string name="event_note_type" translatable="false">📝</string>
<string name="event_temperature_type" translatable="false">🌡️</string>
<string name="event_colic_type" translatable="false">💨</string>
<string name="event_unknown_type" translatable="false">\?</string>
<string name="event_bottle_desc">Baby bottle</string>
@ -32,12 +37,15 @@
<string name="event_medicine_desc">Medicine</string>
<string name="event_enema_desc">Enema</string>
<string name="event_note_desc">Note</string>
<string name="event_temperature_desc">Temperature</string>
<string name="event_colic_desc">Gaseous colic</string>
<string name="event_unknown_desc"></string>
<string name="overflow_event_medicine">💊 Medicine</string>
<string name="overflow_event_enema">🪠 Enema</string>
<string name="overflow_event_note">📝 Note</string>
<string name="overflow_event_custom"> Add custom</string>
<string name="overflow_event_temperature">🌡️ Temperature</string>
<string name="overflow_event_colic">💨 Gaseous colic</string>
<string name="toast_event_added">Event logged</string>
<string name="toast_logbook_saved">Logbook saved</string>
@ -90,6 +98,8 @@
<string name="measurement_unit_liquid_base_imperial" translatable="false">fl oz.</string>
<string name="measurement_unit_weight_base_imperial" translatable="false">oz</string>
<string name="measurement_unit_weight_tiny_imperial" translatable="false">gr</string>
<string name="measurement_unit_temperature_base_imperial" translatable="false">°F</string>
<string name="measurement_unit_temperature_base_metric" translatable="false">°C</string>
<string name="dialog_event_detail_title">Event detail</string>

View File

@ -9,4 +9,9 @@
<style name="OverflowMenuText">
<item name="android:textSize">20sp</item>
</style>
<style name="LTSlider" parent="Theme.MaterialComponents.Light.NoActionBar">
<item name="colorPrimary">@color/accent</item>
</style>
</resources>

View File

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<integer name="human_body_temp_min_imperial">91</integer>
<integer name="human_body_temp_min_metric">33</integer>
<integer name="human_body_temp_max_imperial">109</integer>
<integer name="human_body_temp_max_metric">43</integer>
<integer name="human_body_temp_normal_imperial">98</integer>
<integer name="human_body_temp_normal_metric">37</integer>
</resources>