From b4b6f1b0de911a2f2f288ac88e2a66fc6afd2e99 Mon Sep 17 00:00:00 2001 From: Andrey Ananiev Date: Wed, 15 Feb 2023 16:16:26 +0300 Subject: [PATCH] Add swipe listener --- library/build.gradle.kts | 2 ++ .../io/github/boguszpawlowski/composecalendar/Calendar.kt | 8 ++++++++ .../boguszpawlowski/composecalendar/month/MonthContent.kt | 8 +++++++- .../boguszpawlowski/composecalendar/week/WeekContent.kt | 6 ++++++ .../composecalendar/sample/SelectableCalendarSample.kt | 2 ++ 5 files changed, 25 insertions(+), 1 deletion(-) diff --git a/library/build.gradle.kts b/library/build.gradle.kts index 4dca94d..41b1597 100644 --- a/library/build.gradle.kts +++ b/library/build.gradle.kts @@ -25,6 +25,8 @@ dependencies { implementation(Compose.FoundationLayout) implementation(Compose.Material) implementation(Timber.Core) + implementation("androidx.lifecycle:lifecycle-viewmodel-ktx:2.5.1") + implementation("androidx.lifecycle:lifecycle-livedata-core-ktx:2.5.1") testImplementation(Kotest.Assertions) testImplementation(Kotest.RunnerJunit5) diff --git a/library/src/main/java/io/github/boguszpawlowski/composecalendar/Calendar.kt b/library/src/main/java/io/github/boguszpawlowski/composecalendar/Calendar.kt index c6cbbe3..a2f2e74 100644 --- a/library/src/main/java/io/github/boguszpawlowski/composecalendar/Calendar.kt +++ b/library/src/main/java/io/github/boguszpawlowski/composecalendar/Calendar.kt @@ -2,6 +2,7 @@ package io.github.boguszpawlowski.composecalendar +import android.util.Log import androidx.compose.animation.animateContentSize import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.BoxScope @@ -94,6 +95,7 @@ public fun SelectableCalendar( weekContainer: @Composable (content: @Composable (PaddingValues) -> Unit) -> Unit = { content -> Box { content(PaddingValues()) } }, + onSwipe: (LocalDate) -> Unit = { }, ) { Calendar( modifier = modifier, @@ -108,6 +110,7 @@ public fun SelectableCalendar( weekDaysNames = weekDaysNames, monthContainer = monthContainer, weekContainer = weekContainer, + onSwipe = onSwipe, ) } @@ -151,6 +154,7 @@ public fun StaticCalendar( weekContainer: @Composable (content: @Composable (PaddingValues) -> Unit) -> Unit = { content -> Box { content(PaddingValues()) } }, + onSwipe: (LocalDate) -> Unit = { }, ) { Calendar( modifier = modifier, @@ -165,6 +169,7 @@ public fun StaticCalendar( weekDaysNames = weekDaysNames, monthContainer = monthContainer, weekContainer = weekContainer, + onSwipe = onSwipe, ) } @@ -203,6 +208,7 @@ public fun Calendar( weekContainer: @Composable (content: @Composable (PaddingValues) -> Unit) -> Unit = { content -> Box { content(PaddingValues()) } }, + onSwipe: (LocalDate) -> Unit = { }, ) { val daysOfWeek = remember(firstDayOfWeek) { @@ -227,6 +233,7 @@ public fun Calendar( dayContent = dayContent, weekDaysNames = weekDaysNames, weekContainer = weekContainer, + onSwipe = onSwipe ) } else { WeekContent( @@ -257,6 +264,7 @@ public fun Calendar( dayContent = dayContent, weekDaysNames = weekDaysNames, monthContainer = monthContainer, + onSwipe = onSwipe, ) } else { MonthContent( diff --git a/library/src/main/java/io/github/boguszpawlowski/composecalendar/month/MonthContent.kt b/library/src/main/java/io/github/boguszpawlowski/composecalendar/month/MonthContent.kt index 684bbfe..c8e5fd4 100644 --- a/library/src/main/java/io/github/boguszpawlowski/composecalendar/month/MonthContent.kt +++ b/library/src/main/java/io/github/boguszpawlowski/composecalendar/month/MonthContent.kt @@ -10,7 +10,8 @@ import androidx.compose.foundation.layout.wrapContentHeight import androidx.compose.foundation.lazy.LazyRow import androidx.compose.foundation.lazy.rememberLazyListState import androidx.compose.runtime.Composable -import androidx.compose.runtime.remember +import androidx.compose.runtime.mutableStateOf +import androidx.compose.runtime.* import androidx.compose.runtime.rememberCoroutineScope import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier @@ -48,6 +49,7 @@ internal fun MonthPager( dayContent: @Composable BoxScope.(DayState) -> Unit, weekDaysNames: @Composable BoxScope.(List) -> Unit, monthContainer: @Composable (content: @Composable (PaddingValues) -> Unit) -> Unit, + onSwipe: (LocalDate) -> Unit, ) { val coroutineScope = rememberCoroutineScope() @@ -73,6 +75,10 @@ internal fun MonthPager( ) } + LaunchedEffect(key1 = listState.firstVisibleItemIndex) { + onSwipe(monthListState.getMonthForPage(listState.firstVisibleItemIndex).atDay(1)) + } + LazyRow( modifier = modifier.testTag("MonthPager"), state = listState, diff --git a/library/src/main/java/io/github/boguszpawlowski/composecalendar/week/WeekContent.kt b/library/src/main/java/io/github/boguszpawlowski/composecalendar/week/WeekContent.kt index b2268d4..f8a027a 100644 --- a/library/src/main/java/io/github/boguszpawlowski/composecalendar/week/WeekContent.kt +++ b/library/src/main/java/io/github/boguszpawlowski/composecalendar/week/WeekContent.kt @@ -13,6 +13,7 @@ import androidx.compose.foundation.layout.wrapContentHeight import androidx.compose.foundation.lazy.LazyRow import androidx.compose.foundation.lazy.rememberLazyListState import androidx.compose.runtime.Composable +import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.remember import androidx.compose.runtime.rememberCoroutineScope import androidx.compose.ui.Alignment @@ -45,6 +46,7 @@ internal fun WeekPager( dayContent: @Composable BoxScope.(DayState) -> Unit, weekDaysNames: @Composable BoxScope.(List) -> Unit, weekContainer: @Composable (content: @Composable (PaddingValues) -> Unit) -> Unit, + onSwipe: (LocalDate) -> Unit ) { val coroutineScope = rememberCoroutineScope() @@ -68,6 +70,10 @@ internal fun WeekPager( ) } + LaunchedEffect(key1 = listState.firstVisibleItemIndex) { + onSwipe(weekListState.getWeekForPage(listState.firstVisibleItemIndex)) + } + LazyRow( modifier = modifier.testTag("WeekPager"), state = listState, 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 6fff1ed..0aec1c3 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,6 +11,7 @@ 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 @@ -34,6 +35,7 @@ internal val dayEventList = listOf( @Composable fun SelectableCalendarSample() { + val calendarState = rememberSelectableCalendarState( initialEventList = dayEventList )