Fix filtering by status

pull/2020/head
Dharanish 1 year ago
parent 26260fbbc4
commit 584aace548

@ -220,6 +220,8 @@ class HabitCardListCache @Inject constructor(
data.habits.removeAt(pos) data.habits.removeAt(pos)
data.removeWithUUID(uuid) data.removeWithUUID(uuid)
data.positionTypes.removeAt(pos) data.positionTypes.removeAt(pos)
data.positionIndices.removeAt(pos)
data.positionToHabit.remove(pos)
data.decrementPositions(pos + 1, data.positionTypes.size) data.decrementPositions(pos + 1, data.positionTypes.size)
listener.onItemRemoved(pos) listener.onItemRemoved(pos)
} }
@ -233,6 +235,8 @@ class HabitCardListCache @Inject constructor(
data.subHabits[hgrIdx].remove(h) data.subHabits[hgrIdx].remove(h)
data.removeWithUUID(uuid) data.removeWithUUID(uuid)
data.positionTypes.removeAt(pos) data.positionTypes.removeAt(pos)
data.positionIndices.removeAt(pos)
data.positionToHabit.remove(pos)
data.decrementPositions(pos + 1, data.positionTypes.size) data.decrementPositions(pos + 1, data.positionTypes.size)
listener.onItemRemoved(pos) listener.onItemRemoved(pos)
} }
@ -305,6 +309,7 @@ class HabitCardListCache @Inject constructor(
val subHabits: MutableList<MutableList<Habit>> val subHabits: MutableList<MutableList<Habit>>
val uuidToPosition: HashMap<String?, Int> val uuidToPosition: HashMap<String?, Int>
val positionTypes: MutableList<Int> val positionTypes: MutableList<Int>
val positionIndices: MutableList<Int>
val positionToHabit: HashMap<Int, Habit> val positionToHabit: HashMap<Int, Habit>
val positionToHabitGroup: HashMap<Int, HabitGroup> val positionToHabitGroup: HashMap<Int, HabitGroup>
val checkmarks: HashMap<String?, IntArray> val checkmarks: HashMap<String?, IntArray>
@ -381,12 +386,14 @@ class HabitCardListCache @Inject constructor(
positionToHabitGroup.clear() positionToHabitGroup.clear()
uuidToPosition.clear() uuidToPosition.clear()
positionTypes.clear() positionTypes.clear()
positionIndices.clear()
var position = 0 var position = 0
for (h in habits) { for ((idx, h) in habits.withIndex()) {
uuidToHabit[h.uuid] = h uuidToHabit[h.uuid] = h
uuidToPosition[h.uuid] = position uuidToPosition[h.uuid] = position
positionToHabit[position] = h positionToHabit[position] = h
positionTypes.add(STANDALONE_HABIT) positionTypes.add(STANDALONE_HABIT)
positionIndices.add(idx)
position++ position++
} }
@ -395,14 +402,16 @@ class HabitCardListCache @Inject constructor(
uuidToPosition[hgr.uuid] = position uuidToPosition[hgr.uuid] = position
positionToHabitGroup[position] = hgr positionToHabitGroup[position] = hgr
positionTypes.add(HABIT_GROUP) positionTypes.add(HABIT_GROUP)
positionIndices.add(idx)
val habitList = subHabits[idx] val habitList = subHabits[idx]
position++ position++
for (h in habitList) { for ((hIdx, h) in habitList.withIndex()) {
uuidToHabit[h.uuid] = h uuidToHabit[h.uuid] = h
uuidToPosition[h.uuid] = position uuidToPosition[h.uuid] = position
positionToHabit[position] = h positionToHabit[position] = h
positionTypes.add(SUB_HABIT) positionTypes.add(SUB_HABIT)
positionIndices.add(hIdx)
position++ position++
} }
} }
@ -429,13 +438,13 @@ class HabitCardListCache @Inject constructor(
@Synchronized @Synchronized
fun incrementPositions(from: Int, to: Int) { fun incrementPositions(from: Int, to: Int) {
for (pos in positionToHabit.keys.sortedByDescending { it }) { for (pos in positionToHabit.keys.sortedDescending()) {
if (pos in from..to) { if (pos in from..to) {
positionToHabit[pos + 1] = positionToHabit[pos]!! positionToHabit[pos + 1] = positionToHabit[pos]!!
positionToHabit.remove(pos) positionToHabit.remove(pos)
} }
} }
for (pos in positionToHabitGroup.keys.sortedByDescending { it }) { for (pos in positionToHabitGroup.keys.sortedDescending()) {
if (pos in from..to) { if (pos in from..to) {
positionToHabitGroup[pos + 1] = positionToHabitGroup[pos]!! positionToHabitGroup[pos + 1] = positionToHabitGroup[pos]!!
positionToHabitGroup.remove(pos) positionToHabitGroup.remove(pos)
@ -450,13 +459,13 @@ class HabitCardListCache @Inject constructor(
@Synchronized @Synchronized
fun decrementPositions(fromPosition: Int, toPosition: Int) { fun decrementPositions(fromPosition: Int, toPosition: Int) {
for (pos in positionToHabit.keys.sortedBy { it }) { for (pos in positionToHabit.keys.sorted()) {
if (pos in fromPosition..toPosition) { if (pos in fromPosition..toPosition) {
positionToHabit[pos - 1] = positionToHabit[pos]!! positionToHabit[pos - 1] = positionToHabit[pos]!!
positionToHabit.remove(pos) positionToHabit.remove(pos)
} }
} }
for (pos in positionToHabitGroup.keys.sortedBy { it }) { for (pos in positionToHabitGroup.keys.sorted()) {
if (pos in fromPosition..toPosition) { if (pos in fromPosition..toPosition) {
positionToHabitGroup[pos - 1] = positionToHabitGroup[pos]!! positionToHabitGroup[pos - 1] = positionToHabitGroup[pos]!!
positionToHabitGroup.remove(pos) positionToHabitGroup.remove(pos)
@ -492,6 +501,7 @@ class HabitCardListCache @Inject constructor(
if (type == STANDALONE_HABIT) { if (type == STANDALONE_HABIT) {
habits.removeAt(fromPosition) habits.removeAt(fromPosition)
positionTypes.removeAt(fromPosition) positionTypes.removeAt(fromPosition)
positionIndices.removeAt(fromPosition)
if (fromPosition < checkedToPosition) { if (fromPosition < checkedToPosition) {
decrementPositions(fromPosition + 1, checkedToPosition) decrementPositions(fromPosition + 1, checkedToPosition)
} else { } else {
@ -499,12 +509,14 @@ class HabitCardListCache @Inject constructor(
} }
habits.add(checkedToPosition, habit) habits.add(checkedToPosition, habit)
positionTypes.add(checkedToPosition, STANDALONE_HABIT) positionTypes.add(checkedToPosition, STANDALONE_HABIT)
positionIndices.add(checkedToPosition, checkedToPosition)
} else { } else {
val hgr = uuidToHabitGroup[habit.parentUUID] val hgr = uuidToHabitGroup[habit.parentUUID]
val hgrIdx = habitGroups.indexOf(hgr) val hgrIdx = habitGroups.indexOf(hgr)
val fromIdx = subHabits[hgrIdx].indexOf(habit) val fromIdx = subHabits[hgrIdx].indexOf(habit)
subHabits[hgrIdx].removeAt(fromIdx) subHabits[hgrIdx].removeAt(fromIdx)
positionTypes.removeAt(fromPosition) positionTypes.removeAt(fromPosition)
positionIndices.removeAt(fromPosition)
if (fromPosition < checkedToPosition) { if (fromPosition < checkedToPosition) {
decrementPositions(fromPosition + 1, checkedToPosition) decrementPositions(fromPosition + 1, checkedToPosition)
} else { } else {
@ -513,6 +525,7 @@ class HabitCardListCache @Inject constructor(
val toIdx = checkedToPosition - uuidToPosition[hgr!!.uuid]!! - 1 val toIdx = checkedToPosition - uuidToPosition[hgr!!.uuid]!! - 1
subHabits[hgrIdx].add(toIdx, habit) subHabits[hgrIdx].add(toIdx, habit)
positionTypes.add(checkedToPosition, SUB_HABIT) positionTypes.add(checkedToPosition, SUB_HABIT)
positionIndices.add(checkedToPosition, toIdx)
} }
positionToHabit[checkedToPosition] = habit positionToHabit[checkedToPosition] = habit
@ -528,7 +541,7 @@ class HabitCardListCache @Inject constructor(
) { ) {
if (positionTypes[fromPosition] != HABIT_GROUP) return if (positionTypes[fromPosition] != HABIT_GROUP) return
if (!isValidInsert(habitGroup, toPosition)) return if (!isValidInsert(habitGroup, toPosition)) return
val fromIdx = habitGroups.indexOf(habitGroup) val fromIdx = positionIndices[fromPosition]
val habitList = subHabits[fromIdx] val habitList = subHabits[fromIdx]
val toIdx = habitGroups.indexOf(positionToHabitGroup[toPosition]) - (if (fromPosition < toPosition) 1 else 0) val toIdx = habitGroups.indexOf(positionToHabitGroup[toPosition]) - (if (fromPosition < toPosition) 1 else 0)
@ -559,6 +572,7 @@ class HabitCardListCache @Inject constructor(
habitGroups = LinkedList() habitGroups = LinkedList()
subHabits = LinkedList() subHabits = LinkedList()
positionTypes = LinkedList() positionTypes = LinkedList()
positionIndices = LinkedList()
uuidToPosition = HashMap() uuidToPosition = HashMap()
positionToHabit = HashMap() positionToHabit = HashMap()
positionToHabitGroup = HashMap() positionToHabitGroup = HashMap()
@ -648,12 +662,14 @@ class HabitCardListCache @Inject constructor(
if (habit.parentUUID == null) { if (habit.parentUUID == null) {
data.habits.add(position, habit) data.habits.add(position, habit)
data.positionTypes.add(position, STANDALONE_HABIT) data.positionTypes.add(position, STANDALONE_HABIT)
data.positionIndices.add(position, position)
} else { } else {
val hgr = data.uuidToHabitGroup[habit.parentUUID] val hgr = data.uuidToHabitGroup[habit.parentUUID]
val hgrIdx = data.habitGroups.indexOf(hgr) val hgrIdx = data.habitGroups.indexOf(hgr)
val habitIndex = newData.subHabits[hgrIdx].indexOf(habit) val habitIndex = newData.subHabits[hgrIdx].indexOf(habit)
data.subHabits[hgrIdx].add(habitIndex, habit) data.subHabits[hgrIdx].add(habitIndex, habit)
data.positionTypes.add(position, SUB_HABIT) data.positionTypes.add(position, SUB_HABIT)
data.positionIndices.add(position, habitIndex)
} }
data.incrementPositions(position, data.positionTypes.size - 1) data.incrementPositions(position, data.positionTypes.size - 1)
data.positionToHabit[position] = habit data.positionToHabit[position] = habit

@ -75,6 +75,7 @@ open class ListHabitsBehavior @Inject constructor(
} }
} }
commandRunner.run(CreateRepetitionCommand(list, habit, timestamp, value, newNotes)) commandRunner.run(CreateRepetitionCommand(list, habit, timestamp, value, newNotes))
commandRunner.run(RefreshParentGroupCommand(habit, habitGroupList))
} }
} else { } else {
screen.showCheckmarkPopup( screen.showCheckmarkPopup(
@ -84,6 +85,7 @@ open class ListHabitsBehavior @Inject constructor(
) { newValue: Int, newNotes: String, x: Float, y: Float -> ) { newValue: Int, newNotes: String, x: Float, y: Float ->
if (newValue != entry.value && newValue == YES_MANUAL) screen.showConfetti(habit.color, x, y) if (newValue != entry.value && newValue == YES_MANUAL) screen.showConfetti(habit.color, x, y)
commandRunner.run(CreateRepetitionCommand(list, habit, timestamp, newValue, newNotes)) commandRunner.run(CreateRepetitionCommand(list, habit, timestamp, newValue, newNotes))
commandRunner.run(RefreshParentGroupCommand(habit, habitGroupList))
} }
} }
} }
@ -150,6 +152,9 @@ open class ListHabitsBehavior @Inject constructor(
commandRunner.run( commandRunner.run(
CreateRepetitionCommand(list, habit, timestamp, value, notes) CreateRepetitionCommand(list, habit, timestamp, value, notes)
) )
commandRunner.run(
RefreshParentGroupCommand(habit, habitGroupList)
)
if (value == YES_MANUAL) screen.showConfetti(habit.color, x, y) if (value == YES_MANUAL) screen.showConfetti(habit.color, x, y)
} }

Loading…
Cancel
Save