Skip to content

Commit e22d460

Browse files
authored
fix: consider neighbour events when calculating max slot range (#875)
* fix: consider neighbour events when calculating max slot range Refs: #550
1 parent b5566c8 commit e22d460

File tree

3 files changed

+33
-10
lines changed

3 files changed

+33
-10
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99
- Updated holiday data
1010

1111
### Fixed
12+
- Fixed startup crash in weekly view ([#550])
1213
- Fixed incorrect weekly view start date in some cases ([#45])
1314
- Fixed issue with Up/Arrow button closing the app ([#870])
1415
- Fixed time drift when switching between views ([#590])
@@ -167,6 +168,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
167168
[#394]: https://github.com/FossifyOrg/Calendar/issues/394
168169
[#484]: https://github.com/FossifyOrg/Calendar/issues/484
169170
[#486]: https://github.com/FossifyOrg/Calendar/issues/486
171+
[#550]: https://github.com/FossifyOrg/Calendar/issues/550
170172
[#551]: https://github.com/FossifyOrg/Calendar/issues/551
171173
[#567]: https://github.com/FossifyOrg/Calendar/issues/567
172174
[#574]: https://github.com/FossifyOrg/Calendar/issues/574

app/src/main/kotlin/org/fossify/calendar/fragments/WeekFragment.kt

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -553,25 +553,41 @@ class WeekFragment : Fragment(), WeeklyCalendar {
553553

554554
if (areTouching && doHaveCommonMinutes) {
555555
if (eventWeeklyViewToCheck.slot == 0) {
556-
val nextSlot = eventWeeklyView.slotMax + 1
557-
val slotRange = Array(eventWeeklyView.slotMax) { it + 1 }
558-
val collisionEventWeeklyViews = eventDayList.filter { eventWeeklyView.collisions.contains(it.key) }
556+
val collisionEventWeeklyViews = eventDayList
557+
.filter { eventWeeklyView.collisions.contains(it.key) }
558+
var currentSlotMax = max(
559+
eventWeeklyView.slotMax.coerceAtLeast(1),
560+
eventWeeklyView.slot.coerceAtLeast(1)
561+
)
562+
val maxCollisionSlot = collisionEventWeeklyViews
563+
.maxOfOrNull { it.value.slot.coerceAtLeast(1) } ?: 1
564+
currentSlotMax = max(currentSlotMax, maxCollisionSlot)
565+
val nextSlot = currentSlotMax + 1
566+
val slotRange = IntArray(currentSlotMax) { it + 1 }
559567
for ((_, collisionEventWeeklyView) in collisionEventWeeklyViews) {
560-
if (collisionEventWeeklyView.range.intersects(eventWeeklyViewToCheck.range)) {
568+
if (
569+
collisionEventWeeklyView.range.intersects(eventWeeklyViewToCheck.range)
570+
&& collisionEventWeeklyView.slot in 1..currentSlotMax
571+
) {
561572
slotRange[collisionEventWeeklyView.slot - 1] = nextSlot
562573
}
563574
}
564-
slotRange[eventWeeklyView.slot - 1] = nextSlot
565-
val slot = slotRange.minOrNull()
566-
eventWeeklyViewToCheck.slot = slot!!
575+
if (eventWeeklyView.slot in 1..currentSlotMax) {
576+
slotRange[eventWeeklyView.slot - 1] = nextSlot
577+
}
578+
579+
val slot = slotRange.minOrNull() ?: nextSlot
580+
eventWeeklyViewToCheck.slot = slot
581+
567582
if (slot == nextSlot) {
568583
eventWeeklyViewToCheck.slotMax = nextSlot
569584
eventWeeklyView.slotMax = nextSlot
570585
for ((_, collisionEventWeeklyView) in collisionEventWeeklyViews) {
571-
collisionEventWeeklyView.slotMax++
586+
collisionEventWeeklyView.slotMax =
587+
max(collisionEventWeeklyView.slotMax, nextSlot)
572588
}
573589
} else {
574-
eventWeeklyViewToCheck.slotMax = eventWeeklyView.slotMax
590+
eventWeeklyViewToCheck.slotMax = currentSlotMax
575591
}
576592
}
577593
eventWeeklyView.collisions.add(toCheckId)

app/src/main/kotlin/org/fossify/calendar/models/EventWeeklyView.kt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,9 @@ package org.fossify.calendar.models
22

33
import android.util.Range
44

5-
data class EventWeeklyView(val range: Range<Int>, var slot: Int = 0, var slotMax: Int = 0, var collisions: ArrayList<Long> = ArrayList())
5+
data class EventWeeklyView(
6+
val range: Range<Int>,
7+
var slot: Int = 0,
8+
var slotMax: Int = 0,
9+
var collisions: ArrayList<Long> = ArrayList()
10+
)

0 commit comments

Comments
 (0)