Merge pull request #1737 from Naveen3Singh/weekly_view_fix

Avoid creating new rows unnecessarily
This commit is contained in:
Tibor Kaputa 2022-06-11 22:45:07 +02:00 committed by GitHub
commit 1964e1890e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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++
wasEventHandled = true val lastRow = allDayRows.last()
allDayRows.last().add(dayIndex) dayIndices.forEach {
lastRow.add(it)
} }
allDayEventToRow[event] = allDayRows.lastIndex
wasEventHandled = true
} }
} }