From 7a9a6c8adbe860772050de70f8fd68493d75d0cf Mon Sep 17 00:00:00 2001 From: Andrey Ananiev Date: Sat, 25 Mar 2023 21:57:18 +0300 Subject: [PATCH] Add list of day events --- .../composecalendar/day/DefaultDay.kt | 2 +- .../composecalendar/states/EventState.kt | 13 +++++-------- .../sample/SelectableCalendarSample.kt | 14 ++++++-------- 3 files changed, 12 insertions(+), 17 deletions(-) diff --git a/library/src/main/java/io/github/boguszpawlowski/composecalendar/day/DefaultDay.kt b/library/src/main/java/io/github/boguszpawlowski/composecalendar/day/DefaultDay.kt index 84390bd..70fde93 100644 --- a/library/src/main/java/io/github/boguszpawlowski/composecalendar/day/DefaultDay.kt +++ b/library/src/main/java/io/github/boguszpawlowski/composecalendar/day/DefaultDay.kt @@ -40,7 +40,7 @@ public fun DefaultDay( ) { val date = state.date val selectionState = state.selectionState - val isHasEvent = state.eventState.getEventsByDate(date) > 0 + val isHasEvent = state.eventState.getEventsByDate(date).firstOrNull() { it != 0 } != null val isSelected = selectionState.isDateSelected(date) Card( diff --git a/library/src/main/java/io/github/boguszpawlowski/composecalendar/states/EventState.kt b/library/src/main/java/io/github/boguszpawlowski/composecalendar/states/EventState.kt index 9a3a78e..ef0b4ba 100644 --- a/library/src/main/java/io/github/boguszpawlowski/composecalendar/states/EventState.kt +++ b/library/src/main/java/io/github/boguszpawlowski/composecalendar/states/EventState.kt @@ -4,16 +4,13 @@ import androidx.compose.runtime.Stable import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.saveable.Saver -import androidx.compose.runtime.saveable.listSaver import androidx.compose.runtime.setValue -import io.github.boguszpawlowski.composecalendar.selection.DynamicSelectionState -import io.github.boguszpawlowski.composecalendar.selection.SelectionMode import java.time.LocalDate @Stable public data class DayEvent( val day: LocalDate, - val eventCount: Int + val eventCountList: List ) @Stable @@ -30,21 +27,21 @@ public class EventState( } } - public fun getEventsByDate(date: LocalDate): Int = eventList.firstOrNull{ it.day == date }?.eventCount ?: 0 + public fun getEventsByDate(date: LocalDate): List = eventList.firstOrNull{ it.day == date }?.eventCountList ?: listOf() public companion object { @Suppress("FunctionName", "UNCHECKED_CAST") // Factory function public fun Saver(): Saver = Saver( save = { row -> - row.eventList.map { "${it.day}\n${it.eventCount}" } + row.eventList.map { "${it.day}\n${it.eventCountList}" } }, restore = { restored -> EventState( - initialEventList = (restored as? List)?.map { + initialEventList = (restored as? List)?.map { it -> DayEvent( day = LocalDate.parse(it.lines()[0]), - eventCount = it.lines()[1].toIntOrNull()?: 0 + eventCountList = it.lines()[1].lines().map { eventType -> eventType.toInt() } ) }.orEmpty() ) diff --git a/sample/src/main/java/io/github/boguszpawlowski/composecalendar/sample/SelectableCalendarSample.kt b/sample/src/main/java/io/github/boguszpawlowski/composecalendar/sample/SelectableCalendarSample.kt index 0aec1c3..c8e55fa 100644 --- a/sample/src/main/java/io/github/boguszpawlowski/composecalendar/sample/SelectableCalendarSample.kt +++ b/sample/src/main/java/io/github/boguszpawlowski/composecalendar/sample/SelectableCalendarSample.kt @@ -11,7 +11,6 @@ import androidx.compose.material.MaterialTheme import androidx.compose.material.RadioButton import androidx.compose.material.Text import androidx.compose.runtime.Composable -import androidx.compose.runtime.LaunchedEffect import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.unit.dp @@ -21,16 +20,15 @@ import io.github.boguszpawlowski.composecalendar.rememberSelectableCalendarState import io.github.boguszpawlowski.composecalendar.selection.DynamicSelectionState import io.github.boguszpawlowski.composecalendar.selection.SelectionMode import io.github.boguszpawlowski.composecalendar.states.DayEvent -import io.github.boguszpawlowski.composecalendar.states.EventState import java.time.LocalDate internal val dayEventList = listOf( - DayEvent(LocalDate.now(), 3), - DayEvent(LocalDate.now().minusDays(3L), 3), - DayEvent(LocalDate.now().minusDays(5L), 4), - DayEvent(LocalDate.now().minusDays(7L), 5), - DayEvent(LocalDate.now().plusDays(3L), 3), - DayEvent(LocalDate.now().plusDays(7L), 3), + DayEvent(LocalDate.now(), listOf(3)), + DayEvent(LocalDate.now().minusDays(3L), listOf(30)), + DayEvent(LocalDate.now().minusDays(5L), listOf(7)), + DayEvent(LocalDate.now().minusDays(7L), listOf(6)), + DayEvent(LocalDate.now().plusDays(3L), listOf(9)), + DayEvent(LocalDate.now().plusDays(7L), listOf(7)), ) @Composable