mirror of
https://github.com/iSoron/uhabits.git
synced 2025-12-06 01:08:50 -06:00
Merge pull request #1050 from hiqua/968_IndexOutOfBoundsException
Check position before moving habit
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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()
|
||||
|
||||
Reference in New Issue
Block a user