From 1746d0f7314d5f1c07f05a0410c8b007a46c3e35 Mon Sep 17 00:00:00 2001 From: Igor Bubelov Date: Wed, 22 May 2024 21:50:04 +0700 Subject: [PATCH] Add cross-platform date and time handling --- app/build.gradle.kts | 2 ++ app/src/androidMain/kotlin/area/AreaJson.kt | 4 ++-- app/src/androidMain/kotlin/area/AreaQueries.kt | 5 +++-- app/src/{androidMain => commonMain}/kotlin/area/Area.kt | 4 ++-- app/src/commonMain/kotlin/db/SQLiteStatementExtensions.kt | 5 +++++ gradle/libs.versions.toml | 4 ++++ 6 files changed, 18 insertions(+), 6 deletions(-) rename app/src/{androidMain => commonMain}/kotlin/area/Area.kt (53%) diff --git a/app/build.gradle.kts b/app/build.gradle.kts index c382f9b2..c76106dc 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -42,6 +42,8 @@ kotlin { } commonMain.dependencies { implementation(libs.kotlinx.serialization) + implementation(libs.kotlinx.datetime) + implementation(libs.androidx.sqlite) } } diff --git a/app/src/androidMain/kotlin/area/AreaJson.kt b/app/src/androidMain/kotlin/area/AreaJson.kt index 6864e9ff..fda5b3af 100644 --- a/app/src/androidMain/kotlin/area/AreaJson.kt +++ b/app/src/androidMain/kotlin/area/AreaJson.kt @@ -1,11 +1,11 @@ package area import json.toJsonArray +import kotlinx.datetime.Instant import kotlinx.serialization.json.Json import kotlinx.serialization.json.JsonObject import kotlinx.serialization.json.jsonObject import java.io.InputStream -import java.time.ZonedDateTime data class AreaJson( val id: Long, @@ -18,7 +18,7 @@ fun AreaJson.toArea(): Area { return Area( id = id, tags = tags!!, - updatedAt = ZonedDateTime.parse(updatedAt), + updatedAt = Instant.parse(updatedAt), ) } diff --git a/app/src/androidMain/kotlin/area/AreaQueries.kt b/app/src/androidMain/kotlin/area/AreaQueries.kt index 3da9f77b..b4b3b5be 100644 --- a/app/src/androidMain/kotlin/area/AreaQueries.kt +++ b/app/src/androidMain/kotlin/area/AreaQueries.kt @@ -2,6 +2,7 @@ package area import androidx.sqlite.use import db.Database +import db.getInstant import db.getJsonObject import db.getZonedDateTime import java.time.ZonedDateTime @@ -41,7 +42,7 @@ class AreaQueries(private val db: Database) { Area( id = it.getLong(0), tags = it.getJsonObject(1), - updatedAt = it.getZonedDateTime(2), + updatedAt = it.getInstant(2), ) } else { null @@ -67,7 +68,7 @@ class AreaQueries(private val db: Database) { Area( id = it.getLong(0), tags = it.getJsonObject(1), - updatedAt = it.getZonedDateTime(2), + updatedAt = it.getInstant(2), ) ) } diff --git a/app/src/androidMain/kotlin/area/Area.kt b/app/src/commonMain/kotlin/area/Area.kt similarity index 53% rename from app/src/androidMain/kotlin/area/Area.kt rename to app/src/commonMain/kotlin/area/Area.kt index 23e4ed86..feb1f27a 100644 --- a/app/src/androidMain/kotlin/area/Area.kt +++ b/app/src/commonMain/kotlin/area/Area.kt @@ -1,9 +1,9 @@ package area -import java.time.ZonedDateTime +import kotlinx.datetime.Instant data class Area( val id: Long, val tags: AreaTags, - val updatedAt: ZonedDateTime, + val updatedAt: Instant, ) \ No newline at end of file diff --git a/app/src/commonMain/kotlin/db/SQLiteStatementExtensions.kt b/app/src/commonMain/kotlin/db/SQLiteStatementExtensions.kt index 727baa56..738972db 100644 --- a/app/src/commonMain/kotlin/db/SQLiteStatementExtensions.kt +++ b/app/src/commonMain/kotlin/db/SQLiteStatementExtensions.kt @@ -1,10 +1,15 @@ package db import androidx.sqlite.SQLiteStatement +import kotlinx.datetime.Instant import kotlinx.serialization.json.Json import kotlinx.serialization.json.JsonObject import kotlinx.serialization.json.jsonObject fun SQLiteStatement.getJsonObject(columnIndex: Int): JsonObject { return Json.parseToJsonElement(getText(columnIndex)).jsonObject +} + +fun SQLiteStatement.getInstant(columnIndex: Int): Instant { + return Instant.parse(getText(columnIndex)) } \ No newline at end of file diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 0cb571ec..0cb4ffa2 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -48,10 +48,14 @@ room = "2.7.0-alpha02" # Platform-agnostic JSON handling # https://github.com/Kotlin/kotlinx.serialization/releases serialization = "1.6.3" +# Platform-agnostic date and time handling +# https://github.com/Kotlin/kotlinx-datetime/releases +datetime = "0.6.0" [libraries] kotlinx-coroutines = { group = "org.jetbrains.kotlinx", name = "kotlinx-coroutines-android", version.ref = "coroutines" } kotlinx-serialization = { module = "org.jetbrains.kotlinx:kotlinx-serialization-json", version.ref = "serialization" } +kotlinx-datetime = { module = "org.jetbrains.kotlinx:kotlinx-datetime", version.ref = "datetime" } androidx-work = { module = "androidx.work:work-runtime-ktx", version.ref = "work" } androidx-navigation-fragment = { group = "androidx.navigation", name = "navigation-fragment-ktx", version.ref = "navigation" }