Refactoring db module structure

This commit is contained in:
Shcherbatykh Oleg
2026-02-26 17:15:55 +03:00
parent 37c926ac77
commit 12ce668afb
26 changed files with 283 additions and 102 deletions

View 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)
}

View File

21
core/database/api/proguard-rules.pro vendored Normal file
View 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

View File

@@ -0,0 +1,13 @@
package ru.fincode.tsudesk.core.database.api.schedule
object Constants {
const val SCHEDULE_TABLE = "schedule_cache"
const val LESSON_TABLE = "lesson_cache"
const val COL_SCHEDULE_KEY = "scheduleKey"
const val COL_KEY = "key"
const val GROUP_PRE_KEY = "group:"
const val TEACHER_PRE_KEY = "teacher:"
}

View File

@@ -0,0 +1,25 @@
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.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])]
)
data class LessonCacheEntity(
@PrimaryKey(autoGenerate = true) val id: Long = 0,
val scheduleKey: String,
val date: String,
val time: String,
val name: String,
val typeName: String,
val room: String,
val teacher: String,
val groupIds: List<String>,
val type: String
)

View File

@@ -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"
}

View File

@@ -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
)

View File

@@ -0,0 +1,57 @@
package ru.fincode.tsudesk.core.database.api.schedule
import androidx.room.Dao
import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.Query
import androidx.room.Transaction
import kotlinx.coroutines.flow.Flow
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 {
@Query(SELECT_SCHEDULE_BY_KEY_QUERY)
fun observeSchedule(key: String): Flow<ScheduleCacheEntity?>
@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_LESSON_BY_KEY_QUERY)
suspend fun getLessons(key: String): List<LessonCacheEntity>
@Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun upsertSchedule(schedule: ScheduleCacheEntity)
@Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun insertLessons(lessons: List<LessonCacheEntity>)
@Query(DELETE_LESSON_BY_KEY_QUERY)
suspend fun deleteLessonsByKey(key: String)
@Query(DELETE_SCHEDULE_BY_KEY_QUERY)
suspend fun deleteScheduleByKey(key: String)
@Transaction
suspend fun replaceSchedule(
key: String,
schedule: ScheduleCacheEntity,
lessons: List<LessonCacheEntity>
) {
deleteLessonsByKey(key)
upsertSchedule(schedule)
insertLessons(lessons)
}
@Transaction
suspend fun clearSchedule(key: String) {
deleteLessonsByKey(key)
deleteScheduleByKey(key)
}
}