Fix streaks of HabitGroups

pull/2020/head
Dharanish 1 year ago
parent 584aace548
commit 3ba214ff61

@ -74,19 +74,24 @@ class StreakList {
from: Timestamp, from: Timestamp,
to: Timestamp to: Timestamp
) { ) {
list.clear()
if (habitList.isEmpty) return if (habitList.isEmpty) return
var current = from var current = from
var streakRunning = false var streakRunning = false
var streakStart = from var streakStart = from
while (current <= to) { while (current <= to) {
if (habitList.all { it.streaks.isInStreaks(current) } && !streakRunning) { if (habitList.all { it.streaks.isInStreaks(current) }) {
if (!streakRunning) {
streakStart = current streakStart = current
streakRunning = true streakRunning = true
} else if (streakRunning) { }
} else {
if (streakRunning) {
val streakEnd = current.minus(1) val streakEnd = current.minus(1)
list.add(Streak(streakStart, streakEnd)) list.add(Streak(streakStart, streakEnd))
streakRunning = false streakRunning = false
} }
}
current = current.plus(1) current = current.plus(1)
} }
} }

@ -216,35 +216,28 @@ class HabitCardListCache @Inject constructor(
if (type == STANDALONE_HABIT) { if (type == STANDALONE_HABIT) {
val h = data.uuidToHabit[uuid] val h = data.uuidToHabit[uuid]
if (h != null) { if (h != null) {
val pos = data.habits.indexOf(h) data.habits.removeAt(position)
data.habits.removeAt(pos)
data.removeWithUUID(uuid) data.removeWithUUID(uuid)
data.positionTypes.removeAt(pos) data.removeWithPos(position)
data.positionIndices.removeAt(pos) data.decrementPositions(position + 1, data.positionTypes.size)
data.positionToHabit.remove(pos) listener.onItemRemoved(position)
data.decrementPositions(pos + 1, data.positionTypes.size)
listener.onItemRemoved(pos)
} }
} else if (type == SUB_HABIT) { } else if (type == SUB_HABIT) {
val h = data.uuidToHabit[uuid] val h = data.uuidToHabit[uuid]
if (h != null) { if (h != null) {
val pos = data.uuidToPosition[uuid]!!
val hgrUUID = h.parentUUID val hgrUUID = h.parentUUID
val hgr = data.uuidToHabitGroup[hgrUUID] val hgr = data.uuidToHabitGroup[hgrUUID]
val hgrIdx = data.habitGroups.indexOf(hgr) val hgrIdx = data.habitGroups.indexOf(hgr)
data.subHabits[hgrIdx].remove(h) data.subHabits[hgrIdx].remove(h)
data.removeWithUUID(uuid) data.removeWithUUID(uuid)
data.positionTypes.removeAt(pos) data.removeWithPos(position)
data.positionIndices.removeAt(pos) data.decrementPositions(position + 1, data.positionTypes.size)
data.positionToHabit.remove(pos) listener.onItemRemoved(position)
data.decrementPositions(pos + 1, data.positionTypes.size)
listener.onItemRemoved(pos)
} }
} else if (type == HABIT_GROUP) { } else if (type == HABIT_GROUP) {
val hgr = data.uuidToHabitGroup[uuid] val hgr = data.uuidToHabitGroup[uuid]
if (hgr != null) { if (hgr != null) {
val pos = data.uuidToPosition[uuid]!! val hgrIdx = data.positionIndices[position]
val hgrIdx = data.habitGroups.indexOf(hgr)
for (habit in data.subHabits[hgrIdx].reversed()) { for (habit in data.subHabits[hgrIdx].reversed()) {
val habitPos = data.uuidToPosition[habit.uuid]!! val habitPos = data.uuidToPosition[habit.uuid]!!
@ -255,7 +248,7 @@ class HabitCardListCache @Inject constructor(
data.habitGroups.removeAt(hgrIdx) data.habitGroups.removeAt(hgrIdx)
data.removeWithUUID(hgr.uuid) data.removeWithUUID(hgr.uuid)
data.rebuildPositions() data.rebuildPositions()
listener.onItemRemoved(pos) listener.onItemRemoved(position)
} }
} }
} }
@ -500,8 +493,7 @@ class HabitCardListCache @Inject constructor(
if (type == STANDALONE_HABIT) { if (type == STANDALONE_HABIT) {
habits.removeAt(fromPosition) habits.removeAt(fromPosition)
positionTypes.removeAt(fromPosition) removeWithPos(fromPosition)
positionIndices.removeAt(fromPosition)
if (fromPosition < checkedToPosition) { if (fromPosition < checkedToPosition) {
decrementPositions(fromPosition + 1, checkedToPosition) decrementPositions(fromPosition + 1, checkedToPosition)
} else { } else {
@ -513,10 +505,9 @@ class HabitCardListCache @Inject constructor(
} 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 = positionIndices[fromPosition]
subHabits[hgrIdx].removeAt(fromIdx) subHabits[hgrIdx].removeAt(fromIdx)
positionTypes.removeAt(fromPosition) removeWithPos(fromPosition)
positionIndices.removeAt(fromPosition)
if (fromPosition < checkedToPosition) { if (fromPosition < checkedToPosition) {
decrementPositions(fromPosition + 1, checkedToPosition) decrementPositions(fromPosition + 1, checkedToPosition)
} else { } else {
@ -564,6 +555,12 @@ class HabitCardListCache @Inject constructor(
checkmarks.remove(uuid) checkmarks.remove(uuid)
} }
fun removeWithPos(pos: Int) {
positionTypes.removeAt(pos)
positionIndices.removeAt(pos)
positionToHabit.remove(pos)
}
/** /**
* Creates a new CacheData without any content. * Creates a new CacheData without any content.
*/ */
@ -664,9 +661,9 @@ class HabitCardListCache @Inject constructor(
data.positionTypes.add(position, STANDALONE_HABIT) data.positionTypes.add(position, STANDALONE_HABIT)
data.positionIndices.add(position, position) data.positionIndices.add(position, position)
} else { } else {
val hgr = data.uuidToHabitGroup[habit.parentUUID] val hgrPos = data.uuidToPosition[habit.parentUUID]!!
val hgrIdx = data.habitGroups.indexOf(hgr) val hgrIdx = data.positionIndices[hgrPos]
val habitIndex = newData.subHabits[hgrIdx].indexOf(habit) val habitIndex = newData.positionIndices[position]
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.positionIndices.add(position, habitIndex)
@ -685,10 +682,13 @@ class HabitCardListCache @Inject constructor(
private fun performInsert(habitGroup: HabitGroup, position: Int) { private fun performInsert(habitGroup: HabitGroup, position: Int) {
if (!data.isValidInsert(habitGroup, position)) return if (!data.isValidInsert(habitGroup, position)) return
val uuid = habitGroup.uuid val uuid = habitGroup.uuid
val prevIdx = newData.habitGroups.indexOf(habitGroup) val prevIdx = newData.positionIndices[position]
val habitList = newData.subHabits[prevIdx] val habitList = newData.subHabits[prevIdx]
var idx = data.habitGroups.indexOf(data.positionToHabitGroup[position]) val idx = if (data.positionIndices.size > position) {
if (idx < 0) idx = data.habitGroups.size data.positionIndices[position]
} else {
data.habitGroups.size
}
data.habitGroups.add(idx, habitGroup) data.habitGroups.add(idx, habitGroup)
data.subHabits.add(prevIdx, habitList) data.subHabits.add(prevIdx, habitList)
@ -737,9 +737,9 @@ class HabitCardListCache @Inject constructor(
val newPosition = if (type == STANDALONE_HABIT) { val newPosition = if (type == STANDALONE_HABIT) {
currentPosition currentPosition
} else { } else {
val hgr = data.uuidToHabitGroup[habit.parentUUID] val hgrPos = data.uuidToPosition[habit.parentUUID]!!
val hgrIdx = data.habitGroups.indexOf(hgr) val hgrIdx = data.positionIndices[hgrPos]
newData.subHabits[hgrIdx].indexOf(habit) + data.uuidToPosition[hgr!!.uuid]!! + 1 newData.subHabits[hgrIdx].indexOf(habit) + hgrPos + 1
} }
if (prevPosition < 0) { if (prevPosition < 0) {
performInsert(habit, newPosition) performInsert(habit, newPosition)

@ -158,12 +158,6 @@ open class ListHabitsBehavior @Inject constructor(
if (value == YES_MANUAL) screen.showConfetti(habit.color, x, y) if (value == YES_MANUAL) screen.showConfetti(habit.color, x, y)
} }
fun onChangeScore(habit: Habit) {
commandRunner.run(
RefreshParentGroupCommand(habit, habitGroupList)
)
}
enum class Message { enum class Message {
COULD_NOT_EXPORT, COULD_NOT_EXPORT,
IMPORT_SUCCESSFUL, IMPORT_SUCCESSFUL,

Loading…
Cancel
Save