diff --git a/shared/src/commonMain/kotlin/plus/rua/project/ui/CalendarMonthView.kt b/shared/src/commonMain/kotlin/plus/rua/project/ui/CalendarMonthView.kt index d805129..58feb7d 100644 --- a/shared/src/commonMain/kotlin/plus/rua/project/ui/CalendarMonthView.kt +++ b/shared/src/commonMain/kotlin/plus/rua/project/ui/CalendarMonthView.kt @@ -9,7 +9,6 @@ import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.statusBarsPadding import androidx.compose.foundation.pager.rememberPagerState import androidx.compose.runtime.Composable -import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.derivedStateOf import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableIntStateOf @@ -61,22 +60,6 @@ fun CalendarMonthView( val pagerState = rememberPagerState(initialPage = START_PAGE, pageCount = { Int.MAX_VALUE }) - // 展开时同步 CalendarPager 页面到 selectedDate 所在月份 - LaunchedEffect(viewModel.isCollapsed) { - if (!viewModel.isCollapsed) { - @Suppress("DEPRECATION") // monthNumber 无替代 API - val targetPage = yearMonthToPage( - viewModel.selectedDate.year, - viewModel.selectedDate.month.number, - today.year, - today.month.number - ) - if (targetPage != pagerState.currentPage) { - pagerState.scrollToPage(targetPage) - } - } - } - val collapseProgress = viewModel.collapseProgress val headerHeightPx = monthHeaderHeightPx + weekdayHeaderHeightPx val rowPaddingPx = with(density) { ROW_PADDING_DP.dp.toPx() }.toInt() @@ -179,9 +162,21 @@ fun CalendarMonthView( today = today, onDateClick = { date -> viewModel.selectDate(date) }, onWeekChanged = { weekMonday -> - // 优先选中当周内的今天,否则选中该周周一 val weekSunday = weekMonday.plus(DatePeriod(days = 6)) - val date = if (today in weekMonday..weekSunday) today else weekMonday + val date = when { + today in weekMonday..weekSunday -> today + weekMonday.month != weekSunday.month -> { + if (weekMonday < viewModel.selectedDate) { + // 后退到跨月周(如从5月回到4月27-5月3):选较晚月份1号,留在当月 + @Suppress("DEPRECATION") // monthNumber 无替代 API + LocalDate(weekSunday.year, weekSunday.month.number, 1) + } else { + // 前进到跨月周(如从4月前进到4月27-5月3):选该周周一,留在上个月 + weekMonday + } + } + else -> weekMonday + } viewModel.selectDate(date) }, modifier = pagerModifier diff --git a/shared/src/commonMain/kotlin/plus/rua/project/ui/CalendarPager.kt b/shared/src/commonMain/kotlin/plus/rua/project/ui/CalendarPager.kt index 1d29e21..6d96511 100644 --- a/shared/src/commonMain/kotlin/plus/rua/project/ui/CalendarPager.kt +++ b/shared/src/commonMain/kotlin/plus/rua/project/ui/CalendarPager.kt @@ -52,6 +52,17 @@ fun CalendarPager( val initialMonth = remember { today.month.number } val coroutineScope = rememberCoroutineScope() + // 展开后同步页面到 selectedDate 所在月份(修复折叠态切月后展开闪跳) + LaunchedEffect(Unit) { + @Suppress("DEPRECATION") // monthNumber 无替代 API + val targetPage = yearMonthToPage( + selectedDate.year, selectedDate.month.number, initialYear, initialMonth + ) + if (targetPage != pagerState.currentPage) { + pagerState.scrollToPage(targetPage) + } + } + // 跳过初始发射,保留首次渲染时的"今天"选中状态 LaunchedEffect(pagerState) { snapshotFlow { pagerState.settledPage }.drop(1).collect { page ->