Skip to content

Commit

Permalink
💄 Add time stamp to pasteboard preview ui
Browse files Browse the repository at this point in the history
  • Loading branch information
guiyanakuang committed Feb 19, 2024
1 parent 081fd0f commit 4cb50af
Show file tree
Hide file tree
Showing 8 changed files with 118 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
package com.clipevery.i18n

import java.time.LocalDateTime

interface Copywriter {

fun getText(id: String): String

fun getDate(date: LocalDateTime): String

fun getAbridge(): String
}

Expand Down Expand Up @@ -31,6 +36,10 @@ class PreviewGlobalCopywriter: GlobalCopywriter {
return id
}

override fun getDate(date: LocalDateTime): String {
return date.toString()
}

override fun getAbridge(): String {
return "en"
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,42 @@
package com.clipevery.ui.clip

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.wrapContentSize
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.ExperimentalComposeUiApi
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.input.pointer.PointerEventType
import androidx.compose.ui.input.pointer.onPointerEvent
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontFamily
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.clipevery.LocalKoinApplication
import com.clipevery.dao.clip.ClipAppearItem
import com.clipevery.dao.clip.ClipContent
import com.clipevery.dao.clip.ClipData
import com.clipevery.dao.clip.ClipType
import com.clipevery.i18n.Copywriter
import com.clipevery.i18n.GlobalCopywriter
import com.clipevery.utils.DateUtils
import io.realm.kotlin.types.RealmInstant
import kotlin.reflect.KClass
import kotlin.reflect.cast

Expand All @@ -43,6 +59,8 @@ fun ClipData.getClipItem(): ClipAppearItem? {
fun ClipPreviewItemView(clipData: ClipData, clipContent: @Composable ClipAppearItem.() -> Unit) {

clipData.getClipItem()?.let {
val current = LocalKoinApplication.current
val copywriter = current.koin.get<GlobalCopywriter>()

if (it.getClipType() == ClipType.TEXT) {

Expand Down Expand Up @@ -85,15 +103,42 @@ fun ClipPreviewItemView(clipData: ClipData, clipContent: @Composable ClipAppearI
modifier = Modifier.fillMaxWidth()
.background(color = MaterialTheme.colors.surface)
.height(30.dp)
.padding(end = 16.dp)
,
horizontalArrangement = Arrangement.End,
verticalAlignment = Alignment.CenterVertically
) {

Row(modifier = Modifier.wrapContentSize()
.clip(RoundedCornerShape(3.dp))
.background(color = MaterialTheme.colors.background)
.padding(horizontal = 8.dp, vertical = 4.dp)
) {
Text(
text = getDateText(clipData.createTime, copywriter),
fontFamily = FontFamily.SansSerif,
style = TextStyle(
fontWeight = FontWeight.Light,
color = MaterialTheme.colors.onBackground,
fontSize = 10.sp
)
)
}
}
}
}
}
}
}

fun getDateText(createTime: RealmInstant, copywriter: Copywriter): String {
val date = DateUtils.convertRealmInstantToLocalDateTime(createTime)
DateUtils.getDateText(date)?.let {
return copywriter.getText(it)
} ?: run {
return copywriter.getDate(date)
}
}

@Composable
fun ClipSpecificPreviewItemView(clipData: ClipData) {
if (clipData.preCreate) {
Expand Down
38 changes: 38 additions & 0 deletions composeApp/src/commonMain/kotlin/com/clipevery/utils/DateUtils.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package com.clipevery.utils

import io.realm.kotlin.types.RealmInstant
import java.time.Instant
import java.time.LocalDateTime
import java.time.ZoneId
import java.time.temporal.ChronoUnit
import java.util.Calendar

object DateUtils {
Expand All @@ -13,4 +17,38 @@ object DateUtils {
// The nanosecondAdjustment parameter is 0 as we're not adjusting nanoseconds here
return RealmInstant.from(epochSeconds, 0)
}

fun getDateText(date: LocalDateTime): String? {
val now = LocalDateTime.now()

if (date.toLocalDate().isEqual(now.toLocalDate())) {
val hour = ChronoUnit.HOURS.between(date, now)
val minutes = ChronoUnit.MINUTES.between(date, now)
val seconds = ChronoUnit.SECONDS.between(date, now)

if (hour < 1 && minutes < 1 && seconds < 60) {
return "Just now"
}
return "Today"
}

val yesterday = now.minusDays(1)
if (date.toLocalDate().isEqual(yesterday.toLocalDate())) {
return "Yesterday"
}

return null
}

fun convertRealmInstantToLocalDateTime(realmInstant: RealmInstant): LocalDateTime {
// 1. 从 RealmInstant 获取秒和纳秒
val epochSeconds = realmInstant.epochSeconds
val nanosecondsOfSecond = realmInstant.nanosecondsOfSecond

// 2. 使用 Instant.ofEpochSecond 创建 Instant
val instant = Instant.ofEpochSecond(epochSeconds, nanosecondsOfSecond.toLong())

// 3. 使用系统默认的时区将 Instant 转换为 LocalDateTime
return LocalDateTime.ofInstant(instant, ZoneId.systemDefault())
}
}
9 changes: 9 additions & 0 deletions composeApp/src/desktopMain/kotlin/com/clipevery/i18n/I18n.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import io.github.oshai.kotlinlogging.KotlinLogging
import java.io.FileNotFoundException
import java.io.InputStreamReader
import java.nio.charset.StandardCharsets
import java.time.LocalDateTime
import java.util.Properties
import java.util.concurrent.ConcurrentHashMap

Expand Down Expand Up @@ -49,6 +50,10 @@ open class GlobalCopywriterImpl(private val configManager: ConfigManager): Globa
return copywriter.getText(id)
}

override fun getDate(date: LocalDateTime): String {
return copywriter.getDate(date)
}

override fun getAbridge(): String {
return copywriter.getAbridge()
}
Expand Down Expand Up @@ -95,6 +100,10 @@ class CopywriterImpl(private val language: String) : Copywriter {
}
}

override fun getDate(date: LocalDateTime): String {
return date.toString()
}

override fun getAbridge(): String {
return language
}
Expand Down
5 changes: 4 additions & 1 deletion composeApp/src/desktopMain/resources/i18n/en.properties
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,7 @@ Base_Info=Base Info
Allow_Send_to_this_device=Allow Send to this device
Allow_Receive_from_this_device=Allow Receive from this device
Remove_Device=Remove Device
Text=Text
Text=Text
Just_now=Just now
Today=Today
Yesterday=Yesterday
5 changes: 4 additions & 1 deletion composeApp/src/desktopMain/resources/i18n/es.properties
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,7 @@ Base_Info=Información básica
Allow_Send_to_this_device=Permitir enviar a este dispositivo
Allow_Receive_from_this_device=Permitir recibir de este dispositivo
Remove_Device=Eliminar dispositivo
Text=Texto
Text=Texto
Just_now=Justo ahora
Today=Hoy
Yesterday=Ayer
5 changes: 4 additions & 1 deletion composeApp/src/desktopMain/resources/i18n/jp.properties
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,7 @@ Base_Info=基本情報
Allow_Send_to_this_device=このデバイスに送信を許可する
Allow_Receive_from_this_device=このデバイスからの受信を許可する
Remove_Device=デバイスを削除
Text=テキスト
Text=テキスト
Just_now=たった今
Today=今日
Yesterday=昨日
5 changes: 4 additions & 1 deletion composeApp/src/desktopMain/resources/i18n/zh.properties
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,7 @@ Base_Info=基础信息
Allow_Send_to_this_device=允许发送到此设备
Allow_Receive_from_this_device=允许从此设备接收
Remove_Device=移除设备
Text=文本
Text=文本
Just_now=刚刚
Today=今天
Yesterday=昨天

0 comments on commit 4cb50af

Please sign in to comment.