Make HabitGroups simpler

No subgroups
This commit is contained in:
Dharanish
2024-05-28 19:39:53 +02:00
parent 262b49d025
commit 832d51a055
13 changed files with 361 additions and 80 deletions

View File

@@ -0,0 +1,18 @@
package org.isoron.uhabits.core.commands
import org.isoron.uhabits.core.models.HabitGroup
import org.isoron.uhabits.core.models.HabitGroupList
import org.isoron.uhabits.core.models.ModelFactory
data class CreateHabitGroupCommand(
val modelFactory: ModelFactory,
val habitGroupList: HabitGroupList,
val model: HabitGroup
) : Command {
override fun run() {
val habitGroup = modelFactory.buildHabitGroup()
habitGroup.copyFrom(model)
habitGroupList.add(habitGroup)
habitGroup.recompute()
}
}

View File

@@ -0,0 +1,20 @@
package org.isoron.uhabits.core.commands
import org.isoron.uhabits.core.models.HabitGroup
import org.isoron.uhabits.core.models.HabitGroupList
import org.isoron.uhabits.core.models.HabitNotFoundException
data class EditHabitGroupCommand(
val habitGroupList: HabitGroupList,
val habitGroupId: Long,
val modified: HabitGroup
) : Command {
override fun run() {
val habitGroup = habitGroupList.getById(habitGroupId) ?: throw HabitNotFoundException()
habitGroup.copyFrom(modified)
habitGroupList.update(habitGroup)
habitGroup.observable.notifyListeners()
habitGroup.recompute()
habitGroupList.resort()
}
}

View File

@@ -14,7 +14,6 @@ data class HabitGroup(
var reminder: Reminder? = null,
var uuid: String? = null,
var habitList: HabitList,
var habitGroupList: HabitGroupList,
val scores: ScoreList,
val streaks: StreakList,
var parentID: Long? = null,
@@ -33,11 +32,11 @@ data class HabitGroup(
fun hasReminder(): Boolean = reminder != null
fun isCompletedToday(): Boolean {
return habitList.all { it.isCompletedToday() } && habitGroupList.all { it.isCompletedToday() }
return habitList.all { it.isCompletedToday() }
}
fun isEnteredToday(): Boolean {
return habitList.all { it.isEnteredToday() } && habitGroupList.all { it.isEnteredToday() }
return habitList.all { it.isEnteredToday() }
}
fun firstEntryDate(): Timestamp {
@@ -47,16 +46,11 @@ data class HabitGroup(
val first = h.firstEntryDate()
if (earliest.isNewerThan(first)) earliest = first
}
for (hgr in habitGroupList) {
val first = hgr.firstEntryDate()
if (earliest.isNewerThan(first)) earliest = first
}
return earliest
}
fun recompute() {
for (h in habitList) h.recompute()
for (hgr in habitGroupList) hgr.recompute()
val today = DateUtils.getTodayWithOffset()
val to = today.plus(30)
@@ -65,14 +59,12 @@ data class HabitGroup(
scores.combineFrom(
habitList = habitList,
habitGroupList = habitGroupList,
from = from,
to = to
)
streaks.combineFrom(
habitList = habitList,
habitGroupList = habitGroupList,
from = from,
to = to
)
@@ -134,23 +126,6 @@ data class HabitGroup(
}
}
fun getHabitByUUIDDeep(uuid: String?): Habit? {
val habit = habitList.getByUUID(uuid)
if (habit != null) return habit
for (hgr in habitGroupList) {
val found = hgr.getHabitByUUIDDeep(uuid)
if (found != null) return found
}
return null
}
fun getHabitGroupByUUIDDeep(uuid: String?): HabitGroup? {
val habitGroup = habitGroupList.getByUUID(uuid)
if (habitGroup != null) return habitGroup
for (hgr in habitGroupList) {
val found = hgr.getHabitGroupByUUIDDeep(uuid)
if (found != null) return found
}
return null
}
fun getHabitByUUIDDeep(uuid: String?): Habit? =
habitList.getByUUID(uuid)
}

View File

@@ -78,16 +78,6 @@ abstract class HabitGroupList : Iterable<HabitGroup> {
return null
}
fun getHabitGroupByUUIDDeep(uuid: String?): HabitGroup? {
for (hgr in this) {
val habit = hgr.getHabitGroupByUUIDDeep(uuid)
if (habit != null) {
return habit
}
}
return null
}
/**
* Returns the habit that occupies a certain position.
*
@@ -191,21 +181,6 @@ abstract class HabitGroupList : Iterable<HabitGroup> {
habitList.remove(h)
}
}
toRemove.clear()
for (hgr1 in this) {
val hgr2 = getByUUID(hgr1.parentUUID)
if (hgr2 != null) {
hgr2.habitGroupList.add(hgr1)
toRemove.add(hgr1.uuid)
hgr1.parent = hgr2
}
}
for (uuid in toRemove) {
val h = getByUUID(uuid)
if (h != null) {
remove(h)
}
}
for (hgr in this) {
hgr.recompute()
}

View File

@@ -41,12 +41,10 @@ interface ModelFactory {
}
fun buildHabitGroup(): HabitGroup {
val habits = buildHabitList()
val groups = buildHabitGroupList()
val scores = buildScoreList()
val streaks = buildStreakList()
return HabitGroup(
habitList = habits,
habitGroupList = groups,
scores = scores,
streaks = streaks
)

View File

@@ -141,15 +141,13 @@ class ScoreList {
@Synchronized
fun combineFrom(
habitList: HabitList,
habitGroupList: HabitGroupList,
from: Timestamp,
to: Timestamp
) {
var current = to
while (current >= from) {
val habitScores = habitList.map { it.scores[current].value }
val groupScores = habitGroupList.map { it.scores[current].value }
val averageScore = (habitScores + groupScores).average()
val averageScore = habitScores.average()
map[current] = Score(current, averageScore)
current = current.minus(1)
}

View File

@@ -71,7 +71,6 @@ class StreakList {
@Synchronized
fun combineFrom(
habitList: HabitList,
habitGroupList: HabitGroupList,
from: Timestamp,
to: Timestamp
) {
@@ -79,9 +78,7 @@ class StreakList {
var streakRunning = false
var streakStart = from
while (current <= to) {
if (habitList.all { it.streaks.isInStreaks(current) } &&
habitGroupList.all { it.streaks.isInStreaks(current) } &&
!streakRunning
if (habitList.all { it.streaks.isInStreaks(current) } && !streakRunning
) {
streakStart = current
streakRunning = true

View File

@@ -205,10 +205,6 @@ class MemoryHabitGroupList : HabitGroupList {
hgr.habitList.primaryOrder = primaryOrder
hgr.habitList.secondaryOrder = secondaryOrder
hgr.habitList.resort()
hgr.habitGroupList.primaryOrder = primaryOrder
hgr.habitGroupList.secondaryOrder = secondaryOrder
hgr.habitGroupList.resort()
}
if (comparator != null) list.sortWith(comparator!!)
observable.notifyListeners()

View File

@@ -49,12 +49,6 @@ class HabitGroupRecord {
@field:Column
var uuid: String? = null
@field:Column(name = "parent_id")
var parentID: Long? = null
@field:Column(name = "parent_uuid")
var parentUUID: String? = null
fun copyFrom(model: HabitGroup) {
id = model.id
name = model.name
@@ -68,8 +62,6 @@ class HabitGroupRecord {
reminderDays = 0
reminderMin = null
reminderHour = null
parentID = model.parentID
parentUUID = model.parentUUID
if (model.hasReminder()) {
val reminder = model.reminder
reminderHour = requireNonNull(reminder)!!.hour
@@ -87,8 +79,6 @@ class HabitGroupRecord {
habitGroup.isArchived = archived != 0
habitGroup.position = position!!
habitGroup.uuid = uuid
habitGroup.parentID = parentID
habitGroup.parentUUID = parentUUID
if (reminderHour != null && reminderMin != null) {
habitGroup.reminder = Reminder(
reminderHour!!,

View File

@@ -15,7 +15,5 @@ create table HabitGroups (
reminder_hour integer,
reminder_min integer,
question text not null default "",
uuid text,
parent_id integer,
parent_uuid integer
uuid text
);