Refactoring db module structure
This commit is contained in:
28
core/database/api/build.gradle.kts
Normal file
28
core/database/api/build.gradle.kts
Normal file
@@ -0,0 +1,28 @@
|
||||
plugins {
|
||||
alias(libs.plugins.android.library)
|
||||
alias(libs.plugins.kotlin.android)
|
||||
}
|
||||
|
||||
android {
|
||||
namespace = "ru.fincode.tsudesk.core.database.api"
|
||||
compileSdk = libs.versions.compileSdk.get().toInt()
|
||||
|
||||
defaultConfig {
|
||||
minSdk = libs.versions.minSdk.get().toInt()
|
||||
consumerProguardFiles("consumer-rules.pro")
|
||||
}
|
||||
|
||||
val jvm = JavaVersion.toVersion(libs.versions.jvmTarget.get())
|
||||
compileOptions {
|
||||
sourceCompatibility = jvm
|
||||
targetCompatibility = jvm
|
||||
}
|
||||
kotlinOptions {
|
||||
jvmTarget = jvm.toString()
|
||||
}
|
||||
}
|
||||
|
||||
dependencies {
|
||||
api(libs.room.runtime)
|
||||
api(libs.kotlinx.coroutines.core)
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package ru.fincode.tsudesk.core.database.schedule
|
||||
package ru.fincode.tsudesk.core.database.api.schedule
|
||||
|
||||
object Constants {
|
||||
const val SCHEDULE_TABLE = "schedule_cache"
|
||||
@@ -1,13 +1,14 @@
|
||||
package ru.fincode.tsudesk.core.database.schedule
|
||||
package ru.fincode.tsudesk.core.database.api.schedule
|
||||
|
||||
import androidx.room.Entity
|
||||
import androidx.room.Index
|
||||
import androidx.room.PrimaryKey
|
||||
import ru.fincode.tsudesk.core.database.schedule.Constants.COL_SCHEDULE_KEY
|
||||
import ru.fincode.tsudesk.core.database.schedule.Constants.LESSON_TABLE
|
||||
import ru.fincode.tsudesk.core.database.api.schedule.Constants.COL_SCHEDULE_KEY
|
||||
import ru.fincode.tsudesk.core.database.api.schedule.Constants.LESSON_TABLE
|
||||
|
||||
@Entity(
|
||||
tableName = LESSON_TABLE, indices = [Index(value = [COL_SCHEDULE_KEY])]
|
||||
tableName = LESSON_TABLE,
|
||||
indices = [Index(value = [COL_SCHEDULE_KEY])]
|
||||
)
|
||||
data class LessonCacheEntity(
|
||||
@PrimaryKey(autoGenerate = true) val id: Long = 0,
|
||||
@@ -21,4 +22,4 @@ data class LessonCacheEntity(
|
||||
val teacher: String,
|
||||
val groupIds: List<String>,
|
||||
val type: String
|
||||
)
|
||||
)
|
||||
@@ -0,0 +1,21 @@
|
||||
package ru.fincode.tsudesk.core.database.api.schedule
|
||||
|
||||
import ru.fincode.tsudesk.core.database.api.schedule.Constants.COL_KEY
|
||||
import ru.fincode.tsudesk.core.database.api.schedule.Constants.COL_SCHEDULE_KEY
|
||||
import ru.fincode.tsudesk.core.database.api.schedule.Constants.LESSON_TABLE
|
||||
import ru.fincode.tsudesk.core.database.api.schedule.Constants.SCHEDULE_TABLE
|
||||
|
||||
object Query {
|
||||
|
||||
const val SELECT_LESSON_BY_KEY_QUERY =
|
||||
"SELECT * FROM ${LESSON_TABLE} WHERE ${COL_SCHEDULE_KEY} = :key"
|
||||
|
||||
const val SELECT_SCHEDULE_BY_KEY_QUERY =
|
||||
"SELECT * FROM ${SCHEDULE_TABLE} WHERE `${COL_KEY}` = :key LIMIT 1"
|
||||
|
||||
const val DELETE_LESSON_BY_KEY_QUERY =
|
||||
"DELETE FROM ${LESSON_TABLE} WHERE ${COL_SCHEDULE_KEY} = :key"
|
||||
|
||||
const val DELETE_SCHEDULE_BY_KEY_QUERY =
|
||||
"DELETE FROM ${SCHEDULE_TABLE} WHERE `${COL_KEY}` = :key"
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package ru.fincode.tsudesk.core.database.api.schedule
|
||||
|
||||
import androidx.room.Entity
|
||||
import androidx.room.PrimaryKey
|
||||
import ru.fincode.tsudesk.core.database.api.schedule.Constants.SCHEDULE_TABLE
|
||||
|
||||
@Entity(tableName = SCHEDULE_TABLE)
|
||||
data class ScheduleCacheEntity(
|
||||
@PrimaryKey
|
||||
val key: String, // "group:220631" | "teacher:ФИО"
|
||||
val timestamp: Long
|
||||
)
|
||||
@@ -1,4 +1,4 @@
|
||||
package ru.fincode.tsudesk.core.database.schedule
|
||||
package ru.fincode.tsudesk.core.database.api.schedule
|
||||
|
||||
import androidx.room.Dao
|
||||
import androidx.room.Insert
|
||||
@@ -6,11 +6,10 @@ import androidx.room.OnConflictStrategy
|
||||
import androidx.room.Query
|
||||
import androidx.room.Transaction
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import ru.fincode.tsudesk.core.database.schedule.Query.DELETE_LESSONS_BY_KEY
|
||||
import ru.fincode.tsudesk.core.database.schedule.Query.DELETE_SCHEDULE_BY_KEY
|
||||
import ru.fincode.tsudesk.core.database.schedule.Query.SELECT_LESSONS_BY_KEY_QUERY
|
||||
import ru.fincode.tsudesk.core.database.schedule.Query.SELECT_SCHEDULE_BY_KEY_QUERY
|
||||
|
||||
import ru.fincode.tsudesk.core.database.api.schedule.Query.DELETE_LESSON_BY_KEY_QUERY
|
||||
import ru.fincode.tsudesk.core.database.api.schedule.Query.DELETE_SCHEDULE_BY_KEY_QUERY
|
||||
import ru.fincode.tsudesk.core.database.api.schedule.Query.SELECT_LESSON_BY_KEY_QUERY
|
||||
import ru.fincode.tsudesk.core.database.api.schedule.Query.SELECT_SCHEDULE_BY_KEY_QUERY
|
||||
|
||||
@Dao
|
||||
interface ScheduleDao {
|
||||
@@ -18,13 +17,13 @@ interface ScheduleDao {
|
||||
@Query(SELECT_SCHEDULE_BY_KEY_QUERY)
|
||||
fun observeSchedule(key: String): Flow<ScheduleCacheEntity?>
|
||||
|
||||
@Query(SELECT_LESSONS_BY_KEY_QUERY)
|
||||
@Query(SELECT_LESSON_BY_KEY_QUERY)
|
||||
fun observeLessons(key: String): Flow<List<LessonCacheEntity>>
|
||||
|
||||
@Query(SELECT_SCHEDULE_BY_KEY_QUERY)
|
||||
suspend fun getSchedule(key: String): ScheduleCacheEntity?
|
||||
|
||||
@Query(SELECT_LESSONS_BY_KEY_QUERY)
|
||||
@Query(SELECT_LESSON_BY_KEY_QUERY)
|
||||
suspend fun getLessons(key: String): List<LessonCacheEntity>
|
||||
|
||||
@Insert(onConflict = OnConflictStrategy.REPLACE)
|
||||
@@ -33,10 +32,10 @@ interface ScheduleDao {
|
||||
@Insert(onConflict = OnConflictStrategy.REPLACE)
|
||||
suspend fun insertLessons(lessons: List<LessonCacheEntity>)
|
||||
|
||||
@Query(DELETE_LESSONS_BY_KEY)
|
||||
@Query(DELETE_LESSON_BY_KEY_QUERY)
|
||||
suspend fun deleteLessonsByKey(key: String)
|
||||
|
||||
@Query(DELETE_SCHEDULE_BY_KEY)
|
||||
@Query(DELETE_SCHEDULE_BY_KEY_QUERY)
|
||||
suspend fun deleteScheduleByKey(key: String)
|
||||
|
||||
@Transaction
|
||||
@@ -51,8 +50,8 @@ interface ScheduleDao {
|
||||
}
|
||||
|
||||
@Transaction
|
||||
suspend fun removeSchedule(key: String) {
|
||||
suspend fun clearSchedule(key: String) {
|
||||
deleteLessonsByKey(key)
|
||||
deleteScheduleByKey(key)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -5,7 +5,7 @@ plugins {
|
||||
}
|
||||
|
||||
android {
|
||||
namespace = "ru.fincode.tsudesk.core.database"
|
||||
namespace = "ru.fincode.tsudesk.core.database.impl"
|
||||
compileSdk = libs.versions.compileSdk.get().toInt()
|
||||
|
||||
defaultConfig {
|
||||
@@ -35,6 +35,9 @@ android {
|
||||
}
|
||||
kapt {
|
||||
correctErrorTypes = true
|
||||
arguments {
|
||||
arg("room.schemaLocation", "$projectDir/schemas")
|
||||
}
|
||||
}
|
||||
dependencies {
|
||||
implementation(libs.room.runtime)
|
||||
@@ -43,4 +46,6 @@ dependencies {
|
||||
|
||||
implementation(libs.hilt.android)
|
||||
kapt(libs.hilt.compiler)
|
||||
|
||||
implementation(projects.core.database.api)
|
||||
}
|
||||
0
core/database/impl/consumer-rules.pro
Normal file
0
core/database/impl/consumer-rules.pro
Normal file
21
core/database/impl/proguard-rules.pro
vendored
Normal file
21
core/database/impl/proguard-rules.pro
vendored
Normal file
@@ -0,0 +1,21 @@
|
||||
# Add project specific ProGuard rules here.
|
||||
# You can control the set of applied configuration files using the
|
||||
# proguardFiles setting in build.gradle.
|
||||
#
|
||||
# For more details, see
|
||||
# http://developer.android.com/guide/developing/tools/proguard.html
|
||||
|
||||
# If your project uses WebView with JS, uncomment the following
|
||||
# and specify the fully qualified class name to the JavaScript interface
|
||||
# class:
|
||||
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
|
||||
# public *;
|
||||
#}
|
||||
|
||||
# Uncomment this to preserve the line number information for
|
||||
# debugging stack traces.
|
||||
#-keepattributes SourceFile,LineNumberTable
|
||||
|
||||
# If you keep the line number information, uncomment this to
|
||||
# hide the original source file name.
|
||||
#-renamesourcefileattribute SourceFile
|
||||
@@ -0,0 +1,124 @@
|
||||
{
|
||||
"formatVersion": 1,
|
||||
"database": {
|
||||
"version": 2,
|
||||
"identityHash": "3d269ca42c7bee3550ec1498f99f51f1",
|
||||
"entities": [
|
||||
{
|
||||
"tableName": "schedule_cache",
|
||||
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`key` TEXT NOT NULL, `timestamp` INTEGER NOT NULL, PRIMARY KEY(`key`))",
|
||||
"fields": [
|
||||
{
|
||||
"fieldPath": "key",
|
||||
"columnName": "key",
|
||||
"affinity": "TEXT",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "timestamp",
|
||||
"columnName": "timestamp",
|
||||
"affinity": "INTEGER",
|
||||
"notNull": true
|
||||
}
|
||||
],
|
||||
"primaryKey": {
|
||||
"autoGenerate": false,
|
||||
"columnNames": [
|
||||
"key"
|
||||
]
|
||||
},
|
||||
"indices": [],
|
||||
"foreignKeys": []
|
||||
},
|
||||
{
|
||||
"tableName": "lesson_cache",
|
||||
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `scheduleKey` TEXT NOT NULL, `date` TEXT NOT NULL, `time` TEXT NOT NULL, `name` TEXT NOT NULL, `typeName` TEXT NOT NULL, `room` TEXT NOT NULL, `teacher` TEXT NOT NULL, `groupIds` TEXT NOT NULL, `type` TEXT NOT NULL)",
|
||||
"fields": [
|
||||
{
|
||||
"fieldPath": "id",
|
||||
"columnName": "id",
|
||||
"affinity": "INTEGER",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "scheduleKey",
|
||||
"columnName": "scheduleKey",
|
||||
"affinity": "TEXT",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "date",
|
||||
"columnName": "date",
|
||||
"affinity": "TEXT",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "time",
|
||||
"columnName": "time",
|
||||
"affinity": "TEXT",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "name",
|
||||
"columnName": "name",
|
||||
"affinity": "TEXT",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "typeName",
|
||||
"columnName": "typeName",
|
||||
"affinity": "TEXT",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "room",
|
||||
"columnName": "room",
|
||||
"affinity": "TEXT",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "teacher",
|
||||
"columnName": "teacher",
|
||||
"affinity": "TEXT",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "groupIds",
|
||||
"columnName": "groupIds",
|
||||
"affinity": "TEXT",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "type",
|
||||
"columnName": "type",
|
||||
"affinity": "TEXT",
|
||||
"notNull": true
|
||||
}
|
||||
],
|
||||
"primaryKey": {
|
||||
"autoGenerate": true,
|
||||
"columnNames": [
|
||||
"id"
|
||||
]
|
||||
},
|
||||
"indices": [
|
||||
{
|
||||
"name": "index_lesson_cache_scheduleKey",
|
||||
"unique": false,
|
||||
"columnNames": [
|
||||
"scheduleKey"
|
||||
],
|
||||
"orders": [],
|
||||
"createSql": "CREATE INDEX IF NOT EXISTS `index_lesson_cache_scheduleKey` ON `${TABLE_NAME}` (`scheduleKey`)"
|
||||
}
|
||||
],
|
||||
"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, '3d269ca42c7bee3550ec1498f99f51f1')"
|
||||
]
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package ru.fincode.core.database.impl
|
||||
|
||||
import androidx.room.Database
|
||||
import androidx.room.RoomDatabase
|
||||
import androidx.room.TypeConverters
|
||||
import ru.fincode.tsudesk.core.database.api.schedule.LessonCacheEntity
|
||||
import ru.fincode.tsudesk.core.database.api.schedule.ScheduleCacheEntity
|
||||
import ru.fincode.tsudesk.core.database.api.schedule.ScheduleDao
|
||||
|
||||
@Database(
|
||||
entities = [
|
||||
ScheduleCacheEntity::class,
|
||||
LessonCacheEntity::class
|
||||
],
|
||||
version = 2,
|
||||
exportSchema = true
|
||||
)
|
||||
@TypeConverters(StringListConverter::class)
|
||||
abstract class AppDatabase : RoomDatabase() {
|
||||
|
||||
abstract fun scheduleCacheDao(): ScheduleDao
|
||||
}
|
||||
@@ -1,21 +1,21 @@
|
||||
package ru.fincode.tsudesk.core.database
|
||||
package ru.fincode.core.database.impl
|
||||
|
||||
import androidx.room.TypeConverter
|
||||
|
||||
class StringListConverter {
|
||||
|
||||
private val separator = "||"
|
||||
private companion object {
|
||||
const val SEPARATOR = "||"
|
||||
}
|
||||
|
||||
@TypeConverter
|
||||
fun fromList(list: List<String>?): String =
|
||||
list
|
||||
?.joinToString(separator = separator)
|
||||
.orEmpty()
|
||||
list?.joinToString(SEPARATOR).orEmpty()
|
||||
|
||||
@TypeConverter
|
||||
fun toList(value: String?): List<String> =
|
||||
value
|
||||
?.takeIf { it.isNotBlank() }
|
||||
?.split(separator)
|
||||
.orEmpty()
|
||||
}
|
||||
?.split(SEPARATOR)
|
||||
?: emptyList()
|
||||
}
|
||||
@@ -1,8 +1,8 @@
|
||||
package ru.fincode.tsudesk.core.database.builder
|
||||
package ru.fincode.core.database.impl.builder
|
||||
|
||||
import android.content.Context
|
||||
import androidx.room.Room
|
||||
import ru.fincode.tsudesk.core.database.AppDatabase
|
||||
import ru.fincode.core.database.impl.AppDatabase
|
||||
|
||||
object AppDatabaseBuilder {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package ru.fincode.tsudesk.core.database.di
|
||||
package ru.fincode.core.database.impl.di
|
||||
|
||||
import android.content.Context
|
||||
import dagger.Module
|
||||
@@ -6,9 +6,9 @@ import dagger.Provides
|
||||
import dagger.hilt.InstallIn
|
||||
import dagger.hilt.android.qualifiers.ApplicationContext
|
||||
import dagger.hilt.components.SingletonComponent
|
||||
import ru.fincode.tsudesk.core.database.AppDatabase
|
||||
import ru.fincode.tsudesk.core.database.builder.AppDatabaseBuilder
|
||||
import ru.fincode.tsudesk.core.database.schedule.ScheduleDao
|
||||
import ru.fincode.core.database.impl.AppDatabase
|
||||
import ru.fincode.core.database.impl.builder.AppDatabaseBuilder
|
||||
import ru.fincode.tsudesk.core.database.api.schedule.ScheduleDao
|
||||
import javax.inject.Singleton
|
||||
|
||||
@Module
|
||||
@@ -1,26 +0,0 @@
|
||||
package ru.fincode.tsudesk.core.database
|
||||
|
||||
import androidx.room.Database
|
||||
import androidx.room.RoomDatabase
|
||||
import androidx.room.TypeConverters
|
||||
import ru.fincode.tsudesk.core.database.schedule.LessonCacheEntity
|
||||
import ru.fincode.tsudesk.core.database.schedule.ScheduleCacheEntity
|
||||
import ru.fincode.tsudesk.core.database.schedule.ScheduleDao
|
||||
|
||||
@TypeConverters(StringListConverter::class)
|
||||
@Database(
|
||||
entities = [
|
||||
// schedule feature
|
||||
ScheduleCacheEntity::class,
|
||||
LessonCacheEntity::class,
|
||||
|
||||
// future:
|
||||
// NewsEntity::class,
|
||||
// GradeEntity::class,
|
||||
],
|
||||
version = 2,
|
||||
exportSchema = false
|
||||
)
|
||||
abstract class AppDatabase : RoomDatabase() {
|
||||
abstract fun scheduleCacheDao(): ScheduleDao
|
||||
}
|
||||
@@ -1,21 +0,0 @@
|
||||
package ru.fincode.tsudesk.core.database.schedule
|
||||
|
||||
import ru.fincode.tsudesk.core.database.schedule.Constants.COL_KEY
|
||||
import ru.fincode.tsudesk.core.database.schedule.Constants.COL_SCHEDULE_KEY
|
||||
import ru.fincode.tsudesk.core.database.schedule.Constants.LESSON_TABLE
|
||||
import ru.fincode.tsudesk.core.database.schedule.Constants.SCHEDULE_TABLE
|
||||
|
||||
object Query {
|
||||
|
||||
const val SELECT_LESSONS_BY_KEY_QUERY =
|
||||
"SELECT * FROM $LESSON_TABLE WHERE $COL_SCHEDULE_KEY = :key"
|
||||
|
||||
const val SELECT_SCHEDULE_BY_KEY_QUERY =
|
||||
"SELECT * FROM $SCHEDULE_TABLE WHERE $COL_KEY = :key LIMIT 1"
|
||||
|
||||
const val DELETE_LESSONS_BY_KEY =
|
||||
"DELETE FROM $LESSON_TABLE WHERE $COL_SCHEDULE_KEY = :key"
|
||||
|
||||
const val DELETE_SCHEDULE_BY_KEY =
|
||||
"DELETE FROM $SCHEDULE_TABLE WHERE $COL_KEY = :key"
|
||||
}
|
||||
@@ -1,12 +0,0 @@
|
||||
package ru.fincode.tsudesk.core.database.schedule
|
||||
|
||||
import androidx.room.Entity
|
||||
import androidx.room.PrimaryKey
|
||||
import ru.fincode.tsudesk.core.database.schedule.Constants.SCHEDULE_TABLE
|
||||
|
||||
@Entity(tableName = SCHEDULE_TABLE)
|
||||
data class ScheduleCacheEntity(
|
||||
@PrimaryKey
|
||||
val key: String, // "group:220631" | "teacher:ФИО"
|
||||
val timestamp: Long
|
||||
)
|
||||
Reference in New Issue
Block a user