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 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
| @Composable private fun CalendarGrid( modifier: Modifier, days: MutableList<CalendarDay>, spread: MutableState<Boolean>, selected: MutableState<CalendarDay>, year: Int, month: Int, calendarMonth: MutableState<Int>, onItemSelected: (CalendarDay) -> Unit, onPageChanged: (PageChangedState) -> Unit, ) { val initialPage = getMonthsPassedSince2023(year, month) val pageState = rememberPagerState(pageCount = { 924 }, initialPage = initialPage) val currentPage = remember { mutableIntStateOf(initialPage) }
LaunchedEffect(pageState) { snapshotFlow { pageState.currentPage }.collect { page -> if (spread.value) { if (currentPage.intValue < page) { onPageChanged(PageChangedState.NEXT) } else if (currentPage.intValue > page) { onPageChanged(PageChangedState.PREV) } Log.i("ComposeCalendar", "page: ${page}, currentPage: ${pageState.currentPage}") currentPage.intValue = page } } }
LaunchedEffect(year, month) { val newInitialPage = getMonthsPassedSince2023(year, month) currentPage.intValue = newInitialPage pageState.scrollToPage(newInitialPage) }
HorizontalPager(state = pageState, pageSpacing = 56.dp, verticalAlignment = Alignment.Top, userScrollEnabled = spread.value) { page -> LazyVerticalGrid( columns = GridCells.Fixed(7), contentPadding = PaddingValues(0.dp), modifier = Modifier.heightIn(min = 68.dp, max = (68 * 7).dp) ) { val currentMonth = getMonthFromPage(page)
if (days.isNotEmpty()) { Log.i("ComposeCalendar", "page: ${page}, currentPage: ${currentPage.intValue}, month: $month, currentMonth: $currentMonth, days[15].month: ${days[15].month}") val dayList = if (page < currentPage.intValue || currentMonth < days[15].month) getWeekDays(days.first().getPreviousCalendar()) else if (page == currentPage.intValue && (currentMonth == month) && days[15].month == month) days else getWeekDays(days.last().getNextCalendar())
items(dayList) { date -> AnimatedVisibility(visible = spread.value) { DayOfMonthIcon(date, selected, modifier = modifier) { selected.value = it calendarMonth.value = month onItemSelected(it) } } AnimatedVisibility(visible = !spread.value) { if (date.isSameWeek(selected.value)) { DayOfMonthIcon(date, selected, modifier = modifier) { selected.value = it calendarMonth.value = month onItemSelected(it) } } } } } } } }
|