Refactoring db module structure
This commit is contained in:
51
core/database/impl/build.gradle.kts
Normal file
51
core/database/impl/build.gradle.kts
Normal file
@@ -0,0 +1,51 @@
|
||||
plugins {
|
||||
alias(libs.plugins.android.library)
|
||||
alias(libs.plugins.kotlin.android)
|
||||
alias(libs.plugins.kotlin.kapt)
|
||||
}
|
||||
|
||||
android {
|
||||
namespace = "ru.fincode.tsudesk.core.database.impl"
|
||||
compileSdk = libs.versions.compileSdk.get().toInt()
|
||||
|
||||
defaultConfig {
|
||||
minSdk = libs.versions.minSdk.get().toInt()
|
||||
consumerProguardFiles("consumer-rules.pro")
|
||||
}
|
||||
|
||||
buildTypes {
|
||||
release {
|
||||
isMinifyEnabled = false
|
||||
proguardFiles(
|
||||
getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro"
|
||||
)
|
||||
}
|
||||
}
|
||||
val jvm = JavaVersion.toVersion(libs.versions.jvmTarget.get())
|
||||
compileOptions {
|
||||
sourceCompatibility = jvm
|
||||
targetCompatibility = jvm
|
||||
}
|
||||
kotlinOptions {
|
||||
jvmTarget = jvm.toString()
|
||||
}
|
||||
buildFeatures {
|
||||
buildConfig = true
|
||||
}
|
||||
}
|
||||
kapt {
|
||||
correctErrorTypes = true
|
||||
arguments {
|
||||
arg("room.schemaLocation", "$projectDir/schemas")
|
||||
}
|
||||
}
|
||||
dependencies {
|
||||
implementation(libs.room.runtime)
|
||||
implementation(libs.room.ktx)
|
||||
kapt(libs.room.compiler)
|
||||
|
||||
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
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package ru.fincode.core.database.impl
|
||||
|
||||
import androidx.room.TypeConverter
|
||||
|
||||
class StringListConverter {
|
||||
|
||||
private companion object {
|
||||
const val SEPARATOR = "||"
|
||||
}
|
||||
|
||||
@TypeConverter
|
||||
fun fromList(list: List<String>?): String =
|
||||
list?.joinToString(SEPARATOR).orEmpty()
|
||||
|
||||
@TypeConverter
|
||||
fun toList(value: String?): List<String> =
|
||||
value
|
||||
?.takeIf { it.isNotBlank() }
|
||||
?.split(SEPARATOR)
|
||||
?: emptyList()
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package ru.fincode.core.database.impl.builder
|
||||
|
||||
import android.content.Context
|
||||
import androidx.room.Room
|
||||
import ru.fincode.core.database.impl.AppDatabase
|
||||
|
||||
object AppDatabaseBuilder {
|
||||
|
||||
private const val DB_NAME = "tsudesk.db"
|
||||
|
||||
fun build(context: Context): AppDatabase =
|
||||
Room.databaseBuilder(context, AppDatabase::class.java, DB_NAME)
|
||||
.fallbackToDestructiveMigration()
|
||||
.build()
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package ru.fincode.core.database.impl.di
|
||||
|
||||
import android.content.Context
|
||||
import dagger.Module
|
||||
import dagger.Provides
|
||||
import dagger.hilt.InstallIn
|
||||
import dagger.hilt.android.qualifiers.ApplicationContext
|
||||
import dagger.hilt.components.SingletonComponent
|
||||
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
|
||||
@InstallIn(SingletonComponent::class)
|
||||
object DatabaseModule {
|
||||
|
||||
@Provides
|
||||
@Singleton
|
||||
fun provideAppDatabase(@ApplicationContext context: Context): AppDatabase =
|
||||
AppDatabaseBuilder.build(context)
|
||||
|
||||
@Provides
|
||||
fun provideScheduleDao(db: AppDatabase): ScheduleDao =
|
||||
db.scheduleCacheDao()
|
||||
}
|
||||
Reference in New Issue
Block a user