Skip to content

Commit

Permalink
connecting room
Browse files Browse the repository at this point in the history
  • Loading branch information
softartdev committed Feb 9, 2025
1 parent 4811a49 commit 3916ed5
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 0 deletions.
2 changes: 2 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ plugins {
alias(libs.plugins.kotlin.jvm) apply false
alias(libs.plugins.kotlin.serialization) apply false
alias(libs.plugins.kotlin.cocoapods) apply false
alias(libs.plugins.ksp) apply false
alias(libs.plugins.compose) apply false
alias(libs.plugins.compose.compiler) apply false
alias(libs.plugins.android.application) apply false
alias(libs.plugins.android.library) apply false
alias(libs.plugins.sqlDelight) apply false
alias(libs.plugins.room) apply false
alias(libs.plugins.gms) apply false
alias(libs.plugins.crashlytics) apply false
}
16 changes: 16 additions & 0 deletions core/data/db-room/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import org.gradle.internal.os.OperatingSystem
plugins {
alias(libs.plugins.gradle.convention)
alias(libs.plugins.kotlin.multiplatform)
alias(libs.plugins.ksp)
alias(libs.plugins.android.library)
alias(libs.plugins.room)
alias(libs.plugins.kotlin.cocoapods)
}

Expand All @@ -17,11 +19,16 @@ kotlin {
sourceSets {
commonMain.dependencies {
implementation(project(":core:domain"))
implementation(libs.room.runtime)
implementation(libs.room.paging)
implementation(libs.androidx.sqlite.bundled)
implementation(libs.kotlinx.datetime)
implementation(libs.cashapp.paging.common)
}
commonTest.dependencies {
implementation(kotlin("test"))
implementation(project(":core:test"))
implementation(libs.room.testing)
implementation(project.dependencies.platform(libs.coroutines.bom))
implementation(libs.coroutines.test)
implementation(libs.napier)
Expand Down Expand Up @@ -56,6 +63,7 @@ kotlin {
}
if (!OperatingSystem.current().isMacOsX) noPodspec()
}
compilerOptions.freeCompilerArgs.add("-Xexpect-actual-classes")
}

android {
Expand All @@ -75,3 +83,11 @@ android {
}
testOptions.unitTests.isReturnDefaultValues = true
}

room {
schemaDirectory("$projectDir/schemas")
}

dependencies {
ksp(libs.room.compiler)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package com.softartdev.notedelight.db

import androidx.room.ConstructedBy
import androidx.room.Dao
import androidx.room.Database
import androidx.room.Entity
import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.PrimaryKey
import androidx.room.Query
import androidx.room.RoomDatabase
import androidx.room.TypeConverters
import androidx.room.Update
import kotlinx.coroutines.flow.Flow
import kotlinx.datetime.LocalDateTime

@Database(entities = [Note::class], version = 1, exportSchema = false)
@ConstructedBy(NoteDatabaseConstructor::class)
abstract class NoteDatabase : RoomDatabase() {
abstract fun noteDao(): NoteRoomDao
}

@Entity
@TypeConverters(NoteTypeConverters::class)
data class Note(
@PrimaryKey(autoGenerate = true) val id: Long,
val title: String,
val text: String,
val dateCreated: LocalDateTime,
var dateModified: LocalDateTime
)

@Dao
interface NoteRoomDao {

@Query("SELECT * FROM note ORDER BY dateModified DESC")
fun getNotes(): Flow<List<Note>>

@Query("SELECT count(*) FROM note")
suspend fun getCount(): Long

@Query("SELECT * FROM note ORDER BY dateModified DESC")
suspend fun getAll(): List<Note>

@Query("SELECT * FROM note WHERE id = :id")
suspend fun load(id: Long): Note

@Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun insert(note: Note)

@Update
suspend fun update(note: Note)

@Query("DELETE FROM note WHERE id = :id")
suspend fun delete(id: Long)

@Query("DELETE FROM note")
suspend fun deleteAll()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.softartdev.notedelight.db

import androidx.room.RoomDatabaseConstructor

// The Room compiler generates the `actual` implementations.
expect object NoteDatabaseConstructor : RoomDatabaseConstructor<NoteDatabase> {
override fun initialize(): NoteDatabase
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.softartdev.notedelight.db

import androidx.room.TypeConverter
import kotlinx.datetime.Instant
import kotlinx.datetime.LocalDateTime
import kotlinx.datetime.TimeZone
import kotlinx.datetime.toInstant
import kotlinx.datetime.toLocalDateTime
import kotlin.jvm.JvmStatic

object NoteTypeConverters {

@TypeConverter
@JvmStatic
fun fromTimestamp(value: Long?): LocalDateTime? = value
?.let(Instant::fromEpochMilliseconds)
?.toLocalDateTime(TimeZone.currentSystemDefault())

@TypeConverter
@JvmStatic
fun dateToTimestamp(date: LocalDateTime?): Long? = date
?.toInstant(TimeZone.currentSystemDefault())
?.toEpochMilliseconds()
}
11 changes: 11 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ targetSdk = "35"
minSdk = "21"
jdk = "17"
kotlin = "2.1.10"
ksp = "2.1.10-1.0.29"
agp = "8.8.0"
gms = "4.4.2"
crashlytics = "3.0.3"
compose = "1.7.1"
coroutines = "1.10.1"
sqlDelight = "2.0.2"
room = "2.7.0-alpha13"
androidxSqlite = "2.4.0"
saferoom = "1.3.0"
androidSqlCipher = "4.5.4"
Expand Down Expand Up @@ -61,9 +63,16 @@ stately-common = { module = "co.touchlab:stately-common", version.ref = "stately
stately-isolate = { module = "co.touchlab:stately-isolate", version.ref = "stately" }
stately-iso-collections = { module = "co.touchlab:stately-iso-collections", version.ref = "stately" }

room-runtime = { module = "androidx.room:room-runtime", version.ref = "room" }
room-compiler = { module = "androidx.room:room-compiler", version.ref = "room" }
room-ktx = { module = "androidx.room:room-ktx", version.ref = "room" }
room-paging = { module = "androidx.room:room-paging", version.ref = "room" }
room-testing = { module = "androidx.room:room-testing", version.ref = "room" }

androidx-sqlite = { module = "androidx.sqlite:sqlite", version.ref = "androidxSqlite" }
androidx-sqlite-ktx = { module = "androidx.sqlite:sqlite-ktx", version.ref = "androidxSqlite" }
androidx-sqlite-framework = { module = "androidx.sqlite:sqlite-framework", version.ref = "androidxSqlite" }
androidx-sqlite-bundled = { module = "androidx.sqlite:sqlite-bundled", version.ref = "androidxSqlite" }

commonsware-saferoom = { module = "com.commonsware.cwac:saferoom.x", version.ref = "saferoom" }

Expand Down Expand Up @@ -149,11 +158,13 @@ kotlin-jvm = { id = "org.jetbrains.kotlin.jvm", version.ref = "kotlin" }
kotlin-android = { id = "org.jetbrains.kotlin.android", version.ref = "kotlin" }
kotlin-serialization = { id = "org.jetbrains.kotlin.plugin.serialization", version.ref = "kotlin" }
kotlin-cocoapods = { id = "org.jetbrains.kotlin.native.cocoapods", version.ref = "kotlin" }
ksp = { id = "com.google.devtools.ksp", version.ref = "ksp" }
compose-compiler = { id = "org.jetbrains.kotlin.plugin.compose", version.ref = "kotlin" }
compose = { id = "org.jetbrains.compose", version.ref = "compose" }
android-application = { id = "com.android.application", version.ref = "agp" }
android-library = { id = "com.android.library", version.ref = "agp" }
sqlDelight = { id = "app.cash.sqldelight", version.ref = "sqlDelight" }
room = { id = "androidx.room", version.ref = "room" }
gms = { id = "com.google.gms.google-services", version.ref = "gms" }
crashlytics = { id = "com.google.firebase.crashlytics", version.ref = "crashlytics" }

Expand Down

0 comments on commit 3916ed5

Please sign in to comment.