-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4811a49
commit 3916ed5
Showing
6 changed files
with
120 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
core/data/db-room/src/commonMain/kotlin/com/softartdev/notedelight/db/Database.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
8 changes: 8 additions & 0 deletions
8
...ta/db-room/src/commonMain/kotlin/com/softartdev/notedelight/db/NoteDatabaseConstructor.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
24 changes: 24 additions & 0 deletions
24
core/data/db-room/src/commonMain/kotlin/com/softartdev/notedelight/db/NoteTypeConverters.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters