Skip to content

Commit f5803f6

Browse files
committed
add: SQL for note(ListView)
1 parent 3a67417 commit f5803f6

File tree

3 files changed

+100
-3
lines changed

3 files changed

+100
-3
lines changed

app/build.gradle.kts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ android {
4343
}
4444

4545
dependencies {
46-
4746
implementation(libs.androidx.core.ktx)
4847
implementation(libs.androidx.lifecycle.runtime.ktx)
4948
implementation(libs.androidx.activity.compose)
@@ -52,6 +51,11 @@ dependencies {
5251
implementation(libs.androidx.ui.graphics)
5352
implementation(libs.androidx.ui.tooling.preview)
5453
implementation(libs.androidx.material3)
54+
55+
implementation(libs.androidx.room.ktx)
56+
implementation (libs.androidx.room.runtime)
57+
kapt ("androidx.room:room-compiler:2.6.1")
58+
5559
testImplementation(libs.junit)
5660
androidTestImplementation(libs.androidx.junit)
5761
androidTestImplementation(libs.androidx.espresso.core)
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package com.coolkie.noteultra.data
2+
3+
import android.content.Context
4+
import androidx.lifecycle.ViewModel
5+
import androidx.lifecycle.ViewModelProvider
6+
import androidx.lifecycle.viewModelScope
7+
import androidx.room.Dao
8+
import androidx.room.Database
9+
import androidx.room.Delete
10+
import androidx.room.Entity
11+
import androidx.room.Insert
12+
import androidx.room.PrimaryKey
13+
import androidx.room.Query
14+
import androidx.room.Room
15+
import androidx.room.RoomDatabase
16+
import kotlinx.coroutines.flow.Flow
17+
import kotlinx.coroutines.flow.SharingStarted
18+
import kotlinx.coroutines.flow.stateIn
19+
import kotlinx.coroutines.launch
20+
21+
@Entity(tableName = "notes")
22+
data class Note(
23+
@PrimaryKey(autoGenerate = true) val id: Int = 0,
24+
val title: String,
25+
val content: String,
26+
val date: Long
27+
)
28+
29+
@Dao
30+
interface NotesDao {
31+
@Insert
32+
suspend fun insert(note: Note)
33+
34+
@Delete
35+
suspend fun delete(note: Note)
36+
37+
@Query("SELECT * FROM notes ORDER BY date DESC")
38+
fun getAllNotes(): Flow<List<Note>>
39+
}
40+
41+
@Database(entities = [Note::class], version = 1)
42+
abstract class NotesDatabase : RoomDatabase() {
43+
abstract fun noteDao(): NotesDao
44+
45+
companion object {
46+
@Volatile
47+
private var INSTANCE: NotesDatabase? = null
48+
49+
fun getDatabase(context: Context): NotesDatabase {
50+
return INSTANCE ?: synchronized(this) {
51+
val instance = Room.databaseBuilder(
52+
context.applicationContext,
53+
NotesDatabase::class.java,
54+
"notes_database"
55+
).build()
56+
57+
INSTANCE = instance
58+
instance
59+
}
60+
}
61+
}
62+
}
63+
64+
class NoteViewModel(private val database: NotesDatabase) : ViewModel() {
65+
val noteList = database.noteDao().getAllNotes()
66+
.stateIn(viewModelScope, SharingStarted.WhileSubscribed(5000), emptyList())
67+
68+
fun addNote(title: String, content: String, date: Long) {
69+
val note = Note(title = title, content = content, date = date)
70+
viewModelScope.launch {
71+
database.noteDao().insert(note)
72+
}
73+
}
74+
75+
fun deleteNote(note: Note) {
76+
viewModelScope.launch {
77+
database.noteDao().delete(note)
78+
}
79+
}
80+
}
81+
82+
class NoteViewModelFactory(private val database: NotesDatabase) : ViewModelProvider.Factory {
83+
@Suppress("UNCHECKED_CAST")
84+
override fun <T : ViewModel> create(modelClass: Class<T>): T {
85+
if (modelClass.isAssignableFrom(NoteViewModel::class.java)) {
86+
return NoteViewModel(database) as T
87+
}
88+
throw IllegalArgumentException("Unknown ViewModel class")
89+
}
90+
}

gradle/libs.versions.toml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,12 @@ espressoCore = "3.6.1"
88
lifecycleRuntimeKtx = "2.8.7"
99
activityCompose = "1.9.3"
1010
composeBom = "2024.04.01"
11+
roomKtx = "2.6.1"
12+
roomRuntime = "2.6.1"
1113

1214
[libraries]
1315
androidx-core-ktx = { group = "androidx.core", name = "core-ktx", version.ref = "coreKtx" }
16+
androidx-room-runtime = { module = "androidx.room:room-runtime", version.ref = "roomRuntime" }
1417
junit = { group = "junit", name = "junit", version.ref = "junit" }
1518
androidx-junit = { group = "androidx.test.ext", name = "junit", version.ref = "junitVersion" }
1619
androidx-espresso-core = { group = "androidx.test.espresso", name = "espresso-core", version.ref = "espressoCore" }
@@ -24,9 +27,9 @@ androidx-ui-tooling-preview = { group = "androidx.compose.ui", name = "ui-toolin
2427
androidx-ui-test-manifest = { group = "androidx.compose.ui", name = "ui-test-manifest" }
2528
androidx-ui-test-junit4 = { group = "androidx.compose.ui", name = "ui-test-junit4" }
2629
androidx-material3 = { group = "androidx.compose.material3", name = "material3" }
30+
androidx-room-ktx = { group = "androidx.room", name = "room-ktx", version.ref = "roomKtx" }
2731

2832
[plugins]
2933
android-application = { id = "com.android.application", version.ref = "agp" }
3034
kotlin-android = { id = "org.jetbrains.kotlin.android", version.ref = "kotlin" }
31-
kotlin-compose = { id = "org.jetbrains.kotlin.plugin.compose", version.ref = "kotlin" }
32-
35+
kotlin-compose = { id = "org.jetbrains.kotlin.plugin.compose", version.ref = "kotlin" }

0 commit comments

Comments
 (0)