From 213b67fdfa5334ce91a8ae47fc0da366804d5543 Mon Sep 17 00:00:00 2001
From: abazov73 <92822431+abazov73@users.noreply.github.com>
Date: Tue, 5 Dec 2023 23:31:27 +0400
Subject: [PATCH] Lab 05: add remote keys
---
.idea/deploymentTargetDropDown.xml | 17 ++++++++
.idea/inspectionProfiles/Project_Default.xml | 41 +++++++++++++++++++
app/build.gradle.kts | 2 +-
.../mobile_labs/database/AppDatabase.kt | 21 ++++++----
.../database/remotekeys/dao/RemoteKeysDao.kt | 20 +++++++++
.../database/remotekeys/model/RemoteKeys.kt | 30 ++++++++++++++
.../repository/OfflineRemoteKeyRepository.kt | 16 ++++++++
.../repository/RemoteKeyRepository.kt | 10 +++++
8 files changed, 147 insertions(+), 10 deletions(-)
create mode 100644 .idea/deploymentTargetDropDown.xml
create mode 100644 .idea/inspectionProfiles/Project_Default.xml
create mode 100644 app/src/main/java/com/example/mobile_labs/database/remotekeys/dao/RemoteKeysDao.kt
create mode 100644 app/src/main/java/com/example/mobile_labs/database/remotekeys/model/RemoteKeys.kt
create mode 100644 app/src/main/java/com/example/mobile_labs/database/remotekeys/repository/OfflineRemoteKeyRepository.kt
create mode 100644 app/src/main/java/com/example/mobile_labs/database/remotekeys/repository/RemoteKeyRepository.kt
diff --git a/.idea/deploymentTargetDropDown.xml b/.idea/deploymentTargetDropDown.xml
new file mode 100644
index 0000000..ac823c7
--- /dev/null
+++ b/.idea/deploymentTargetDropDown.xml
@@ -0,0 +1,17 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml
new file mode 100644
index 0000000..44ca2d9
--- /dev/null
+++ b/.idea/inspectionProfiles/Project_Default.xml
@@ -0,0 +1,41 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/app/build.gradle.kts b/app/build.gradle.kts
index ee3ebe4..66803e2 100644
--- a/app/build.gradle.kts
+++ b/app/build.gradle.kts
@@ -10,7 +10,7 @@ android {
defaultConfig {
applicationId = "com.example.mobile_labs"
- minSdk = 26
+ minSdk = 30
targetSdk = 33
versionCode = 1
versionName = "1.0"
diff --git a/app/src/main/java/com/example/mobile_labs/database/AppDatabase.kt b/app/src/main/java/com/example/mobile_labs/database/AppDatabase.kt
index afcd584..3637726 100644
--- a/app/src/main/java/com/example/mobile_labs/database/AppDatabase.kt
+++ b/app/src/main/java/com/example/mobile_labs/database/AppDatabase.kt
@@ -13,6 +13,8 @@ import com.example.mobile_labs.database.performance.model.Performance
import com.example.mobile_labs.database.performance.model.PerformancePersonCrossRef
import com.example.mobile_labs.database.person.dao.PersonDao
import com.example.mobile_labs.database.person.model.Person
+import com.example.mobile_labs.database.remotekeys.dao.RemoteKeysDao
+import com.example.mobile_labs.database.remotekeys.model.RemoteKeys
import com.example.mobile_labs.user.dao.UserDao
import com.example.mobile_labs.user.model.User
import kotlinx.coroutines.CoroutineScope
@@ -20,13 +22,14 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import java.time.LocalDate
-@Database(entities = [Performance::class, Person::class, Event::class, PerformancePersonCrossRef::class, User::class], version = 1, exportSchema = false)
+@Database(entities = [Performance::class, Person::class, Event::class, PerformancePersonCrossRef::class, User::class, RemoteKeys::class], version = 1, exportSchema = false)
abstract class AppDatabase : RoomDatabase() {
abstract fun personDao(): PersonDao
abstract fun performanceDao(): PerformanceDao
abstract fun eventDao(): EventDao
abstract fun performancePersonDao(): PerformancePersonDao
abstract fun userDao(): UserDao
+ abstract fun remoteKeysDao(): RemoteKeysDao
companion object {
private const val DB_NAME: String = "theatre-db"
@@ -102,14 +105,14 @@ abstract class AppDatabase : RoomDatabase() {
AppDatabase::class.java,
DB_NAME
)
- .addCallback(object : RoomDatabase.Callback() {
- override fun onCreate(db: SupportSQLiteDatabase) {
- super.onCreate(db)
- CoroutineScope(Dispatchers.IO).launch {
- populateDatabase()
- }
- }
- })
+// .addCallback(object : RoomDatabase.Callback() {
+// override fun onCreate(db: SupportSQLiteDatabase) {
+// super.onCreate(db)
+// CoroutineScope(Dispatchers.IO).launch {
+// populateDatabase()
+// }
+// }
+// })
.build()
.also { INSTANCE = it }
}
diff --git a/app/src/main/java/com/example/mobile_labs/database/remotekeys/dao/RemoteKeysDao.kt b/app/src/main/java/com/example/mobile_labs/database/remotekeys/dao/RemoteKeysDao.kt
new file mode 100644
index 0000000..f1fd0a6
--- /dev/null
+++ b/app/src/main/java/com/example/mobile_labs/database/remotekeys/dao/RemoteKeysDao.kt
@@ -0,0 +1,20 @@
+package com.example.mobile_labs.database.remotekeys.dao
+
+import androidx.room.Dao
+import androidx.room.Insert
+import androidx.room.OnConflictStrategy
+import androidx.room.Query
+import com.example.mobile_labs.database.remotekeys.model.RemoteKeyType
+import com.example.mobile_labs.database.remotekeys.model.RemoteKeys
+
+@Dao
+interface RemoteKeysDao {
+ @Query("SELECT * FROM remote_keys WHERE entityId = :entityId AND type = :type")
+ suspend fun getRemoteKeys(entityId: Int, type: RemoteKeyType): RemoteKeys?
+
+ @Insert(onConflict = OnConflictStrategy.REPLACE)
+ suspend fun insertAll(remoteKey: List)
+
+ @Query("DELETE FROM remote_keys WHERE type = :type")
+ suspend fun clearRemoteKeys(type: RemoteKeyType)
+}
\ No newline at end of file
diff --git a/app/src/main/java/com/example/mobile_labs/database/remotekeys/model/RemoteKeys.kt b/app/src/main/java/com/example/mobile_labs/database/remotekeys/model/RemoteKeys.kt
new file mode 100644
index 0000000..a381a86
--- /dev/null
+++ b/app/src/main/java/com/example/mobile_labs/database/remotekeys/model/RemoteKeys.kt
@@ -0,0 +1,30 @@
+package com.example.mobile_labs.database.remotekeys.model
+
+import android.app.Person
+import androidx.room.Entity
+import androidx.room.PrimaryKey
+import androidx.room.TypeConverter
+import androidx.room.TypeConverters
+import com.example.mobile_labs.database.event.model.Event
+import com.example.mobile_labs.database.performance.model.Performance
+
+enum class RemoteKeyType(private val type: String) {
+ PERSON(Person::class.simpleName ?: "Person"),
+ EVENT(Event::class.simpleName ?: "Event"),
+ PERFORMANCE(Performance::class.simpleName ?: "Performances");
+
+ @TypeConverter
+ fun toRemoteKeyType(value: String) = RemoteKeyType.values().first { it.type == value }
+
+ @TypeConverter
+ fun fromRemoteKeyType(value: RemoteKeyType) = value.type
+}
+
+@Entity(tableName = "remote_keys")
+data class RemoteKeys(
+ @PrimaryKey val entityId: Int,
+ @TypeConverters(RemoteKeyType::class)
+ val type: RemoteKeyType,
+ val prevKey: Int?,
+ val nextKey: Int?
+)
diff --git a/app/src/main/java/com/example/mobile_labs/database/remotekeys/repository/OfflineRemoteKeyRepository.kt b/app/src/main/java/com/example/mobile_labs/database/remotekeys/repository/OfflineRemoteKeyRepository.kt
new file mode 100644
index 0000000..ccf08e2
--- /dev/null
+++ b/app/src/main/java/com/example/mobile_labs/database/remotekeys/repository/OfflineRemoteKeyRepository.kt
@@ -0,0 +1,16 @@
+package com.example.mobile_labs.database.remotekeys.repository
+
+import com.example.mobile_labs.database.remotekeys.dao.RemoteKeysDao
+import com.example.mobile_labs.database.remotekeys.model.RemoteKeyType
+import com.example.mobile_labs.database.remotekeys.model.RemoteKeys
+
+class OfflineRemoteKeyRepository(private val remoteKeysDao: RemoteKeysDao) : RemoteKeyRepository {
+ override suspend fun getAllRemoteKeys(id: Int, type: RemoteKeyType) =
+ remoteKeysDao.getRemoteKeys(id, type)
+
+ override suspend fun createRemoteKeys(remoteKeys: List) =
+ remoteKeysDao.insertAll(remoteKeys)
+
+ override suspend fun deleteRemoteKey(type: RemoteKeyType) =
+ remoteKeysDao.clearRemoteKeys(type)
+}
\ No newline at end of file
diff --git a/app/src/main/java/com/example/mobile_labs/database/remotekeys/repository/RemoteKeyRepository.kt b/app/src/main/java/com/example/mobile_labs/database/remotekeys/repository/RemoteKeyRepository.kt
new file mode 100644
index 0000000..faedf72
--- /dev/null
+++ b/app/src/main/java/com/example/mobile_labs/database/remotekeys/repository/RemoteKeyRepository.kt
@@ -0,0 +1,10 @@
+package com.example.mobile_labs.database.remotekeys.repository
+
+import com.example.mobile_labs.database.remotekeys.model.RemoteKeyType
+import com.example.mobile_labs.database.remotekeys.model.RemoteKeys
+
+interface RemoteKeyRepository {
+ suspend fun getAllRemoteKeys(id: Int, type: RemoteKeyType): RemoteKeys?
+ suspend fun createRemoteKeys(remoteKeys: List)
+ suspend fun deleteRemoteKey(type: RemoteKeyType)
+}
\ No newline at end of file