Skip to content

Commit

Permalink
Add swipe listener
Browse files Browse the repository at this point in the history
  • Loading branch information
andrey-ananiev committed Feb 15, 2023
1 parent 799b427 commit b4b6f1b
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 1 deletion.
2 changes: 2 additions & 0 deletions library/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -94,6 +95,7 @@ public fun SelectableCalendar(
weekContainer: @Composable (content: @Composable (PaddingValues) -> Unit) -> Unit = { content ->
Box { content(PaddingValues()) }
},
onSwipe: (LocalDate) -> Unit = { },
) {
Calendar(
modifier = modifier,
Expand All @@ -108,6 +110,7 @@ public fun SelectableCalendar(
weekDaysNames = weekDaysNames,
monthContainer = monthContainer,
weekContainer = weekContainer,
onSwipe = onSwipe,
)
}

Expand Down Expand Up @@ -151,6 +154,7 @@ public fun StaticCalendar(
weekContainer: @Composable (content: @Composable (PaddingValues) -> Unit) -> Unit = { content ->
Box { content(PaddingValues()) }
},
onSwipe: (LocalDate) -> Unit = { },
) {
Calendar(
modifier = modifier,
Expand All @@ -165,6 +169,7 @@ public fun StaticCalendar(
weekDaysNames = weekDaysNames,
monthContainer = monthContainer,
weekContainer = weekContainer,
onSwipe = onSwipe,
)
}

Expand Down Expand Up @@ -203,6 +208,7 @@ public fun <T : SelectionState> Calendar(
weekContainer: @Composable (content: @Composable (PaddingValues) -> Unit) -> Unit = { content ->
Box { content(PaddingValues()) }
},
onSwipe: (LocalDate) -> Unit = { },
) {

val daysOfWeek = remember(firstDayOfWeek) {
Expand All @@ -227,6 +233,7 @@ public fun <T : SelectionState> Calendar(
dayContent = dayContent,
weekDaysNames = weekDaysNames,
weekContainer = weekContainer,
onSwipe = onSwipe
)
} else {
WeekContent(
Expand Down Expand Up @@ -257,6 +264,7 @@ public fun <T : SelectionState> Calendar(
dayContent = dayContent,
weekDaysNames = weekDaysNames,
monthContainer = monthContainer,
onSwipe = onSwipe,
)
} else {
MonthContent(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -48,6 +49,7 @@ internal fun <T : SelectionState> MonthPager(
dayContent: @Composable BoxScope.(DayState<T>) -> Unit,
weekDaysNames: @Composable BoxScope.(List<DayOfWeek>) -> Unit,
monthContainer: @Composable (content: @Composable (PaddingValues) -> Unit) -> Unit,
onSwipe: (LocalDate) -> Unit,
) {
val coroutineScope = rememberCoroutineScope()

Expand All @@ -73,6 +75,10 @@ internal fun <T : SelectionState> MonthPager(
)
}

LaunchedEffect(key1 = listState.firstVisibleItemIndex) {
onSwipe(monthListState.getMonthForPage(listState.firstVisibleItemIndex).atDay(1))
}

LazyRow(
modifier = modifier.testTag("MonthPager"),
state = listState,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -45,6 +46,7 @@ internal fun <T : SelectionState> WeekPager(
dayContent: @Composable BoxScope.(DayState<T>) -> Unit,
weekDaysNames: @Composable BoxScope.(List<DayOfWeek>) -> Unit,
weekContainer: @Composable (content: @Composable (PaddingValues) -> Unit) -> Unit,
onSwipe: (LocalDate) -> Unit
) {
val coroutineScope = rememberCoroutineScope()

Expand All @@ -68,6 +70,10 @@ internal fun <T : SelectionState> WeekPager(
)
}

LaunchedEffect(key1 = listState.firstVisibleItemIndex) {
onSwipe(weekListState.getWeekForPage(listState.firstVisibleItemIndex))
}

LazyRow(
modifier = modifier.testTag("WeekPager"),
state = listState,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -34,6 +35,7 @@ internal val dayEventList = listOf(

@Composable
fun SelectableCalendarSample() {

val calendarState = rememberSelectableCalendarState(
initialEventList = dayEventList
)
Expand Down

0 comments on commit b4b6f1b

Please sign in to comment.