Started to extract persistence library which contains all Sqldelight specific code as Sqldelight conflicts with Compose on iOS

This commit is contained in:
dankito 2024-09-19 17:05:06 +02:00
parent 4fa7adeeb1
commit 0f89314ba3
7 changed files with 182 additions and 0 deletions

View File

@ -0,0 +1,108 @@
plugins {
alias(libs.plugins.kotlinMultiplatform)
alias(libs.plugins.androidLibrary)
alias(libs.plugins.sqldelight)
}
kotlin {
jvmToolchain(11)
jvm()
js {
moduleName = "BankingPersistence"
binaries.executable()
browser()
}
androidTarget {
}
listOf(
iosX64(),
iosArm64(),
iosSimulatorArm64()
).forEach { iosTarget ->
iosTarget.binaries.framework {
baseName = "ComposeApp"
isStatic = false
}
}
applyDefaultHierarchyTemplate()
sourceSets {
commonMain.dependencies {
implementation(libs.sqldelight.runtime)
implementation(libs.sqldelight.coroutines.extensions)
implementation(libs.sqldelight.paging.extensions)
}
commonTest.dependencies {
implementation(libs.kotlin.test)
implementation(libs.coroutines.test)
}
androidMain.dependencies {
implementation(libs.sqldelight.android.driver)
}
iosMain.dependencies {
implementation(libs.sqldelight.native.driver)
}
jvmTest.dependencies {
implementation(libs.kotlin.test.junit)
}
jvmMain.dependencies {
implementation(libs.sqldelight.sqlite.driver)
}
}
}
sqldelight {
databases {
create("BankmeisterDb") {
packageName.set("net.codinux.banking.persistence")
generateAsync = true
schemaOutputDirectory = file("src/commonMain/sqldelight/databases")
}
}
}
android {
namespace = "net.codinux.banking.persistence"
compileSdk = libs.versions.android.compileSdk.get().toInt()
// sourceSets["main"].manifest.srcFile("src/androidMain/AndroidManifest.xml")
defaultConfig {
minSdk = libs.versions.android.minSdk.get().toInt()
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
consumerProguardFiles("consumer-rules.pro")
}
buildTypes {
release {
isMinifyEnabled = false
// proguardFiles(getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro")
}
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11
}
}

View File

@ -0,0 +1,7 @@
package net.codinux.banking.persistence
import android.content.Context
object AndroidContext {
lateinit var applicationContext: Context
}

View File

@ -0,0 +1,10 @@
package net.codinux.banking.persistence
import app.cash.sqldelight.async.coroutines.synchronous
import app.cash.sqldelight.db.QueryResult
import app.cash.sqldelight.db.SqlDriver
import app.cash.sqldelight.db.SqlSchema
import app.cash.sqldelight.driver.android.AndroidSqliteDriver
actual fun createSqlDriverDriver(dbName: String, schema: SqlSchema<QueryResult.AsyncValue<Unit>>, version: Long): SqlDriver =
AndroidSqliteDriver(schema.synchronous(), AndroidContext.applicationContext, dbName)

View File

@ -0,0 +1,16 @@
package net.codinux.banking.persistence
import app.cash.sqldelight.db.QueryResult
import app.cash.sqldelight.db.SqlDriver
import app.cash.sqldelight.db.SqlSchema
expect fun createSqlDriverDriver(dbName: String, schema: SqlSchema<QueryResult.AsyncValue<Unit>>, version: Long): SqlDriver
class BankingRepositoryNewStart {
private val schema = BankmeisterDb.Schema
private val sqlDriver = createSqlDriverDriver("Bankmeister.db", schema, 1L)
}

View File

@ -0,0 +1,10 @@
package net.codinux.banking.persistence
import app.cash.sqldelight.async.coroutines.synchronous
import app.cash.sqldelight.db.QueryResult
import app.cash.sqldelight.db.SqlDriver
import app.cash.sqldelight.db.SqlSchema
import app.cash.sqldelight.driver.native.NativeSqliteDriver
actual fun createSqlDriverDriver(dbName: String, schema: SqlSchema<QueryResult.AsyncValue<Unit>>, version: Long): SqlDriver =
NativeSqliteDriver(schema.synchronous(), dbName)

View File

@ -0,0 +1,9 @@
package net.codinux.banking.persistence
import app.cash.sqldelight.db.QueryResult
import app.cash.sqldelight.db.SqlDriver
import app.cash.sqldelight.db.SqlSchema
actual fun createSqlDriverDriver(dbName: String, schema: SqlSchema<QueryResult.AsyncValue<Unit>>, version: Long): SqlDriver {
throw NotImplementedError("TODO")
}

View File

@ -0,0 +1,22 @@
package net.codinux.banking.persistence
import app.cash.sqldelight.async.coroutines.synchronous
import app.cash.sqldelight.db.QueryResult
import app.cash.sqldelight.db.SqlDriver
import app.cash.sqldelight.db.SqlSchema
import app.cash.sqldelight.driver.jdbc.sqlite.JdbcSqliteDriver
import java.io.File
actual fun createSqlDriverDriver(dbName: String, schema: SqlSchema<QueryResult.AsyncValue<Unit>>, version: Long): SqlDriver {
val dbDir = File("data/db").also { it.mkdirs() }
return JdbcSqliteDriver("jdbc:sqlite:data/db/$dbName").also { driver ->
schema.synchronous().also { schema ->
if (File(dbDir, dbName).exists() == false) {
schema.create(driver)
}
schema.migrate(driver, schema.version, version)
}
}
}