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:
@@ -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")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
16
shared/src/commonMain/sqldelight/app/klottr/db/Notes.sq
Normal file
16
shared/src/commonMain/sqldelight/app/klottr/db/Notes.sq
Normal 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 (?, ?, ?, ?, ?);
|
||||
Reference in New Issue
Block a user