From 69ddc43d9954ecc8254b170518dcd179d7c20c4c Mon Sep 17 00:00:00 2001 From: Naveen Date: Tue, 31 May 2022 23:04:33 +0530 Subject: [PATCH] Properly handle repeating and overlapping events --- .../calendar/pro/views/MonthView.kt | 24 ++++++++++--------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/views/MonthView.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/views/MonthView.kt index 7530ffd99..1d73518cc 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/views/MonthView.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/views/MonthView.kt @@ -116,16 +116,24 @@ class MonthView(context: Context, attrs: AttributeSet, defStyle: Int) : View(con private fun groupAllEvents() { days.forEach { day -> + val dayIndexOnMonthView = day.indexOnMonthView + day.dayEvents.forEach { event -> // make sure we properly handle events lasting multiple days and repeating ones - val lastEvent = allEvents.lastOrNull { it.id == event.id } - val daysCnt = getEventLastingDaysCount(event) val validDayEvent = isDayValid(event, day.code) + val lastEvent = allEvents.lastOrNull { it.id == event.id } + val notYetAddedOrIsRepeatingEvent = lastEvent == null || lastEvent.endTS <= event.startTS + + // handle overlapping repeating events e.g. an event that lasts 3 days, but repeats every 2 days has a one day overlap + val canOverlap = event.endTS - event.startTS > event.repeatInterval + val shouldAddEvent = notYetAddedOrIsRepeatingEvent || canOverlap && (lastEvent!!.startTS < event.startTS) + + if (shouldAddEvent && !validDayEvent) { + val daysCnt = getEventLastingDaysCount(event) - if ((lastEvent == null || lastEvent.startDayIndex + daysCnt <= findLastDay(event).indexOnMonthView) && !validDayEvent) { val monthViewEvent = MonthViewEvent( - event.id!!, event.title, event.startTS, event.endTS, event.color, day.indexOnMonthView, - daysCnt, day.indexOnMonthView, event.getIsAllDay(), event.isPastEvent, event.isTask(), event.isTaskCompleted() + event.id!!, event.title, event.startTS, event.endTS, event.color, dayIndexOnMonthView, + daysCnt, dayIndexOnMonthView, event.getIsAllDay(), event.isPastEvent, event.isTask(), event.isTaskCompleted() ) allEvents.add(monthViewEvent) } @@ -429,12 +437,6 @@ class MonthView(context: Context, attrs: AttributeSet, defStyle: Int) : View(con return event.startTS != event.endTS && Formatter.getDateTimeFromTS(event.endTS) == Formatter.getDateTimeFromTS(date.seconds()).withTimeAtStartOfDay() } - private fun findLastDay(event: Event): DayMonthly { - return days.last { day -> - day.dayEvents.find { it.id == event.id } != null - } - } - fun togglePrintMode() { isPrintVersion = !isPrintVersion textColor = if (isPrintVersion) {