From c0602498062de3e2808d5323e24b9b5f6915ed8b Mon Sep 17 00:00:00 2001 From: Dharanish Date: Sat, 6 Jul 2024 11:29:55 +0200 Subject: [PATCH] Fixed sorting habit groups and sub habits by score --- .../isoron/uhabits/core/models/HabitList.kt | 11 +++++++++++ .../core/models/memory/MemoryHabitList.kt | 7 +++++++ .../core/models/sqlite/SQLiteHabitList.kt | 6 ++++++ .../screens/habits/list/HabitCardListCache.kt | 19 ++++++------------- 4 files changed, 30 insertions(+), 13 deletions(-) diff --git a/uhabits-core/src/jvmMain/java/org/isoron/uhabits/core/models/HabitList.kt b/uhabits-core/src/jvmMain/java/org/isoron/uhabits/core/models/HabitList.kt index 54b19dac1..2309260e2 100644 --- a/uhabits-core/src/jvmMain/java/org/isoron/uhabits/core/models/HabitList.kt +++ b/uhabits-core/src/jvmMain/java/org/isoron/uhabits/core/models/HabitList.kt @@ -136,6 +136,17 @@ abstract class HabitList : Iterable { */ abstract fun remove(h: Habit) + /** + * Removes the reference to the habit from the list at the given position. + * + * Does not affect the repository or records + * + * If the given habit is not in the list, does nothing. + * + * @param h the habit to be removed. + */ + abstract fun removeAt(position: Int) + /** * Removes all the habits from the list. */ diff --git a/uhabits-core/src/jvmMain/java/org/isoron/uhabits/core/models/memory/MemoryHabitList.kt b/uhabits-core/src/jvmMain/java/org/isoron/uhabits/core/models/memory/MemoryHabitList.kt index c17a113fb..15e6f7ef0 100644 --- a/uhabits-core/src/jvmMain/java/org/isoron/uhabits/core/models/memory/MemoryHabitList.kt +++ b/uhabits-core/src/jvmMain/java/org/isoron/uhabits/core/models/memory/MemoryHabitList.kt @@ -191,6 +191,13 @@ class MemoryHabitList : HabitList { observable.notifyListeners() } + @Synchronized + override fun removeAt(position: Int) { + throwIfHasParent() + list.removeAt(position) + observable.notifyListeners() + } + @Synchronized override fun reorder(from: Habit, to: Habit) { throwIfHasParent() diff --git a/uhabits-core/src/jvmMain/java/org/isoron/uhabits/core/models/sqlite/SQLiteHabitList.kt b/uhabits-core/src/jvmMain/java/org/isoron/uhabits/core/models/sqlite/SQLiteHabitList.kt index c53144a90..0988ebfda 100644 --- a/uhabits-core/src/jvmMain/java/org/isoron/uhabits/core/models/sqlite/SQLiteHabitList.kt +++ b/uhabits-core/src/jvmMain/java/org/isoron/uhabits/core/models/sqlite/SQLiteHabitList.kt @@ -157,6 +157,12 @@ class SQLiteHabitList @Inject constructor(private val modelFactory: ModelFactory observable.notifyListeners() } + @Synchronized + override fun removeAt(position: Int) { + loadRecords() + list.removeAt(position) + } + @Synchronized override fun removeAll() { list.removeAll() diff --git a/uhabits-core/src/jvmMain/java/org/isoron/uhabits/core/ui/screens/habits/list/HabitCardListCache.kt b/uhabits-core/src/jvmMain/java/org/isoron/uhabits/core/ui/screens/habits/list/HabitCardListCache.kt index be2612b0c..489e95df4 100644 --- a/uhabits-core/src/jvmMain/java/org/isoron/uhabits/core/ui/screens/habits/list/HabitCardListCache.kt +++ b/uhabits-core/src/jvmMain/java/org/isoron/uhabits/core/ui/screens/habits/list/HabitCardListCache.kt @@ -261,11 +261,6 @@ class HabitCardListCache @Inject constructor( @Synchronized fun reorder(from: Int, to: Int) { if (from == to) return - val uuid = if (data.positionTypes[from] == STANDALONE_HABIT) { - data.positionToHabit[from]!!.uuid - } else { - data.positionToHabitGroup[from]!!.uuid - } if (data.positionTypes[from] == STANDALONE_HABIT) { val habit = data.positionToHabit[from]!! data.performMove(habit, from, to) @@ -423,10 +418,7 @@ class HabitCardListCache @Inject constructor( if (habit.parentUUID == null) { return position <= habits.size } else { - val parent = uuidToHabitGroup[habit.parentUUID] - if (parent == null) { - return false - } + val parent = uuidToHabitGroup[habit.parentUUID] ?: return false val parentPosition = uuidToPosition[habit.parentUUID]!! val parentIndex = habitGroups.indexOf(parent) val nextGroup = habitGroups.getOrNull(parentIndex + 1) @@ -494,7 +486,7 @@ class HabitCardListCache @Inject constructor( // Workaround for https://github.com/iSoron/uhabits/issues/968 val checkedToPosition = if (toPosition > positionTypes.size) { logger.error("performMove: $toPosition for habit is strictly higher than ${habits.size}") - positionTypes.size + positionTypes.size - 1 } else { toPosition } @@ -515,15 +507,16 @@ class HabitCardListCache @Inject constructor( } else { val hgr = uuidToHabitGroup[habit.parentUUID] val hgrIdx = habitGroups.indexOf(hgr) - val h = positionToHabit[fromPosition]!! - subHabits[hgrIdx].remove(h) + val fromIdx = subHabits[hgrIdx].indexOf(habit) + subHabits[hgrIdx].removeAt(fromIdx) positionTypes.removeAt(fromPosition) if (fromPosition < checkedToPosition) { decrementPositions(fromPosition + 1, checkedToPosition) } else { incrementPositions(checkedToPosition, fromPosition - 1) } - subHabits[hgrIdx].add(checkedToPosition - uuidToPosition[hgr!!.uuid]!! - 1, habit) + val toIdx = checkedToPosition - uuidToPosition[hgr!!.uuid]!! - 1 + subHabits[hgrIdx].add(toIdx, habit) positionTypes.add(checkedToPosition, SUB_HABIT) }