Allow user to show/hide archived and completed habits

This commit is contained in:
2019-04-02 07:46:41 -05:00
parent 90f553b4f6
commit 082c575f82
6 changed files with 84 additions and 45 deletions

View File

@@ -70,7 +70,8 @@ class Backend(databaseName: String,
checkmarks[habit]?.setManualCheckmarks(checks)
}
}
mainScreenDataSource = MainScreenDataSource(habits,
mainScreenDataSource = MainScreenDataSource(preferences,
habits,
checkmarks,
taskRunner)
}
@@ -99,6 +100,4 @@ class Backend(databaseName: String,
habitsRepository.update(modified)
}
}
}

View File

@@ -22,8 +22,10 @@ package org.isoron.uhabits.backend
import org.isoron.platform.concurrency.*
import org.isoron.platform.time.*
import org.isoron.uhabits.models.*
import org.isoron.uhabits.models.Checkmark.Companion.UNCHECKED
class MainScreenDataSource(val habits: MutableMap<Int, Habit>,
class MainScreenDataSource(val preferences: Preferences,
val habits: MutableMap<Int, Habit>,
val checkmarks: MutableMap<Habit, CheckmarkList>,
val taskRunner: TaskRunner) {
@@ -41,16 +43,31 @@ class MainScreenDataSource(val habits: MutableMap<Int, Habit>,
fun requestData() {
taskRunner.runInBackground {
val filtered = habits.values.filter { h -> !h.isArchived }
val currentScores = filtered.associate { it to 0.0 /* TODO */}
val recentCheckmarks = filtered.associate {
val allValues = checkmarks[it]!!.getValuesUntil(today)
if (allValues.size <= 7) it to allValues
else it to allValues.subList(0, 7)
var filtered = habits.values.toList()
if (!preferences.showArchived) {
filtered = filtered.filter { !it.isArchived }
}
val data = Data(filtered, currentScores, recentCheckmarks)
val recentCheckmarks = filtered.associate { habit ->
val allValues = checkmarks.getValue(habit).getValuesUntil(today)
if (allValues.size <= 7) habit to allValues
else habit to allValues.subList(0, 7)
}
if (!preferences.showCompleted) {
filtered = filtered.filter { habit ->
recentCheckmarks.getValue(habit)[0] == UNCHECKED
}
}
val currentScores = filtered.associate {
it to 0.0 /* TODO */
}
taskRunner.runInForeground {
observable.notifyListeners { listener ->
val data = Data(filtered, currentScores, recentCheckmarks)
listener.onDataChanged(data)
}
}

View File

@@ -186,4 +186,5 @@ open class Strings() {
open val weekends = "Weekends"
open val year = "Year"
open val yes = "Yes"
open val day_mode = "Day mode"
}