Introduce HabitType and NumericalHabitType enums

pull/1056/head
Quentin Hibon 4 years ago committed by Alinson S. Xavier
parent 95a1786c4a
commit b9eb244b0b

@ -23,10 +23,10 @@ import org.isoron.uhabits.core.models.Entry.Companion.YES_MANUAL
import org.isoron.uhabits.core.models.Frequency import org.isoron.uhabits.core.models.Frequency
import org.isoron.uhabits.core.models.Frequency.Companion.DAILY import org.isoron.uhabits.core.models.Frequency.Companion.DAILY
import org.isoron.uhabits.core.models.Habit import org.isoron.uhabits.core.models.Habit
import org.isoron.uhabits.core.models.Habit.Companion.AT_LEAST
import org.isoron.uhabits.core.models.Habit.Companion.NUMBER_HABIT
import org.isoron.uhabits.core.models.HabitList import org.isoron.uhabits.core.models.HabitList
import org.isoron.uhabits.core.models.HabitType
import org.isoron.uhabits.core.models.ModelFactory import org.isoron.uhabits.core.models.ModelFactory
import org.isoron.uhabits.core.models.NumericalHabitType
import org.isoron.uhabits.core.models.PaletteColor import org.isoron.uhabits.core.models.PaletteColor
import org.isoron.uhabits.core.models.Timestamp import org.isoron.uhabits.core.models.Timestamp
import org.isoron.uhabits.core.utils.DateUtils.Companion.getToday import org.isoron.uhabits.core.utils.DateUtils.Companion.getToday
@ -102,8 +102,8 @@ class HabitFixtures(private val modelFactory: ModelFactory, private val habitLis
val habit = modelFactory.buildHabit().apply { val habit = modelFactory.buildHabit().apply {
name = "Read" name = "Read"
question = "How many pages did you walk today?" question = "How many pages did you walk today?"
type = NUMBER_HABIT type = HabitType.NUMERICAL
targetType = AT_LEAST targetType = NumericalHabitType.AT_LEAST
targetValue = 200.0 targetValue = 200.0
unit = "pages" unit = "pages"
} }

@ -35,11 +35,7 @@ import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.DialogFragment import androidx.fragment.app.DialogFragment
import com.android.datetimepicker.time.RadialPickerLayout import com.android.datetimepicker.time.RadialPickerLayout
import com.android.datetimepicker.time.TimePickerDialog import com.android.datetimepicker.time.TimePickerDialog
import kotlinx.android.synthetic.main.activity_edit_habit.nameInput import kotlinx.android.synthetic.main.activity_edit_habit.*
import kotlinx.android.synthetic.main.activity_edit_habit.notesInput
import kotlinx.android.synthetic.main.activity_edit_habit.questionInput
import kotlinx.android.synthetic.main.activity_edit_habit.targetInput
import kotlinx.android.synthetic.main.activity_edit_habit.unitInput
import org.isoron.uhabits.HabitsApplication import org.isoron.uhabits.HabitsApplication
import org.isoron.uhabits.R import org.isoron.uhabits.R
import org.isoron.uhabits.activities.AndroidThemeSwitcher import org.isoron.uhabits.activities.AndroidThemeSwitcher
@ -51,6 +47,8 @@ import org.isoron.uhabits.core.commands.CreateHabitCommand
import org.isoron.uhabits.core.commands.EditHabitCommand import org.isoron.uhabits.core.commands.EditHabitCommand
import org.isoron.uhabits.core.models.Frequency import org.isoron.uhabits.core.models.Frequency
import org.isoron.uhabits.core.models.Habit 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.PaletteColor import org.isoron.uhabits.core.models.PaletteColor
import org.isoron.uhabits.core.models.Reminder import org.isoron.uhabits.core.models.Reminder
import org.isoron.uhabits.core.models.WeekdayList import org.isoron.uhabits.core.models.WeekdayList
@ -77,7 +75,7 @@ class EditHabitActivity : AppCompatActivity() {
private lateinit var commandRunner: CommandRunner private lateinit var commandRunner: CommandRunner
var habitId = -1L var habitId = -1L
var habitType = -1 lateinit var habitType: HabitType
var unit = "" var unit = ""
var color = PaletteColor(11) var color = PaletteColor(11)
var androidColor = 0 var androidColor = 0
@ -116,12 +114,12 @@ class EditHabitActivity : AppCompatActivity() {
binding.unitInput.setText(habit.unit) binding.unitInput.setText(habit.unit)
binding.targetInput.setText(habit.targetValue.toString()) binding.targetInput.setText(habit.targetValue.toString())
} else { } else {
habitType = intent.getIntExtra("habitType", Habit.YES_NO_HABIT) habitType = HabitType.fromInt(intent.getIntExtra("habitType", HabitType.YES_NO.value))
} }
if (state != null) { if (state != null) {
habitId = state.getLong("habitId") habitId = state.getLong("habitId")
habitType = state.getInt("habitType") habitType = HabitType.fromInt(state.getInt("habitType"))
color = PaletteColor(state.getInt("paletteColor")) color = PaletteColor(state.getInt("paletteColor"))
freqNum = state.getInt("freqNum") freqNum = state.getInt("freqNum")
freqDen = state.getInt("freqDen") freqDen = state.getInt("freqDen")
@ -132,13 +130,16 @@ class EditHabitActivity : AppCompatActivity() {
updateColors() updateColors()
if (habitType == Habit.YES_NO_HABIT) { when (habitType) {
binding.unitOuterBox.visibility = View.GONE HabitType.YES_NO -> {
binding.targetOuterBox.visibility = View.GONE binding.unitOuterBox.visibility = View.GONE
} else { binding.targetOuterBox.visibility = View.GONE
binding.nameInput.hint = getString(R.string.measurable_short_example) }
binding.questionInput.hint = getString(R.string.measurable_question_example) HabitType.NUMERICAL -> {
binding.frequencyOuterBox.visibility = View.GONE binding.nameInput.hint = getString(R.string.measurable_short_example)
binding.questionInput.hint = getString(R.string.measurable_question_example)
binding.frequencyOuterBox.visibility = View.GONE
}
} }
setSupportActionBar(binding.toolbar) setSupportActionBar(binding.toolbar)
@ -255,9 +256,9 @@ class EditHabitActivity : AppCompatActivity() {
} }
habit.frequency = Frequency(freqNum, freqDen) habit.frequency = Frequency(freqNum, freqDen)
if (habitType == Habit.NUMBER_HABIT) { if (habitType == HabitType.NUMERICAL) {
habit.targetValue = targetInput.text.toString().toDouble() habit.targetValue = targetInput.text.toString().toDouble()
habit.targetType = Habit.AT_LEAST habit.targetType = NumericalHabitType.AT_LEAST
habit.unit = unitInput.text.trim().toString() habit.unit = unitInput.text.trim().toString()
} }
habit.type = habitType habit.type = habitType
@ -285,7 +286,7 @@ class EditHabitActivity : AppCompatActivity() {
nameInput.error = getFormattedValidationError(R.string.validation_cannot_be_blank) nameInput.error = getFormattedValidationError(R.string.validation_cannot_be_blank)
isValid = false isValid = false
} }
if (habitType == Habit.NUMBER_HABIT) { if (habitType == HabitType.NUMERICAL) {
if (targetInput.text.isEmpty()) { if (targetInput.text.isEmpty()) {
targetInput.error = getString(R.string.validation_cannot_be_blank) targetInput.error = getString(R.string.validation_cannot_be_blank)
isValid = false isValid = false
@ -338,7 +339,7 @@ class EditHabitActivity : AppCompatActivity() {
super.onSaveInstanceState(state) super.onSaveInstanceState(state)
with(state) { with(state) {
putLong("habitId", habitId) putLong("habitId", habitId)
putInt("habitType", habitType) putInt("habitType", habitType.value)
putInt("paletteColor", color.paletteIndex) putInt("paletteColor", color.paletteIndex)
putInt("androidColor", androidColor) putInt("androidColor", androidColor)
putInt("freqNum", freqNum) putInt("freqNum", freqNum)

@ -25,7 +25,7 @@ import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import androidx.appcompat.app.AppCompatDialogFragment import androidx.appcompat.app.AppCompatDialogFragment
import org.isoron.uhabits.R import org.isoron.uhabits.R
import org.isoron.uhabits.core.models.Habit import org.isoron.uhabits.core.models.HabitType
import org.isoron.uhabits.databinding.SelectHabitTypeBinding import org.isoron.uhabits.databinding.SelectHabitTypeBinding
import org.isoron.uhabits.intents.IntentFactory import org.isoron.uhabits.intents.IntentFactory
@ -40,13 +40,13 @@ class HabitTypeDialog : AppCompatDialogFragment() {
val binding = SelectHabitTypeBinding.inflate(inflater, container, false) val binding = SelectHabitTypeBinding.inflate(inflater, container, false)
binding.buttonYesNo.setOnClickListener { binding.buttonYesNo.setOnClickListener {
val intent = IntentFactory().startEditActivity(activity!!, Habit.YES_NO_HABIT) val intent = IntentFactory().startEditActivity(activity!!, HabitType.YES_NO.value)
startActivity(intent) startActivity(intent)
dismiss() dismiss()
} }
binding.buttonMeasurable.setOnClickListener { binding.buttonMeasurable.setOnClickListener {
val intent = IntentFactory().startEditActivity(activity!!, Habit.NUMBER_HABIT) val intent = IntentFactory().startEditActivity(activity!!, HabitType.NUMERICAL.value)
startActivity(intent) startActivity(intent)
dismiss() dismiss()
} }

@ -31,9 +31,9 @@ data class Habit(
var position: Int = 0, var position: Int = 0,
var question: String = "", var question: String = "",
var reminder: Reminder? = null, var reminder: Reminder? = null,
var targetType: Int = AT_LEAST, var targetType: NumericalHabitType = NumericalHabitType.AT_LEAST,
var targetValue: Double = 0.0, var targetValue: Double = 0.0,
var type: Int = YES_NO_HABIT, var type: HabitType = HabitType.YES_NO,
var unit: String = "", var unit: String = "",
var uuid: String? = null, var uuid: String? = null,
val computedEntries: EntryList, val computedEntries: EntryList,
@ -48,7 +48,7 @@ data class Habit(
var observable = ModelObservable() var observable = ModelObservable()
val isNumerical: Boolean val isNumerical: Boolean
get() = type == NUMBER_HABIT get() = type == HabitType.NUMERICAL
val uriString: String val uriString: String
get() = "content://org.isoron.uhabits/habit/$id" get() = "content://org.isoron.uhabits/habit/$id"
@ -59,10 +59,9 @@ data class Habit(
val today = DateUtils.getTodayWithOffset() val today = DateUtils.getTodayWithOffset()
val value = computedEntries.get(today).value val value = computedEntries.get(today).value
return if (isNumerical) { return if (isNumerical) {
if (targetType == AT_LEAST) { when (targetType) {
value / 1000.0 >= targetValue NumericalHabitType.AT_LEAST -> value / 1000.0 >= targetValue
} else { NumericalHabitType.AT_MOST -> value / 1000.0 <= targetValue
value / 1000.0 <= targetValue
} }
} else { } else {
value != Entry.NO && value != Entry.UNKNOWN value != Entry.NO && value != Entry.UNKNOWN
@ -146,18 +145,11 @@ data class Habit(
result = 31 * result + position result = 31 * result + position
result = 31 * result + question.hashCode() result = 31 * result + question.hashCode()
result = 31 * result + (reminder?.hashCode() ?: 0) result = 31 * result + (reminder?.hashCode() ?: 0)
result = 31 * result + targetType result = 31 * result + targetType.value
result = 31 * result + targetValue.hashCode() result = 31 * result + targetValue.hashCode()
result = 31 * result + type result = 31 * result + type.value
result = 31 * result + unit.hashCode() result = 31 * result + unit.hashCode()
result = 31 * result + (uuid?.hashCode() ?: 0) result = 31 * result + (uuid?.hashCode() ?: 0)
return result return result
} }
companion object {
const val AT_LEAST = 0
const val AT_MOST = 1
const val NUMBER_HABIT = 1
const val YES_NO_HABIT = 0
}
} }

@ -0,0 +1,17 @@
package org.isoron.uhabits.core.models
import java.lang.IllegalStateException
enum class HabitType(val value: Int) {
YES_NO(0), NUMERICAL(1);
companion object {
fun fromInt(value: Int): HabitType {
return when (value) {
YES_NO.value -> YES_NO
NUMERICAL.value -> NUMERICAL
else -> throw IllegalStateException()
}
}
}
}

@ -0,0 +1,17 @@
package org.isoron.uhabits.core.models
import java.lang.IllegalStateException
enum class NumericalHabitType(val value: Int) {
AT_LEAST(0), AT_MOST(1);
companion object {
fun fromInt(value: Int): NumericalHabitType {
return when (value) {
AT_LEAST.value -> AT_LEAST
AT_MOST.value -> AT_MOST
else -> throw IllegalStateException()
}
}
}
}

@ -22,10 +22,12 @@ import org.isoron.uhabits.core.database.Column
import org.isoron.uhabits.core.database.Table import org.isoron.uhabits.core.database.Table
import org.isoron.uhabits.core.models.Frequency import org.isoron.uhabits.core.models.Frequency
import org.isoron.uhabits.core.models.Habit 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.PaletteColor import org.isoron.uhabits.core.models.PaletteColor
import org.isoron.uhabits.core.models.Reminder import org.isoron.uhabits.core.models.Reminder
import org.isoron.uhabits.core.models.WeekdayList import org.isoron.uhabits.core.models.WeekdayList
import java.util.Objects import java.util.Objects.requireNonNull
/** /**
* The SQLite database record corresponding to a [Habit]. * The SQLite database record corresponding to a [Habit].
@ -93,8 +95,8 @@ class HabitRecord {
highlight = 0 highlight = 0
color = model.color.paletteIndex color = model.color.paletteIndex
archived = if (model.isArchived) 1 else 0 archived = if (model.isArchived) 1 else 0
type = model.type type = model.type.value
targetType = model.targetType targetType = model.targetType.value
targetValue = model.targetValue targetValue = model.targetValue
unit = model.unit unit = model.unit
position = model.position position = model.position
@ -108,7 +110,7 @@ class HabitRecord {
reminderHour = null reminderHour = null
if (model.hasReminder()) { if (model.hasReminder()) {
val reminder = model.reminder val reminder = model.reminder
reminderHour = Objects.requireNonNull(reminder)!!.hour reminderHour = requireNonNull(reminder)!!.hour
reminderMin = reminder!!.minute reminderMin = reminder!!.minute
reminderDays = reminder.days.toInteger() reminderDays = reminder.days.toInteger()
} }
@ -122,8 +124,8 @@ class HabitRecord {
habit.frequency = Frequency(freqNum!!, freqDen!!) habit.frequency = Frequency(freqNum!!, freqDen!!)
habit.color = PaletteColor(color!!) habit.color = PaletteColor(color!!)
habit.isArchived = archived != 0 habit.isArchived = archived != 0
habit.type = type!! habit.type = HabitType.fromInt(type!!)
habit.targetType = targetType!! habit.targetType = NumericalHabitType.fromInt(targetType!!)
habit.targetValue = targetValue!! habit.targetValue = targetValue!!
habit.unit = unit!! habit.unit = unit!!
habit.position = position!! habit.position = position!!

@ -22,7 +22,9 @@ import org.isoron.uhabits.core.models.Entry
import org.isoron.uhabits.core.models.Frequency import org.isoron.uhabits.core.models.Frequency
import org.isoron.uhabits.core.models.Habit import org.isoron.uhabits.core.models.Habit
import org.isoron.uhabits.core.models.HabitList import org.isoron.uhabits.core.models.HabitList
import org.isoron.uhabits.core.models.HabitType
import org.isoron.uhabits.core.models.ModelFactory import org.isoron.uhabits.core.models.ModelFactory
import org.isoron.uhabits.core.models.NumericalHabitType
import org.isoron.uhabits.core.models.PaletteColor import org.isoron.uhabits.core.models.PaletteColor
import org.isoron.uhabits.core.models.Timestamp import org.isoron.uhabits.core.models.Timestamp
import org.isoron.uhabits.core.models.sqlite.SQLiteEntryList import org.isoron.uhabits.core.models.sqlite.SQLiteEntryList
@ -65,11 +67,11 @@ class HabitFixtures(private val modelFactory: ModelFactory, private val habitLis
fun createNumericalHabit(): Habit { fun createNumericalHabit(): Habit {
val habit = modelFactory.buildHabit() val habit = modelFactory.buildHabit()
habit.type = Habit.NUMBER_HABIT habit.type = HabitType.NUMERICAL
habit.name = "Run" habit.name = "Run"
habit.question = "How many miles did you run today?" habit.question = "How many miles did you run today?"
habit.unit = "miles" habit.unit = "miles"
habit.targetType = Habit.AT_LEAST habit.targetType = NumericalHabitType.AT_LEAST
habit.targetValue = 2.0 habit.targetValue = 2.0
habit.color = PaletteColor(1) habit.color = PaletteColor(1)
saveIfSQLite(habit) saveIfSQLite(habit)
@ -86,11 +88,11 @@ class HabitFixtures(private val modelFactory: ModelFactory, private val habitLis
fun createLongNumericalHabit(reference: Timestamp): Habit { fun createLongNumericalHabit(reference: Timestamp): Habit {
val habit = modelFactory.buildHabit() val habit = modelFactory.buildHabit()
habit.type = Habit.NUMBER_HABIT habit.type = HabitType.NUMERICAL
habit.name = "Walk" habit.name = "Walk"
habit.question = "How many steps did you walk today?" habit.question = "How many steps did you walk today?"
habit.unit = "steps" habit.unit = "steps"
habit.targetType = Habit.AT_LEAST habit.targetType = NumericalHabitType.AT_LEAST
habit.targetValue = 100.0 habit.targetValue = 100.0
habit.color = PaletteColor(1) habit.color = PaletteColor(1)
saveIfSQLite(habit) saveIfSQLite(habit)

@ -84,8 +84,8 @@ class HabitTest : BaseUnitTest() {
@Throws(Exception::class) @Throws(Exception::class)
fun test_isCompleted_numerical() { fun test_isCompleted_numerical() {
val h = modelFactory.buildHabit() val h = modelFactory.buildHabit()
h.type = Habit.NUMBER_HABIT h.type = HabitType.NUMERICAL
h.targetType = Habit.AT_LEAST h.targetType = NumericalHabitType.AT_LEAST
h.targetValue = 100.0 h.targetValue = 100.0
assertFalse(h.isCompletedToday()) assertFalse(h.isCompletedToday())
h.originalEntries.add(Entry(getToday(), 200000)) h.originalEntries.add(Entry(getToday(), 200000))
@ -97,7 +97,7 @@ class HabitTest : BaseUnitTest() {
h.originalEntries.add(Entry(getToday(), 50000)) h.originalEntries.add(Entry(getToday(), 50000))
h.recompute() h.recompute()
assertFalse(h.isCompletedToday()) assertFalse(h.isCompletedToday())
h.targetType = Habit.AT_MOST h.targetType = NumericalHabitType.AT_MOST
h.originalEntries.add(Entry(getToday(), 200000)) h.originalEntries.add(Entry(getToday(), 200000))
h.recompute() h.recompute()
assertFalse(h.isCompletedToday()) assertFalse(h.isCompletedToday())

@ -22,7 +22,8 @@ import org.hamcrest.CoreMatchers.equalTo
import org.hamcrest.MatcherAssert.assertThat import org.hamcrest.MatcherAssert.assertThat
import org.isoron.uhabits.core.BaseUnitTest import org.isoron.uhabits.core.BaseUnitTest
import org.isoron.uhabits.core.models.Frequency 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.PaletteColor import org.isoron.uhabits.core.models.PaletteColor
import org.isoron.uhabits.core.models.Reminder import org.isoron.uhabits.core.models.Reminder
import org.isoron.uhabits.core.models.WeekdayList import org.isoron.uhabits.core.models.WeekdayList
@ -59,9 +60,9 @@ class HabitRecordTest : BaseUnitTest() {
reminder = null reminder = null
id = 1L id = 1L
position = 15 position = 15
type = Habit.NUMBER_HABIT type = HabitType.NUMERICAL
targetValue = 100.0 targetValue = 100.0
targetType = Habit.AT_LEAST targetType = NumericalHabitType.AT_LEAST
unit = "miles" unit = "miles"
} }
val record = HabitRecord() val record = HabitRecord()

Loading…
Cancel
Save