diff --git a/examples/chat/shared/build.gradle.kts b/examples/chat/shared/build.gradle.kts index 0c8bd1dfc1..4ca6a52b6a 100644 --- a/examples/chat/shared/build.gradle.kts +++ b/examples/chat/shared/build.gradle.kts @@ -53,6 +53,7 @@ kotlin { implementation(compose.material) @OptIn(org.jetbrains.compose.ExperimentalComposeLibrary::class) implementation(compose.components.resources) + implementation("org.jetbrains.kotlinx:kotlinx-datetime:0.5.0") } } val androidMain by getting { diff --git a/examples/chat/shared/src/androidMain/kotlin/currentTime.android.kt b/examples/chat/shared/src/androidMain/kotlin/currentTime.android.kt deleted file mode 100644 index 12e4e07f7a..0000000000 --- a/examples/chat/shared/src/androidMain/kotlin/currentTime.android.kt +++ /dev/null @@ -1,3 +0,0 @@ -actual fun timestampMs(): Long { - return System.currentTimeMillis() -} diff --git a/examples/chat/shared/src/commonMain/kotlin/ChatApp.kt b/examples/chat/shared/src/commonMain/kotlin/ChatApp.kt index d5e4ba3b3b..3cfc77889b 100644 --- a/examples/chat/shared/src/commonMain/kotlin/ChatApp.kt +++ b/examples/chat/shared/src/commonMain/kotlin/ChatApp.kt @@ -77,7 +77,7 @@ fun ChatApp(displayTextField: Boolean = true) { SendMessage { text -> store.send( Action.SendMessage( - Message(myUser, timeMs = timestampMs(), text) + Message(myUser, text) ) ) } @@ -100,7 +100,6 @@ fun ChatApp(displayTextField: Boolean = true) { Action.SendMessage( message = Message( user = thisFriend, - timeMs = timestampMs(), text = thisMessage ) ) diff --git a/examples/chat/shared/src/commonMain/kotlin/ChatMessage.kt b/examples/chat/shared/src/commonMain/kotlin/ChatMessage.kt index 0ab64b36a7..9fd5f3969f 100644 --- a/examples/chat/shared/src/commonMain/kotlin/ChatMessage.kt +++ b/examples/chat/shared/src/commonMain/kotlin/ChatMessage.kt @@ -96,7 +96,7 @@ inline fun ChatMessage(isMyMessage: Boolean, message: Message) { modifier = Modifier.align(Alignment.End) ) { Text( - text = timeToString(message.timeMs), + text = timeToString(message.seconds), textAlign = TextAlign.End, style = MaterialTheme.typography.subtitle1.copy(fontSize = 10.sp), color = ChatColors.TIME_TEXT diff --git a/examples/chat/shared/src/commonMain/kotlin/Data.kt b/examples/chat/shared/src/commonMain/kotlin/Data.kt index 78ea0f469b..821a70cd80 100644 --- a/examples/chat/shared/src/commonMain/kotlin/Data.kt +++ b/examples/chat/shared/src/commonMain/kotlin/Data.kt @@ -1,21 +1,21 @@ import androidx.compose.ui.graphics.Color +import kotlinx.datetime.Clock import kotlin.random.Random import kotlin.random.nextInt -data class Message private constructor( +data class Message( val user: User, - val timeMs: Long, val text: String, + val seconds: Long, val id: Long ) { constructor( user: User, - timeMs: Long, text: String ) : this( user = user, - timeMs = timeMs, text = text, + seconds = Clock.System.now().epochSeconds, id = Random.nextLong() ) } diff --git a/examples/chat/shared/src/commonMain/kotlin/currentTime.common.kt b/examples/chat/shared/src/commonMain/kotlin/currentTime.common.kt index 63f6928951..8a2c400970 100644 --- a/examples/chat/shared/src/commonMain/kotlin/currentTime.common.kt +++ b/examples/chat/shared/src/commonMain/kotlin/currentTime.common.kt @@ -1,10 +1,15 @@ -fun timeToString(timestampMs: Long): String { - val seconds = timestampMs - val minutes = seconds / 1000 / 60 - val hours = minutes / 24 +import kotlinx.datetime.Clock +import kotlinx.datetime.Instant +import kotlinx.datetime.LocalDate +import kotlinx.datetime.TimeZone +import kotlinx.datetime.toLocalDateTime - val m = minutes % 60 - val h = hours % 24 + +fun timeToString(seconds: Long): String { + val instant: Instant = Instant.fromEpochSeconds(seconds) + val localTime = instant.toLocalDateTime(TimeZone.currentSystemDefault()) + val m = localTime.minute + val h = localTime.hour val mm = if (m < 10) { "0$m" @@ -19,4 +24,3 @@ fun timeToString(timestampMs: Long): String { return "$hh:$mm" } -expect fun timestampMs(): Long diff --git a/examples/chat/shared/src/desktopMain/kotlin/currentTime.desktop.kt b/examples/chat/shared/src/desktopMain/kotlin/currentTime.desktop.kt deleted file mode 100644 index 12e4e07f7a..0000000000 --- a/examples/chat/shared/src/desktopMain/kotlin/currentTime.desktop.kt +++ /dev/null @@ -1,3 +0,0 @@ -actual fun timestampMs(): Long { - return System.currentTimeMillis() -} diff --git a/examples/chat/shared/src/iosMain/kotlin/currentTime.ios.kt b/examples/chat/shared/src/iosMain/kotlin/currentTime.ios.kt deleted file mode 100644 index defd831606..0000000000 --- a/examples/chat/shared/src/iosMain/kotlin/currentTime.ios.kt +++ /dev/null @@ -1,6 +0,0 @@ -import platform.Foundation.NSDate -import platform.Foundation.timeIntervalSince1970 - -actual fun timestampMs(): Long { - return (NSDate().timeIntervalSince1970() * 1000).toLong() -} diff --git a/examples/chat/shared/src/iosMain/kotlin/main.ios.kt b/examples/chat/shared/src/iosMain/kotlin/main.ios.kt index 2a97fca260..d634c846c4 100644 --- a/examples/chat/shared/src/iosMain/kotlin/main.ios.kt +++ b/examples/chat/shared/src/iosMain/kotlin/main.ios.kt @@ -6,7 +6,7 @@ fun ChatViewController(): UIViewController = ComposeUIViewController { } fun sendMessage(text: String) { - store.send(Action.SendMessage(Message(myUser, timestampMs(), text))) + store.send(Action.SendMessage(Message(myUser, text))) } fun gradient3Colors() = ChatColors.GRADIENT_3 diff --git a/examples/chat/shared/src/jsMain/kotlin/currentTime.js.kt b/examples/chat/shared/src/jsMain/kotlin/currentTime.js.kt deleted file mode 100644 index c242ad0bd6..0000000000 --- a/examples/chat/shared/src/jsMain/kotlin/currentTime.js.kt +++ /dev/null @@ -1,5 +0,0 @@ -import kotlin.js.Date - -actual fun timestampMs(): Long { - return Date.now().toLong() -} diff --git a/examples/chat/shared/src/macosMain/kotlin/currentTime.macos.kt b/examples/chat/shared/src/macosMain/kotlin/currentTime.macos.kt deleted file mode 100644 index 0493656647..0000000000 --- a/examples/chat/shared/src/macosMain/kotlin/currentTime.macos.kt +++ /dev/null @@ -1,6 +0,0 @@ -import platform.Foundation.NSDate -import platform.Foundation.timeIntervalSince1970 - -actual fun timestampMs(): Long { - return NSDate().timeIntervalSince1970().toLong() -}