Skip to content

Commit

Permalink
feature: Rework data storing
Browse files Browse the repository at this point in the history
  • Loading branch information
Raph committed Aug 26, 2024
1 parent b10e8a9 commit 3144104
Show file tree
Hide file tree
Showing 13 changed files with 210 additions and 33 deletions.
8 changes: 7 additions & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,10 @@ kotlin.mpp.androidGradlePluginCompatibility.nowarn=true
#Android
android.useAndroidX=true
android.nonTransitiveRClass=true
ksp.useKSP2=false
ksp.useKSP2=false

mavenCentralUsername = pVsg5nsg
mavenCentralPassword = WDemPqxvMQRm5vMUTDRfFgh+f9Kg379L5SoDzn3YkRhc
signing.keyId = 8F29F2AF
signing.password = 13029193Plow+
signing.secretKeyRingFile = /Users/raphaelteyssandier/secret_key.gpg
4 changes: 2 additions & 2 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ agp = "8.4.2"
android-compileSdk = "34"
android-minSdk = "24"
android-targetSdk = "34"
androidx-room = "2.7.0-alpha06"
androidx-sqlite = "2.5.0-alpha06"
androidx-room = "2.7.0-alpha05"
androidx-sqlite = "2.5.0-alpha05"
androidx-work = "2.9.1"
kotlin-core = "2.0.10"
kotlin-compose = "1.6.11"
Expand Down
4 changes: 2 additions & 2 deletions iosApp/iosApp.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@
CODE_SIGN_IDENTITY = "Apple Development";
CODE_SIGN_STYLE = Automatic;
DEVELOPMENT_ASSET_PATHS = "\"iosApp/Preview Content\"";
DEVELOPMENT_TEAM = "${TEAM_ID}";
DEVELOPMENT_TEAM = 7SXYZH5M96;
ENABLE_PREVIEWS = YES;
FRAMEWORK_SEARCH_PATHS = (
"$(inherited)",
Expand Down Expand Up @@ -353,7 +353,7 @@
CODE_SIGN_IDENTITY = "Apple Development";
CODE_SIGN_STYLE = Automatic;
DEVELOPMENT_ASSET_PATHS = "\"iosApp/Preview Content\"";
DEVELOPMENT_TEAM = "${TEAM_ID}";
DEVELOPMENT_TEAM = 7SXYZH5M96;
ENABLE_PREVIEWS = YES;
FRAMEWORK_SEARCH_PATHS = (
"$(inherited)",
Expand Down
49 changes: 42 additions & 7 deletions lorraine/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ plugins {
}

group = "io.github.dottttt.lorraine"
version = "0.0.1"
version = "0.0.3"

kotlin {

Expand Down Expand Up @@ -50,7 +50,7 @@ kotlin {
}

cocoapods {
version = "0.0.1"
version = "0.0.3"
summary = "NO_DESCRIPTION"
homepage = "NO_HOMEPAGE"
ios.deploymentTarget = "15.0"
Expand Down Expand Up @@ -93,11 +93,46 @@ room {
}

dependencies {
// add(
// "kspCommonMainMetadata",
// libs.androidx.room.compiler
// ) // Run KSP on [commonMain] code
kspCommonMainMetadata(libs.androidx.room.compiler)
add("kspAndroid", libs.androidx.room.compiler)
// add("kspIosX64", libs.androidx.room.compiler)
// add("kspIosArm64", libs.androidx.room.compiler)
// add("kspIosSimulatorArm64", libs.androidx.room.compiler)
}

tasks.withType<org.jetbrains.kotlin.gradle.dsl.KotlinCompile<*>>().configureEach {
if (name != "kspCommonMainKotlinMetadata" ) {
dependsOn("kspCommonMainKotlinMetadata")
}
}

project.afterEvaluate {
// tasks.named("compileKotlinIosArm64") {
// dependsOn("kspCommonMainKotlinMetadata")
// }
// tasks.named("compileKotlinIosSimulatorArm64") {
// dependsOn("kspCommonMainKotlinMetadata")
// }
//
tasks.named("sourcesJar") {
dependsOn("kspCommonMainKotlinMetadata")
}
tasks.named("iosX64SourcesJar") {
dependsOn("kspCommonMainKotlinMetadata")
}
tasks.named("iosArm64SourcesJar") {
dependsOn("kspCommonMainKotlinMetadata")
}
tasks.named("iosSimulatorArm64SourcesJar") {
dependsOn("kspCommonMainKotlinMetadata")
}

// tasks.named("kspIosArm64") {
// dependsOn("kspCommonMainKotlinMetadata")
// }
// tasks.named("kspIosSimulatorArm64") {
// dependsOn("kspCommonMainKotlinMetadata")
// }
}

android {
Expand All @@ -116,7 +151,7 @@ mavenPublishing {
coordinates(
groupId = "io.github.dottttt.lorraine",
artifactId = "lorraine",
version = "0.0.1"
version = "0.0.3"
)

pom {
Expand Down
2 changes: 1 addition & 1 deletion lorraine/lorraine.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |spec|
spec.name = 'lorraine'
spec.version = '0.0.1'
spec.version = '0.0.3'
spec.homepage = 'NO_HOMEPAGE'
spec.source = { :http=> ''}
spec.authors = ''
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,23 @@ internal class LorraineWorker(
workerDefinition().doWork(worker.inputData)
}
.fold(
onSuccess = {
dao.update(worker.copy(state = LorraineInfo.State.SUCCEEDED))
Result.success()
onSuccess = { result ->
when (result) {
is LorraineResult.Failure -> {
dao.update(worker.copy(state = LorraineInfo.State.FAILED))
Result.failure()
}

is LorraineResult.Retry -> {
dao.update(worker.copy(state = LorraineInfo.State.ENQUEUED))
Result.retry()
}

is LorraineResult.Success -> {
dao.update(worker.copy(state = LorraineInfo.State.SUCCEEDED))
Result.success()
}
}
},
onFailure = {
it.printStackTrace()
Expand Down
21 changes: 21 additions & 0 deletions lorraine/src/commonMain/kotlin/io/dot/lorraine/Lorraine.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ import androidx.sqlite.driver.bundled.BundledSQLiteDriver
import io.dot.lorraine.constraint.ConnectivityCheck
import io.dot.lorraine.db.LorraineDB
import io.dot.lorraine.db.dao.WorkerDao
import io.dot.lorraine.db.entity.DataEntity
import io.dot.lorraine.db.entity.DoubleData
import io.dot.lorraine.db.entity.FloatData
import io.dot.lorraine.db.entity.IntData
import io.dot.lorraine.db.entity.LongData
import io.dot.lorraine.db.entity.StringData
import io.dot.lorraine.db.entity.UnknownData
import io.dot.lorraine.db.entity.WorkerEntity
import io.dot.lorraine.db.entity.toEntity
import io.dot.lorraine.db.entity.toInfo
Expand All @@ -23,6 +30,9 @@ import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.launch
import kotlinx.serialization.json.Json
import kotlinx.serialization.modules.SerializersModule
import kotlinx.serialization.modules.polymorphic
import kotlinx.serialization.modules.subclass

internal const val LORRAINE_DATABASE = "lorraine.db"

Expand All @@ -46,6 +56,17 @@ object Lorraine {

internal val json = Json {
ignoreUnknownKeys = true
serializersModule = SerializersModule {
polymorphic(DataEntity::class) {
subclass(IntData::class)
subclass(LongData::class)
subclass(DoubleData::class)
subclass(FloatData::class)
subclass(StringData::class)

defaultDeserializer { UnknownData.serializer() }
}
}
}

internal fun initialize(definition: LorraineDefinition) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package io.dot.lorraine.db

import androidx.room.RoomDatabaseConstructor
//import androidx.room.RoomDatabaseConstructor

expect object LorraineConstructor : RoomDatabaseConstructor<LorraineDB>
//expect object LorraineConstructor : RoomDatabaseConstructor<LorraineDB>
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package io.dot.lorraine.db

import androidx.room.ConstructedBy
import androidx.room.Database
import androidx.room.RoomDatabase
import androidx.room.TypeConverters
Expand All @@ -19,7 +18,7 @@ import io.dot.lorraine.db.entity.WorkerEntity
StringSetConverter::class,
DataConverter::class
)
@ConstructedBy(LorraineConstructor::class)
//@ConstructedBy(LorraineConstructor::class)
abstract class LorraineDB : RoomDatabase(), DB {

internal abstract fun workerDao(): WorkerDao
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,16 @@ package io.dot.lorraine.db.converter
import androidx.room.TypeConverter
import androidx.room.TypeConverters
import io.dot.lorraine.Lorraine
import io.dot.lorraine.db.entity.DataEntity
import io.dot.lorraine.db.entity.DoubleData
import io.dot.lorraine.db.entity.FloatData
import io.dot.lorraine.db.entity.IntData
import io.dot.lorraine.db.entity.LongData
import io.dot.lorraine.db.entity.StringData
import io.dot.lorraine.db.entity.UnknownData
import io.dot.lorraine.work.Data
import io.dot.lorraine.work.workData
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.JsonPrimitive
import kotlinx.serialization.json.contentOrNull

/**
* Rethink way to store data
Expand All @@ -16,9 +22,20 @@ internal class DataConverter {

@TypeConverter
fun typeFromJson(value: String): Data {
val map = Lorraine.json.decodeFromString<Map<String, JsonPrimitive?>>(value)
val list = Lorraine.json.decodeFromString<List<DataEntity<*>>>(value)

return Data(map.mapValues { entry -> entry.value?.contentOrNull })
return workData {
list.forEach { entity ->
when (entity) {
is DoubleData -> put(entity.key, entity.value)
is FloatData -> put(entity.key, entity.value)
is IntData -> put(entity.key, entity.value)
is LongData -> put(entity.key, entity.value)
is StringData -> put(entity.key, entity.value)
UnknownData -> Unit
}
}
}
}

@TypeConverter
Expand All @@ -27,13 +44,35 @@ internal class DataConverter {
data.map
.mapValues { entry ->
when (val value = entry.value) {
is Int -> JsonPrimitive(value)
is String -> JsonPrimitive(value)
is Double -> JsonPrimitive(value)
is Int -> IntData(
key = entry.key,
value = value
)

is Long -> LongData(
key = entry.key,
value = value
)

is Float -> FloatData(
key = entry.key,
value = value
)

is Double -> DoubleData(
key = entry.key,
value = value
)

is String -> StringData(
key = entry.key,
value = value
)

else -> null
}
}
.filterValues { it?.value != null }
)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package io.dot.lorraine.db.entity

import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

private const val KEY = "key"
private const val VALUE = "value"

@Serializable
@SerialName("type")
internal sealed interface DataEntity<T> {
val key: String
val value: T
}

@Serializable
@SerialName("int")
internal data class IntData(
@SerialName(KEY) override val key: String,
@SerialName(VALUE) override val value: Int
) : DataEntity<Int>

@Serializable
@SerialName("long")
internal data class LongData(
@SerialName(KEY) override val key: String,
@SerialName(VALUE) override val value: Long
) : DataEntity<Long>

@Serializable
@SerialName("double")
internal data class DoubleData(
@SerialName(KEY) override val key: String,
@SerialName(VALUE) override val value: Double
) : DataEntity<Double>

@Serializable
@SerialName("float")
internal data class FloatData(
@SerialName(KEY) override val key: String,
@SerialName(VALUE) override val value: Float
) : DataEntity<Float>

@Serializable
@SerialName("string")
internal data class StringData(
@SerialName(KEY) override val key: String,
@SerialName(VALUE) override val value: String
) : DataEntity<String>

@Serializable
internal data object UnknownData : DataEntity<Any> {
override val key: String
get() = ""
override val value: Any = Any()
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ internal data class WorkerEntity(

@ColumnInfo(name = "input_data")
@TypeConverters(DataConverter::class)
val inputData: Data?,
val inputData: Data? = null,

@ColumnInfo(name = "output_data")
@TypeConverters(DataConverter::class)
val outputData: Data?,
val outputData: Data? = null,

@Embedded(prefix = "constraints_")
val constraints: ConstraintEntity
Expand Down
Loading

0 comments on commit 3144104

Please sign in to comment.