Skip to content

Commit

Permalink
Introduce HabitType and NumericalHabitType enums
Browse files Browse the repository at this point in the history
  • Loading branch information
hiqua authored and iSoron committed Aug 6, 2021
1 parent 95a1786 commit b9eb244
Show file tree
Hide file tree
Showing 10 changed files with 90 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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")
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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"
Expand All @@ -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
Expand Down Expand Up @@ -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
}
}
Original file line number Diff line number Diff line change
@@ -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()
}
}
}
}
Original file line number Diff line number Diff line change
@@ -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()
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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].
Expand Down Expand Up @@ -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
Expand All @@ -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()
}
Expand All @@ -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!!
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit b9eb244

Please sign in to comment.