From fbdbcf4b9714fef05717e17985a08b5a1f94c3e4 Mon Sep 17 00:00:00 2001 From: Janssen Date: Fri, 5 Sep 2025 23:37:20 +0200 Subject: [PATCH] Add history type option (Total/Average) to numerical habits --- .../habits/edit/EditHabitActivity.kt | 29 +++++++++++++++++++ .../main/res/layout/activity_edit_habit.xml | 15 ++++++++++ .../src/main/res/values/strings.xml | 2 ++ .../org/isoron/uhabits/core/models/Habit.kt | 4 +++ .../core/models/NumericalHistoryType.kt | 17 +++++++++++ 5 files changed, 67 insertions(+) create mode 100644 uhabits-core/src/jvmMain/java/org/isoron/uhabits/core/models/NumericalHistoryType.kt diff --git a/uhabits-android/src/main/java/org/isoron/uhabits/activities/habits/edit/EditHabitActivity.kt b/uhabits-android/src/main/java/org/isoron/uhabits/activities/habits/edit/EditHabitActivity.kt index 1708a930a..00d7d3c63 100644 --- a/uhabits-android/src/main/java/org/isoron/uhabits/activities/habits/edit/EditHabitActivity.kt +++ b/uhabits-android/src/main/java/org/isoron/uhabits/activities/habits/edit/EditHabitActivity.kt @@ -48,6 +48,7 @@ import org.isoron.uhabits.core.models.Frequency import org.isoron.uhabits.core.models.Habit import org.isoron.uhabits.core.models.HabitType import org.isoron.uhabits.core.models.NumericalHabitType +import org.isoron.uhabits.core.models.NumericalHistoryType import org.isoron.uhabits.core.models.PaletteColor import org.isoron.uhabits.core.models.Reminder import org.isoron.uhabits.core.models.WeekdayList @@ -84,6 +85,7 @@ class EditHabitActivity : AppCompatActivity() { var reminderHour = -1 var reminderMin = -1 var reminderDays: WeekdayList = WeekdayList.EVERY_DAY + var historyType = NumericalHistoryType.TOTAL var targetType = NumericalHabitType.AT_LEAST override fun onCreate(state: Bundle?) { @@ -106,6 +108,7 @@ class EditHabitActivity : AppCompatActivity() { color = habit.color freqNum = habit.frequency.numerator freqDen = habit.frequency.denominator + historyType = habit.historyType targetType = habit.targetType habit.reminder?.let { reminderHour = it.hour @@ -137,6 +140,7 @@ class EditHabitActivity : AppCompatActivity() { when (habitType) { HabitType.YES_NO -> { binding.unitOuterBox.visibility = View.GONE + binding.historyTypeOuterBox.visibility = View.GONE binding.targetOuterBox.visibility = View.GONE binding.targetTypeOuterBox.visibility = View.GONE } @@ -173,6 +177,24 @@ class EditHabitActivity : AppCompatActivity() { picker.dismissCurrentAndShow(supportFragmentManager, "frequencyPicker") } + populateHistoryType() + binding.historyTypePicker.setOnClickListener { + val builder = AlertDialog.Builder(this) + val arrayAdapter = ArrayAdapter(this, android.R.layout.select_dialog_item) + arrayAdapter.add(getString(R.string.total)) + arrayAdapter.add(getString(R.string.average)) + builder.setAdapter(arrayAdapter) { dialog, which -> + historyType = when (which) { + 0 -> NumericalHistoryType.TOTAL + else -> NumericalHistoryType.AVERAGE + } + populateHistoryType() + dialog.dismiss() + } + val dialog = builder.create() + dialog.dismissCurrentAndShow() + } + populateTargetType() binding.targetTypePicker.setOnClickListener { val builder = AlertDialog.Builder(this) @@ -281,6 +303,7 @@ class EditHabitActivity : AppCompatActivity() { habit.frequency = Frequency(freqNum, freqDen) if (habitType == HabitType.NUMERICAL) { habit.targetValue = binding.targetInput.text.toString().toDouble() + habit.historyType = historyType habit.targetType = targetType habit.unit = binding.unitInput.text.trim().toString() } @@ -343,6 +366,12 @@ class EditHabitActivity : AppCompatActivity() { } } + private fun populateHistoryType() { + binding.historyTypePicker.text = when (historyType) { + NumericalHistoryType.TOTAL -> getString(R.string.total) + else -> getString(R.string.average) + } + } private fun populateTargetType() { binding.targetTypePicker.text = when (targetType) { NumericalHabitType.AT_MOST -> getString(R.string.target_type_at_most) diff --git a/uhabits-android/src/main/res/layout/activity_edit_habit.xml b/uhabits-android/src/main/res/layout/activity_edit_habit.xml index c21cd678b..de728ba6d 100644 --- a/uhabits-android/src/main/res/layout/activity_edit_habit.xml +++ b/uhabits-android/src/main/res/layout/activity_edit_habit.xml @@ -171,6 +171,21 @@ + + + + + + + + Value Calendar Unit + History Type Target Type At least At most e.g. Did you exercise today? Question Target + Average Yes No Change sound, vibration, light and other notification settings diff --git a/uhabits-core/src/jvmMain/java/org/isoron/uhabits/core/models/Habit.kt b/uhabits-core/src/jvmMain/java/org/isoron/uhabits/core/models/Habit.kt index 0be2d012e..7e711ea96 100644 --- a/uhabits-core/src/jvmMain/java/org/isoron/uhabits/core/models/Habit.kt +++ b/uhabits-core/src/jvmMain/java/org/isoron/uhabits/core/models/Habit.kt @@ -31,6 +31,7 @@ data class Habit( var position: Int = 0, var question: String = "", var reminder: Reminder? = null, + var historyType: NumericalHistoryType = NumericalHistoryType.TOTAL, var targetType: NumericalHabitType = NumericalHabitType.AT_LEAST, var targetValue: Double = 0.0, var type: HabitType = HabitType.YES_NO, @@ -117,6 +118,7 @@ data class Habit( this.position = other.position this.question = other.question this.reminder = other.reminder + this.historyType = other.historyType this.targetType = other.targetType this.targetValue = other.targetValue this.type = other.type @@ -137,6 +139,7 @@ data class Habit( if (position != other.position) return false if (question != other.question) return false if (reminder != other.reminder) return false + if (historyType != other.historyType) return false if (targetType != other.targetType) return false if (targetValue != other.targetValue) return false if (type != other.type) return false @@ -156,6 +159,7 @@ data class Habit( result = 31 * result + position result = 31 * result + question.hashCode() result = 31 * result + (reminder?.hashCode() ?: 0) + result = 31 * result + historyType.value result = 31 * result + targetType.value result = 31 * result + targetValue.hashCode() result = 31 * result + type.value diff --git a/uhabits-core/src/jvmMain/java/org/isoron/uhabits/core/models/NumericalHistoryType.kt b/uhabits-core/src/jvmMain/java/org/isoron/uhabits/core/models/NumericalHistoryType.kt new file mode 100644 index 000000000..767c55785 --- /dev/null +++ b/uhabits-core/src/jvmMain/java/org/isoron/uhabits/core/models/NumericalHistoryType.kt @@ -0,0 +1,17 @@ +package org.isoron.uhabits.core.models + +import java.lang.IllegalStateException + +enum class NumericalHistoryType(val value: Int) { + TOTAL(0), AVERAGE(1); + + companion object { + fun fromInt(value: Int): NumericalHistoryType { + return when (value) { + TOTAL.value -> TOTAL + AVERAGE.value -> AVERAGE + else -> throw IllegalStateException() + } + } + } +}