Avoid creating new rows unnecessarily

Before this change, each repeating event was created on a different set of rows even when there was enough space for the events to fit
This commit is contained in:
Naveen 2022-06-08 22:51:21 +05:30
parent 9a27d3c78e
commit 6fd495cb3b
1 changed files with 29 additions and 12 deletions

View File

@ -73,6 +73,7 @@ class WeekFragment : Fragment(), WeeklyCalendar {
private var fadeOutHandler = Handler() private var fadeOutHandler = Handler()
private var allDayHolders = ArrayList<RelativeLayout>() private var allDayHolders = ArrayList<RelativeLayout>()
private var allDayRows = ArrayList<HashSet<Int>>() private var allDayRows = ArrayList<HashSet<Int>>()
private var allDayEventToRow = LinkedHashMap<Event, Int>()
private var currEvents = ArrayList<Event>() private var currEvents = ArrayList<Event>()
private var dayColumns = ArrayList<RelativeLayout>() private var dayColumns = ArrayList<RelativeLayout>()
private var eventTypeColors = LongSparseArray<Int>() private var eventTypeColors = LongSparseArray<Int>()
@ -440,6 +441,7 @@ class WeekFragment : Fragment(), WeeklyCalendar {
allDayRows.add(HashSet()) allDayRows.add(HashSet())
week_all_day_holder?.removeAllViews() week_all_day_holder?.removeAllViews()
addNewLine() addNewLine()
allDayEventToRow.clear()
val minuteHeight = rowHeight / 60 val minuteHeight = rowHeight / 60
val minimalHeight = res.getDimension(R.dimen.weekly_view_minimal_event_height).toInt() val minimalHeight = res.getDimension(R.dimen.weekly_view_minimal_event_height).toInt()
@ -725,6 +727,8 @@ class WeekFragment : Fragment(), WeeklyCalendar {
val startDateTime = Formatter.getDateTimeFromTS(event.startTS) val startDateTime = Formatter.getDateTimeFromTS(event.startTS)
val endDateTime = Formatter.getDateTimeFromTS(event.endTS) val endDateTime = Formatter.getDateTimeFromTS(event.endTS)
val eventStartDayStartTime = startDateTime.withTimeAtStartOfDay().seconds()
val eventEndDayStartTime = endDateTime.withTimeAtStartOfDay().seconds()
val minTS = max(startDateTime.seconds(), weekTimestamp) val minTS = max(startDateTime.seconds(), weekTimestamp)
val maxTS = min(endDateTime.seconds(), weekTimestamp + 2 * WEEK_SECONDS) val maxTS = min(endDateTime.seconds(), weekTimestamp + 2 * WEEK_SECONDS)
@ -741,7 +745,8 @@ class WeekFragment : Fragment(), WeeklyCalendar {
val firstDayIndex = startDateTimeInWeek.dayOfMonth // indices must be unique for the visible range (2 weeks) val firstDayIndex = startDateTimeInWeek.dayOfMonth // indices must be unique for the visible range (2 weeks)
val lastDayIndex = firstDayIndex + daysCnt val lastDayIndex = firstDayIndex + daysCnt
val dayIndices = firstDayIndex..lastDayIndex val dayIndices = firstDayIndex..lastDayIndex
val isSameDayEvent = firstDayIndex == lastDayIndex val isAllDayEvent = firstDayIndex == lastDayIndex
val isRepeatingOverlappingEvent = eventEndDayStartTime - eventStartDayStartTime >= event.repeatInterval
var doesEventFit: Boolean var doesEventFit: Boolean
var wasEventHandled = false var wasEventHandled = false
@ -751,22 +756,34 @@ class WeekFragment : Fragment(), WeeklyCalendar {
drawAtLine = index drawAtLine = index
val row = allDayRows[index] val row = allDayRows[index]
doesEventFit = dayIndices.all { !row.contains(it) } doesEventFit = dayIndices.all { !row.contains(it) }
if (doesEventFit && isSameDayEvent) { val firstEvent = allDayEventToRow.keys.firstOrNull { it.id == event.id }
row.add(firstDayIndex) val lastEvent = allDayEventToRow.keys.lastOrNull { it.id == event.id }
val firstEventRowIdx = allDayEventToRow[firstEvent]
val lastEventRowIdx = allDayEventToRow[lastEvent]
val adjacentEvents = currEvents.filter { event.id == it.id }
val repeatingEventIndex = adjacentEvents.indexOf(event)
val isRowValidForEvent = lastEvent == null || firstEventRowIdx!! + repeatingEventIndex == index && lastEventRowIdx!! < index
if (doesEventFit && (!isRepeatingOverlappingEvent || isAllDayEvent || isRowValidForEvent)) {
dayIndices.forEach {
row.add(it)
}
allDayEventToRow[event] = index
wasEventHandled = true wasEventHandled = true
} else { } else {
// handle events spanning midnight // create new row
for (dayIndex in dayIndices) { if (index == allDayRows.lastIndex) {
if (index == allDayRows.lastIndex) { allDayRows.add(HashSet())
allDayRows.add(HashSet()) addNewLine()
addNewLine() drawAtLine++
drawAtLine++ val lastRow = allDayRows.last()
wasEventHandled = true dayIndices.forEach {
allDayRows.last().add(dayIndex) lastRow.add(it)
} }
allDayEventToRow[event] = allDayRows.lastIndex
wasEventHandled = true
} }
} }