// com/example/noteapp/data/model/Note.kt
package com.example.noteapp.data.model
import androidx.room.Entity
import androidx.room.PrimaryKey
import kotlinx.datetime.Instant
import kotlinx.serialization.Serializable
@Entity(tableName = "notes")
@Serializable
data class Note(
@PrimaryKey(autoGenerate = true) val id: Long = 0,
val title: String = "",
val content: String = "",
val updateDate: Instant = Instant.DISTANT_PAST,
val reminderDate: Instant? = null,
val color: Int = 0xFFFFFFFF.toInt(),
val isTrashed: Boolean = false,
val isPinned: Boolean = false,
val isArchived: Boolean = false
)
// com/example/noteapp/data/model/ChecklistItem.kt
package com.example.noteapp.data.model
import androidx.room.Entity
import androidx.room.ForeignKey
import androidx.room.Index
import androidx.room.PrimaryKey
import kotlinx.serialization.Serializable
@Entity(
tableName = "checklist_items",
foreignKeys = [
ForeignKey(
entity = Note::class,
parentColumns = ["id"],
childColumns = ["noteId"],
onDelete = ForeignKey.CASCADE
)
],
indices = [Index("noteId")]
)
@Serializable
data class ChecklistItem(
@PrimaryKey(autoGenerate = true) val id: Long = 0,
val noteId: Long,
val text: String,
val isChecked: Boolean = false,
val order: Int
)
// com/example/noteapp/data/model/Tag.kt
package com.example.noteapp.data.model
import androidx.room.Entity
import androidx.room.PrimaryKey
import kotlinx.serialization.Serializable
@Entity(tableName = "tags")
@Serializable
data class Tag(
@PrimaryKey(autoGenerate = true) val id: Long = 0,
val name: String
)
// com/example/noteapp/data/model/NoteTagCrossRef.kt
package com.example.noteapp.data.model
import androidx.room.Entity
import androidx.room.ForeignKey
import androidx.room.Index
@Entity(
tableName = "note_tag_cross_ref",
primaryKeys = ["noteId", "tagId"],
foreignKeys = [
ForeignKey(
entity = Note::class,
parentColumns = ["id"],
childColumns = ["noteId"],
onDelete = ForeignKey.CASCADE
),
ForeignKey(
entity = Tag::class,
parentColumns = ["id"],
childColumns = ["tagId"],
onDelete = ForeignKey.CASCADE
)
],
indices = [Index("noteId"), Index("tagId")]
)
data class NoteTagCrossRef(
val noteId: Long,
val tagId: Long
)
// com/example/noteapp/data/model/NoteWithDetails.kt
package com.example.noteapp.data.model
import androidx.room.Embedded
import androidx.room.Junction
import androidx.room.Relation
import kotlinx.serialization.Serializable
@Serializable
data class NoteWithDetails(
@Embedded val note: Note,
@Relation(
parentColumn = "id",
entityColumn = "noteId"
)
val checklistItems: List<ChecklistItem> = emptyList(),
@Relation(
parentColumn = "id",
entityColumn = "id",
associateBy = Junction(
value = NoteTagCrossRef::class,
parentColumn = "noteId",
entityColumn = "tagId"
)
)
val tags: List<Tag> = emptyList()
)
// com/example/noteapp/data/local/NoteDatabase.kt
package com.example.noteapp.data.local
import androidx.room.Database
import androidx.room.RoomDatabase
import androidx.room.TypeConverters
import com.example.noteapp.data.model.ChecklistItem
import com.example.noteapp.data.model.Note
import com.example.noteapp.data.model.NoteTagCrossRef
import com.example.noteapp.data.model.Tag
@Database(
entities = [Note::class, ChecklistItem::class, Tag::class, NoteTagCrossRef::class],
version = 1,
exportSchema = false
)
@TypeConverters(Converters::class)
abstract class NoteDatabase : RoomDatabase() {
abstract fun noteDao(): NoteDao
abstract fun checklistItemDao(): ChecklistItemDao
abstract fun tagDao(): TagDao
}
// com/example/noteapp/data/local/Converters.kt
package com.example.noteapp.data.local
import androidx.room.TypeConverter
import kotlinx.datetime.Instant
class Converters {
@TypeConverter
fun fromTimestamp(value: Long?): Instant? {
return value?.let { Instant.fromEpochMilliseconds(it) }
}
@TypeConverter
fun dateToTimestamp(instant: Instant?): Long? {
return instant?.toEpochMilliseconds()
}
}
// com/example/noteapp/data/local/NoteDao.kt
package com.example.noteapp.data.local
import androidx.room.*
import com.example.noteapp.data.model.Note
import com.example.noteapp.data.model.NoteWithDetails
import kotlinx.coroutines.flow.Flow
@Dao
interface NoteDao {
@Transaction
@Query("SELECT * FROM notes WHERE isTrashed = 0 AND isArchived = 0 ORDER BY isPinned DESC, updateDate DESC")
fun getAllNotesFlow(): Flow<List<NoteWithDetails>>
@Transaction
@Query("SELECT * FROM notes WHERE id = :id")
fun getNoteByIdFlow(id: Long): Flow<NoteWithDetails?>
@Transaction
@Query("SELECT * FROM notes WHERE id = :id")
suspend fun getNoteById(id: Long): NoteWithDetails?
@Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun insertNote(note: Note): Long
@Update
suspend fun updateNote(note: Note)
@Delete
suspend fun deleteNote(note: Note)
@Query("UPDATE notes SET isTrashed = 1 WHERE id = :id")
suspend fun trashNote(id: Long)
@Query("UPDATE notes SET isArchived = 1 WHERE id = :id")
suspend fun archiveNote(id: Long)
@Query("UPDATE notes SET isPinned = :isPinned WHERE id = :id")
suspend fun updateNotePinStatus(id: Long, isPinned: Boolean)
}
// com/example/noteapp/data/local/ChecklistItemDao.kt
package com.example.noteapp.data.local
import androidx.room.*
import com.example.noteapp.data.model.ChecklistItem
import kotlinx.coroutines.flow.Flow
@Dao
interface ChecklistItemDao {
@Query("SELECT * FROM checklist_items WHERE noteId = :noteId ORDER BY `order`")
fun getChecklistItemsForNoteFlow(noteId: Long): Flow<List<ChecklistItem>>
@Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun insertChecklistItem(item: ChecklistItem): Long
@Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun insertChecklistItems(items: List<ChecklistItem>)
@Update
suspend fun updateChecklistItem(item: ChecklistItem)
@Delete
suspend fun deleteChecklistItem(item: ChecklistItem)
@Query("DELETE FROM checklist_items WHERE noteId = :noteId")
suspend fun deleteAllChecklistItemsForNote(noteId: Long)
@Transaction
suspend fun updateChecklistItemsOrder(items: List<ChecklistItem>) {
items.forEach { updateChecklistItem(it) }
}
}
// com/example/noteapp/data/local/TagDao.kt
package com.example.noteapp.data.local
import androidx.room.*
import com.example.noteapp.data.model.NoteTagCrossRef
import com.example.noteapp.data.model.Tag
import kotlinx.coroutines.flow.Flow
@Dao
interface TagDao {
@Query("SELECT * FROM tags ORDER BY name")
fun getAllTagsFlow(): Flow<List<Tag>>
@Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun insertTag(tag: Tag): Long
@Update
suspend fun updateTag(tag: Tag)
@Delete
suspend fun deleteTag(tag: Tag)
@Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun insertNoteTagCrossRef(crossRef: NoteTagCrossRef)
@Delete
suspend fun deleteNoteTagCrossRef(crossRef: NoteTagCrossRef)
@Query("DELETE FROM note_tag_cross_ref WHERE noteId = :noteId")
suspend fun deleteAllTagsForNote(noteId: Long)
@Transaction
suspend fun updateNoteTags(noteId: Long, tagIds: List<Long>) {
deleteAllTagsForNote(noteId)
tagIds.forEach { tagId ->
insertNoteTagCrossRef(NoteTagCrossRef(noteId, tagId))
}
}
}