Skip to content

Commit

Permalink
Merge pull request #1050 from hiqua/968_IndexOutOfBoundsException
Browse files Browse the repository at this point in the history
Check position before moving habit
  • Loading branch information
iSoron authored Aug 6, 2021
2 parents e756a63 + 420a99f commit 9c39524
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 9c39524

Please sign in to comment.