86 lines
No EOL
2.9 KiB
Kotlin
86 lines
No EOL
2.9 KiB
Kotlin
package cn.super12138.todo.utils
|
||
|
||
import androidx.compose.foundation.shape.CornerBasedShape
|
||
import androidx.compose.material3.MaterialTheme
|
||
import androidx.compose.runtime.Composable
|
||
import androidx.compose.runtime.Stable
|
||
import androidx.compose.ui.geometry.Offset
|
||
import androidx.compose.ui.geometry.Size
|
||
import androidx.compose.ui.graphics.BlendMode
|
||
import androidx.compose.ui.graphics.Brush
|
||
import androidx.compose.ui.graphics.Color
|
||
import androidx.compose.ui.graphics.drawscope.ContentDrawScope
|
||
import androidx.compose.ui.unit.Dp
|
||
import cn.super12138.todo.logic.model.Priority
|
||
import java.text.SimpleDateFormat
|
||
import java.util.Date
|
||
|
||
@Composable
|
||
@Stable
|
||
fun Priority.containerColor(): Color =
|
||
when (this) {
|
||
Priority.NotUrgent -> MaterialTheme.colorScheme.surfaceContainerHighest
|
||
Priority.NotImportant -> MaterialTheme.colorScheme.surfaceContainerHighest
|
||
Priority.Default -> MaterialTheme.colorScheme.secondary
|
||
Priority.Important -> MaterialTheme.colorScheme.tertiary
|
||
Priority.Urgent -> MaterialTheme.colorScheme.error
|
||
}
|
||
|
||
/**
|
||
* 获取部分圆角的形状
|
||
*
|
||
* @param topRounded 顶部是否圆角
|
||
* @param bottomRounded 底部是否圆角
|
||
* @param roundedShape 所需圆角形状
|
||
*/
|
||
@Composable
|
||
fun CornerBasedShape.getPartialRoundedShape(
|
||
topRounded: Boolean,
|
||
bottomRounded: Boolean,
|
||
roundedShape: CornerBasedShape
|
||
): CornerBasedShape =
|
||
this.copy(
|
||
topStart = if (topRounded) roundedShape.topStart else this.topStart,
|
||
topEnd = if (topRounded) roundedShape.topEnd else this.topEnd,
|
||
bottomEnd = if (bottomRounded) roundedShape.bottomEnd else this.bottomEnd,
|
||
bottomStart = if (bottomRounded) roundedShape.bottomStart else this.bottomStart,
|
||
)
|
||
|
||
/**
|
||
* 绘制渐变边缘遮罩
|
||
*
|
||
* @param edgeWidth 渐变边缘宽度
|
||
* @param maskColor 遮罩颜色
|
||
* @param leftEdge 是否在左侧边缘添加遮罩(否即在右侧边缘添加)
|
||
*/
|
||
fun ContentDrawScope.drawFadedEdge(
|
||
edgeWidth: Dp,
|
||
maskColor: Color,
|
||
leftEdge: Boolean
|
||
) {
|
||
val edgeWidthPx = edgeWidth.toPx()
|
||
drawRect(
|
||
topLeft = Offset(if (leftEdge) 0f else size.width - edgeWidthPx, 0f),
|
||
size = Size(edgeWidthPx, size.height),
|
||
brush =
|
||
Brush.horizontalGradient(
|
||
colors = listOf(Color.Transparent, maskColor),
|
||
startX = if (leftEdge) 0f else size.width,
|
||
endX = if (leftEdge) edgeWidthPx else size.width - edgeWidthPx
|
||
),
|
||
blendMode = BlendMode.DstIn
|
||
)
|
||
}
|
||
|
||
/**
|
||
* 将时间戳转换为本地日期字符串
|
||
*
|
||
* @receiver Long? 时间戳(单位为毫秒)或 null
|
||
* @return String 格式化后的日期字符串。如果为传入参数为null则返回空字符串,反之格式为 “yyyy-MM-dd”
|
||
*/
|
||
fun Long?.toLocalDateString(): String {
|
||
if (this == null) return ""
|
||
val date = Date(this)
|
||
val format = SimpleDateFormat("yyyy-MM-dd", java.util.Locale.getDefault())
|
||
return format.format(date)
|
||
} |