diff --git a/app/build.gradle.kts b/app/build.gradle.kts index 061a951..64d7200 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -44,7 +44,7 @@ android { composeOptions { kotlinCompilerExtensionVersion = rootProject.extra["compose_version"] as String // kotlinCompilerVersion = "1.4.32" - kotlinCompilerVersion = "1.5.10" +// kotlinCompilerVersion = "1.5.10" kotlinCompilerExtensionVersion = "1.0.0" } } @@ -52,6 +52,7 @@ android { dependencies { implementation(project(":domain")) implementation(project(":data")) + implementation(project(":device")) val koin_version = "3.1.2" val compose_version = "1.0.0" diff --git a/app/src/main/java/com/soulesidibe/todoapp/TodoApp.kt b/app/src/main/java/com/soulesidibe/todoapp/TodoApp.kt index 6e7b3b7..f6325fc 100644 --- a/app/src/main/java/com/soulesidibe/todoapp/TodoApp.kt +++ b/app/src/main/java/com/soulesidibe/todoapp/TodoApp.kt @@ -2,7 +2,9 @@ package com.soulesidibe.todoapp import android.app.Application import com.soulesidibe.data.dataModule +import com.soulesidibe.device.deviceModule import com.soulesidibe.domain.domainModule +import org.koin.android.ext.koin.androidContext import org.koin.android.ext.koin.androidLogger import org.koin.core.context.GlobalContext.startKoin @@ -13,8 +15,8 @@ class TodoApp : Application() { startKoin { androidLogger() -// androidContext(instance) - modules(appModule, dataModule, domainModule) + androidContext(this@TodoApp) + modules(appModule, dataModule, domainModule, deviceModule) } } } \ No newline at end of file diff --git a/build.gradle.kts b/build.gradle.kts index d187f81..b33e9bd 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -7,7 +7,7 @@ buildscript { } dependencies { classpath("com.android.tools.build:gradle:7.0.0") - classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.21") + classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.10") // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle.kts files diff --git a/data/build.gradle b/data/build.gradle index 76ea7ea..6dec8a4 100644 --- a/data/build.gradle +++ b/data/build.gradle @@ -11,6 +11,6 @@ java { dependencies { implementation project(":domain") implementation "org.jetbrains.kotlin:kotlin-stdlib:1.5.10" - implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.5.0" + implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.5.1" implementation("io.insert-koin:koin-core:3.1.2") } \ No newline at end of file diff --git a/data/src/main/java/com/soulesidibe/data/datasource/TodoLocalDataSource.kt b/data/src/main/java/com/soulesidibe/data/datasource/TodoLocalDataSource.kt new file mode 100644 index 0000000..d71565a --- /dev/null +++ b/data/src/main/java/com/soulesidibe/data/datasource/TodoLocalDataSource.kt @@ -0,0 +1,10 @@ +package com.soulesidibe.data.datasource + +import com.soulesidibe.data.model.TodoData +import com.soulesidibe.domain.entity.TodoEntity +import kotlinx.coroutines.flow.Flow + +interface TodoLocalDataSource { + + fun getAll(): Flow> +} \ No newline at end of file diff --git a/data/src/main/java/com/soulesidibe/data/di.kt b/data/src/main/java/com/soulesidibe/data/di.kt index 0c8a772..2183e0b 100644 --- a/data/src/main/java/com/soulesidibe/data/di.kt +++ b/data/src/main/java/com/soulesidibe/data/di.kt @@ -5,5 +5,5 @@ import com.soulesidibe.domain.repository.TodoRepository import org.koin.dsl.module val dataModule = module { - single { TodoRepositoryImpl() } + single { TodoRepositoryImpl(get()) } } \ No newline at end of file diff --git a/data/src/main/java/com/soulesidibe/data/model/TodoData.kt b/data/src/main/java/com/soulesidibe/data/model/TodoData.kt new file mode 100644 index 0000000..8b3462d --- /dev/null +++ b/data/src/main/java/com/soulesidibe/data/model/TodoData.kt @@ -0,0 +1,5 @@ +package com.soulesidibe.data.model + +import java.util.* + +data class TodoData(val id: String = UUID.randomUUID().toString(), val title: String) \ No newline at end of file diff --git a/data/src/main/java/com/soulesidibe/data/model/TodoDb.kt b/data/src/main/java/com/soulesidibe/data/model/TodoDb.kt deleted file mode 100644 index 1be2f85..0000000 --- a/data/src/main/java/com/soulesidibe/data/model/TodoDb.kt +++ /dev/null @@ -1,5 +0,0 @@ -package com.soulesidibe.data.model - -import java.util.* - -internal data class TodoDb(val id: String = UUID.randomUUID().toString(), val title: String) \ No newline at end of file diff --git a/data/src/main/java/com/soulesidibe/data/model/mapper/mapper.kt b/data/src/main/java/com/soulesidibe/data/model/mapper/mapper.kt index a10858a..1719f2b 100644 --- a/data/src/main/java/com/soulesidibe/data/model/mapper/mapper.kt +++ b/data/src/main/java/com/soulesidibe/data/model/mapper/mapper.kt @@ -1,12 +1,12 @@ package com.soulesidibe.data.model.mapper -import com.soulesidibe.data.model.TodoDb +import com.soulesidibe.data.model.TodoData import com.soulesidibe.domain.entity.TodoEntity -internal fun TodoEntity.toDb(): TodoDb { - return TodoDb(this.id, this.title) +internal fun TodoEntity.toDb(): TodoData { + return TodoData(this.id, this.title) } -internal fun TodoDb.toEntity(): TodoEntity { +internal fun TodoData.toEntity(): TodoEntity { return TodoEntity(id, title) } \ No newline at end of file diff --git a/data/src/main/java/com/soulesidibe/data/repository/TodoRepositoryImpl.kt b/data/src/main/java/com/soulesidibe/data/repository/TodoRepositoryImpl.kt index c3c72b2..776034d 100644 --- a/data/src/main/java/com/soulesidibe/data/repository/TodoRepositoryImpl.kt +++ b/data/src/main/java/com/soulesidibe/data/repository/TodoRepositoryImpl.kt @@ -1,6 +1,7 @@ package com.soulesidibe.data.repository -import com.soulesidibe.data.model.TodoDb +import com.soulesidibe.data.datasource.TodoLocalDataSource +import com.soulesidibe.data.model.TodoData import com.soulesidibe.data.model.mapper.toDb import com.soulesidibe.data.model.mapper.toEntity import com.soulesidibe.domain.ResponseResult @@ -9,29 +10,31 @@ import com.soulesidibe.domain.repository.TodoRepository import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.MutableStateFlow -internal class TodoRepositoryImpl : TodoRepository { +internal class TodoRepositoryImpl( + private val dataSource: TodoLocalDataSource +) : TodoRepository { - private val database: MutableList = mutableListOf( - TodoDb(title = "Task 1"), - TodoDb(title = "Task 2"), - TodoDb(title = "Task 3"), - TodoDb(title = "Task 4"), - TodoDb(title = "Task 5"), - TodoDb(title = "Task 6"), - TodoDb(title = "Task 7"), - TodoDb(title = "Task 8"), - TodoDb(title = "Task 9"), - TodoDb(title = "Task 10"), - TodoDb(title = "Task 11"), - TodoDb(title = "Task 12"), - TodoDb(title = "Task 13"), - TodoDb(title = "Task 14"), - TodoDb(title = "Task 15"), + private val database: MutableList = mutableListOf( + TodoData(title = "Task 1"), + TodoData(title = "Task 2"), + TodoData(title = "Task 3"), + TodoData(title = "Task 4"), + TodoData(title = "Task 5"), + TodoData(title = "Task 6"), + TodoData(title = "Task 7"), + TodoData(title = "Task 8"), + TodoData(title = "Task 9"), + TodoData(title = "Task 10"), + TodoData(title = "Task 11"), + TodoData(title = "Task 12"), + TodoData(title = "Task 13"), + TodoData(title = "Task 14"), + TodoData(title = "Task 15"), ) private lateinit var todos: MutableStateFlow>> - private val transform: (TodoDb) -> TodoEntity = { todoDb -> todoDb.toEntity() } + private val transform: (TodoData) -> TodoEntity = { todoDb -> todoDb.toEntity() } override suspend fun get(): Flow>> { if (database.isEmpty()) { diff --git a/device/build.gradle b/device/build.gradle index 108a2ca..82312ff 100644 --- a/device/build.gradle +++ b/device/build.gradle @@ -1,6 +1,7 @@ plugins { id 'com.android.library' id 'kotlin-android' + id 'kotlin-kapt' } android { @@ -14,6 +15,16 @@ android { testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" consumerProguardFiles "consumer-rules.pro" + + javaCompileOptions { + annotationProcessorOptions { + arguments += [ + "room.schemaLocation":"$projectDir/schemas".toString(), + "room.incremental":"true", + "room.expandProjection":"true"] + } + } + } buildTypes { @@ -32,11 +43,21 @@ android { } dependencies { + implementation project(":data") + + implementation("io.insert-koin:koin-core:3.1.2") + implementation "io.insert-koin:koin-android:3.1.2" implementation 'androidx.core:core-ktx:1.6.0' implementation 'androidx.appcompat:appcompat:1.3.1' implementation 'com.google.android.material:material:1.4.0' - testImplementation 'junit:junit:4.+' + + def room_version = "2.3.0" + implementation("androidx.room:room-runtime:$room_version") + implementation("androidx.room:room-ktx:$room_version") + kapt("androidx.room:room-compiler:$room_version") + + testImplementation 'junit:junit:4.13.2' androidTestImplementation 'androidx.test.ext:junit:1.1.3' androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0' } \ No newline at end of file diff --git a/device/schemas/com.soulesidibe.device.db.TodoDatabase/1.json b/device/schemas/com.soulesidibe.device.db.TodoDatabase/1.json new file mode 100644 index 0000000..5c8ff0d --- /dev/null +++ b/device/schemas/com.soulesidibe.device.db.TodoDatabase/1.json @@ -0,0 +1,40 @@ +{ + "formatVersion": 1, + "database": { + "version": 1, + "identityHash": "b8326ea7d60208f490d5cc177907105b", + "entities": [ + { + "tableName": "todos", + "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` TEXT NOT NULL, `title` TEXT NOT NULL, PRIMARY KEY(`id`))", + "fields": [ + { + "fieldPath": "id", + "columnName": "id", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "title", + "columnName": "title", + "affinity": "TEXT", + "notNull": true + } + ], + "primaryKey": { + "columnNames": [ + "id" + ], + "autoGenerate": false + }, + "indices": [], + "foreignKeys": [] + } + ], + "views": [], + "setupQueries": [ + "CREATE TABLE IF NOT EXISTS room_master_table (id INTEGER PRIMARY KEY,identity_hash TEXT)", + "INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, 'b8326ea7d60208f490d5cc177907105b')" + ] + } +} \ No newline at end of file diff --git a/device/src/main/java/com/soulesidibe/device/datasource/TodoLocalDataSourceImpl.kt b/device/src/main/java/com/soulesidibe/device/datasource/TodoLocalDataSourceImpl.kt new file mode 100644 index 0000000..de55f32 --- /dev/null +++ b/device/src/main/java/com/soulesidibe/device/datasource/TodoLocalDataSourceImpl.kt @@ -0,0 +1,16 @@ +package com.soulesidibe.device.datasource + +import com.soulesidibe.data.datasource.TodoLocalDataSource +import com.soulesidibe.data.model.TodoData +import com.soulesidibe.device.db.dao.TodoDao +import kotlinx.coroutines.flow.Flow +import kotlinx.coroutines.flow.map + +internal class TodoLocalDataSourceImpl( + private val dao: TodoDao +) : TodoLocalDataSource { + + override fun getAll(): Flow> { + return dao.getAll().map { todoDb -> todoDb.map { TodoData(it.id, it.title) } } + } +} \ No newline at end of file diff --git a/device/src/main/java/com/soulesidibe/device/db/TodoDatabase.kt b/device/src/main/java/com/soulesidibe/device/db/TodoDatabase.kt new file mode 100644 index 0000000..ac76e73 --- /dev/null +++ b/device/src/main/java/com/soulesidibe/device/db/TodoDatabase.kt @@ -0,0 +1,12 @@ +package com.soulesidibe.device.db + +import androidx.room.Database +import androidx.room.RoomDatabase +import com.soulesidibe.device.db.dao.TodoDao +import com.soulesidibe.device.model.TodoDb + +@Database(entities = [TodoDb::class], version = 1) +internal abstract class TodoDatabase: RoomDatabase() { + + abstract fun getTodoDao(): TodoDao +} \ No newline at end of file diff --git a/device/src/main/java/com/soulesidibe/device/db/dao/TodoDao.kt b/device/src/main/java/com/soulesidibe/device/db/dao/TodoDao.kt new file mode 100644 index 0000000..d4c25cb --- /dev/null +++ b/device/src/main/java/com/soulesidibe/device/db/dao/TodoDao.kt @@ -0,0 +1,21 @@ +package com.soulesidibe.device.db.dao + +import androidx.room.Dao +import androidx.room.Delete +import androidx.room.Insert +import androidx.room.Query +import com.soulesidibe.device.model.TodoDb +import kotlinx.coroutines.flow.Flow + +@Dao +interface TodoDao { + + @Query("select * from todos") + fun getAll(): Flow> + + @Insert + suspend fun insert(todoDb: TodoDb): Long + + @Delete + suspend fun delete(todoDb: TodoDb) +} \ No newline at end of file diff --git a/device/src/main/java/com/soulesidibe/device/di.kt b/device/src/main/java/com/soulesidibe/device/di.kt new file mode 100644 index 0000000..36f8674 --- /dev/null +++ b/device/src/main/java/com/soulesidibe/device/di.kt @@ -0,0 +1,19 @@ +package com.soulesidibe.device + +import androidx.room.Room +import com.soulesidibe.data.datasource.TodoLocalDataSource +import com.soulesidibe.device.datasource.TodoLocalDataSourceImpl +import com.soulesidibe.device.db.TodoDatabase +import org.koin.android.ext.koin.androidContext +import org.koin.dsl.module + + +val deviceModule = module { + + single { + Room.databaseBuilder(androidContext(), TodoDatabase::class.java, "todos_db") + .build() + } + single { get().getTodoDao() } + single { TodoLocalDataSourceImpl(get()) } +} \ No newline at end of file diff --git a/device/src/main/java/com/soulesidibe/device/model/TodoDb.kt b/device/src/main/java/com/soulesidibe/device/model/TodoDb.kt new file mode 100644 index 0000000..f82e4c1 --- /dev/null +++ b/device/src/main/java/com/soulesidibe/device/model/TodoDb.kt @@ -0,0 +1,10 @@ +package com.soulesidibe.device.model + +import androidx.room.Entity +import androidx.room.PrimaryKey + +@Entity(tableName = "todos") +data class TodoDb( + @PrimaryKey val id: String, + val title: String +) diff --git a/domain/build.gradle b/domain/build.gradle index fd69c95..e886ab6 100644 --- a/domain/build.gradle +++ b/domain/build.gradle @@ -10,6 +10,6 @@ java { dependencies { implementation "org.jetbrains.kotlin:kotlin-stdlib:1.5.10" - implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.5.0" + implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.5.1" implementation("io.insert-koin:koin-core:3.1.2") } \ No newline at end of file