Skip to content

Commit c5b3b63

Browse files
authored
[feature/define-todo-data-layer] Define ToDo Data Layer (#12)
* Add data-task module * Add core-database module * Set room scheme location * Define entity, dao, database * Define database di module * Add core model module * Task Model 정의 * Add LocalDataSource interface * Define implementation of local data source * Define datasource di module * Replace datasource with repository
1 parent 6b6e213 commit c5b3b63

File tree

23 files changed

+299
-0
lines changed

23 files changed

+299
-0
lines changed

core/database/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build

core/database/build.gradle.kts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
@Suppress("DSL_SCOPE_VIOLATION") // TODO: Remove once KTIJ-19369 is fixed
2+
plugins {
3+
id("conf.mad.convention.android.feature")
4+
id("conf.mad.primitive.android.room")
5+
}
6+
7+
android {
8+
namespace = "com.conf.mad.todo.database"
9+
10+
defaultConfig {
11+
ksp {
12+
arg("room.schemaLocation", "$projectDir/schemas")
13+
}
14+
consumerProguardFiles("consumer-rules.pro")
15+
}
16+
}

core/database/consumer-rules.pro

Whitespace-only changes.
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
{
2+
"formatVersion": 1,
3+
"database": {
4+
"version": 1,
5+
"identityHash": "5f0a9d58cd7ca69ecf3bddf360fbcf28",
6+
"entities": [
7+
{
8+
"tableName": "TaskEntity",
9+
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `title` TEXT NOT NULL, `is_completed` INTEGER NOT NULL, `is_favorite` INTEGER NOT NULL)",
10+
"fields": [
11+
{
12+
"fieldPath": "id",
13+
"columnName": "id",
14+
"affinity": "INTEGER",
15+
"notNull": true
16+
},
17+
{
18+
"fieldPath": "title",
19+
"columnName": "title",
20+
"affinity": "TEXT",
21+
"notNull": true
22+
},
23+
{
24+
"fieldPath": "isCompleted",
25+
"columnName": "is_completed",
26+
"affinity": "INTEGER",
27+
"notNull": true
28+
},
29+
{
30+
"fieldPath": "isFavorite",
31+
"columnName": "is_favorite",
32+
"affinity": "INTEGER",
33+
"notNull": true
34+
}
35+
],
36+
"primaryKey": {
37+
"autoGenerate": true,
38+
"columnNames": [
39+
"id"
40+
]
41+
},
42+
"indices": [],
43+
"foreignKeys": []
44+
}
45+
],
46+
"views": [],
47+
"setupQueries": [
48+
"CREATE TABLE IF NOT EXISTS room_master_table (id INTEGER PRIMARY KEY,identity_hash TEXT)",
49+
"INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, '5f0a9d58cd7ca69ecf3bddf360fbcf28')"
50+
]
51+
}
52+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android" />
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.conf.mad.todo.database
2+
3+
import androidx.room.Dao
4+
import androidx.room.Delete
5+
import androidx.room.Insert
6+
import androidx.room.OnConflictStrategy
7+
import androidx.room.Query
8+
import com.conf.mad.todo.database.entity.TaskEntity
9+
import kotlinx.coroutines.flow.Flow
10+
11+
@Dao
12+
interface TaskDao {
13+
@Insert(onConflict = OnConflictStrategy.REPLACE)
14+
suspend fun insertTask(task: TaskEntity)
15+
16+
/**
17+
* Get tasks by isCompleted is false
18+
*/
19+
@Query("SELECT * FROM TaskEntity WHERE is_completed = 0")
20+
fun getTodoTasks(): Flow<List<TaskEntity>>
21+
22+
/**
23+
* Get tasks by isCompleted is true
24+
*/
25+
@Query("SELECT * FROM TaskEntity WHERE is_completed = 1")
26+
fun getCompletedTasks(): Flow<List<TaskEntity>>
27+
28+
@Delete
29+
suspend fun deleteTask(task: TaskEntity)
30+
31+
/**
32+
* Updating only isFavorite
33+
* By id
34+
*/
35+
@Query("UPDATE TaskEntity SET is_favorite = :isFavorite WHERE id = :id")
36+
suspend fun updateFavorite(id: Long, isFavorite: Boolean)
37+
38+
/**
39+
* Updating only isCompleted
40+
* By id
41+
*/
42+
@Query("UPDATE TaskEntity SET is_completed = :isCompleted WHERE id = :id")
43+
suspend fun updateCompleted(id: Long, isCompleted: Boolean)
44+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.conf.mad.todo.database
2+
3+
import androidx.room.Database
4+
import androidx.room.RoomDatabase
5+
import com.conf.mad.todo.database.entity.TaskEntity
6+
7+
@Database(entities = [TaskEntity::class], version = 1, exportSchema = true)
8+
abstract class TodoDatabase : RoomDatabase() {
9+
abstract fun taskDao(): TaskDao
10+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.conf.mad.todo.database.di
2+
3+
import android.content.Context
4+
import androidx.room.Room
5+
import com.conf.mad.todo.database.TaskDao
6+
import com.conf.mad.todo.database.TodoDatabase
7+
import dagger.Module
8+
import dagger.Provides
9+
import dagger.hilt.InstallIn
10+
import dagger.hilt.android.qualifiers.ApplicationContext
11+
import dagger.hilt.components.SingletonComponent
12+
import javax.inject.Singleton
13+
14+
@Module
15+
@InstallIn(SingletonComponent::class)
16+
object DatabaseModule {
17+
@Provides
18+
@Singleton
19+
fun provideTodoDatabase(
20+
@ApplicationContext context: Context
21+
): TodoDatabase = Room.databaseBuilder(context, TodoDatabase::class.java, "todo.db")
22+
.fallbackToDestructiveMigration()
23+
.build()
24+
25+
@Provides
26+
@Singleton
27+
fun provideTaskDao(todoDatabase: TodoDatabase): TaskDao = todoDatabase.taskDao()
28+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.conf.mad.todo.database.entity
2+
3+
import androidx.room.ColumnInfo
4+
import androidx.room.Entity
5+
import androidx.room.PrimaryKey
6+
7+
@Entity
8+
data class TaskEntity(
9+
@PrimaryKey(autoGenerate = true) val id: Long,
10+
val title: String,
11+
@ColumnInfo("is_completed") val isCompleted: Boolean = false,
12+
@ColumnInfo("is_favorite") val isFavorite: Boolean = false,
13+
)

core/model/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build

0 commit comments

Comments
 (0)