forked from melod1n/fast-messenger
Upstream changes (#23)
This commit is contained in:
@@ -0,0 +1 @@
|
||||
/build
|
||||
@@ -0,0 +1,44 @@
|
||||
plugins {
|
||||
alias(libs.plugins.android.library)
|
||||
alias(libs.plugins.org.jetbrains.kotlin.android)
|
||||
alias(libs.plugins.com.google.devtools.ksp)
|
||||
}
|
||||
|
||||
group = "com.meloda.app.fast.database"
|
||||
|
||||
android {
|
||||
namespace = "com.meloda.app.fast.database"
|
||||
compileSdk = Configs.compileSdk
|
||||
|
||||
defaultConfig {
|
||||
minSdk = Configs.minSdk
|
||||
}
|
||||
|
||||
ksp {
|
||||
arg("room.schemaLocation", "$projectDir/schemas")
|
||||
arg("room.generateKotlin", "true")
|
||||
}
|
||||
|
||||
buildTypes {
|
||||
release {
|
||||
isMinifyEnabled = false
|
||||
}
|
||||
}
|
||||
compileOptions {
|
||||
sourceCompatibility = Configs.java
|
||||
targetCompatibility = Configs.java
|
||||
}
|
||||
kotlinOptions {
|
||||
jvmTarget = Configs.java.toString()
|
||||
}
|
||||
}
|
||||
|
||||
dependencies {
|
||||
api(projects.core.model)
|
||||
|
||||
implementation(libs.room.ktx)
|
||||
implementation(libs.room.runtime)
|
||||
ksp(libs.room.compiler)
|
||||
|
||||
implementation(libs.koin.android)
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
|
||||
</manifest>
|
||||
@@ -0,0 +1,16 @@
|
||||
package com.meloda.app.fast.database
|
||||
|
||||
import androidx.room.Database
|
||||
import androidx.room.RoomDatabase
|
||||
import com.meloda.app.fast.database.dao.AccountDao
|
||||
import com.meloda.app.fast.model.database.AccountEntity
|
||||
|
||||
@Database(
|
||||
entities = [AccountEntity::class],
|
||||
version = 2
|
||||
)
|
||||
abstract class AccountsDatabase : RoomDatabase() {
|
||||
abstract fun accountDao(): AccountDao
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
package com.meloda.app.fast.database
|
||||
|
||||
import androidx.room.Database
|
||||
import androidx.room.RoomDatabase
|
||||
import androidx.room.TypeConverters
|
||||
import com.meloda.app.fast.database.dao.ConversationDao
|
||||
import com.meloda.app.fast.database.dao.GroupDao
|
||||
import com.meloda.app.fast.database.dao.MessageDao
|
||||
import com.meloda.app.fast.database.dao.UsersDao
|
||||
import com.meloda.app.fast.database.typeconverters.Converters
|
||||
import com.meloda.app.fast.model.database.VkConversationEntity
|
||||
import com.meloda.app.fast.model.database.VkGroupEntity
|
||||
import com.meloda.app.fast.model.database.VkMessageEntity
|
||||
import com.meloda.app.fast.model.database.VkUserEntity
|
||||
|
||||
@Database(
|
||||
entities = [
|
||||
VkUserEntity::class,
|
||||
VkGroupEntity::class,
|
||||
VkMessageEntity::class,
|
||||
VkConversationEntity::class
|
||||
],
|
||||
|
||||
version = 5
|
||||
)
|
||||
@TypeConverters(Converters::class)
|
||||
abstract class CacheDatabase : RoomDatabase() {
|
||||
abstract fun userDao(): UsersDao
|
||||
abstract fun groupDao(): GroupDao
|
||||
abstract fun messageDao(): MessageDao
|
||||
abstract fun conversationDao(): ConversationDao
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package com.meloda.app.fast.database.dao
|
||||
|
||||
import androidx.room.Dao
|
||||
import androidx.room.Query
|
||||
import com.meloda.app.fast.model.database.AccountEntity
|
||||
|
||||
@Dao
|
||||
abstract class AccountDao : EntityDao<AccountEntity> {
|
||||
|
||||
@Query("SELECT * FROM accounts")
|
||||
abstract suspend fun getAll(): List<AccountEntity>
|
||||
|
||||
@Query("DELETE FROM accounts WHERE userId = :userId")
|
||||
abstract suspend fun deleteById(userId: Int)
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package com.meloda.app.fast.database.dao
|
||||
|
||||
import androidx.room.Dao
|
||||
import androidx.room.Query
|
||||
import androidx.room.Transaction
|
||||
import com.meloda.app.fast.model.database.ConversationWithMessage
|
||||
import com.meloda.app.fast.model.database.VkConversationEntity
|
||||
|
||||
@Dao
|
||||
abstract class ConversationDao : EntityDao<VkConversationEntity> {
|
||||
|
||||
@Query("SELECT * FROM conversations")
|
||||
abstract suspend fun getAll(): List<VkConversationEntity>
|
||||
|
||||
@Query("SELECT * FROM conversations WHERE id IN (:ids)")
|
||||
abstract suspend fun getAllByIds(ids: List<Int>): List<VkConversationEntity>
|
||||
|
||||
@Query("SELECT * FROM conversations WHERE id IS (:id)")
|
||||
abstract suspend fun getById(id: Int): VkConversationEntity?
|
||||
|
||||
@Transaction
|
||||
@Query("SELECT * FROM conversations WHERE id IS (:id)")
|
||||
abstract suspend fun getByIdWithMessage(id: Int): ConversationWithMessage?
|
||||
|
||||
@Query("DELETE FROM conversations WHERE rowid IN (:ids)")
|
||||
abstract suspend fun deleteByIds(ids: List<Int>): Int
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.meloda.app.fast.database.dao
|
||||
|
||||
import androidx.room.Delete
|
||||
import androidx.room.Insert
|
||||
import androidx.room.OnConflictStrategy
|
||||
|
||||
interface EntityDao<T> {
|
||||
|
||||
@Insert(onConflict = OnConflictStrategy.REPLACE)
|
||||
suspend fun insertAll(values: List<T>)
|
||||
|
||||
@Insert(onConflict = OnConflictStrategy.REPLACE)
|
||||
suspend fun insert(value: T)
|
||||
|
||||
@Delete
|
||||
suspend fun delete(value: T): Int
|
||||
|
||||
@Delete
|
||||
suspend fun deleteAll(values: List<T>): Int
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package com.meloda.app.fast.database.dao
|
||||
|
||||
import androidx.room.Dao
|
||||
import androidx.room.Query
|
||||
import com.meloda.app.fast.model.database.VkGroupEntity
|
||||
|
||||
@Dao
|
||||
abstract class GroupDao : EntityDao<VkGroupEntity> {
|
||||
|
||||
@Query("SELECT * FROM groups")
|
||||
abstract suspend fun getAll(): List<VkGroupEntity>
|
||||
|
||||
@Query("SELECT * FROM groups WHERE id IN (:ids)")
|
||||
abstract suspend fun getAllByIds(ids: List<Int>): List<VkGroupEntity>
|
||||
|
||||
@Query("DELETE FROM groups WHERE id IN (:ids)")
|
||||
abstract suspend fun deleteByIds(ids: List<Int>): Int
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package com.meloda.app.fast.database.dao
|
||||
|
||||
import androidx.room.Dao
|
||||
import androidx.room.Query
|
||||
import com.meloda.app.fast.model.database.VkMessageEntity
|
||||
|
||||
@Dao
|
||||
abstract class MessageDao : EntityDao<VkMessageEntity> {
|
||||
|
||||
@Query("SELECT * FROM messages")
|
||||
abstract suspend fun getAll(): List<VkMessageEntity>
|
||||
|
||||
@Query("SELECT * FROM messages WHERE peerId IS (:conversationId)")
|
||||
abstract suspend fun getAll(conversationId: Int): List<VkMessageEntity>
|
||||
|
||||
@Query("SELECT * FROM messages WHERE id IN (:ids)")
|
||||
abstract suspend fun getAllByIds(ids: List<Int>): List<VkMessageEntity>
|
||||
|
||||
@Query("SELECT * FROM messages WHERE id IS (:messageId)")
|
||||
abstract suspend fun getById(messageId: Int): VkMessageEntity?
|
||||
|
||||
@Query("DELETE FROM messages WHERE id IN (:ids)")
|
||||
abstract suspend fun deleteByIds(ids: List<Int>): Int
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package com.meloda.app.fast.database.dao
|
||||
|
||||
import androidx.room.Dao
|
||||
import androidx.room.Query
|
||||
import com.meloda.app.fast.model.database.VkUserEntity
|
||||
|
||||
@Dao
|
||||
abstract class UsersDao : EntityDao<VkUserEntity> {
|
||||
|
||||
@Query("SELECT * FROM users")
|
||||
abstract suspend fun getAll(): List<VkUserEntity>
|
||||
|
||||
@Query("SELECT * FROM users WHERE id IN (:ids)")
|
||||
abstract suspend fun getAllByIds(ids: List<Int>): List<VkUserEntity>
|
||||
|
||||
@Query("DELETE FROM users WHERE id IN (:ids)")
|
||||
abstract suspend fun deleteByIds(ids: List<Int>): Int
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package com.meloda.app.fast.database.di
|
||||
|
||||
import androidx.room.Room
|
||||
import com.meloda.app.fast.database.AccountsDatabase
|
||||
import org.koin.core.scope.Scope
|
||||
import org.koin.dsl.module
|
||||
|
||||
val databaseModule = module {
|
||||
single {
|
||||
Room.databaseBuilder(get(), AccountsDatabase::class.java, "accounts").build()
|
||||
}
|
||||
single { get<AccountsDatabase>().accountDao() }
|
||||
|
||||
single {
|
||||
Room.databaseBuilder(get(), com.meloda.app.fast.database.CacheDatabase::class.java, "cache")
|
||||
.fallbackToDestructiveMigration()
|
||||
.build()
|
||||
}
|
||||
single { cacheDB().userDao() }
|
||||
single { cacheDB().groupDao() }
|
||||
single { cacheDB().messageDao() }
|
||||
single { cacheDB().conversationDao() }
|
||||
}
|
||||
|
||||
private fun Scope.cacheDB(): com.meloda.app.fast.database.CacheDatabase = get()
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
package com.meloda.app.fast.database.typeconverters
|
||||
|
||||
import androidx.room.TypeConverter
|
||||
|
||||
class Converters {
|
||||
|
||||
@TypeConverter
|
||||
fun intListToString(list: List<Int>): String = list.joinToString()
|
||||
|
||||
@TypeConverter
|
||||
fun stringToIntList(string: String): List<Int> =
|
||||
string
|
||||
.split(", ")
|
||||
.mapNotNull(String::toIntOrNull)
|
||||
|
||||
@TypeConverter
|
||||
fun stringListToString(list: List<String>): String = list.joinToString()
|
||||
|
||||
@TypeConverter
|
||||
fun stringToStringList(string: String): List<String> = string.split(", ")
|
||||
}
|
||||
Reference in New Issue
Block a user