refactor: 清理不必要的 @Suppress 注解并替换弃用 API

移除 23 处不必要或可通过改写避免的 @Suppress。
- 删除 12 处完全不必要(误加/重构遗留)
- 替换 9 处 monthNumber/dayOfMonth → month.number/day
- 替换 4 处弃用构造函数 LocalDate(year,month,1) → LocalDate(year,Month(month),1)
- 保留 6 处必要 @Suppress(NOTHING_TO_INLINE、Android API 兼容等)
This commit is contained in:
xfy 2026-06-01 17:55:18 +08:00
parent 484045950f
commit e249700ee5
11 changed files with 28 additions and 45 deletions

View File

@ -12,6 +12,7 @@ import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.stateIn import kotlinx.coroutines.flow.stateIn
import kotlinx.datetime.DatePeriod import kotlinx.datetime.DatePeriod
import kotlinx.datetime.LocalDate import kotlinx.datetime.LocalDate
import kotlinx.datetime.Month
import kotlinx.datetime.TimeZone import kotlinx.datetime.TimeZone
import kotlinx.datetime.daysUntil import kotlinx.datetime.daysUntil
import kotlinx.datetime.minus import kotlinx.datetime.minus
@ -100,8 +101,6 @@ class CalendarViewModel(
// 预计算当前月前后各 1 个月(在协程中异步执行) // 预计算当前月前后各 1 个月(在协程中异步执行)
val currentYear = today.year val currentYear = today.year
val currentMonth = today.month.number val currentMonth = today.month.number
@Suppress("DEPRECATION") // monthNumber 无替代 API
val monthsToPrecompute = listOf( val monthsToPrecompute = listOf(
currentMonth - 1 to currentYear, currentMonth - 1 to currentYear,
currentMonth to currentYear, currentMonth to currentYear,
@ -135,7 +134,6 @@ class CalendarViewModel(
private val _collapseProgress = MutableStateFlow(0f) private val _collapseProgress = MutableStateFlow(0f)
val collapseProgress: StateFlow<Float> = _collapseProgress.asStateFlow() val collapseProgress: StateFlow<Float> = _collapseProgress.asStateFlow()
@Suppress("DEPRECATION") // monthNumber 无替代 APIkotlinx-datetime 尚未提供新接口
val currentMonth: Int get() = selectedDate.value.month.number val currentMonth: Int get() = selectedDate.value.month.number
val currentYear: Int get() = selectedDate.value.year val currentYear: Int get() = selectedDate.value.year
@ -244,13 +242,12 @@ class CalendarViewModel(
/** /**
* 从年视图选择月份后返回月视图 * 从年视图选择月份后返回月视图
*/ */
@Suppress("DEPRECATION") // monthNumber 无替代 API
fun selectMonthFromYearView(month: Int) { fun selectMonthFromYearView(month: Int) {
val t0 = System.nanoTime() val t0 = System.nanoTime()
logd(TAG_VM, "[selectMonthFromYearView] ===== START month=$month t=$t0 =====") logd(TAG_VM, "[selectMonthFromYearView] ===== START month=$month t=$t0 =====")
composeTraceBeginSection("YearView:SelectMonth") composeTraceBeginSection("YearView:SelectMonth")
val date = if (_yearViewYear.value == today.year && today.month.number == month) today val date = if (_yearViewYear.value == today.year && today.month.number == month) today
else LocalDate(_yearViewYear.value, month, 1) else LocalDate(_yearViewYear.value, Month(month), 1)
logd(TAG_VM, "[selectMonthFromYearView] targetDate=$date dt=${(System.nanoTime() - t0) / 1_000_000}ms") logd(TAG_VM, "[selectMonthFromYearView] targetDate=$date dt=${(System.nanoTime() - t0) / 1_000_000}ms")
_selectedDate.value = date _selectedDate.value = date
logd(TAG_VM, "[selectMonthFromYearView] selectedDate set dt=${(System.nanoTime() - t0) / 1_000_000}ms") logd(TAG_VM, "[selectMonthFromYearView] selectedDate set dt=${(System.nanoTime() - t0) / 1_000_000}ms")
@ -376,7 +373,6 @@ class CalendarViewModel(
* @param month 月份1-12 * @param month 月份1-12
* @return 日历网格列表每项包含日期是否当月是否今天是否选中 * @return 日历网格列表每项包含日期是否当月是否今天是否选中
*/ */
@Suppress("DEPRECATION") // monthNumber 无替代 APIkotlinx-datetime 尚未提供新接口
fun getMonthDays(year: Int, month: Int): List<CalendarDay> { fun getMonthDays(year: Int, month: Int): List<CalendarDay> {
composeTraceBeginSection("getMonthDays:$year-$month") composeTraceBeginSection("getMonthDays:$year-$month")
val info = getMonthGridInfo(year, month) val info = getMonthGridInfo(year, month)

View File

@ -4,6 +4,7 @@ import com.tyme.solar.SolarDay
import kotlinx.coroutines.sync.Mutex import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock import kotlinx.coroutines.sync.withLock
import kotlinx.datetime.LocalDate import kotlinx.datetime.LocalDate
import kotlinx.datetime.number
/** /**
* 农历/节气/节假日信息缓存 * 农历/节气/节假日信息缓存
@ -18,13 +19,11 @@ class LunarCache(
) { ) {
private val mutex = Mutex() private val mutex = Mutex()
@Suppress("DEPRECATION")
private val cache = LinkedHashMap<LocalDate, DayCellInfo>(256, 0.75f, true) private val cache = LinkedHashMap<LocalDate, DayCellInfo>(256, 0.75f, true)
/** /**
* 获取指定日期的信息缓存 miss 时计算 * 获取指定日期的信息缓存 miss 时计算
*/ */
@Suppress("DEPRECATION") // monthNumber 无替代 API
suspend fun getOrCompute(date: LocalDate): DayCellInfo = mutex.withLock { suspend fun getOrCompute(date: LocalDate): DayCellInfo = mutex.withLock {
cache[date]?.let { return@withLock it } cache[date]?.let { return@withLock it }
val computed = compute(date) val computed = compute(date)
@ -52,7 +51,6 @@ class LunarCache(
* *
* 复用缓存中的 lunarMonthName annotationText避免重复创建 SolarDay * 复用缓存中的 lunarMonthName annotationText避免重复创建 SolarDay
*/ */
@Suppress("DEPRECATION") // monthNumber 无替代 API
suspend fun formatLunarDate(date: LocalDate): String { suspend fun formatLunarDate(date: LocalDate): String {
val info = getOrCompute(date) val info = getOrCompute(date)
val dayText = info.annotationText.removeSuffix("") val dayText = info.annotationText.removeSuffix("")
@ -71,9 +69,8 @@ class LunarCache(
} }
} }
@Suppress("DEPRECATION") // monthNumber 无替代 API
private fun compute(date: LocalDate): DayCellInfo { private fun compute(date: LocalDate): DayCellInfo {
val solarDay = SolarDay.fromYmd(date.year, date.monthNumber, date.day) val solarDay = SolarDay.fromYmd(date.year, date.month.number, date.day)
val holidayBadge = solarDay.getLegalHoliday()?.let { if (it.isWork()) "" else "" } val holidayBadge = solarDay.getLegalHoliday()?.let { if (it.isWork()) "" else "" }
val lunarDay = solarDay.getLunarDay() val lunarDay = solarDay.getLunarDay()
val lunarMonth = lunarDay.getLunarMonth() val lunarMonth = lunarDay.getLunarMonth()

View File

@ -31,6 +31,7 @@ import androidx.compose.ui.semantics.semantics
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp import androidx.compose.ui.unit.sp
import kotlinx.datetime.LocalDate import kotlinx.datetime.LocalDate
import kotlinx.datetime.number
import plus.rua.project.LunarCache import plus.rua.project.LunarCache
import plus.rua.project.ShiftKind import plus.rua.project.ShiftKind
@ -68,8 +69,7 @@ fun BottomCard(
) { ) {
val relativeDesc = relativeDayDescription(selectedDate, today) val relativeDesc = relativeDayDescription(selectedDate, today)
@Suppress("DEPRECATION") // monthNumber 无替代 APIkotlinx-datetime 尚未提供新接口 val solarDesc = "${selectedDate.month.number}${selectedDate.day}"
val solarDesc = "${selectedDate.monthNumber}${selectedDate.day}"
val lunarDesc by produceState( val lunarDesc by produceState(
initialValue = "", initialValue = "",
key1 = selectedDate key1 = selectedDate

View File

@ -26,6 +26,7 @@ import plus.rua.project.composeTraceEndSection
import plus.rua.project.util.logd import plus.rua.project.util.logd
import kotlinx.datetime.DatePeriod import kotlinx.datetime.DatePeriod
import kotlinx.datetime.LocalDate import kotlinx.datetime.LocalDate
import kotlinx.datetime.Month
import kotlinx.datetime.minus import kotlinx.datetime.minus
import kotlinx.datetime.number import kotlinx.datetime.number
import kotlinx.datetime.plus import kotlinx.datetime.plus
@ -290,12 +291,11 @@ data class HolidayEdgeInfo(
val isEnd: Boolean val isEnd: Boolean
) )
@Suppress("DEPRECATION") // monthNumber 无替代 APIkotlinx-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(month), 1)
val offset = firstOfMonth.dayOfWeek.ordinal val offset = firstOfMonth.dayOfWeek.ordinal
val startDate = firstOfMonth.minus(DatePeriod(days = offset)) val startDate = firstOfMonth.minus(DatePeriod(days = offset))
val nextMonth = if (month == 12) LocalDate(year + 1, 1, 1) else LocalDate(year, month + 1, 1) val nextMonth = if (month == 12) LocalDate(year + 1, 1, 1) else LocalDate(year, Month(month + 1), 1)
val daysInMonth = nextMonth.minus(DatePeriod(days = 1)).day val daysInMonth = nextMonth.minus(DatePeriod(days = 1)).day
val rows = ((offset + daysInMonth - 1) / 7) + 1 val rows = ((offset + daysInMonth - 1) / 7) + 1
val totalDays = rows * 7 val totalDays = rows * 7

View File

@ -72,6 +72,7 @@ import androidx.compose.ui.unit.dp
import kotlinx.coroutines.delay import kotlinx.coroutines.delay
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.datetime.LocalDate import kotlinx.datetime.LocalDate
import kotlinx.datetime.Month
import kotlinx.datetime.TimeZone import kotlinx.datetime.TimeZone
import kotlinx.datetime.number import kotlinx.datetime.number
import kotlinx.datetime.plus import kotlinx.datetime.plus
@ -105,7 +106,6 @@ fun CalendarMonthView(
val uiState by viewModel.uiState.collectAsState() val uiState by viewModel.uiState.collectAsState()
val selectedDate = uiState.selectedDate val selectedDate = uiState.selectedDate
val currentYear = selectedDate.year val currentYear = selectedDate.year
@Suppress("DEPRECATION") // monthNumber 无替代 APIkotlinx-datetime 尚未提供新接口
val currentMonth = selectedDate.month.number val currentMonth = selectedDate.month.number
val isCollapsed = uiState.isCollapsed val isCollapsed = uiState.isCollapsed
val isYearView = uiState.isYearView val isYearView = uiState.isYearView
@ -162,7 +162,6 @@ fun CalendarMonthView(
// 折叠态 WeekPager 切月时,持续同步 CalendarPager 的 pagerState // 折叠态 WeekPager 切月时,持续同步 CalendarPager 的 pagerState
LaunchedEffect(selectedDate) { LaunchedEffect(selectedDate) {
@Suppress("DEPRECATION") // monthNumber 无替代 API
val targetPage = yearMonthToPage( val targetPage = yearMonthToPage(
selectedDate.year, selectedDate.month.number, selectedDate.year, selectedDate.month.number,
today.year, today.month.number today.year, today.month.number
@ -248,9 +247,8 @@ fun CalendarMonthView(
} }
val onMonthChanged = remember(viewModel, today) { val onMonthChanged = remember(viewModel, today) {
{ year: Int, month: Int -> { year: Int, month: Int ->
@Suppress("DEPRECATION")
val date = if (year == today.year && today.month.number == month) today val date = if (year == today.year && today.month.number == month) today
else LocalDate(year, month, 1) else LocalDate(year, Month(month), 1)
viewModel.selectDate(date) viewModel.selectDate(date)
} }
} }
@ -342,7 +340,6 @@ fun CalendarMonthView(
val clickT = System.nanoTime() val clickT = System.nanoTime()
logd("AnimLog") { "[YearGridView] MonthClick month=$month year=$pageYear t=$clickT" } logd("AnimLog") { "[YearGridView] MonthClick month=$month year=$pageYear t=$clickT" }
viewModel.selectMonthFromYearView(month) viewModel.selectMonthFromYearView(month)
@Suppress("DEPRECATION") // monthNumber 无替代 API
val targetPage = yearMonthToPage( val targetPage = yearMonthToPage(
yearViewYear, month, yearViewYear, month,
today.year, today.month.number today.year, today.month.number

View File

@ -62,8 +62,6 @@ fun CalendarPager(
modifier: Modifier = Modifier modifier: Modifier = Modifier
) { ) {
val initialYear = remember { today.year } val initialYear = remember { today.year }
@Suppress("DEPRECATION") // monthNumber 无替代 APIkotlinx-datetime 尚未提供新接口
val initialMonth = remember { today.month.number } val initialMonth = remember { today.month.number }
val coroutineScope = rememberCoroutineScope() val coroutineScope = rememberCoroutineScope()
@ -118,8 +116,6 @@ fun CalendarPager(
onDateClick(date) onDateClick(date)
// 点击跨月日期时,滚动到该月对应的页 // 点击跨月日期时,滚动到该月对应的页
val clickedYear = date.year val clickedYear = date.year
@Suppress("DEPRECATION") // monthNumber 无替代 APIkotlinx-datetime 尚未提供新接口
val clickedMonth = date.month.number val clickedMonth = date.month.number
if (clickedYear != year || clickedMonth != month) { if (clickedYear != year || clickedMonth != month) {
val targetPage = val targetPage =

View File

@ -3,6 +3,7 @@ package plus.rua.project.ui
import com.tyme.solar.SolarDay import com.tyme.solar.SolarDay
import kotlinx.datetime.DatePeriod import kotlinx.datetime.DatePeriod
import kotlinx.datetime.LocalDate import kotlinx.datetime.LocalDate
import kotlinx.datetime.Month
import kotlinx.datetime.daysUntil import kotlinx.datetime.daysUntil
import kotlinx.datetime.minus import kotlinx.datetime.minus
import kotlinx.datetime.number import kotlinx.datetime.number
@ -60,10 +61,10 @@ data class MonthGridInfo(
* @return 月份网格信息 * @return 月份网格信息
*/ */
fun getMonthGridInfo(year: Int, month: Int): MonthGridInfo { fun getMonthGridInfo(year: Int, month: Int): MonthGridInfo {
val firstOfMonth = LocalDate(year, month, 1) val firstOfMonth = LocalDate(year, Month(month), 1)
val offset = firstOfMonth.dayOfWeek.ordinal val offset = firstOfMonth.dayOfWeek.ordinal
val startDate = firstOfMonth.minus(DatePeriod(days = offset)) val startDate = firstOfMonth.minus(DatePeriod(days = offset))
val nextMonth = if (month == 12) LocalDate(year + 1, 1, 1) else LocalDate(year, month + 1, 1) val nextMonth = if (month == 12) LocalDate(year + 1, 1, 1) else LocalDate(year, Month(month + 1), 1)
val daysInMonth = nextMonth.minus(DatePeriod(days = 1)).day val daysInMonth = nextMonth.minus(DatePeriod(days = 1)).day
val rows = ((offset + daysInMonth - 1) / 7) + 1 val rows = ((offset + daysInMonth - 1) / 7) + 1
val totalDays = rows * 7 val totalDays = rows * 7
@ -90,8 +91,6 @@ fun calculateWeeksCount(year: Int, month: Int): Int {
*/ */
fun calculateWeeksCountForPage(page: Int, today: LocalDate): Int { fun calculateWeeksCountForPage(page: Int, today: LocalDate): Int {
val initialYear = today.year val initialYear = today.year
@Suppress("DEPRECATION") // monthNumber 无替代 APIkotlinx-datetime 尚未提供新接口
val initialMonth = today.month.number 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
@ -190,9 +189,8 @@ fun relativeDayDescription(selectedDate: LocalDate, today: LocalDate): String {
* @param date 公历日期 * @param date 公历日期
* @return 农历日期描述 * @return 农历日期描述
*/ */
@Suppress("DEPRECATION") // monthNumber 无替代 APIkotlinx-datetime 尚未提供新接口
fun formatLunarDate(date: LocalDate): String { fun formatLunarDate(date: LocalDate): String {
val solarDay = SolarDay.fromYmd(date.year, date.monthNumber, date.day) val solarDay = SolarDay.fromYmd(date.year, date.month.number, date.day)
val lunarDay = solarDay.getLunarDay() val lunarDay = solarDay.getLunarDay()
val lunarMonth = lunarDay.getLunarMonth() val lunarMonth = lunarDay.getLunarMonth()
return "农历${lunarMonth.getName()}${lunarDay.getName()}" return "农历${lunarMonth.getName()}${lunarDay.getName()}"

View File

@ -74,6 +74,7 @@ import kotlinx.datetime.LocalDate
import kotlinx.datetime.TimeZone import kotlinx.datetime.TimeZone
import kotlinx.datetime.atStartOfDayIn import kotlinx.datetime.atStartOfDayIn
import kotlinx.datetime.daysUntil import kotlinx.datetime.daysUntil
import kotlinx.datetime.number
import kotlinx.datetime.plus import kotlinx.datetime.plus
import kotlinx.datetime.toLocalDateTime import kotlinx.datetime.toLocalDateTime
import kotlinx.datetime.todayIn import kotlinx.datetime.todayIn
@ -689,13 +690,12 @@ private fun LocalDate.toEpochMillis(): Long =
private fun Long.toLocalDate(): LocalDate = private fun Long.toLocalDate(): LocalDate =
Instant.fromEpochMilliseconds(this).toLocalDateTime(TimeZone.UTC).date Instant.fromEpochMilliseconds(this).toLocalDateTime(TimeZone.UTC).date
@Suppress("DEPRECATION") // monthNumber/dayOfMonth 无替代 APIkotlinx-datetime 尚未提供新接口
private fun LocalDate.formatChinese(): String = private fun LocalDate.formatChinese(): String =
"${year}${monthNumber}${dayOfMonth}" "${year}${month.number}${day}"
@Suppress("DEPRECATION", "Unused") // monthNumber/dayOfMonth 无替代 API @Suppress("Unused")
private fun LocalDate.formatShortChinese(): String = private fun LocalDate.formatShortChinese(): String =
"${monthNumber}${dayOfMonth}" "${month.number}${day}"
private fun LocalDate.dayOfWeekChinese(): String = when (dayOfWeek.ordinal) { private fun LocalDate.dayOfWeekChinese(): String = when (dayOfWeek.ordinal) {
0 -> "周一" 0 -> "周一"

View File

@ -40,6 +40,7 @@ import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp import androidx.compose.ui.unit.sp
import androidx.compose.ui.zIndex import androidx.compose.ui.zIndex
import kotlinx.datetime.LocalDate import kotlinx.datetime.LocalDate
import kotlinx.datetime.number
import plus.rua.project.DayCellInfo import plus.rua.project.DayCellInfo
import plus.rua.project.LunarCache import plus.rua.project.LunarCache
import plus.rua.project.ShiftKind import plus.rua.project.ShiftKind
@ -265,11 +266,10 @@ private fun DayCellImpl(
modifier = Modifier modifier = Modifier
.fillMaxSize() .fillMaxSize()
.semantics { .semantics {
@Suppress("DEPRECATION")
contentDescription = if (isToday) { contentDescription = if (isToday) {
"今天 ${date.year}${date.monthNumber}${date.day}" "今天 ${date.year}${date.month.number}${date.day}"
} else { } else {
"${date.year}${date.monthNumber}${date.day}" "${date.year}${date.month.number}${date.day}"
} }
} }
.clip(CircleShape) .clip(CircleShape)

View File

@ -46,6 +46,7 @@ import androidx.compose.ui.semantics.semantics
import com.tyme.lunar.LunarYear import com.tyme.lunar.LunarYear
import kotlinx.datetime.DatePeriod import kotlinx.datetime.DatePeriod
import kotlinx.datetime.LocalDate import kotlinx.datetime.LocalDate
import kotlinx.datetime.Month
import kotlinx.datetime.minus import kotlinx.datetime.minus
import kotlinx.datetime.number import kotlinx.datetime.number
import kotlinx.datetime.plus import kotlinx.datetime.plus
@ -304,13 +305,12 @@ private data class MiniDayData(
val isCurrentMonth: Boolean val isCurrentMonth: Boolean
) )
@Suppress("DEPRECATION") // monthNumber 无替代 API
private fun generateMiniMonthDays(year: Int, month: Int): List<MiniDayData> { private fun generateMiniMonthDays(year: Int, month: Int): List<MiniDayData> {
composeTraceBeginSection("generateMiniMonthDays:$year-$month") composeTraceBeginSection("generateMiniMonthDays:$year-$month")
val firstOfMonth = LocalDate(year, month, 1) val firstOfMonth = LocalDate(year, Month(month), 1)
val offset = firstOfMonth.dayOfWeek.ordinal val offset = firstOfMonth.dayOfWeek.ordinal
val startDate = firstOfMonth.minus(DatePeriod(days = offset)) val startDate = firstOfMonth.minus(DatePeriod(days = offset))
val nextMonth = if (month == 12) LocalDate(year + 1, 1, 1) else LocalDate(year, month + 1, 1) val nextMonth = if (month == 12) LocalDate(year + 1, 1, 1) else LocalDate(year, Month(month + 1), 1)
val daysInMonth = nextMonth.minus(DatePeriod(days = 1)).day val daysInMonth = nextMonth.minus(DatePeriod(days = 1)).day
val rows = ((offset + daysInMonth - 1) / 7) + 1 val rows = ((offset + daysInMonth - 1) / 7) + 1
val totalDays = rows * 7 val totalDays = rows * 7

View File

@ -3,6 +3,7 @@ package plus.rua.project
import kotlinx.datetime.Clock import kotlinx.datetime.Clock
import kotlinx.datetime.Instant import kotlinx.datetime.Instant
import kotlinx.datetime.LocalDate import kotlinx.datetime.LocalDate
import kotlinx.datetime.number
import kotlin.test.Test import kotlin.test.Test
import kotlin.test.assertEquals import kotlin.test.assertEquals
import kotlin.test.assertFalse import kotlin.test.assertFalse
@ -73,8 +74,7 @@ class CalendarViewModelTest {
val vm = createViewModel() val vm = createViewModel()
val days = vm.getMonthDays(2026, 5) val days = vm.getMonthDays(2026, 5)
assertFalse(days[0].isCurrentMonth) assertFalse(days[0].isCurrentMonth)
@Suppress("DEPRECATION") // monthNumber — needed for Int comparison assertEquals(4, days[0].date.month.number)
assertEquals(4, days[0].date.monthNumber)
assertEquals(27, days[0].date.day) assertEquals(27, days[0].date.day)
} }
@ -84,8 +84,7 @@ class CalendarViewModelTest {
val days = vm.getMonthDays(2026, 5) val days = vm.getMonthDays(2026, 5)
assertTrue(days[4].isCurrentMonth) assertTrue(days[4].isCurrentMonth)
assertEquals(1, days[4].date.day) assertEquals(1, days[4].date.day)
@Suppress("DEPRECATION") // monthNumber — needed for Int comparison assertEquals(5, days[4].date.month.number)
assertEquals(5, days[4].date.monthNumber)
} }
@Test @Test