Fix lint warnings: remove unused variable, replace monthNumber, use range check
- Remove unused currentWeeksCount variable and onWeeksChanged callback
- Replace deprecated monthNumber with month.number (requires explicit import)
- Remove all @Suppress("DEPRECATION") annotations for monthNumber
- Convert double comparison to range check (today in weekMonday..weekSunday)
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
parent
b794601134
commit
7e972bb4fb
@ -12,6 +12,7 @@ import kotlinx.datetime.LocalDate
|
|||||||
import kotlinx.datetime.TimeZone
|
import kotlinx.datetime.TimeZone
|
||||||
import kotlinx.datetime.daysUntil
|
import kotlinx.datetime.daysUntil
|
||||||
import kotlinx.datetime.minus
|
import kotlinx.datetime.minus
|
||||||
|
import kotlinx.datetime.number
|
||||||
import kotlinx.datetime.plus
|
import kotlinx.datetime.plus
|
||||||
import kotlinx.datetime.todayIn
|
import kotlinx.datetime.todayIn
|
||||||
import kotlin.time.Clock
|
import kotlin.time.Clock
|
||||||
@ -42,8 +43,7 @@ class CalendarViewModel(private val coroutineScope: CoroutineScope) {
|
|||||||
val collapseProgress: Float get() = _collapseAnimatable.value
|
val collapseProgress: Float get() = _collapseAnimatable.value
|
||||||
|
|
||||||
val currentYear: Int get() = selectedDate.year
|
val currentYear: Int get() = selectedDate.year
|
||||||
@Suppress("DEPRECATION") // monthNumber 无替代 API,kotlinx-datetime 尚未提供新接口
|
val currentMonth: Int get() = selectedDate.month.number
|
||||||
val currentMonth: Int get() = selectedDate.monthNumber
|
|
||||||
|
|
||||||
fun selectDate(date: LocalDate) {
|
fun selectDate(date: LocalDate) {
|
||||||
selectedDate = date
|
selectedDate = date
|
||||||
@ -132,7 +132,6 @@ class CalendarViewModel(private val coroutineScope: CoroutineScope) {
|
|||||||
return diff / 7 + 1
|
return diff / 7 + 1
|
||||||
}
|
}
|
||||||
|
|
||||||
@Suppress("DEPRECATION") // monthNumber 无替代 API,kotlinx-datetime 尚未提供新接口
|
|
||||||
fun getMonthDays(year: Int, month: Int): List<CalendarDay> {
|
fun getMonthDays(year: Int, month: Int): List<CalendarDay> {
|
||||||
val firstOfMonth = LocalDate(year, month, 1)
|
val firstOfMonth = LocalDate(year, month, 1)
|
||||||
val dayOfWeekOffset = firstOfMonth.dayOfWeek.ordinal
|
val dayOfWeekOffset = firstOfMonth.dayOfWeek.ordinal
|
||||||
@ -146,7 +145,7 @@ class CalendarViewModel(private val coroutineScope: CoroutineScope) {
|
|||||||
val date = startDate.plus(DatePeriod(days = i))
|
val date = startDate.plus(DatePeriod(days = i))
|
||||||
CalendarDay(
|
CalendarDay(
|
||||||
date = date,
|
date = date,
|
||||||
isCurrentMonth = date.monthNumber == month && date.year == year,
|
isCurrentMonth = date.month.number == month && date.year == year,
|
||||||
isToday = date == today,
|
isToday = date == today,
|
||||||
isSelected = date == selectedDate
|
isSelected = date == selectedDate
|
||||||
)
|
)
|
||||||
|
|||||||
@ -17,6 +17,7 @@ import androidx.compose.ui.zIndex
|
|||||||
import kotlinx.datetime.DatePeriod
|
import kotlinx.datetime.DatePeriod
|
||||||
import kotlinx.datetime.LocalDate
|
import kotlinx.datetime.LocalDate
|
||||||
import kotlinx.datetime.minus
|
import kotlinx.datetime.minus
|
||||||
|
import kotlinx.datetime.number
|
||||||
import kotlinx.datetime.plus
|
import kotlinx.datetime.plus
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -141,7 +142,6 @@ private data class DayData(
|
|||||||
val isCurrentMonth: Boolean
|
val isCurrentMonth: Boolean
|
||||||
)
|
)
|
||||||
|
|
||||||
@Suppress("DEPRECATION") // monthNumber 无替代 API,kotlinx-datetime 尚未提供新接口
|
|
||||||
private fun generateMonthDays(year: Int, month: Int): List<DayData> {
|
private fun generateMonthDays(year: Int, month: Int): List<DayData> {
|
||||||
val firstOfMonth = LocalDate(year, month, 1)
|
val firstOfMonth = LocalDate(year, month, 1)
|
||||||
val offset = firstOfMonth.dayOfWeek.ordinal
|
val offset = firstOfMonth.dayOfWeek.ordinal
|
||||||
@ -155,7 +155,7 @@ private fun generateMonthDays(year: Int, month: Int): List<DayData> {
|
|||||||
val date = startDate.plus(DatePeriod(days = i))
|
val date = startDate.plus(DatePeriod(days = i))
|
||||||
DayData(
|
DayData(
|
||||||
date = date,
|
date = date,
|
||||||
isCurrentMonth = date.monthNumber == month && date.year == year
|
isCurrentMonth = date.month.number == month && date.year == year
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -24,6 +24,7 @@ import androidx.compose.ui.unit.dp
|
|||||||
import kotlinx.datetime.DatePeriod
|
import kotlinx.datetime.DatePeriod
|
||||||
import kotlinx.datetime.LocalDate
|
import kotlinx.datetime.LocalDate
|
||||||
import kotlinx.datetime.TimeZone
|
import kotlinx.datetime.TimeZone
|
||||||
|
import kotlinx.datetime.number
|
||||||
import kotlinx.datetime.plus
|
import kotlinx.datetime.plus
|
||||||
import kotlinx.datetime.todayIn
|
import kotlinx.datetime.todayIn
|
||||||
import kotlin.math.abs
|
import kotlin.math.abs
|
||||||
@ -56,8 +57,6 @@ fun CalendarMonthView(
|
|||||||
var monthHeaderHeightPx by remember { mutableIntStateOf(0) }
|
var monthHeaderHeightPx by remember { mutableIntStateOf(0) }
|
||||||
var weekdayHeaderHeightPx by remember { mutableIntStateOf(0) }
|
var weekdayHeaderHeightPx by remember { mutableIntStateOf(0) }
|
||||||
var rowHeightPx 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 screenWidthPx by remember { mutableIntStateOf(0) }
|
||||||
var screenHeightPx by remember { mutableIntStateOf(0) }
|
var screenHeightPx by remember { mutableIntStateOf(0) }
|
||||||
|
|
||||||
@ -153,11 +152,10 @@ fun CalendarMonthView(
|
|||||||
onDateClick = { date -> viewModel.selectDate(date) },
|
onDateClick = { date -> viewModel.selectDate(date) },
|
||||||
onWeekChanged = { weekMonday ->
|
onWeekChanged = { weekMonday ->
|
||||||
val weekSunday = weekMonday.plus(DatePeriod(days = 6))
|
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)
|
viewModel.selectDate(date)
|
||||||
currentYear = date.year
|
currentYear = date.year
|
||||||
@Suppress("DEPRECATION") // monthNumber 无替代 API,kotlinx-datetime 尚未提供新接口
|
currentMonth = date.month.number
|
||||||
currentMonth = date.monthNumber
|
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
} else {
|
} else {
|
||||||
@ -166,7 +164,7 @@ fun CalendarMonthView(
|
|||||||
today = today,
|
today = today,
|
||||||
onDateClick = { date -> viewModel.selectDate(date) },
|
onDateClick = { date -> viewModel.selectDate(date) },
|
||||||
onMonthChanged = { year, month ->
|
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)
|
else LocalDate(year, month, 1)
|
||||||
viewModel.selectDate(date)
|
viewModel.selectDate(date)
|
||||||
currentYear = year
|
currentYear = year
|
||||||
@ -175,9 +173,6 @@ fun CalendarMonthView(
|
|||||||
collapseProgress = viewModel.collapseProgress,
|
collapseProgress = viewModel.collapseProgress,
|
||||||
rowHeightPx = rowHeightPx,
|
rowHeightPx = rowHeightPx,
|
||||||
effectiveWeeks = effectiveWeeks,
|
effectiveWeeks = effectiveWeeks,
|
||||||
onWeeksChanged = { weeks ->
|
|
||||||
currentWeeksCount = weeks
|
|
||||||
},
|
|
||||||
onRowHeightMeasured = { h ->
|
onRowHeightMeasured = { h ->
|
||||||
if (h > 0 && rowHeightPx == 0) rowHeightPx = 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
|
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 {
|
private fun calculateWeeksCountForPage(page: Int, today: LocalDate): Int {
|
||||||
val initialYear = today.year
|
val initialYear = today.year
|
||||||
val initialMonth = today.monthNumber
|
val initialMonth = today.month.number
|
||||||
val offset = page - START_PAGE
|
val offset = page - START_PAGE
|
||||||
val totalMonths = initialYear * 12 + (initialMonth - 1) + offset
|
val totalMonths = initialYear * 12 + (initialMonth - 1) + offset
|
||||||
val year = totalMonths / 12
|
val year = totalMonths / 12
|
||||||
|
|||||||
@ -15,6 +15,7 @@ import kotlinx.coroutines.launch
|
|||||||
import kotlinx.datetime.DatePeriod
|
import kotlinx.datetime.DatePeriod
|
||||||
import kotlinx.datetime.LocalDate
|
import kotlinx.datetime.LocalDate
|
||||||
import kotlinx.datetime.minus
|
import kotlinx.datetime.minus
|
||||||
|
import kotlinx.datetime.number
|
||||||
|
|
||||||
/** 无限分页中心页,用于 HorizontalPager 的起始位置 */
|
/** 无限分页中心页,用于 HorizontalPager 的起始位置 */
|
||||||
private const val START_PAGE = Int.MAX_VALUE / 2
|
private const val START_PAGE = Int.MAX_VALUE / 2
|
||||||
@ -39,7 +40,6 @@ fun CalendarPager(
|
|||||||
collapseProgress: Float,
|
collapseProgress: Float,
|
||||||
rowHeightPx: Int,
|
rowHeightPx: Int,
|
||||||
effectiveWeeks: Float,
|
effectiveWeeks: Float,
|
||||||
onWeeksChanged: ((Int) -> Unit)? = null,
|
|
||||||
onRowHeightMeasured: ((Int) -> Unit)? = null,
|
onRowHeightMeasured: ((Int) -> Unit)? = null,
|
||||||
pagerState: PagerState,
|
pagerState: PagerState,
|
||||||
modifier: Modifier = Modifier
|
modifier: Modifier = Modifier
|
||||||
@ -51,7 +51,6 @@ fun CalendarPager(
|
|||||||
LaunchedEffect(pagerState) {
|
LaunchedEffect(pagerState) {
|
||||||
snapshotFlow { pagerState.settledPage }.drop(1).collect { page ->
|
snapshotFlow { pagerState.settledPage }.drop(1).collect { page ->
|
||||||
val yearMonth = pageToYearMonth(page, initialYearMonth)
|
val yearMonth = pageToYearMonth(page, initialYearMonth)
|
||||||
onWeeksChanged?.invoke(calculateWeeksCount(yearMonth.first, yearMonth.second))
|
|
||||||
onMonthChanged(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<Int, Int> = Pair(year, month.number)
|
||||||
private fun LocalDate.toYearMonth(): Pair<Int, Int> = Pair(year, monthNumber)
|
|
||||||
|
|
||||||
// 页码→年月:偏移量 + 初始月份的绝对月数,再拆分回年月
|
// 页码→年月:偏移量 + 初始月份的绝对月数,再拆分回年月
|
||||||
private fun pageToYearMonth(page: Int, initial: Pair<Int, Int>): Pair<Int, Int> {
|
private fun pageToYearMonth(page: Int, initial: Pair<Int, Int>): Pair<Int, Int> {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user