From 418ae4c04efe8471ef8d1337e2b05cbdd76f821a Mon Sep 17 00:00:00 2001 From: yisroelBaum Date: Thu, 19 Sep 2024 13:11:47 +0300 Subject: [PATCH] initializing room db --- .gitignore | 15 + .idea/.gitignore | 3 + .idea/compiler.xml | 6 + .idea/deploymentTargetSelector.xml | 10 + .idea/gradle.xml | 18 + .idea/kotlinc.xml | 6 + .idea/migrations.xml | 10 + .idea/misc.xml | 10 + .idea/other.xml | 318 ++++++++++++++++++ .idea/vcs.xml | 6 + app/build.gradle.kts | 2 + .../data/AccountableTask.kt | 16 + .../data/AccountableTaskDao.kt | 23 ++ .../data/CheshbonUiState.kt | 4 - .../cheshbonhanefeshdaily/data/DataSource.kt | 4 - .../data/TaskDatabase.kt | 27 ++ gradle/libs.versions.toml | 4 + 17 files changed, 474 insertions(+), 8 deletions(-) create mode 100644 .gitignore create mode 100644 .idea/.gitignore create mode 100644 .idea/compiler.xml create mode 100644 .idea/deploymentTargetSelector.xml create mode 100644 .idea/gradle.xml create mode 100644 .idea/kotlinc.xml create mode 100644 .idea/migrations.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/other.xml create mode 100644 .idea/vcs.xml create mode 100644 app/src/main/java/com/example/cheshbonhanefeshdaily/data/AccountableTask.kt create mode 100644 app/src/main/java/com/example/cheshbonhanefeshdaily/data/AccountableTaskDao.kt delete mode 100644 app/src/main/java/com/example/cheshbonhanefeshdaily/data/CheshbonUiState.kt delete mode 100644 app/src/main/java/com/example/cheshbonhanefeshdaily/data/DataSource.kt create mode 100644 app/src/main/java/com/example/cheshbonhanefeshdaily/data/TaskDatabase.kt diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..aa724b7 --- /dev/null +++ b/.gitignore @@ -0,0 +1,15 @@ +*.iml +.gradle +/local.properties +/.idea/caches +/.idea/libraries +/.idea/modules.xml +/.idea/workspace.xml +/.idea/navEditor.xml +/.idea/assetWizardSettings.xml +.DS_Store +/build +/captures +.externalNativeBuild +.cxx +local.properties diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/.idea/compiler.xml b/.idea/compiler.xml new file mode 100644 index 0000000..b589d56 --- /dev/null +++ b/.idea/compiler.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/deploymentTargetSelector.xml b/.idea/deploymentTargetSelector.xml new file mode 100644 index 0000000..b268ef3 --- /dev/null +++ b/.idea/deploymentTargetSelector.xml @@ -0,0 +1,10 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/gradle.xml b/.idea/gradle.xml new file mode 100644 index 0000000..32522c1 --- /dev/null +++ b/.idea/gradle.xml @@ -0,0 +1,18 @@ + + + + + + \ No newline at end of file diff --git a/.idea/kotlinc.xml b/.idea/kotlinc.xml new file mode 100644 index 0000000..fdf8d99 --- /dev/null +++ b/.idea/kotlinc.xml @@ -0,0 +1,6 @@ + + + + + \ No newline at end of file diff --git a/.idea/migrations.xml b/.idea/migrations.xml new file mode 100644 index 0000000..f8051a6 --- /dev/null +++ b/.idea/migrations.xml @@ -0,0 +1,10 @@ + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..0ad17cb --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,10 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/other.xml b/.idea/other.xml new file mode 100644 index 0000000..94c96f6 --- /dev/null +++ b/.idea/other.xml @@ -0,0 +1,318 @@ + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/app/build.gradle.kts b/app/build.gradle.kts index ac2b57f..33d0897 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -60,6 +60,8 @@ dependencies { implementation(libs.androidx.ui.tooling.preview) implementation(libs.androidx.material3) implementation(libs.androidx.navigation.compose) + implementation(libs.androidx.room.common) + implementation(libs.androidx.room.ktx) testImplementation(libs.junit) androidTestImplementation(libs.androidx.junit) androidTestImplementation(libs.androidx.espresso.core) diff --git a/app/src/main/java/com/example/cheshbonhanefeshdaily/data/AccountableTask.kt b/app/src/main/java/com/example/cheshbonhanefeshdaily/data/AccountableTask.kt new file mode 100644 index 0000000..3180d60 --- /dev/null +++ b/app/src/main/java/com/example/cheshbonhanefeshdaily/data/AccountableTask.kt @@ -0,0 +1,16 @@ +package com.example.cheshbonhanefeshdaily.data + + +import androidx.room.Entity +import androidx.room.PrimaryKey + + +@Entity(tableName = "accountable_task") +data class AccountableTask ( + @PrimaryKey(autoGenerate = true) val id: Int = 0, + val date: Int, + val name: String, + val type: String, + val active: Boolean + +) \ No newline at end of file diff --git a/app/src/main/java/com/example/cheshbonhanefeshdaily/data/AccountableTaskDao.kt b/app/src/main/java/com/example/cheshbonhanefeshdaily/data/AccountableTaskDao.kt new file mode 100644 index 0000000..1008259 --- /dev/null +++ b/app/src/main/java/com/example/cheshbonhanefeshdaily/data/AccountableTaskDao.kt @@ -0,0 +1,23 @@ +package com.example.cheshbonhanefeshdaily.data + +import androidx.room.Dao +import androidx.room.Delete +import androidx.room.Insert +import androidx.room.OnConflictStrategy +import androidx.room.Update + +// when writing Queries, its good to specify a Flow<> as the return type, see step 13 in https://developer.android.com/codelabs/basic-android-kotlin-compose-persisting-data-room?continue=https%3A%2F%2Fdeveloper.android.com%2Fcourses%2Fpathways%2Fandroid-basics-compose-unit-6-pathway-2%23codelab-https%3A%2F%2Fdeveloper.android.com%2Fcodelabs%2Fbasic-android-kotlin-compose-persisting-data-room#5 + + +@Dao +interface AccountableTaskDao { + + @Insert(onConflict = OnConflictStrategy.IGNORE) + suspend fun insert(accountableTask: AccountableTask) + + @Update + suspend fun update(accountableTask: AccountableTask) + + @Delete + suspend fun delete(accountableTask: AccountableTask) +} \ No newline at end of file diff --git a/app/src/main/java/com/example/cheshbonhanefeshdaily/data/CheshbonUiState.kt b/app/src/main/java/com/example/cheshbonhanefeshdaily/data/CheshbonUiState.kt deleted file mode 100644 index db19f97..0000000 --- a/app/src/main/java/com/example/cheshbonhanefeshdaily/data/CheshbonUiState.kt +++ /dev/null @@ -1,4 +0,0 @@ -package com.example.cheshbonhanefeshdaily.data - -class CheshbonUiState { -} \ No newline at end of file diff --git a/app/src/main/java/com/example/cheshbonhanefeshdaily/data/DataSource.kt b/app/src/main/java/com/example/cheshbonhanefeshdaily/data/DataSource.kt deleted file mode 100644 index 3c78bbb..0000000 --- a/app/src/main/java/com/example/cheshbonhanefeshdaily/data/DataSource.kt +++ /dev/null @@ -1,4 +0,0 @@ -package com.example.cheshbonhanefeshdaily.data - -object DataSource { -} \ No newline at end of file diff --git a/app/src/main/java/com/example/cheshbonhanefeshdaily/data/TaskDatabase.kt b/app/src/main/java/com/example/cheshbonhanefeshdaily/data/TaskDatabase.kt new file mode 100644 index 0000000..0ef0a4f --- /dev/null +++ b/app/src/main/java/com/example/cheshbonhanefeshdaily/data/TaskDatabase.kt @@ -0,0 +1,27 @@ +package com.example.cheshbonhanefeshdaily.data + +import android.content.Context +import androidx.room.Database +import androidx.room.Room +import androidx.room.RoomDatabase + + +@Database(entities = [AccountableTask::class], version = 1, exportSchema = false) +abstract class TaskDatabase : RoomDatabase(){ + + abstract fun accountableTaskDao() : AccountableTaskDao + + companion object { + @Volatile + private var Instance : TaskDatabase? = null + + fun getDatabase(context:Context) : TaskDatabase { + return Instance ?: synchronized(this) { + Room.databaseBuilder(context, TaskDatabase::class.java, "task_database") + .fallbackToDestructiveMigration() + .build() + .also { Instance = it } + } + } + } +} \ No newline at end of file diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 3b25b32..0067161 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -9,6 +9,8 @@ lifecycleRuntimeKtx = "2.8.5" activityCompose = "1.9.2" composeBom = "2024.04.01" navigationCompose = "2.8.0" +roomCommon = "2.6.1" +roomKtx = "2.6.1" [libraries] androidx-core-ktx = { group = "androidx.core", name = "core-ktx", version.ref = "coreKtx" } @@ -26,6 +28,8 @@ androidx-ui-test-manifest = { group = "androidx.compose.ui", name = "ui-test-man androidx-ui-test-junit4 = { group = "androidx.compose.ui", name = "ui-test-junit4" } androidx-material3 = { group = "androidx.compose.material3", name = "material3" } androidx-navigation-compose = { group = "androidx.navigation", name = "navigation-compose", version.ref = "navigationCompose" } +androidx-room-common = { group = "androidx.room", name = "room-common", version.ref = "roomCommon" } +androidx-room-ktx = { group = "androidx.room", name = "room-ktx", version.ref = "roomKtx" } [plugins] android-application = { id = "com.android.application", version.ref = "agp" }