diff --git a/uhabits-android/src/androidTest/java/org/isoron/uhabits/HabitFixtures.kt b/uhabits-android/src/androidTest/java/org/isoron/uhabits/HabitFixtures.kt index 3cc5f14fc..9cbfc1f9c 100644 --- a/uhabits-android/src/androidTest/java/org/isoron/uhabits/HabitFixtures.kt +++ b/uhabits-android/src/androidTest/java/org/isoron/uhabits/HabitFixtures.kt @@ -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.Companion.DAILY 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.HabitType 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.Timestamp 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 { name = "Read" question = "How many pages did you walk today?" - type = NUMBER_HABIT - targetType = AT_LEAST + type = HabitType.NUMERICAL + targetType = NumericalHabitType.AT_LEAST targetValue = 200.0 unit = "pages" } 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 c5e432b1f..e10652ee3 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 @@ -35,11 +35,7 @@ import androidx.appcompat.app.AppCompatActivity import androidx.fragment.app.DialogFragment import com.android.datetimepicker.time.RadialPickerLayout import com.android.datetimepicker.time.TimePickerDialog -import kotlinx.android.synthetic.main.activity_edit_habit.nameInput -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 kotlinx.android.synthetic.main.activity_edit_habit.* import org.isoron.uhabits.HabitsApplication import org.isoron.uhabits.R 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.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.Reminder import org.isoron.uhabits.core.models.WeekdayList @@ -77,7 +75,7 @@ class EditHabitActivity : AppCompatActivity() { private lateinit var commandRunner: CommandRunner var habitId = -1L - var habitType = -1 + lateinit var habitType: HabitType var unit = "" var color = PaletteColor(11) var androidColor = 0 @@ -116,12 +114,12 @@ class EditHabitActivity : AppCompatActivity() { binding.unitInput.setText(habit.unit) binding.targetInput.setText(habit.targetValue.toString()) } else { - habitType = intent.getIntExtra("habitType", Habit.YES_NO_HABIT) + habitType = HabitType.fromInt(intent.getIntExtra("habitType", HabitType.YES_NO.value)) } if (state != null) { habitId = state.getLong("habitId") - habitType = state.getInt("habitType") + habitType = HabitType.fromInt(state.getInt("habitType")) color = PaletteColor(state.getInt("paletteColor")) freqNum = state.getInt("freqNum") freqDen = state.getInt("freqDen") @@ -132,13 +130,16 @@ class EditHabitActivity : AppCompatActivity() { updateColors() - if (habitType == Habit.YES_NO_HABIT) { - binding.unitOuterBox.visibility = View.GONE - binding.targetOuterBox.visibility = View.GONE - } else { - binding.nameInput.hint = getString(R.string.measurable_short_example) - binding.questionInput.hint = getString(R.string.measurable_question_example) - binding.frequencyOuterBox.visibility = View.GONE + when (habitType) { + HabitType.YES_NO -> { + binding.unitOuterBox.visibility = View.GONE + binding.targetOuterBox.visibility = View.GONE + } + HabitType.NUMERICAL -> { + 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) @@ -255,9 +256,9 @@ class EditHabitActivity : AppCompatActivity() { } habit.frequency = Frequency(freqNum, freqDen) - if (habitType == Habit.NUMBER_HABIT) { + if (habitType == HabitType.NUMERICAL) { habit.targetValue = targetInput.text.toString().toDouble() - habit.targetType = Habit.AT_LEAST + habit.targetType = NumericalHabitType.AT_LEAST habit.unit = unitInput.text.trim().toString() } habit.type = habitType @@ -285,7 +286,7 @@ class EditHabitActivity : AppCompatActivity() { nameInput.error = getFormattedValidationError(R.string.validation_cannot_be_blank) isValid = false } - if (habitType == Habit.NUMBER_HABIT) { + if (habitType == HabitType.NUMERICAL) { if (targetInput.text.isEmpty()) { targetInput.error = getString(R.string.validation_cannot_be_blank) isValid = false @@ -338,7 +339,7 @@ class EditHabitActivity : AppCompatActivity() { super.onSaveInstanceState(state) with(state) { putLong("habitId", habitId) - putInt("habitType", habitType) + putInt("habitType", habitType.value) putInt("paletteColor", color.paletteIndex) putInt("androidColor", androidColor) putInt("freqNum", freqNum) diff --git a/uhabits-android/src/main/java/org/isoron/uhabits/activities/habits/edit/HabitTypeDialog.kt b/uhabits-android/src/main/java/org/isoron/uhabits/activities/habits/edit/HabitTypeDialog.kt index 382dab282..25ba9537e 100644 --- a/uhabits-android/src/main/java/org/isoron/uhabits/activities/habits/edit/HabitTypeDialog.kt +++ b/uhabits-android/src/main/java/org/isoron/uhabits/activities/habits/edit/HabitTypeDialog.kt @@ -25,7 +25,7 @@ import android.view.View import android.view.ViewGroup import androidx.appcompat.app.AppCompatDialogFragment 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.intents.IntentFactory @@ -40,13 +40,13 @@ class HabitTypeDialog : AppCompatDialogFragment() { val binding = SelectHabitTypeBinding.inflate(inflater, container, false) binding.buttonYesNo.setOnClickListener { - val intent = IntentFactory().startEditActivity(activity!!, Habit.YES_NO_HABIT) + val intent = IntentFactory().startEditActivity(activity!!, HabitType.YES_NO.value) startActivity(intent) dismiss() } binding.buttonMeasurable.setOnClickListener { - val intent = IntentFactory().startEditActivity(activity!!, Habit.NUMBER_HABIT) + val intent = IntentFactory().startEditActivity(activity!!, HabitType.NUMERICAL.value) startActivity(intent) dismiss() } 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 b9bafb90f..d25e7b310 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,9 +31,9 @@ data class Habit( var position: Int = 0, var question: String = "", var reminder: Reminder? = null, - var targetType: Int = AT_LEAST, + var targetType: NumericalHabitType = NumericalHabitType.AT_LEAST, var targetValue: Double = 0.0, - var type: Int = YES_NO_HABIT, + var type: HabitType = HabitType.YES_NO, var unit: String = "", var uuid: String? = null, val computedEntries: EntryList, @@ -48,7 +48,7 @@ data class Habit( var observable = ModelObservable() val isNumerical: Boolean - get() = type == NUMBER_HABIT + get() = type == HabitType.NUMERICAL val uriString: String get() = "content://org.isoron.uhabits/habit/$id" @@ -59,10 +59,9 @@ data class Habit( val today = DateUtils.getTodayWithOffset() val value = computedEntries.get(today).value return if (isNumerical) { - if (targetType == AT_LEAST) { - value / 1000.0 >= targetValue - } else { - value / 1000.0 <= targetValue + when (targetType) { + NumericalHabitType.AT_LEAST -> value / 1000.0 >= targetValue + NumericalHabitType.AT_MOST -> value / 1000.0 <= targetValue } } else { value != Entry.NO && value != Entry.UNKNOWN @@ -146,18 +145,11 @@ data class Habit( result = 31 * result + position result = 31 * result + question.hashCode() result = 31 * result + (reminder?.hashCode() ?: 0) - result = 31 * result + targetType + result = 31 * result + targetType.value result = 31 * result + targetValue.hashCode() - result = 31 * result + type + result = 31 * result + type.value result = 31 * result + unit.hashCode() result = 31 * result + (uuid?.hashCode() ?: 0) 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 - } } diff --git a/uhabits-core/src/jvmMain/java/org/isoron/uhabits/core/models/HabitType.kt b/uhabits-core/src/jvmMain/java/org/isoron/uhabits/core/models/HabitType.kt new file mode 100644 index 000000000..c9a4a674d --- /dev/null +++ b/uhabits-core/src/jvmMain/java/org/isoron/uhabits/core/models/HabitType.kt @@ -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() + } + } + } +} diff --git a/uhabits-core/src/jvmMain/java/org/isoron/uhabits/core/models/NumericalHabitType.kt b/uhabits-core/src/jvmMain/java/org/isoron/uhabits/core/models/NumericalHabitType.kt new file mode 100644 index 000000000..d0150d92e --- /dev/null +++ b/uhabits-core/src/jvmMain/java/org/isoron/uhabits/core/models/NumericalHabitType.kt @@ -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() + } + } + } +} diff --git a/uhabits-core/src/jvmMain/java/org/isoron/uhabits/core/models/sqlite/records/HabitRecord.kt b/uhabits-core/src/jvmMain/java/org/isoron/uhabits/core/models/sqlite/records/HabitRecord.kt index 3da7b3ed0..dc0386799 100644 --- a/uhabits-core/src/jvmMain/java/org/isoron/uhabits/core/models/sqlite/records/HabitRecord.kt +++ b/uhabits-core/src/jvmMain/java/org/isoron/uhabits/core/models/sqlite/records/HabitRecord.kt @@ -22,10 +22,12 @@ import org.isoron.uhabits.core.database.Column import org.isoron.uhabits.core.database.Table 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.Reminder import org.isoron.uhabits.core.models.WeekdayList -import java.util.Objects +import java.util.Objects.requireNonNull /** * The SQLite database record corresponding to a [Habit]. @@ -93,8 +95,8 @@ class HabitRecord { highlight = 0 color = model.color.paletteIndex archived = if (model.isArchived) 1 else 0 - type = model.type - targetType = model.targetType + type = model.type.value + targetType = model.targetType.value targetValue = model.targetValue unit = model.unit position = model.position @@ -108,7 +110,7 @@ class HabitRecord { reminderHour = null if (model.hasReminder()) { val reminder = model.reminder - reminderHour = Objects.requireNonNull(reminder)!!.hour + reminderHour = requireNonNull(reminder)!!.hour reminderMin = reminder!!.minute reminderDays = reminder.days.toInteger() } @@ -122,8 +124,8 @@ class HabitRecord { habit.frequency = Frequency(freqNum!!, freqDen!!) habit.color = PaletteColor(color!!) habit.isArchived = archived != 0 - habit.type = type!! - habit.targetType = targetType!! + habit.type = HabitType.fromInt(type!!) + habit.targetType = NumericalHabitType.fromInt(targetType!!) habit.targetValue = targetValue!! habit.unit = unit!! habit.position = position!! diff --git a/uhabits-core/src/jvmMain/java/org/isoron/uhabits/core/test/HabitFixtures.kt b/uhabits-core/src/jvmMain/java/org/isoron/uhabits/core/test/HabitFixtures.kt index fca65af71..38a34f0e1 100644 --- a/uhabits-core/src/jvmMain/java/org/isoron/uhabits/core/test/HabitFixtures.kt +++ b/uhabits-core/src/jvmMain/java/org/isoron/uhabits/core/test/HabitFixtures.kt @@ -22,7 +22,9 @@ import org.isoron.uhabits.core.models.Entry import org.isoron.uhabits.core.models.Frequency import org.isoron.uhabits.core.models.Habit 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.NumericalHabitType import org.isoron.uhabits.core.models.PaletteColor import org.isoron.uhabits.core.models.Timestamp import org.isoron.uhabits.core.models.sqlite.SQLiteEntryList @@ -65,11 +67,11 @@ class HabitFixtures(private val modelFactory: ModelFactory, private val habitLis fun createNumericalHabit(): Habit { val habit = modelFactory.buildHabit() - habit.type = Habit.NUMBER_HABIT + habit.type = HabitType.NUMERICAL habit.name = "Run" habit.question = "How many miles did you run today?" habit.unit = "miles" - habit.targetType = Habit.AT_LEAST + habit.targetType = NumericalHabitType.AT_LEAST habit.targetValue = 2.0 habit.color = PaletteColor(1) saveIfSQLite(habit) @@ -86,11 +88,11 @@ class HabitFixtures(private val modelFactory: ModelFactory, private val habitLis fun createLongNumericalHabit(reference: Timestamp): Habit { val habit = modelFactory.buildHabit() - habit.type = Habit.NUMBER_HABIT + habit.type = HabitType.NUMERICAL habit.name = "Walk" habit.question = "How many steps did you walk today?" habit.unit = "steps" - habit.targetType = Habit.AT_LEAST + habit.targetType = NumericalHabitType.AT_LEAST habit.targetValue = 100.0 habit.color = PaletteColor(1) saveIfSQLite(habit) diff --git a/uhabits-core/src/jvmTest/java/org/isoron/uhabits/core/models/HabitTest.kt b/uhabits-core/src/jvmTest/java/org/isoron/uhabits/core/models/HabitTest.kt index d13c8468e..8d098c762 100644 --- a/uhabits-core/src/jvmTest/java/org/isoron/uhabits/core/models/HabitTest.kt +++ b/uhabits-core/src/jvmTest/java/org/isoron/uhabits/core/models/HabitTest.kt @@ -84,8 +84,8 @@ class HabitTest : BaseUnitTest() { @Throws(Exception::class) fun test_isCompleted_numerical() { val h = modelFactory.buildHabit() - h.type = Habit.NUMBER_HABIT - h.targetType = Habit.AT_LEAST + h.type = HabitType.NUMERICAL + h.targetType = NumericalHabitType.AT_LEAST h.targetValue = 100.0 assertFalse(h.isCompletedToday()) h.originalEntries.add(Entry(getToday(), 200000)) @@ -97,7 +97,7 @@ class HabitTest : BaseUnitTest() { h.originalEntries.add(Entry(getToday(), 50000)) h.recompute() assertFalse(h.isCompletedToday()) - h.targetType = Habit.AT_MOST + h.targetType = NumericalHabitType.AT_MOST h.originalEntries.add(Entry(getToday(), 200000)) h.recompute() assertFalse(h.isCompletedToday()) diff --git a/uhabits-core/src/jvmTest/java/org/isoron/uhabits/core/models/sqlite/records/HabitRecordTest.kt b/uhabits-core/src/jvmTest/java/org/isoron/uhabits/core/models/sqlite/records/HabitRecordTest.kt index 4fad8f0b6..d23c7d8a9 100644 --- a/uhabits-core/src/jvmTest/java/org/isoron/uhabits/core/models/sqlite/records/HabitRecordTest.kt +++ b/uhabits-core/src/jvmTest/java/org/isoron/uhabits/core/models/sqlite/records/HabitRecordTest.kt @@ -22,7 +22,8 @@ import org.hamcrest.CoreMatchers.equalTo import org.hamcrest.MatcherAssert.assertThat import org.isoron.uhabits.core.BaseUnitTest 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.Reminder import org.isoron.uhabits.core.models.WeekdayList @@ -59,9 +60,9 @@ class HabitRecordTest : BaseUnitTest() { reminder = null id = 1L position = 15 - type = Habit.NUMBER_HABIT + type = HabitType.NUMERICAL targetValue = 100.0 - targetType = Habit.AT_LEAST + targetType = NumericalHabitType.AT_LEAST unit = "miles" } val record = HabitRecord()