Lab 05: add remote keys

This commit is contained in:
abazov73 2023-12-05 23:31:27 +04:00
parent 80318e7d8f
commit 213b67fdfa
8 changed files with 147 additions and 10 deletions

View File

@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="deploymentTargetDropDown">
<targetSelectedWithDropDown>
<Target>
<type value="QUICK_BOOT_TARGET" />
<deviceKey>
<Key>
<type value="VIRTUAL_DEVICE_PATH" />
<value value="C:\Users\Андрей\.android\avd\Pixel_7_Pro_API_30.avd" />
</Key>
</deviceKey>
</Target>
</targetSelectedWithDropDown>
<timeTargetWasSelectedWithDropDown value="2023-12-05T19:20:06.168865100Z" />
</component>
</project>

View File

@ -0,0 +1,41 @@
<component name="InspectionProjectProfileManager">
<profile version="1.0">
<option name="myName" value="Project Default" />
<inspection_tool class="PreviewAnnotationInFunctionWithParameters" enabled="true" level="ERROR" enabled_by_default="true">
<option name="composableFile" value="true" />
<option name="previewFile" value="true" />
</inspection_tool>
<inspection_tool class="PreviewApiLevelMustBeValid" enabled="true" level="ERROR" enabled_by_default="true">
<option name="composableFile" value="true" />
<option name="previewFile" value="true" />
</inspection_tool>
<inspection_tool class="PreviewDimensionRespectsLimit" enabled="true" level="WARNING" enabled_by_default="true">
<option name="composableFile" value="true" />
<option name="previewFile" value="true" />
</inspection_tool>
<inspection_tool class="PreviewFontScaleMustBeGreaterThanZero" enabled="true" level="ERROR" enabled_by_default="true">
<option name="composableFile" value="true" />
<option name="previewFile" value="true" />
</inspection_tool>
<inspection_tool class="PreviewMultipleParameterProviders" enabled="true" level="ERROR" enabled_by_default="true">
<option name="composableFile" value="true" />
<option name="previewFile" value="true" />
</inspection_tool>
<inspection_tool class="PreviewMustBeTopLevelFunction" enabled="true" level="ERROR" enabled_by_default="true">
<option name="composableFile" value="true" />
<option name="previewFile" value="true" />
</inspection_tool>
<inspection_tool class="PreviewNeedsComposableAnnotation" enabled="true" level="ERROR" enabled_by_default="true">
<option name="composableFile" value="true" />
<option name="previewFile" value="true" />
</inspection_tool>
<inspection_tool class="PreviewNotSupportedInUnitTestFiles" enabled="true" level="ERROR" enabled_by_default="true">
<option name="composableFile" value="true" />
<option name="previewFile" value="true" />
</inspection_tool>
<inspection_tool class="PreviewPickerAnnotation" enabled="true" level="ERROR" enabled_by_default="true">
<option name="composableFile" value="true" />
<option name="previewFile" value="true" />
</inspection_tool>
</profile>
</component>

View File

@ -10,7 +10,7 @@ android {
defaultConfig { defaultConfig {
applicationId = "com.example.mobile_labs" applicationId = "com.example.mobile_labs"
minSdk = 26 minSdk = 30
targetSdk = 33 targetSdk = 33
versionCode = 1 versionCode = 1
versionName = "1.0" versionName = "1.0"

View File

@ -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.performance.model.PerformancePersonCrossRef
import com.example.mobile_labs.database.person.dao.PersonDao import com.example.mobile_labs.database.person.dao.PersonDao
import com.example.mobile_labs.database.person.model.Person 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.dao.UserDao
import com.example.mobile_labs.user.model.User import com.example.mobile_labs.user.model.User
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
@ -20,13 +22,14 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import java.time.LocalDate 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 class AppDatabase : RoomDatabase() {
abstract fun personDao(): PersonDao abstract fun personDao(): PersonDao
abstract fun performanceDao(): PerformanceDao abstract fun performanceDao(): PerformanceDao
abstract fun eventDao(): EventDao abstract fun eventDao(): EventDao
abstract fun performancePersonDao(): PerformancePersonDao abstract fun performancePersonDao(): PerformancePersonDao
abstract fun userDao(): UserDao abstract fun userDao(): UserDao
abstract fun remoteKeysDao(): RemoteKeysDao
companion object { companion object {
private const val DB_NAME: String = "theatre-db" private const val DB_NAME: String = "theatre-db"
@ -102,14 +105,14 @@ abstract class AppDatabase : RoomDatabase() {
AppDatabase::class.java, AppDatabase::class.java,
DB_NAME DB_NAME
) )
.addCallback(object : RoomDatabase.Callback() { // .addCallback(object : RoomDatabase.Callback() {
override fun onCreate(db: SupportSQLiteDatabase) { // override fun onCreate(db: SupportSQLiteDatabase) {
super.onCreate(db) // super.onCreate(db)
CoroutineScope(Dispatchers.IO).launch { // CoroutineScope(Dispatchers.IO).launch {
populateDatabase() // populateDatabase()
} // }
} // }
}) // })
.build() .build()
.also { INSTANCE = it } .also { INSTANCE = it }
} }

View File

@ -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<RemoteKeys>)
@Query("DELETE FROM remote_keys WHERE type = :type")
suspend fun clearRemoteKeys(type: RemoteKeyType)
}

View File

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

View File

@ -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<RemoteKeys>) =
remoteKeysDao.insertAll(remoteKeys)
override suspend fun deleteRemoteKey(type: RemoteKeyType) =
remoteKeysDao.clearRemoteKeys(type)
}

View File

@ -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<RemoteKeys>)
suspend fun deleteRemoteKey(type: RemoteKeyType)
}