chore: update and use multiplatform datetime

This commit is contained in:
aminecmi 2024-12-21 20:32:58 +01:00
parent 9e83af0302
commit aad93ef722
9 changed files with 114 additions and 44 deletions

View File

@ -7,10 +7,10 @@ buildscript {
plugins { plugins {
//trick: for the same plugin versions in all sub-modules //trick: for the same plugin versions in all sub-modules
id("com.android.application").version("8.7.2").apply(false) id("com.android.application").version("8.7.3").apply(false)
id("com.android.library").version("8.7.2").apply(false) id("com.android.library").version("8.7.3").apply(false)
id("org.jetbrains.kotlin.android").version("1.9.10").apply(false) id("org.jetbrains.kotlin.android").version("2.1.0").apply(false)
kotlin("multiplatform").version("1.9.10").apply(false) kotlin("multiplatform").version("2.1.0").apply(false)
id("com.mikepenz.aboutlibraries.plugin").version("10.5.1").apply(false) id("com.mikepenz.aboutlibraries.plugin").version("10.5.1").apply(false)
id("org.jetbrains.kotlinx.kover").version("0.6.1").apply(true) id("org.jetbrains.kotlinx.kover").version("0.6.1").apply(true)
} }
@ -25,7 +25,7 @@ allprojects {
tasks.register("clean", Delete::class) { tasks.register("clean", Delete::class) {
delete(rootProject.buildDir) delete(layout.buildDirectory)
} }
koverMerged { koverMerged {

View File

@ -18,7 +18,6 @@ kotlin.code.style=official
#Android #Android
android.useAndroidX=true android.useAndroidX=true
#android.nonTransitiveRClass=true #android.nonTransitiveRClass=true
android.enableJetifier=true
android.nonTransitiveRClass=false android.nonTransitiveRClass=false
#MPP #MPP
kotlin.mpp.enableCInteropCommonization=true kotlin.mpp.enableCInteropCommonization=true

View File

@ -173,7 +173,7 @@
); );
runOnlyForDeploymentPostprocessing = 0; runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/sh; shellPath = /bin/sh;
shellScript = "cd \"$SRCROOT/..\"\n./gradlew :shared:embedAndSignAppleFrameworkForXcode\n"; shellScript = "export JAVA_HOME=/Users/amine/.sdkman/candidates/java/17.0.8.1-jbr\ncd \"$SRCROOT/..\"\n./gradlew :shared:embedAndSignAppleFrameworkForXcode --stacktrace\n";
}; };
/* End PBXShellScriptBuildPhase section */ /* End PBXShellScriptBuildPhase section */

View File

@ -52,6 +52,9 @@ kotlin {
// Sql // Sql
implementation(SqlDelight.runtime) implementation(SqlDelight.runtime)
// Sql
implementation("org.jetbrains.kotlinx:kotlinx-datetime:0.6.1")
} }
} }
val commonTest by getting { val commonTest by getting {
@ -80,10 +83,6 @@ kotlin {
val iosArm64Main by getting val iosArm64Main by getting
// val iosSimulatorArm64Main by getting // val iosSimulatorArm64Main by getting
val iosMain by creating { val iosMain by creating {
dependsOn(commonMain)
iosX64Main.dependsOn(this)
iosArm64Main.dependsOn(this)
// iosSimulatorArm64Main.dependsOn(this)
dependencies { dependencies {
implementation(SqlDelight.native) implementation(SqlDelight.native)
@ -94,10 +93,6 @@ kotlin {
val iosArm64Test by getting val iosArm64Test by getting
// val iosSimulatorArm64Test by getting // val iosSimulatorArm64Test by getting
val iosTest by creating { val iosTest by creating {
dependsOn(commonTest)
iosX64Test.dependsOn(this)
iosArm64Test.dependsOn(this)
// iosSimulatorArm64Test.dependsOn(this)
} }
} }
} }

View File

@ -6,33 +6,8 @@ import kotlinx.datetime.*
actual class DateUtils { actual class DateUtils {
actual companion object { actual companion object {
// Possible formats are
// yyyy-mm-dd hh:mm:ss format
private val oldVersionFormat = "\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2}(.()\\d*)?".toRegex()
// yyyy-MM-dd'T'HH:mm:ss[.SSS]XXX (RFC3339)
private val newVersionFormat = "(\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2})[+-](\\d{2}(:\\d{2})?)?".toRegex()
// TODO: do not fix any more issues here. Move everything to plateform specific code.
actual fun parseDate(dateString: String): Long {
var isoDateString: String =
try {
if (dateString.matches(oldVersionFormat)) {
dateString.replace(" ", "T")
} else if (dateString.matches(newVersionFormat)) {
newVersionFormat.find(dateString)?.groups?.get(1)?.value ?: throw Exception("Couldn't parse $dateString")
} else {
throw Exception("Unrecognized format for $dateString")
}
} catch (e: Exception) {
throw Exception("parseDate failed for $dateString", e)
}
return LocalDateTime.parse(isoDateString).toInstant(TimeZone.currentSystemDefault()).toEpochMilliseconds()
}
actual fun parseRelativeDate(dateString: String): String { actual fun parseRelativeDate(dateString: String): String {
val date = parseDate(dateString) val date = dateString.toParsedDate()
return " " + return " " +
DateUtils.getRelativeTimeSpanString( DateUtils.getRelativeTimeSpanString(

View File

@ -74,7 +74,7 @@ class Repository(
dbItems = dbItems.filter { it.sourcetitle == sourceFilter.value!!.title } dbItems = dbItems.filter { it.sourcetitle == sourceFilter.value!!.title }
} }
val itemsList = ArrayList(dbItems.map { it.toView() }) val itemsList = ArrayList(dbItems.map { it.toView() })
itemsList.sortByDescending { DateUtils.parseDate(it.datetime) } itemsList.sortByDescending { it.datetime.toParsedDate() }
fetchedItems = fetchedItems =
StatusAndData.succes( StatusAndData.succes(
itemsList, itemsList,

View File

@ -1,9 +1,35 @@
package bou.amine.apps.readerforselfossv2.utils package bou.amine.apps.readerforselfossv2.utils
import kotlinx.datetime.LocalDateTime
import kotlinx.datetime.TimeZone
import kotlinx.datetime.toInstant
fun String.toParsedDate(): Long {
// Possible formats are
// yyyy-mm-dd hh:mm:ss format
val oldVersionFormat = "\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2}(.()\\d*)?".toRegex()
// yyyy-MM-dd'T'HH:mm:ss[.SSS]XXX (RFC3339)
val newVersionFormat = "(\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2})[+-](\\d{2}(:\\d{2})?)?".toRegex()
val isoDateString: String =
try {
if (this.matches(oldVersionFormat)) {
this.replace(" ", "T")
} else if (this.matches(newVersionFormat)) {
newVersionFormat.find(this)?.groups?.get(1)?.value ?: throw Exception("Couldn't parse $this")
} else {
throw Exception("Unrecognized format for $this")
}
} catch (e: Exception) {
throw Exception("parseDate failed for $this", e)
}
return LocalDateTime.parse(isoDateString).toInstant(TimeZone.currentSystemDefault()).toEpochMilliseconds()
}
expect class DateUtils() { expect class DateUtils() {
companion object { companion object {
fun parseDate(dateString: String): Long
fun parseRelativeDate(dateString: String): String fun parseRelativeDate(dateString: String): String
} }
} }

View File

@ -0,0 +1,66 @@
package bou.amine.apps.readerforselfossv2.repository
import bou.amine.apps.readerforselfossv2.utils.toParsedDate
import kotlinx.datetime.LocalDateTime
import kotlinx.datetime.TimeZone
import kotlinx.datetime.toInstant
import kotlin.test.Test
import kotlin.test.assertEquals
class DatesTest {
private val newVersionDateVariant = "2022-12-24T17:00:08+00"
private val newVersionDate = "2013-04-07T13:43:00+01:00"
private val newVersionDate2 = "2013-04-07T13:43:00-01:00"
private val oldVersionDate = "2013-05-07 13:46:00"
private val oldVersionDateVariant = "2021-03-21 10:32:00.000000"
@Test
fun new_version_date_should_be_parsed() {
val date = newVersionDate.toParsedDate()
val expected =
LocalDateTime(2013, 4, 7, 13, 43, 0, 0).toInstant(TimeZone.currentSystemDefault())
.toEpochMilliseconds()
assertEquals(expected, date)
}
@Test
fun new_version_date2_should_be_parsed() {
val date = newVersionDate2.toParsedDate()
val expected =
LocalDateTime(2013, 4, 7, 13, 43, 0, 0).toInstant(TimeZone.currentSystemDefault())
.toEpochMilliseconds()
assertEquals(expected, date)
}
@Test
fun old_version_date_should_be_parsed() {
val date = oldVersionDate.toParsedDate()
val expected =
LocalDateTime(2013, 5, 7, 13, 46, 0, 0).toInstant(TimeZone.currentSystemDefault())
.toEpochMilliseconds()
assertEquals(expected, date)
}
@Test
fun old_version_variant_date_should_be_parsed() {
val date = oldVersionDateVariant.toParsedDate()
val expected =
LocalDateTime(2021, 3, 21, 10, 32, 0, 0).toInstant(TimeZone.currentSystemDefault())
.toEpochMilliseconds()
assertEquals(expected, date)
}
@Test
fun new_version_variant_date_should_be_parsed() {
val date = newVersionDateVariant.toParsedDate()
val expected =
LocalDateTime(2022, 12, 24, 17, 0, 8, 0).toInstant(TimeZone.currentSystemDefault())
.toEpochMilliseconds()
assertEquals(expected, date)
}
}

View File

@ -0,0 +1,9 @@
package bou.amine.apps.readerforselfossv2.utils
actual class DateUtils actual constructor() {
actual companion object {
actual fun parseRelativeDate(dateString: String): String {
TODO("Not yet implemented")
}
}
}