DayCell 增加农历日期显示

使用 tyme4kt 库在日期数字下方显示农历日名,初一显示月名。
This commit is contained in:
meyou 2026-05-16 15:15:27 +08:00
parent c86cdd61b8
commit dfda6fa5a9
3 changed files with 52 additions and 6 deletions

View File

@ -14,6 +14,7 @@ junit = "4.13.2"
kotlin = "2.3.21" kotlin = "2.3.21"
material3 = "1.10.0-alpha05" material3 = "1.10.0-alpha05"
kotlinx-datetime = "0.8.0" kotlinx-datetime = "0.8.0"
tyme4kt = "1.4.4"
[libraries] [libraries]
kotlin-test = { module = "org.jetbrains.kotlin:kotlin-test", version.ref = "kotlin" } kotlin-test = { module = "org.jetbrains.kotlin:kotlin-test", version.ref = "kotlin" }
@ -28,6 +29,7 @@ compose-components-resources = { module = "org.jetbrains.compose.components:comp
compose-uiToolingPreview = { module = "org.jetbrains.compose.ui:ui-tooling-preview", version.ref = "composeMultiplatform" } compose-uiToolingPreview = { module = "org.jetbrains.compose.ui:ui-tooling-preview", version.ref = "composeMultiplatform" }
kotlinx-datetime = { module = "org.jetbrains.kotlinx:kotlinx-datetime", version.ref = "kotlinx-datetime" } kotlinx-datetime = { module = "org.jetbrains.kotlinx:kotlinx-datetime", version.ref = "kotlinx-datetime" }
kotlinx-coroutines-test = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-test", version = "1.11.0" } kotlinx-coroutines-test = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-test", version = "1.11.0" }
tyme4kt = { module = "cn.6tail:tyme4kt", version.ref = "tyme4kt" }
[plugins] [plugins]
androidApplication = { id = "com.android.application", version.ref = "agp" } androidApplication = { id = "com.android.application", version.ref = "agp" }

View File

@ -45,6 +45,7 @@ kotlin {
implementation(libs.androidx.lifecycle.viewmodelCompose) implementation(libs.androidx.lifecycle.viewmodelCompose)
implementation(libs.androidx.lifecycle.runtimeCompose) implementation(libs.androidx.lifecycle.runtimeCompose)
implementation(libs.kotlinx.datetime) implementation(libs.kotlinx.datetime)
implementation(libs.tyme4kt)
} }
commonTest.dependencies { commonTest.dependencies {
implementation(libs.kotlin.test) implementation(libs.kotlin.test)

View File

@ -7,12 +7,15 @@ import androidx.compose.animation.core.tween
import androidx.compose.animation.core.updateTransition import androidx.compose.animation.core.updateTransition
import androidx.compose.foundation.clickable import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.aspectRatio import androidx.compose.foundation.layout.aspectRatio
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.shape.CircleShape import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material3.MaterialTheme import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text import androidx.compose.material3.Text
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip import androidx.compose.ui.draw.clip
@ -23,7 +26,10 @@ import androidx.compose.ui.semantics.semantics
import androidx.compose.ui.graphics.Color import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.drawscope.Stroke import androidx.compose.ui.graphics.drawscope.Stroke
import androidx.compose.ui.text.style.TextAlign import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.tyme.solar.SolarDay
import kotlinx.datetime.LocalDate import kotlinx.datetime.LocalDate
enum class DayCellState { enum class DayCellState {
@ -105,6 +111,30 @@ fun DayCell(
val todayBorderColor = MaterialTheme.colorScheme.primary val todayBorderColor = MaterialTheme.colorScheme.primary
val lunarText = remember(date) {
val lunarDay = SolarDay.fromYmd(date.year, date.monthNumber, date.day).getLunarDay()
val name = lunarDay.getName()
if (name == "初一") {
val lunarMonth = lunarDay.getLunarMonth()
"${lunarMonth.getName()}"
} else {
name
}
}
val lunarColor by transition.animateColor(
transitionSpec = { tween(250, easing = FastOutSlowInEasing) },
label = "lunarColor"
) { state ->
when (state) {
DayCellState.SELECTED_TODAY -> MaterialTheme.colorScheme.onPrimaryContainer.copy(alpha = 0.7f)
DayCellState.SELECTED -> MaterialTheme.colorScheme.onPrimary.copy(alpha = 0.7f)
DayCellState.TODAY -> MaterialTheme.colorScheme.primary.copy(alpha = 0.6f)
DayCellState.OTHER_MONTH -> MaterialTheme.colorScheme.onSurface.copy(alpha = 0.26f)
DayCellState.NORMAL -> MaterialTheme.colorScheme.onSurface.copy(alpha = 0.5f)
}
}
Box( Box(
modifier = modifier modifier = modifier
.aspectRatio(1f) .aspectRatio(1f)
@ -134,11 +164,24 @@ fun DayCell(
.clickable(onClick = onClick), .clickable(onClick = onClick),
contentAlignment = Alignment.Center contentAlignment = Alignment.Center
) { ) {
Text( Column(
text = date.day.toString(), horizontalAlignment = Alignment.CenterHorizontally
textAlign = TextAlign.Center, ) {
color = contentColor, Text(
style = MaterialTheme.typography.bodyMedium text = date.day.toString(),
) textAlign = TextAlign.Center,
color = contentColor,
style = MaterialTheme.typography.bodyMedium
)
Text(
text = lunarText,
textAlign = TextAlign.Center,
color = lunarColor,
fontSize = 7.sp,
maxLines = 1,
overflow = TextOverflow.Clip,
lineHeight = 9.sp
)
}
} }
} }