Add Room database, create tables and DAO for schedule
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
plugins {
|
||||
alias(libs.plugins.android.library)
|
||||
alias(libs.plugins.kotlin.android)
|
||||
alias(libs.plugins.kotlin.kapt)
|
||||
}
|
||||
|
||||
android {
|
||||
@@ -16,8 +17,7 @@ android {
|
||||
release {
|
||||
isMinifyEnabled = false
|
||||
proguardFiles(
|
||||
getDefaultProguardFile("proguard-android-optimize.txt"),
|
||||
"proguard-rules.pro"
|
||||
getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro"
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -30,7 +30,14 @@ android {
|
||||
jvmTarget = jvm.toString()
|
||||
}
|
||||
}
|
||||
|
||||
kapt {
|
||||
correctErrorTypes = true
|
||||
}
|
||||
dependencies {
|
||||
implementation(libs.core.ktx)
|
||||
implementation(libs.room.runtime)
|
||||
implementation(libs.room.ktx)
|
||||
kapt(libs.room.compiler)
|
||||
implementation(libs.hiltandroid)
|
||||
kapt(libs.hilt.compiler)
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package ru.fincode.tsudesk.core.database
|
||||
|
||||
import androidx.room.Database
|
||||
import androidx.room.RoomDatabase
|
||||
import ru.fincode.tsudesk.core.database.schedule.ScheduleDao
|
||||
import ru.fincode.tsudesk.core.database.schedule.LessonCacheEntity
|
||||
import ru.fincode.tsudesk.core.database.schedule.ScheduleCacheEntity
|
||||
|
||||
@Database(
|
||||
entities = [
|
||||
// schedule feature
|
||||
ScheduleCacheEntity::class,
|
||||
LessonCacheEntity::class,
|
||||
|
||||
// future:
|
||||
// NewsEntity::class,
|
||||
// GradeEntity::class,
|
||||
],
|
||||
version = 1,
|
||||
exportSchema = false
|
||||
)
|
||||
abstract class AppDatabase : RoomDatabase() {
|
||||
abstract fun scheduleCacheDao(): ScheduleDao
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package ru.fincode.tsudesk.core.database.di
|
||||
|
||||
import android.content.Context
|
||||
import androidx.room.Room
|
||||
import dagger.Module
|
||||
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.schedule.ScheduleDao
|
||||
import javax.inject.Singleton
|
||||
|
||||
@Module
|
||||
@InstallIn(SingletonComponent::class)
|
||||
object DatabaseModule {
|
||||
|
||||
private const val DB_NAME = "tsudesk.db"
|
||||
|
||||
@Provides
|
||||
@Singleton
|
||||
fun provideAppDatabase(@ApplicationContext context: Context): AppDatabase =
|
||||
Room.databaseBuilder(context, AppDatabase::class.java, DB_NAME)
|
||||
.fallbackToDestructiveMigration()
|
||||
.build()
|
||||
|
||||
@Provides
|
||||
fun provideScheduleCacheDao(db: AppDatabase): ScheduleDao =
|
||||
db.scheduleCacheDao()
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package ru.fincode.tsudesk.core.database.schedule
|
||||
|
||||
object ScheduleDbConstants {
|
||||
const val SCHEDULE_TABLE = "schedule_cache"
|
||||
const val LESSON_TABLE = "lesson_cache"
|
||||
|
||||
const val COL_SCHEDULE_KEY = "scheduleKey"
|
||||
const val COL_KEY = "key"
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package ru.fincode.tsudesk.core.database.schedule
|
||||
|
||||
import androidx.room.Entity
|
||||
import androidx.room.Index
|
||||
import androidx.room.PrimaryKey
|
||||
import ru.fincode.tsudesk.core.database.schedule.ScheduleDbConstants.COL_SCHEDULE_KEY
|
||||
import ru.fincode.tsudesk.core.database.schedule.ScheduleDbConstants.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 groupId: String,
|
||||
val type: String
|
||||
)
|
||||
@@ -0,0 +1,12 @@
|
||||
package ru.fincode.tsudesk.core.database.schedule
|
||||
|
||||
import androidx.room.Entity
|
||||
import androidx.room.PrimaryKey
|
||||
import ru.fincode.tsudesk.core.database.schedule.ScheduleDbConstants.SCHEDULE_TABLE
|
||||
|
||||
@Entity(tableName = SCHEDULE_TABLE)
|
||||
data class ScheduleCacheEntity(
|
||||
@PrimaryKey
|
||||
val key: String, // "group:220631" | "teacher:ФИО"
|
||||
val timestamp: Long
|
||||
)
|
||||
@@ -0,0 +1,57 @@
|
||||
package ru.fincode.tsudesk.core.database.schedule
|
||||
|
||||
import androidx.room.Dao
|
||||
import androidx.room.Delete
|
||||
import androidx.room.Insert
|
||||
import androidx.room.OnConflictStrategy
|
||||
import androidx.room.Query
|
||||
import androidx.room.Transaction
|
||||
|
||||
@Dao
|
||||
interface ScheduleDao {
|
||||
|
||||
@Insert(onConflict = OnConflictStrategy.REPLACE)
|
||||
suspend fun upsertSchedule(schedule: ScheduleCacheEntity)
|
||||
|
||||
@Insert(onConflict = OnConflictStrategy.REPLACE)
|
||||
suspend fun insertLessons(lessons: List<LessonCacheEntity>)
|
||||
|
||||
|
||||
@Query("SELECT * FROM schedule_cache WHERE `key` = :key LIMIT 1")
|
||||
suspend fun getSchedule(key: String): ScheduleCacheEntity?
|
||||
|
||||
@Query("SELECT * FROM lesson_cache WHERE scheduleKey = :key")
|
||||
suspend fun getLessons(key: String): List<LessonCacheEntity>
|
||||
|
||||
// Bulk delete уроков по ключу — самый простой/эффективный путь в Room это @Query
|
||||
@Query("DELETE FROM lesson_cache WHERE scheduleKey = :key")
|
||||
suspend fun deleteLessonsByKey(key: String)
|
||||
|
||||
// Удаление schedule_cache без SQL: типобезопасно
|
||||
@Delete
|
||||
suspend fun deleteSchedule(entity: ScheduleCacheEntity)
|
||||
|
||||
/**
|
||||
* Обновить кэш расписания (header + lessons) атомарно.
|
||||
*/
|
||||
@Transaction
|
||||
suspend fun replaceSchedule(
|
||||
key: String,
|
||||
schedule: ScheduleCacheEntity,
|
||||
lessons: List<LessonCacheEntity>
|
||||
) {
|
||||
upsertSchedule(schedule)
|
||||
deleteLessonsByKey(key)
|
||||
insertLessons(lessons)
|
||||
}
|
||||
|
||||
/**
|
||||
* Полная очистка кэша по ключу без прямого DELETE для schedule_cache.
|
||||
*/
|
||||
@Transaction
|
||||
suspend fun clearSchedule(key: String) {
|
||||
deleteLessonsByKey(key)
|
||||
val header = getSchedule(key) ?: return
|
||||
deleteSchedule(header)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user