Browse Source

Fix ChatApp time (#4381)

Fix time of message in ChatApp sample.
Added library kotlinx-datetime.

It resolves issue:
 - https://github.com/JetBrains/compose-multiplatform/issues/4378
pull/4382/head^2
dima.avdeev 3 months ago committed by GitHub
parent
commit
86795afc6f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 1
      examples/chat/shared/build.gradle.kts
  2. 3
      examples/chat/shared/src/androidMain/kotlin/currentTime.android.kt
  3. 3
      examples/chat/shared/src/commonMain/kotlin/ChatApp.kt
  4. 2
      examples/chat/shared/src/commonMain/kotlin/ChatMessage.kt
  5. 8
      examples/chat/shared/src/commonMain/kotlin/Data.kt
  6. 18
      examples/chat/shared/src/commonMain/kotlin/currentTime.common.kt
  7. 3
      examples/chat/shared/src/desktopMain/kotlin/currentTime.desktop.kt
  8. 6
      examples/chat/shared/src/iosMain/kotlin/currentTime.ios.kt
  9. 2
      examples/chat/shared/src/iosMain/kotlin/main.ios.kt
  10. 5
      examples/chat/shared/src/jsMain/kotlin/currentTime.js.kt
  11. 6
      examples/chat/shared/src/macosMain/kotlin/currentTime.macos.kt

1
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 {

3
examples/chat/shared/src/androidMain/kotlin/currentTime.android.kt

@ -1,3 +0,0 @@
actual fun timestampMs(): Long {
return System.currentTimeMillis()
}

3
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
)
)

2
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

8
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()
)
}

18
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

3
examples/chat/shared/src/desktopMain/kotlin/currentTime.desktop.kt

@ -1,3 +0,0 @@
actual fun timestampMs(): Long {
return System.currentTimeMillis()
}

6
examples/chat/shared/src/iosMain/kotlin/currentTime.ios.kt

@ -1,6 +0,0 @@
import platform.Foundation.NSDate
import platform.Foundation.timeIntervalSince1970
actual fun timestampMs(): Long {
return (NSDate().timeIntervalSince1970() * 1000).toLong()
}

2
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

5
examples/chat/shared/src/jsMain/kotlin/currentTime.js.kt

@ -1,5 +0,0 @@
import kotlin.js.Date
actual fun timestampMs(): Long {
return Date.now().toLong()
}

6
examples/chat/shared/src/macosMain/kotlin/currentTime.macos.kt

@ -1,6 +0,0 @@
import platform.Foundation.NSDate
import platform.Foundation.timeIntervalSince1970
actual fun timestampMs(): Long {
return NSDate().timeIntervalSince1970().toLong()
}
Loading…
Cancel
Save