-
Notifications
You must be signed in to change notification settings - Fork 3
/
CalendarPageLoader.kt
51 lines (45 loc) · 1.98 KB
/
CalendarPageLoader.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package com.boswelja.ephemeris.core.ui
import com.boswelja.ephemeris.core.data.CalendarPageSource
import com.boswelja.ephemeris.core.model.CalendarPage
import kotlinx.datetime.LocalDate
/**
* Handles loading data from [CalendarPageSource], caching and prefetching entries. Platform UIs
* should make use of this class for their data loading. If the page source or focus mode change, it
* is expected a new instance will be created and the existing instance discarded.
*/
public class CalendarPageLoader(
public val calendarPageSource: CalendarPageSource
) {
private val pageCache = mutableMapOf<Int, CalendarPage>()
/**
* Gets the data to display for the given page. If necessary, a cache load operation will be
* started to ensure there's enough data available ahead of time.
*/
public fun getPageData(page: Int): CalendarPage {
return pageCache.getOrPut(page) { calendarPageSource.loadPageData(page) }
}
/**
* Gets the range of focused dates displayed on the given page. If no dates are in focus on the
* given page, falls back to the full range of dates displayed.
*/
public fun getDateRangeFor(page: Int): ClosedRange<LocalDate> {
val pageData = getPageData(page)
val startDate = pageData.rows.first().days.firstOrDefault { it.isFocusedDate }.date
val endDate = pageData.rows.last().days.lastOrDefault { it.isFocusedDate }.date
return startDate..endDate
}
/**
* Returns the first item matching the given predicate. If no matches are found, the first item in
* the list is returned.
*/
private fun <T> List<T>.firstOrDefault(predicate: (T) -> Boolean): T {
return firstOrNull(predicate) ?: first()
}
/**
* Returns the last item matching the given predicate. If no matches are found, the last item in the
* list is returned.
*/
private fun <T> List<T>.lastOrDefault(predicate: (T) -> Boolean): T {
return lastOrNull(predicate) ?: last()
}
}