Skip to content

Commit

Permalink
Add cross-platform date and time handling
Browse files Browse the repository at this point in the history
  • Loading branch information
bubelov committed May 22, 2024
1 parent bd3fcd7 commit 1746d0f
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 6 deletions.
2 changes: 2 additions & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ kotlin {
}
commonMain.dependencies {
implementation(libs.kotlinx.serialization)
implementation(libs.kotlinx.datetime)

implementation(libs.androidx.sqlite)
}
}
Expand Down
4 changes: 2 additions & 2 deletions app/src/androidMain/kotlin/area/AreaJson.kt
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -18,7 +18,7 @@ fun AreaJson.toArea(): Area {
return Area(
id = id,
tags = tags!!,
updatedAt = ZonedDateTime.parse(updatedAt),
updatedAt = Instant.parse(updatedAt),
)
}

Expand Down
5 changes: 3 additions & 2 deletions app/src/androidMain/kotlin/area/AreaQueries.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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),
)
)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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,
)
5 changes: 5 additions & 0 deletions app/src/commonMain/kotlin/db/SQLiteStatementExtensions.kt
Original file line number Diff line number Diff line change
@@ -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))
}
4 changes: 4 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down

0 comments on commit 1746d0f

Please sign in to comment.