diff --git a/shared/src/commonMain/kotlin/plus/rua/project/CalendarViewModel.kt b/shared/src/commonMain/kotlin/plus/rua/project/CalendarViewModel.kt index 0930b69..20698c5 100644 --- a/shared/src/commonMain/kotlin/plus/rua/project/CalendarViewModel.kt +++ b/shared/src/commonMain/kotlin/plus/rua/project/CalendarViewModel.kt @@ -12,6 +12,7 @@ import kotlinx.datetime.LocalDate import kotlinx.datetime.TimeZone import kotlinx.datetime.daysUntil import kotlinx.datetime.minus +import kotlinx.datetime.number import kotlinx.datetime.plus import kotlinx.datetime.todayIn import kotlin.time.Clock @@ -42,8 +43,7 @@ class CalendarViewModel(private val coroutineScope: CoroutineScope) { val collapseProgress: Float get() = _collapseAnimatable.value val currentYear: Int get() = selectedDate.year - @Suppress("DEPRECATION") // monthNumber 无替代 API,kotlinx-datetime 尚未提供新接口 - val currentMonth: Int get() = selectedDate.monthNumber + val currentMonth: Int get() = selectedDate.month.number fun selectDate(date: LocalDate) { selectedDate = date @@ -132,7 +132,6 @@ class CalendarViewModel(private val coroutineScope: CoroutineScope) { return diff / 7 + 1 } - @Suppress("DEPRECATION") // monthNumber 无替代 API,kotlinx-datetime 尚未提供新接口 fun getMonthDays(year: Int, month: Int): List { val firstOfMonth = LocalDate(year, month, 1) val dayOfWeekOffset = firstOfMonth.dayOfWeek.ordinal @@ -146,7 +145,7 @@ class CalendarViewModel(private val coroutineScope: CoroutineScope) { val date = startDate.plus(DatePeriod(days = i)) CalendarDay( date = date, - isCurrentMonth = date.monthNumber == month && date.year == year, + isCurrentMonth = date.month.number == month && date.year == year, isToday = date == today, isSelected = date == selectedDate ) diff --git a/shared/src/commonMain/kotlin/plus/rua/project/ui/CalendarMonthPage.kt b/shared/src/commonMain/kotlin/plus/rua/project/ui/CalendarMonthPage.kt index 7d59351..776967e 100644 --- a/shared/src/commonMain/kotlin/plus/rua/project/ui/CalendarMonthPage.kt +++ b/shared/src/commonMain/kotlin/plus/rua/project/ui/CalendarMonthPage.kt @@ -17,6 +17,7 @@ import androidx.compose.ui.zIndex import kotlinx.datetime.DatePeriod import kotlinx.datetime.LocalDate import kotlinx.datetime.minus +import kotlinx.datetime.number import kotlinx.datetime.plus /** @@ -141,7 +142,6 @@ private data class DayData( val isCurrentMonth: Boolean ) -@Suppress("DEPRECATION") // monthNumber 无替代 API,kotlinx-datetime 尚未提供新接口 private fun generateMonthDays(year: Int, month: Int): List { val firstOfMonth = LocalDate(year, month, 1) val offset = firstOfMonth.dayOfWeek.ordinal @@ -155,7 +155,7 @@ private fun generateMonthDays(year: Int, month: Int): List { val date = startDate.plus(DatePeriod(days = i)) DayData( date = date, - isCurrentMonth = date.monthNumber == month && date.year == year + isCurrentMonth = date.month.number == month && date.year == year ) } } 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 ff8adfd..7c6509e 100644 --- a/shared/src/commonMain/kotlin/plus/rua/project/ui/CalendarMonthView.kt +++ b/shared/src/commonMain/kotlin/plus/rua/project/ui/CalendarMonthView.kt @@ -24,6 +24,7 @@ import androidx.compose.ui.unit.dp import kotlinx.datetime.DatePeriod import kotlinx.datetime.LocalDate import kotlinx.datetime.TimeZone +import kotlinx.datetime.number import kotlinx.datetime.plus import kotlinx.datetime.todayIn import kotlin.math.abs @@ -56,8 +57,6 @@ fun CalendarMonthView( var monthHeaderHeightPx by remember { mutableIntStateOf(0) } var weekdayHeaderHeightPx by remember { mutableIntStateOf(0) } var rowHeightPx by remember { mutableIntStateOf(0) } - @Suppress("DEPRECATION") // monthNumber 无替代 API,kotlinx-datetime 尚未提供新接口 - var currentWeeksCount by remember { mutableIntStateOf(calculateWeeksCount(today.year, today.monthNumber)) } var screenWidthPx by remember { mutableIntStateOf(0) } var screenHeightPx by remember { mutableIntStateOf(0) } @@ -153,11 +152,10 @@ fun CalendarMonthView( onDateClick = { date -> viewModel.selectDate(date) }, onWeekChanged = { weekMonday -> val weekSunday = weekMonday.plus(DatePeriod(days = 6)) - val date = if (today >= weekMonday && today <= weekSunday) today else weekMonday + val date = if (today in weekMonday..weekSunday) today else weekMonday viewModel.selectDate(date) currentYear = date.year - @Suppress("DEPRECATION") // monthNumber 无替代 API,kotlinx-datetime 尚未提供新接口 - currentMonth = date.monthNumber + currentMonth = date.month.number } ) } else { @@ -166,7 +164,7 @@ fun CalendarMonthView( today = today, onDateClick = { date -> viewModel.selectDate(date) }, onMonthChanged = { year, month -> - val date = if (year == today.year && today.monthNumber == month) today + val date = if (year == today.year && today.month.number == month) today else LocalDate(year, month, 1) viewModel.selectDate(date) currentYear = year @@ -175,9 +173,6 @@ fun CalendarMonthView( collapseProgress = viewModel.collapseProgress, rowHeightPx = rowHeightPx, effectiveWeeks = effectiveWeeks, - onWeeksChanged = { weeks -> - currentWeeksCount = weeks - }, onRowHeightMeasured = { h -> if (h > 0 && rowHeightPx == 0) rowHeightPx = h }, @@ -201,10 +196,9 @@ fun CalendarMonthView( private fun lerp(start: Float, end: Float, fraction: Float): Float = start + (end - start) * fraction -@Suppress("DEPRECATION") // monthNumber 无替代 API,kotlinx-datetime 尚未提供新接口 private fun calculateWeeksCountForPage(page: Int, today: LocalDate): Int { val initialYear = today.year - val initialMonth = today.monthNumber + val initialMonth = today.month.number val offset = page - START_PAGE val totalMonths = initialYear * 12 + (initialMonth - 1) + offset val year = totalMonths / 12 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 c176561..e43eaa0 100644 --- a/shared/src/commonMain/kotlin/plus/rua/project/ui/CalendarPager.kt +++ b/shared/src/commonMain/kotlin/plus/rua/project/ui/CalendarPager.kt @@ -15,6 +15,7 @@ import kotlinx.coroutines.launch import kotlinx.datetime.DatePeriod import kotlinx.datetime.LocalDate import kotlinx.datetime.minus +import kotlinx.datetime.number /** 无限分页中心页,用于 HorizontalPager 的起始位置 */ private const val START_PAGE = Int.MAX_VALUE / 2 @@ -39,7 +40,6 @@ fun CalendarPager( collapseProgress: Float, rowHeightPx: Int, effectiveWeeks: Float, - onWeeksChanged: ((Int) -> Unit)? = null, onRowHeightMeasured: ((Int) -> Unit)? = null, pagerState: PagerState, modifier: Modifier = Modifier @@ -51,7 +51,6 @@ fun CalendarPager( LaunchedEffect(pagerState) { snapshotFlow { pagerState.settledPage }.drop(1).collect { page -> val yearMonth = pageToYearMonth(page, initialYearMonth) - onWeeksChanged?.invoke(calculateWeeksCount(yearMonth.first, yearMonth.second)) onMonthChanged(yearMonth.first, yearMonth.second) } } @@ -89,8 +88,7 @@ fun CalendarPager( } } -@Suppress("DEPRECATION") // monthNumber 无替代 API,kotlinx-datetime 尚未提供新接口 -private fun LocalDate.toYearMonth(): Pair = Pair(year, monthNumber) +private fun LocalDate.toYearMonth(): Pair = Pair(year, month.number) // 页码→年月:偏移量 + 初始月份的绝对月数,再拆分回年月 private fun pageToYearMonth(page: Int, initial: Pair): Pair {