Merge pull request #1050 from hiqua/968_IndexOutOfBoundsException

Check position before moving habit
feature/sync^2^2^2
Alinson S. Xavier 4 years ago committed by GitHub
commit 9c395243f4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -23,6 +23,7 @@ import org.isoron.uhabits.core.AppScope
import org.isoron.uhabits.core.commands.Command import org.isoron.uhabits.core.commands.Command
import org.isoron.uhabits.core.commands.CommandRunner import org.isoron.uhabits.core.commands.CommandRunner
import org.isoron.uhabits.core.commands.CreateRepetitionCommand 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.Habit
import org.isoron.uhabits.core.models.HabitList import org.isoron.uhabits.core.models.HabitList
import org.isoron.uhabits.core.models.HabitList.Order import org.isoron.uhabits.core.models.HabitList.Order
@ -54,8 +55,12 @@ import javax.inject.Inject
class HabitCardListCache @Inject constructor( class HabitCardListCache @Inject constructor(
private val allHabits: HabitList, private val allHabits: HabitList,
private val commandRunner: CommandRunner, private val commandRunner: CommandRunner,
taskRunner: TaskRunner taskRunner: TaskRunner,
logging: Logging,
) : CommandRunner.Listener { ) : CommandRunner.Listener {
private val logger = logging.getLogger("HabitCardListCache")
private var checkmarkCount = 0 private var checkmarkCount = 0
private var currentFetchTask: Task? = null private var currentFetchTask: Task? = null
private var listener: Listener private var listener: Listener
@ -316,8 +321,17 @@ class HabitCardListCache @Inject constructor(
toPosition: Int toPosition: Int
) { ) {
data.habits.removeAt(fromPosition) 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 @Synchronized

@ -43,7 +43,7 @@ class HabitCardListCacheTest : BaseUnitTest() {
for (i in 0..9) { for (i in 0..9) {
if (i == 3) habitList.add(fixtures.createLongHabit()) else habitList.add(fixtures.createShortHabit()) 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.setCheckmarkCount(10)
cache.refreshAllHabits() cache.refreshAllHabits()
cache.onAttached() cache.onAttached()

Loading…
Cancel
Save