mirror of
https://github.com/iSoron/uhabits.git
synced 2025-12-06 09:08:52 -06:00
Introduce HabitType and NumericalHabitType enums
This commit is contained in:
@@ -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
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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.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!!
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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())
|
||||
|
||||
@@ -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()
|
||||
|
||||
Reference in New Issue
Block a user