Skip to content

Commit

Permalink
Fix issue where multi-day all-day events weren't displayed correctly
Browse files Browse the repository at this point in the history
Issue #118
  • Loading branch information
thellmund committed Oct 31, 2019
1 parent 3b4c6f9 commit 7770a59
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 12 deletions.
6 changes: 5 additions & 1 deletion core/src/main/java/com/alamkanak/weekview/EventsCache.kt
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,11 @@ internal abstract class EventsCache<T> {

operator fun get(
dateRange: List<Calendar>
) = allEvents.filter { dateRange.contains(it.startTime.atStartOfDay) }
): List<WeekViewEvent<T>> {
val startDate = checkNotNull(dateRange.min())
val endDate = checkNotNull(dateRange.max())
return allEvents.filter { it.endTime >= startDate || it.startTime <= endDate }
}

operator fun get(
fetchRange: FetchRange
Expand Down
9 changes: 4 additions & 5 deletions core/src/main/java/com/alamkanak/weekview/WeekView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,12 @@ class WeekView<T> @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()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 ->
Expand All @@ -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
Expand All @@ -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")
}
}

0 comments on commit 7770a59

Please sign in to comment.