From 9f15c920bea20a34e09345d925d71aad79e6a4fb Mon Sep 17 00:00:00 2001 From: engineering4good <77828983+engineering4good@users.noreply.github.com> Date: Sun, 11 Apr 2021 20:57:02 +0300 Subject: [PATCH] Fix app crash on habit deletion when sorted not manually. --- .../isoron/uhabits/core/models/sqlite/SQLiteHabitList.kt | 4 +++- .../uhabits/core/models/sqlite/SQLiteHabitListTest.kt | 8 ++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) 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 eb08ae3cf..d15a29fe3 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 @@ -131,7 +131,9 @@ class SQLiteHabitList @Inject constructor(private val modelFactory: ModelFactory @Synchronized override fun remove(h: Habit) { loadRecords() - reorder(h, list.getByPosition(size() - 1)) + if (Order.BY_POSITION.equals(list.primaryOrder)) { + reorder(h, list.getByPosition(size() - 1)) + } list.remove(h) val record = repository.find( h.id!! diff --git a/uhabits-core/src/jvmTest/java/org/isoron/uhabits/core/models/sqlite/SQLiteHabitListTest.kt b/uhabits-core/src/jvmTest/java/org/isoron/uhabits/core/models/sqlite/SQLiteHabitListTest.kt index 07ac72e02..787f04845 100644 --- a/uhabits-core/src/jvmTest/java/org/isoron/uhabits/core/models/sqlite/SQLiteHabitListTest.kt +++ b/uhabits-core/src/jvmTest/java/org/isoron/uhabits/core/models/sqlite/SQLiteHabitListTest.kt @@ -157,6 +157,14 @@ class SQLiteHabitListTest : BaseUnitTest() { val h = habitList.getById(2) habitList.remove(h!!) assertThat(habitList.indexOf(h), equalTo(-1)) + + habitList.add(h) + val primaryOrder = habitList.primaryOrder + habitList.primaryOrder = HabitList.Order.BY_NAME_DESC + habitList.remove(h) + assertThat(habitList.indexOf(h), equalTo(-1)) + habitList.primaryOrder = primaryOrder + var rec = repository.find(2L) assertNull(rec) rec = repository.find(3L)!!