From 420a99f1cf4d3ed7c260a8b6d00dff2f6d918b4d Mon Sep 17 00:00:00 2001 From: Quentin Hibon Date: Tue, 3 Aug 2021 07:28:33 +0200 Subject: [PATCH] Check position before moving habit Workaround for #968. --- .../screens/habits/list/HabitCardListCache.kt | 20 ++++++++++++++++--- .../habits/list/HabitCardListCacheTest.kt | 2 +- 2 files changed, 18 insertions(+), 4 deletions(-) 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 304a38d70..a1490421a 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 @@ -23,6 +23,7 @@ import org.isoron.uhabits.core.AppScope import org.isoron.uhabits.core.commands.Command import org.isoron.uhabits.core.commands.CommandRunner import org.isoron.uhabits.core.commands.CreateRepetitionCommand +import org.isoron.uhabits.core.io.Logging import org.isoron.uhabits.core.models.Habit import org.isoron.uhabits.core.models.HabitList import org.isoron.uhabits.core.models.HabitList.Order @@ -54,8 +55,12 @@ import javax.inject.Inject class HabitCardListCache @Inject constructor( private val allHabits: HabitList, private val commandRunner: CommandRunner, - taskRunner: TaskRunner + taskRunner: TaskRunner, + logging: Logging, ) : CommandRunner.Listener { + + private val logger = logging.getLogger("HabitCardListCache") + private var checkmarkCount = 0 private var currentFetchTask: Task? = null private var listener: Listener @@ -316,8 +321,17 @@ class HabitCardListCache @Inject constructor( toPosition: Int ) { data.habits.removeAt(fromPosition) - data.habits.add(toPosition, habit) - listener.onItemMoved(fromPosition, toPosition) + + // Workaround for https://github.com/iSoron/uhabits/issues/968 + val checkedToPosition = if (toPosition > data.habits.size) { + logger.error("performMove: $toPosition is strictly higher than ${data.habits.size}") + data.habits.size + } else { + toPosition + } + + data.habits.add(checkedToPosition, habit) + listener.onItemMoved(fromPosition, checkedToPosition) } @Synchronized diff --git a/uhabits-core/src/jvmTest/java/org/isoron/uhabits/core/ui/screens/habits/list/HabitCardListCacheTest.kt b/uhabits-core/src/jvmTest/java/org/isoron/uhabits/core/ui/screens/habits/list/HabitCardListCacheTest.kt index cf4c284d8..af3132dde 100644 --- a/uhabits-core/src/jvmTest/java/org/isoron/uhabits/core/ui/screens/habits/list/HabitCardListCacheTest.kt +++ b/uhabits-core/src/jvmTest/java/org/isoron/uhabits/core/ui/screens/habits/list/HabitCardListCacheTest.kt @@ -43,7 +43,7 @@ class HabitCardListCacheTest : BaseUnitTest() { for (i in 0..9) { if (i == 3) habitList.add(fixtures.createLongHabit()) else habitList.add(fixtures.createShortHabit()) } - cache = HabitCardListCache(habitList, commandRunner, taskRunner) + cache = HabitCardListCache(habitList, commandRunner, taskRunner, mock()) cache.setCheckmarkCount(10) cache.refreshAllHabits() cache.onAttached()