Show numerical habits

This commit is contained in:
2019-04-02 20:16:00 -05:00
parent 082c575f82
commit 33bae657a3
10 changed files with 63 additions and 21 deletions

View File

@@ -66,7 +66,7 @@ class Backend(databaseName: String,
habits.putAll(habitsRepository.findAll())
for ((key, habit) in habits) {
val checks = checkmarkRepository.findAll(key)
checkmarks[habit] = CheckmarkList(habit.frequency)
checkmarks[habit] = CheckmarkList(habit.frequency, habit.type)
checkmarks[habit]?.setManualCheckmarks(checks)
}
}
@@ -81,7 +81,7 @@ class Backend(databaseName: String,
habit.id = id
habit.position = habits.size
habits[id] = habit
checkmarks[habit] = CheckmarkList(habit.frequency)
checkmarks[habit] = CheckmarkList(habit.frequency, habit.type)
habitsRepository.insert(habit)
mainScreenDataSource.requestData()
}

View File

@@ -57,7 +57,8 @@ class MainScreenDataSource(val preferences: Preferences,
if (!preferences.showCompleted) {
filtered = filtered.filter { habit ->
recentCheckmarks.getValue(habit)[0] == UNCHECKED
(habit.type == HabitType.BOOLEAN_HABIT && recentCheckmarks.getValue(habit)[0] == UNCHECKED) ||
(habit.type == HabitType.NUMERICAL_HABIT && recentCheckmarks.getValue(habit)[0] * 1000 < habit.target)
}
}

View File

@@ -24,7 +24,8 @@ import org.isoron.uhabits.models.Checkmark.Companion.CHECKED_AUTOMATIC
import org.isoron.uhabits.models.Checkmark.Companion.CHECKED_MANUAL
import org.isoron.uhabits.models.Checkmark.Companion.UNCHECKED
class CheckmarkList(private val frequency: Frequency) {
class CheckmarkList(private val frequency: Frequency,
private val habitType: HabitType) {
private val manualCheckmarks = mutableListOf<Checkmark>()
private val automaticCheckmarks = mutableListOf<Checkmark>()
@@ -37,8 +38,12 @@ class CheckmarkList(private val frequency: Frequency) {
manualCheckmarks.clear()
automaticCheckmarks.clear()
manualCheckmarks.addAll(checks)
automaticCheckmarks.addAll(computeAutomaticCheckmarks(checks,
frequency))
if (habitType == HabitType.NUMERICAL_HABIT) {
automaticCheckmarks.addAll(checks)
} else {
val computed = computeAutomaticCheckmarks(checks, frequency)
automaticCheckmarks.addAll(computed)
}
}
/**

View File

@@ -75,7 +75,7 @@ class HabitRepository(var db: Database) {
position = stmt.getInt(7),
unit = stmt.getText(8),
target = stmt.getReal(9),
type = if (stmt.getInt(10) == 0) HabitType.YES_NO_HABIT else HabitType.NUMERICAL_HABIT)
type = if (stmt.getInt(10) == 0) HabitType.BOOLEAN_HABIT else HabitType.NUMERICAL_HABIT)
}
private fun bindHabitToStatement(habit: Habit, statement: PreparedStatement) {

View File

@@ -20,6 +20,6 @@
package org.isoron.uhabits.models
enum class HabitType(val code: Int) {
YES_NO_HABIT(0),
BOOLEAN_HABIT(0),
NUMERICAL_HABIT(1),
}

View File

@@ -159,7 +159,7 @@ class CheckmarkListTest : BaseTest() {
@Test
fun testGetValuesUntil() {
val list = CheckmarkList(Frequency(1, 2))
val list = CheckmarkList(Frequency(1, 2), HabitType.BOOLEAN_HABIT)
list.setManualCheckmarks(listOf(Checkmark(day(4), CHECKED_MANUAL),
Checkmark(day(7), CHECKED_MANUAL)))
val expected = listOf(UNCHECKED,
@@ -182,7 +182,7 @@ class CheckmarkListTest : BaseTest() {
@Test
fun testGetValuesUntil2() {
val list = CheckmarkList(Frequency(1, 2))
val list = CheckmarkList(Frequency(1, 2), HabitType.BOOLEAN_HABIT)
val expected = listOf<Int>()
assertEquals(expected, list.getValuesUntil(day(0)))
}

View File

@@ -44,7 +44,7 @@ class HabitRepositoryTest : BaseTest() {
position = 0,
unit = "",
target = 0.0,
type = HabitType.YES_NO_HABIT)
type = HabitType.BOOLEAN_HABIT)
original1 = Habit(id = 1,
name = "Exercise",
@@ -55,7 +55,7 @@ class HabitRepositoryTest : BaseTest() {
position = 1,
unit = "",
target = 0.0,
type = HabitType.YES_NO_HABIT)
type = HabitType.BOOLEAN_HABIT)
original2 = Habit(id = 2,
name = "Learn Japanese",
@@ -66,7 +66,7 @@ class HabitRepositoryTest : BaseTest() {
position = 2,
unit = "",
target = 0.0,
type = HabitType.YES_NO_HABIT)
type = HabitType.BOOLEAN_HABIT)
repository = HabitRepository(db)
}