Migrate local persistence from Room to SQLDelight with multiplatform support (Android+iOS); replace NoteEntity/DAO/Repository with generated SQLDelight schema, add DatabaseFactory for drivers, integrate Flow support for Compose UI updates

This commit is contained in:
2025-09-26 20:57:05 +02:00
parent a42070e36b
commit 52ffb2c958
14 changed files with 99 additions and 101 deletions

View File

@@ -22,6 +22,7 @@ plugins {
id("com.codingfeline.buildkonfig") version "0.17.1"
kotlin("native.cocoapods")
id("app.cash.sqldelight") version "2.1.0"
}
kotlin {
@@ -128,6 +129,13 @@ kotlin {
// Use Ktor-based multiplatform networking backend for Coil
implementation("io.coil-kt.coil3:coil-network-ktor3:3.3.0")
// SQLDelight Coroutines Extensions:
// Gör att queries (t.ex. notesQueries.all()) kan exponeras som Flow.
// Flow används ihop med Compose för att automatiskt uppdatera UI när DB-data ändras.
implementation("app.cash.sqldelight:coroutines-extensions:2.1.0")
// Kotlinx coroutines-core (krävs för Flow + SQLDelight coroutines-extensions)
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.9.0")
// för Compose i :shared (Android-del)
@@ -165,6 +173,10 @@ kotlin {
implementation("androidx.activity:activity-compose:1.11.0")
// PickVisualMediaRequest + ActivityResultContracts.*
implementation("androidx.activity:activity:1.11.0")
// SQLDelight Android-driver
implementation("app.cash.sqldelight:android-driver:2.1.0")
// SQLDelight coroutine-extensions (JVM/Android)
implementation("app.cash.sqldelight:coroutines-extensions:2.1.0")
}
}
@@ -228,4 +240,12 @@ buildkonfig {
buildConfigField(BOOLEAN, "KLLOG_DEBUG", propOr("KLLOG_DEBUG", "false"))
buildConfigField(BOOLEAN, "KLLOG_HTTP_DEBUG", propOr("KLLOG_HTTP_DEBUG", "false"))
}
}
}
sqldelight {
databases {
create("KlottrDatabase") {
packageName.set("app.klottr.db")
}
}
}

View File

@@ -0,0 +1,19 @@
package app.klottr.db
import android.content.Context
import app.cash.sqldelight.db.SqlDriver
import app.cash.sqldelight.driver.android.AndroidSqliteDriver
/**
* Android-implementation av DatabaseFactory.
* Skapar en SqlDriver med AndroidSqliteDriver för att köra KlottrDatabase.
*/
actual class DatabaseFactory(private val context: Context) {
actual fun createDriver(): SqlDriver {
return AndroidSqliteDriver(
schema = KlottrDatabase.Schema,
context = context,
name = "klottr.db" // samma namn som tidigare Room använde
)
}
}

View File

@@ -0,0 +1,11 @@
package app.klottr.db
import app.cash.sqldelight.db.SqlDriver
/**
* Common deklaration: här säger vi bara att varje plattform
* (Android/iOS) måste implementera DatabaseFactory.
*/
expect class DatabaseFactory {
fun createDriver(): SqlDriver
}

View File

@@ -0,0 +1,16 @@
CREATE TABLE notes (
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
lat REAL NOT NULL,
lon REAL NOT NULL,
text TEXT NOT NULL,
createdAt INTEGER NOT NULL DEFAULT 0
);
CREATE UNIQUE INDEX notes_lat_lon_index ON notes(lat, lon);
all:
SELECT * FROM notes ORDER BY id DESC;
upsert:
INSERT OR REPLACE INTO notes(id, lat, lon, text, createdAt)
VALUES (?, ?, ?, ?, ?);