Dark theme
This commit is contained in:
@@ -0,0 +1,20 @@
|
||||
package com.meloda.fast.database
|
||||
|
||||
import androidx.room.Database
|
||||
import androidx.room.RoomDatabase
|
||||
import com.meloda.fast.api.model.*
|
||||
import com.meloda.fast.database.dao.*
|
||||
|
||||
@Database(
|
||||
entities = [VKConversation::class, VKMessage::class, VKUser::class, VKGroup::class, VKFriend::class],
|
||||
version = 1,
|
||||
exportSchema = false
|
||||
)
|
||||
|
||||
abstract class AppDatabase : RoomDatabase() {
|
||||
abstract val conversations: ConversationsDao
|
||||
abstract val messages: MessagesDao
|
||||
abstract val users: UsersDao
|
||||
abstract val groups: GroupsDao
|
||||
abstract val friends: FriendsDao
|
||||
}
|
||||
@@ -0,0 +1,176 @@
|
||||
package com.meloda.fast.database
|
||||
|
||||
import android.util.SparseArray
|
||||
import androidx.annotation.WorkerThread
|
||||
import com.meloda.fast.api.model.*
|
||||
import com.meloda.fast.common.AppGlobal
|
||||
|
||||
object MemoryCache {
|
||||
|
||||
private val users = SparseArray<VKUser>()
|
||||
private val groups = SparseArray<VKGroup>()
|
||||
|
||||
@WorkerThread
|
||||
fun getUserById(id: Int): VKUser? {
|
||||
var user = users[id]
|
||||
if (user == null) {
|
||||
user = AppGlobal.database.users.getById(id)
|
||||
|
||||
user?.let { append(it) }
|
||||
}
|
||||
return user
|
||||
}
|
||||
|
||||
@WorkerThread
|
||||
fun getGroupById(positiveId: Int): VKGroup? {
|
||||
var group = groups[positiveId]
|
||||
if (group == null) {
|
||||
group = AppGlobal.database.groups.getById(positiveId)
|
||||
|
||||
group?.let { append(it) }
|
||||
}
|
||||
return group
|
||||
}
|
||||
|
||||
@WorkerThread
|
||||
fun getMessageById(id: Int): VKMessage? {
|
||||
return AppGlobal.database.messages.getById(id)
|
||||
}
|
||||
|
||||
@WorkerThread
|
||||
fun getMessagesByPeerId(peerId: Int): List<VKMessage> {
|
||||
return AppGlobal.database.messages.getByPeerId(peerId)
|
||||
}
|
||||
|
||||
@WorkerThread
|
||||
fun getMessages(): List<VKMessage> {
|
||||
return AppGlobal.database.messages.getAll()
|
||||
}
|
||||
|
||||
@WorkerThread
|
||||
fun getConversationById(id: Int): VKConversation? {
|
||||
return AppGlobal.database.conversations.getById(id)
|
||||
}
|
||||
|
||||
@WorkerThread
|
||||
fun getConversations(): List<VKConversation> {
|
||||
return AppGlobal.database.conversations.getAll()
|
||||
}
|
||||
|
||||
@WorkerThread
|
||||
fun getFriends(userId: Int): List<VKFriend> {
|
||||
return AppGlobal.database.friends.getByUserId(userId)
|
||||
}
|
||||
|
||||
fun appendUsers(users: Collection<VKUser>) {
|
||||
for (user in users) {
|
||||
append(user)
|
||||
}
|
||||
}
|
||||
|
||||
fun appendGroups(groups: Collection<VKGroup>) {
|
||||
for (group in groups) {
|
||||
append(group)
|
||||
}
|
||||
}
|
||||
|
||||
fun append(value: VKGroup) {
|
||||
groups.append(value.groupId, value)
|
||||
}
|
||||
|
||||
fun append(value: VKUser) {
|
||||
users.append(value.userId, value)
|
||||
}
|
||||
|
||||
@WorkerThread
|
||||
fun put(value: VKUser) {
|
||||
append(value)
|
||||
|
||||
AppGlobal.database.users.insert(value)
|
||||
}
|
||||
|
||||
@WorkerThread
|
||||
fun putUsers(users: List<VKUser>) {
|
||||
appendUsers(users)
|
||||
|
||||
AppGlobal.database.users.insert(users)
|
||||
}
|
||||
|
||||
@WorkerThread
|
||||
fun put(value: VKFriend) {
|
||||
AppGlobal.database.friends.insert(value)
|
||||
}
|
||||
|
||||
@WorkerThread
|
||||
fun putFriends(friends: List<VKFriend>) {
|
||||
AppGlobal.database.friends.insert(friends)
|
||||
}
|
||||
|
||||
@WorkerThread
|
||||
fun put(value: VKMessage) {
|
||||
AppGlobal.database.messages.insert(value)
|
||||
}
|
||||
|
||||
@WorkerThread
|
||||
fun putMessages(messages: List<VKMessage>) {
|
||||
AppGlobal.database.messages.insert(messages)
|
||||
}
|
||||
|
||||
@WorkerThread
|
||||
fun put(value: VKGroup) {
|
||||
append(value)
|
||||
|
||||
AppGlobal.database.groups.insert(value)
|
||||
}
|
||||
|
||||
@WorkerThread
|
||||
fun putGroups(groups: List<VKGroup>) {
|
||||
appendGroups(groups)
|
||||
|
||||
AppGlobal.database.groups.insert(groups)
|
||||
}
|
||||
|
||||
@WorkerThread
|
||||
fun put(value: VKConversation) {
|
||||
AppGlobal.database.conversations.insert(value)
|
||||
}
|
||||
|
||||
@WorkerThread
|
||||
fun putConversations(conversations: List<VKConversation>) {
|
||||
AppGlobal.database.conversations.insert(conversations)
|
||||
}
|
||||
|
||||
@WorkerThread
|
||||
fun deleteMessage(messageId: Int, safe: Boolean = true) {
|
||||
if (safe) {
|
||||
AppGlobal.database.messages.getById(messageId) ?: return
|
||||
}
|
||||
|
||||
AppGlobal.database.messages.deleteById(messageId)
|
||||
}
|
||||
|
||||
@WorkerThread
|
||||
fun deleteConversation(conversationId: Int, safe: Boolean = true) {
|
||||
if (safe) {
|
||||
AppGlobal.database.conversations.getById(conversationId) ?: return
|
||||
}
|
||||
|
||||
AppGlobal.database.conversations.deleteById(conversationId)
|
||||
}
|
||||
|
||||
@WorkerThread
|
||||
fun edit(message: VKMessage?, safe: Boolean = true) {
|
||||
message ?: return
|
||||
|
||||
if (safe) {
|
||||
AppGlobal.database.messages.getById(message.messageId) ?: return
|
||||
}
|
||||
|
||||
AppGlobal.database.messages.update(message)
|
||||
}
|
||||
|
||||
fun clear() {
|
||||
users.clear()
|
||||
groups.clear()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package com.meloda.fast.database.dao
|
||||
|
||||
import androidx.room.*
|
||||
import com.meloda.fast.api.model.VKConversation
|
||||
|
||||
@Dao
|
||||
interface ConversationsDao {
|
||||
|
||||
@Query("SELECT * FROM conversations")
|
||||
fun getAll(): List<VKConversation>
|
||||
|
||||
@Query("SELECT * FROM conversations WHERE conversationId = :id")
|
||||
fun getById(id: Int): VKConversation?
|
||||
|
||||
@Insert(onConflict = OnConflictStrategy.REPLACE)
|
||||
fun insert(item: VKConversation)
|
||||
|
||||
@Insert(onConflict = OnConflictStrategy.REPLACE)
|
||||
fun insert(items: List<VKConversation>)
|
||||
|
||||
@Update
|
||||
fun update(item: VKConversation)
|
||||
|
||||
@Update
|
||||
fun update(items: List<VKConversation>)
|
||||
|
||||
@Delete
|
||||
fun delete(item: VKConversation)
|
||||
|
||||
@Delete
|
||||
fun delete(items: List<VKConversation>)
|
||||
|
||||
@Query("DELETE FROM conversations WHERE conversationId = :id")
|
||||
fun deleteById(id: Int)
|
||||
|
||||
@Query("DELETE FROM conversations")
|
||||
fun clear()
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package com.meloda.fast.database.dao
|
||||
|
||||
import androidx.room.*
|
||||
import com.meloda.fast.api.model.VKFriend
|
||||
|
||||
@Dao
|
||||
interface FriendsDao {
|
||||
|
||||
@Query("SELECT * FROM friends")
|
||||
fun getAll(): List<VKFriend>
|
||||
|
||||
@Query("SELECT * FROM friends WHERE userId = :id")
|
||||
fun getById(id: Int): VKFriend?
|
||||
|
||||
@Insert(onConflict = OnConflictStrategy.REPLACE)
|
||||
fun insert(item: VKFriend)
|
||||
|
||||
@Insert(onConflict = OnConflictStrategy.REPLACE)
|
||||
fun insert(items: List<VKFriend>)
|
||||
|
||||
@Update
|
||||
fun update(item: VKFriend)
|
||||
|
||||
@Update
|
||||
fun update(items: List<VKFriend>)
|
||||
|
||||
@Delete
|
||||
fun delete(item: VKFriend)
|
||||
|
||||
@Delete
|
||||
fun delete(items: List<VKFriend>)
|
||||
|
||||
@Query("DELETE FROM friends WHERE userId = :id")
|
||||
fun deleteById(id: Int)
|
||||
|
||||
@Query("DELETE FROM friends")
|
||||
fun clear()
|
||||
|
||||
@Query("SELECT * FROM friends WHERE userId = :id")
|
||||
fun getByUserId(id: Int): List<VKFriend>
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package com.meloda.fast.database.dao
|
||||
|
||||
import androidx.room.*
|
||||
import com.meloda.fast.api.model.VKGroup
|
||||
|
||||
@Dao
|
||||
interface GroupsDao {
|
||||
@Query("SELECT * FROM groups")
|
||||
fun getAll(): List<VKGroup>
|
||||
|
||||
@Query("SELECT * FROM groups WHERE groupId = :id")
|
||||
fun getById(id: Int): VKGroup?
|
||||
|
||||
@Insert(onConflict = OnConflictStrategy.REPLACE)
|
||||
fun insert(item: VKGroup)
|
||||
|
||||
@Insert(onConflict = OnConflictStrategy.REPLACE)
|
||||
fun insert(items: List<VKGroup>)
|
||||
|
||||
@Update
|
||||
fun update(item: VKGroup)
|
||||
|
||||
@Update
|
||||
fun update(items: List<VKGroup>)
|
||||
|
||||
@Delete
|
||||
fun delete(item: VKGroup)
|
||||
|
||||
@Delete
|
||||
fun delete(items: List<VKGroup>)
|
||||
|
||||
@Query("DELETE FROM groups WHERE groupId = :id")
|
||||
fun deleteById(id: Int)
|
||||
|
||||
@Query("DELETE FROM groups")
|
||||
fun clear()
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package com.meloda.fast.database.dao
|
||||
|
||||
import androidx.room.*
|
||||
import com.meloda.fast.api.model.VKMessage
|
||||
|
||||
@Dao
|
||||
interface MessagesDao {
|
||||
|
||||
@Query("SELECT * FROM messages")
|
||||
fun getAll(): List<VKMessage>
|
||||
|
||||
@Query("SELECT * FROM messages WHERE messageId = :id")
|
||||
fun getById(id: Int): VKMessage?
|
||||
|
||||
@Insert(onConflict = OnConflictStrategy.REPLACE)
|
||||
fun insert(item: VKMessage)
|
||||
|
||||
@Insert(onConflict = OnConflictStrategy.REPLACE)
|
||||
fun insert(items: List<VKMessage>)
|
||||
|
||||
@Update
|
||||
fun update(item: VKMessage)
|
||||
|
||||
@Update
|
||||
fun update(items: List<VKMessage>)
|
||||
|
||||
@Delete
|
||||
fun delete(item: VKMessage)
|
||||
|
||||
@Delete
|
||||
fun delete(items: List<VKMessage>)
|
||||
|
||||
@Query("DELETE FROM messages WHERE messageId = :id")
|
||||
fun deleteById(id: Int)
|
||||
|
||||
@Query("DELETE FROM messages")
|
||||
fun clear()
|
||||
|
||||
@Query("SELECT * FROM messages WHERE peerId = :peerId")
|
||||
fun getByPeerId(peerId: Int): List<VKMessage>
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package com.meloda.fast.database.dao
|
||||
|
||||
import androidx.room.*
|
||||
import com.meloda.fast.api.model.VKUser
|
||||
|
||||
@Dao
|
||||
interface UsersDao {
|
||||
|
||||
@Query("SELECT * FROM users")
|
||||
fun getAll(): List<VKUser>
|
||||
|
||||
@Query("SELECT * FROM users WHERE userId = :id")
|
||||
fun getById(id: Int): VKUser?
|
||||
|
||||
@Insert(onConflict = OnConflictStrategy.REPLACE)
|
||||
fun insert(item: VKUser)
|
||||
|
||||
@Insert(onConflict = OnConflictStrategy.REPLACE)
|
||||
fun insert(items: List<VKUser>)
|
||||
|
||||
@Update
|
||||
fun update(item: VKUser)
|
||||
|
||||
@Update
|
||||
fun update(items: List<VKUser>)
|
||||
|
||||
@Delete
|
||||
fun delete(item: VKUser)
|
||||
|
||||
@Delete
|
||||
fun delete(items: List<VKUser>)
|
||||
|
||||
@Query("DELETE FROM users WHERE userId = :id")
|
||||
fun deleteById(id: Int)
|
||||
|
||||
@Query("DELETE FROM users")
|
||||
fun clear()
|
||||
}
|
||||
+24
@@ -0,0 +1,24 @@
|
||||
package com.meloda.fast.database.dao.converters
|
||||
|
||||
import androidx.room.TypeConverter
|
||||
import com.meloda.fast.api.model.VKModel
|
||||
import com.meloda.fast.extensions.ArrayExtensions.isNullOrEmpty
|
||||
import com.meloda.fast.util.Utils
|
||||
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
class ArrayListToByteArrayConverter {
|
||||
|
||||
@TypeConverter
|
||||
fun toForwarded(data: ByteArray?): ArrayList<VKModel> {
|
||||
return if (data.isNullOrEmpty()) arrayListOf() else {
|
||||
val deserializedData = Utils.deserialize(data)
|
||||
if (deserializedData == null) arrayListOf() else deserializedData as ArrayList<VKModel>
|
||||
}
|
||||
}
|
||||
|
||||
@TypeConverter
|
||||
fun fromForwarded(forwarded: List<VKModel>): ByteArray {
|
||||
return Utils.serialize(forwarded) ?: return byteArrayOf()
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package com.meloda.fast.database.dao.converters
|
||||
|
||||
import androidx.room.TypeConverter
|
||||
import com.meloda.fast.api.model.VKMessage
|
||||
import com.meloda.fast.extensions.ArrayExtensions.isNullOrEmpty
|
||||
import com.meloda.fast.util.Utils
|
||||
import java.util.*
|
||||
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
class ForwardedConverter {
|
||||
|
||||
@TypeConverter
|
||||
fun toForwarded(data: ByteArray?): ArrayList<VKMessage> {
|
||||
return if (data.isNullOrEmpty()) arrayListOf() else {
|
||||
val deserializedData = Utils.deserialize(data)
|
||||
if (deserializedData == null) arrayListOf() else deserializedData as ArrayList<VKMessage>
|
||||
}
|
||||
}
|
||||
|
||||
@TypeConverter
|
||||
fun fromForwarded(forwarded: List<VKMessage>): ByteArray {
|
||||
return Utils.serialize(forwarded) ?: return byteArrayOf()
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user