organize
This commit is contained in:
@@ -0,0 +1,45 @@
|
||||
plugins {
|
||||
alias(libs.plugins.kotlin.multiplatform)
|
||||
alias(libs.plugins.android.library)
|
||||
alias(libs.plugins.sqldelight)
|
||||
}
|
||||
|
||||
kotlin {
|
||||
androidTarget {
|
||||
compilations.all {
|
||||
kotlinOptions {
|
||||
jvmTarget = "17"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
sourceSets {
|
||||
commonMain.dependencies {
|
||||
implementation(libs.coroutines.core)
|
||||
implementation(libs.sqldelight.coroutines)
|
||||
}
|
||||
androidMain.dependencies {
|
||||
implementation(libs.sqldelight.android.driver)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
android {
|
||||
namespace = "com.example.todokmp.shared"
|
||||
compileSdk = 35
|
||||
defaultConfig {
|
||||
minSdk = 26
|
||||
}
|
||||
compileOptions {
|
||||
sourceCompatibility = JavaVersion.VERSION_17
|
||||
targetCompatibility = JavaVersion.VERSION_17
|
||||
}
|
||||
}
|
||||
|
||||
sqldelight {
|
||||
databases {
|
||||
create("TodoDatabase") {
|
||||
packageName.set("com.example.todokmp.db")
|
||||
}
|
||||
}
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
package com.example.todokmp.db
|
||||
|
||||
import android.content.Context
|
||||
import app.cash.sqldelight.db.SqlDriver
|
||||
import app.cash.sqldelight.driver.android.AndroidSqliteDriver
|
||||
|
||||
actual class DatabaseDriverFactory(private val context: Context) {
|
||||
actual fun createDriver(): SqlDriver {
|
||||
return AndroidSqliteDriver(TodoDatabase.Schema, context, "todo.db")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package com.example.todokmp.db
|
||||
|
||||
import app.cash.sqldelight.db.SqlDriver
|
||||
|
||||
expect class DatabaseDriverFactory {
|
||||
fun createDriver(): SqlDriver
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
package com.example.todokmp.db
|
||||
|
||||
import app.cash.sqldelight.coroutines.asFlow
|
||||
import app.cash.sqldelight.coroutines.mapToList
|
||||
import com.example.todokmp.model.Priority
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
|
||||
class TodoRepository(driverFactory: DatabaseDriverFactory) {
|
||||
private val database = TodoDatabase(driverFactory.createDriver())
|
||||
private val queries = database.todoQueries
|
||||
|
||||
fun getAllTodos(): Flow<List<TodoEntity>> {
|
||||
return queries.selectAll().asFlow().mapToList(Dispatchers.Default)
|
||||
}
|
||||
|
||||
fun addTodo(title: String, category: String, priority: Priority) {
|
||||
queries.insert(
|
||||
title = title,
|
||||
completed = false,
|
||||
category = category,
|
||||
priority = priority.name.lowercase(),
|
||||
created_at = System.currentTimeMillis()
|
||||
)
|
||||
}
|
||||
|
||||
fun toggleTodo(id: Long, completed: Boolean) {
|
||||
queries.toggleCompleted(completed = completed, id = id)
|
||||
}
|
||||
|
||||
fun updateTodo(id: Long, title: String, category: String, priority: Priority) {
|
||||
queries.update(
|
||||
title = title,
|
||||
category = category,
|
||||
priority = priority.name.lowercase(),
|
||||
id = id
|
||||
)
|
||||
}
|
||||
|
||||
fun deleteTodo(id: Long) {
|
||||
queries.delete(id = id)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.example.todokmp.model
|
||||
|
||||
enum class Priority(val label: String) {
|
||||
LOW("Low"),
|
||||
MEDIUM("Medium"),
|
||||
HIGH("High");
|
||||
|
||||
companion object {
|
||||
fun fromString(value: String): Priority =
|
||||
entries.firstOrNull { it.name.equals(value, ignoreCase = true) } ?: MEDIUM
|
||||
}
|
||||
}
|
||||
|
||||
enum class TodoFilter(val label: String) {
|
||||
ALL("All"),
|
||||
ACTIVE("Active"),
|
||||
DONE("Done")
|
||||
}
|
||||
|
||||
val categories = listOf("Personal", "Work", "Shopping", "Health", "Learning")
|
||||
@@ -0,0 +1,24 @@
|
||||
CREATE TABLE IF NOT EXISTS TodoEntity (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
title TEXT NOT NULL,
|
||||
completed INTEGER AS kotlin.Boolean NOT NULL DEFAULT 0,
|
||||
category TEXT NOT NULL DEFAULT 'Personal',
|
||||
priority TEXT NOT NULL DEFAULT 'medium',
|
||||
created_at INTEGER NOT NULL
|
||||
);
|
||||
|
||||
selectAll:
|
||||
SELECT * FROM TodoEntity ORDER BY created_at DESC;
|
||||
|
||||
insert:
|
||||
INSERT INTO TodoEntity(title, completed, category, priority, created_at)
|
||||
VALUES (?, ?, ?, ?, ?);
|
||||
|
||||
update:
|
||||
UPDATE TodoEntity SET title = ?, category = ?, priority = ? WHERE id = ?;
|
||||
|
||||
toggleCompleted:
|
||||
UPDATE TodoEntity SET completed = ? WHERE id = ?;
|
||||
|
||||
delete:
|
||||
DELETE FROM TodoEntity WHERE id = ?;
|
||||
Reference in New Issue
Block a user