Add history type option (Total/Average) to numerical habits

pull/2215/head
Janssen 3 weeks ago
parent 5aa8744ef4
commit fbdbcf4b97

@ -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<String>(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)

@ -171,6 +171,21 @@
</LinearLayout>
</FrameLayout>
<!-- History Type -->
<FrameLayout
android:id="@+id/historyTypeOuterBox"
style="@style/FormOuterBox">
<LinearLayout style="@style/FormInnerBox">
<TextView
style="@style/FormLabel"
android:text="@string/history_type" />
<TextView
style="@style/FormDropdown"
android:id="@+id/historyTypePicker"
android:text="@string/total" />
</LinearLayout>
</FrameLayout>
<LinearLayout
android:id="@+id/targetOuterBox"
android:layout_width="match_parent"

@ -184,12 +184,14 @@
<string name="value">Value</string>
<string name="calendar">Calendar</string>
<string name="unit">Unit</string>
<string name="history_type">History Type</string>
<string name="target_type">Target Type</string>
<string name="target_type_at_least">At least</string>
<string name="target_type_at_most">At most</string>
<string name="example_question_boolean">e.g. Did you exercise today?</string>
<string name="question">Question</string>
<string name="target">Target</string>
<string name="average">Average</string>
<string name="yes">Yes</string>
<string name="no">No</string>
<string name="customize_notification_summary">Change sound, vibration, light and other notification settings</string>

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

@ -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()
}
}
}
}
Loading…
Cancel
Save