Properly handle repeating events

This commit is contained in:
Naveen 2022-05-21 09:07:48 +05:30
parent 6c3f833972
commit 25998aa6b8
2 changed files with 20 additions and 10 deletions

View File

@ -115,16 +115,17 @@ class MonthView(context: Context, attrs: AttributeSet, defStyle: Int) : View(con
}
private fun groupAllEvents() {
days.forEach {
val day = it
day.dayEvents.forEach {
val event = it
days.forEach { day ->
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)
if ((lastEvent == null || lastEvent.startDayIndex + daysCnt <= day.indexOnMonthView) && !validDayEvent) {
val lastDay = { findLastDay(event) ?: day }
if ((lastEvent == null || lastEvent.startDayIndex + daysCnt <= lastDay().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()
@ -135,8 +136,9 @@ class MonthView(context: Context, attrs: AttributeSet, defStyle: Int) : View(con
}
allEvents =
allEvents.asSequence().sortedWith(compareBy({ -it.daysCnt }, { !it.isAllDay }, { it.startTS }, { it.endTS }, { it.startDayIndex }, { it.title }))
.toMutableList() as ArrayList<MonthViewEvent>
allEvents.asSequence().sortedWith(
compareBy({ -it.daysCnt }, { !it.isAllDay }, { it.startTS }, { it.endTS }, { it.startDayIndex }, { it.title })
).toMutableList() as ArrayList<MonthViewEvent>
}
override fun onDraw(canvas: Canvas) {
@ -287,7 +289,7 @@ class MonthView(context: Context, attrs: AttributeSet, defStyle: Int) : View(con
}
val startDayIndex = days[event.originalStartDayIndex]
val endDayIndex = days[Math.min(event.startDayIndex + event.daysCnt - 1, 41)]
val endDayIndex = days[min(event.startDayIndex + event.daysCnt - 1, 41)]
bgRectF.set(bgLeft, bgTop, bgRight, bgBottom)
canvas.drawRoundRect(bgRectF, BG_CORNER_RADIUS, BG_CORNER_RADIUS, getEventBackgroundColor(event, startDayIndex, endDayIndex))
@ -303,7 +305,7 @@ class MonthView(context: Context, attrs: AttributeSet, defStyle: Int) : View(con
drawEventTitle(event, canvas, xPos + taskIconWidth, yPos + verticalOffset, bgRight - bgLeft - smallPadding - taskIconWidth, specificEventTitlePaint)
for (i in 0 until Math.min(event.daysCnt, 7 - event.startDayIndex % 7)) {
for (i in 0 until min(event.daysCnt, 7 - event.startDayIndex % 7)) {
dayVerticalOffsets.put(event.startDayIndex + i, verticalOffset + eventTitleHeight + smallPadding * 2)
}
}
@ -431,6 +433,11 @@ 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) =
days.lastOrNull { day ->
day.dayEvents.find { it.id == event.id } != null
}
fun togglePrintMode() {
isPrintVersion = !isPrintVersion
textColor = if (isPrintVersion) {

View File

@ -59,7 +59,10 @@ class MonthViewWrapper(context: Context, attrs: AttributeSet, defStyle: Int) : F
continue
}
child.measure(MeasureSpec.makeMeasureSpec(dayWidth.toInt(), MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(dayHeight.toInt(), MeasureSpec.EXACTLY))
child.measure(
MeasureSpec.makeMeasureSpec(dayWidth.toInt(), MeasureSpec.EXACTLY),
MeasureSpec.makeMeasureSpec(dayHeight.toInt(), MeasureSpec.EXACTLY)
)
val childLeft = x * dayWidth + horizontalOffset - child.translationX
val childTop = y * dayHeight + weekDaysLetterHeight - child.translationY