diff --git a/core/src/main/java/com/alamkanak/weekview/EventsCache.kt b/core/src/main/java/com/alamkanak/weekview/EventsCache.kt index 063e4fc78..2b8868e9e 100644 --- a/core/src/main/java/com/alamkanak/weekview/EventsCache.kt +++ b/core/src/main/java/com/alamkanak/weekview/EventsCache.kt @@ -45,7 +45,11 @@ internal abstract class EventsCache { operator fun get( dateRange: List - ) = allEvents.filter { dateRange.contains(it.startTime.atStartOfDay) } + ): List> { + val startDate = checkNotNull(dateRange.min()) + val endDate = checkNotNull(dateRange.max()) + return allEvents.filter { it.endTime >= startDate || it.startTime <= endDate } + } operator fun get( fetchRange: FetchRange diff --git a/core/src/main/java/com/alamkanak/weekview/WeekView.kt b/core/src/main/java/com/alamkanak/weekview/WeekView.kt index 15781ef85..a26b56363 100644 --- a/core/src/main/java/com/alamkanak/weekview/WeekView.kt +++ b/core/src/main/java/com/alamkanak/weekview/WeekView.kt @@ -94,13 +94,12 @@ class WeekView @JvmOverloads constructor( } val firstVisibleDate = checkNotNull(viewState.firstVisibleDate) - val events = eventsLoader.refresh(firstVisibleDate) // These can either be newly loaded events or previously cached events - if (events.isNullOrEmpty()) { - eventChipCache.clear() - } else { - eventChipCache.clear() + val events = eventsLoader.refresh(firstVisibleDate) + eventChipCache.clear() + + if (events.isNotEmpty()) { eventChipsLoader.createAndCacheEventChips(events) eventChipsExpander.calculateEventChipPositions() } diff --git a/sample/src/main/java/com/alamkanak/weekview/sample/util/ToolbarExtensions.kt b/sample/src/main/java/com/alamkanak/weekview/sample/util/ToolbarExtensions.kt index 8d3a4b2a3..0ce5a4eed 100644 --- a/sample/src/main/java/com/alamkanak/weekview/sample/util/ToolbarExtensions.kt +++ b/sample/src/main/java/com/alamkanak/weekview/sample/util/ToolbarExtensions.kt @@ -10,14 +10,20 @@ import com.alamkanak.weekview.sample.R private enum class WeekViewType(val value: Int) { DayView(1), ThreeDayView(3), - WeekView(7) + WeekView(7); + + companion object { + fun create( + numberOfVisibleDays: Int + ): WeekViewType = values().first { it.value == numberOfVisibleDays } + } } fun Toolbar.setupWithWeekView(weekView: WeekView<*>) { val activity = context as Activity title = activity.label - var currentViewType = WeekViewType.DayView + var currentViewType = WeekViewType.create(weekView.numberOfVisibleDays) inflateMenu(R.menu.menu_main) setOnMenuItemClickListener { item -> @@ -27,7 +33,7 @@ fun Toolbar.setupWithWeekView(weekView: WeekView<*>) { true } else -> { - val viewType = mapMenuItemToWeekViewType(item) + val viewType = item.mapToWeekViewType() if (viewType != currentViewType) { item.isChecked = !item.isChecked currentViewType = viewType @@ -48,11 +54,11 @@ fun Toolbar.setupWithWeekView(weekView: WeekView<*>) { private val Activity.label: String get() = getString(packageManager.getActivityInfo(componentName, 0).labelRes) -private fun mapMenuItemToWeekViewType(menuItem: MenuItem): WeekViewType { - return when (menuItem.itemId) { +private fun MenuItem.mapToWeekViewType(): WeekViewType { + return when (itemId) { R.id.action_day_view -> WeekViewType.DayView R.id.action_three_day_view -> WeekViewType.ThreeDayView R.id.action_week_view -> WeekViewType.WeekView - else -> throw IllegalArgumentException("Invalid menu item ID ${menuItem.itemId}") + else -> throw IllegalArgumentException("Invalid menu item ID $itemId") } }