Skip initial snapshotFlow emission in pagers to preserve today selection

Adding .drop(1) prevents the settledPage flow from firing on initial
composition, which would override the "today" date selection.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
xfy 2026-05-14 17:37:27 +08:00
parent 1cf2766e84
commit e5d7efd603
2 changed files with 5 additions and 3 deletions

View File

@ -9,6 +9,7 @@ import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.snapshotFlow import androidx.compose.runtime.snapshotFlow
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import kotlinx.coroutines.flow.drop
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.datetime.LocalDate import kotlinx.datetime.LocalDate
@ -41,9 +42,9 @@ fun CalendarPager(
) )
val coroutineScope = rememberCoroutineScope() val coroutineScope = rememberCoroutineScope()
// Sync settled page to onMonthChanged // Sync settled page to onMonthChanged (skip initial emission to preserve "today" selection)
LaunchedEffect(pagerState) { LaunchedEffect(pagerState) {
snapshotFlow { pagerState.settledPage }.collect { page -> snapshotFlow { pagerState.settledPage }.drop(1).collect { page ->
val yearMonth = pageToYearMonth(page, initialYearMonth) val yearMonth = pageToYearMonth(page, initialYearMonth)
onMonthChanged(yearMonth.first, yearMonth.second) onMonthChanged(yearMonth.first, yearMonth.second)
} }

View File

@ -13,6 +13,7 @@ import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.snapshotFlow import androidx.compose.runtime.snapshotFlow
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import kotlinx.coroutines.flow.drop
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.datetime.DatePeriod import kotlinx.datetime.DatePeriod
import kotlinx.datetime.LocalDate import kotlinx.datetime.LocalDate
@ -46,7 +47,7 @@ fun WeekPager(
) )
LaunchedEffect(pagerState) { LaunchedEffect(pagerState) {
snapshotFlow { pagerState.settledPage }.collect { page -> snapshotFlow { pagerState.settledPage }.drop(1).collect { page ->
val weekMonday = pageToWeekMonday(page, initialWeekMonday) val weekMonday = pageToWeekMonday(page, initialWeekMonday)
onWeekChanged(weekMonday) onWeekChanged(weekMonday)
} }