Fix background sync, move DB Helpers into the db class
parent
5c68060bda
commit
2df8d425c8
|
@ -42,7 +42,7 @@ Description: |-
|
|||
|
||||
'''AntiFeatures:'''
|
||||
|
||||
* NonFreeNet - Currently the integration server is not configurable in this client and linked to the non-free New Vector implementation. Free alternatives exists but supporting them in Riot Android is still being worked on.
|
||||
* Tracking - Sentry.io is used for bug reporting. A dialog to disable it and asking to allow to track is not yet implemented
|
||||
|
||||
RepoType: git
|
||||
Repo: https://gitlab.com/Nordgedanken/simplematrix/SimpleMatrix.git
|
||||
|
|
|
@ -1,10 +1,10 @@
|
|||
package blog.nordgedanken.simplematrix.fcm
|
||||
|
||||
import blog.nordgedanken.matrix_android_sdk.account.Account
|
||||
import blog.nordgedanken.simplematrix.data.db.AppDatabase
|
||||
import blog.nordgedanken.simplematrix.data.view.Message
|
||||
import blog.nordgedanken.simplematrix.data.view.MessageFull
|
||||
import blog.nordgedanken.simplematrix.utils.Notification
|
||||
import blog.nordgedanken.simplematrix.utils.db.DBHelpers
|
||||
import com.google.firebase.messaging.FirebaseMessagingService
|
||||
import com.google.firebase.messaging.RemoteMessage
|
||||
import com.orhanobut.logger.Logger
|
||||
|
@ -19,6 +19,7 @@ import org.koin.standalone.KoinComponent
|
|||
*/
|
||||
class SMFirebaseMessagingService : FirebaseMessagingService(), KoinComponent {
|
||||
val account: Account by inject()
|
||||
val db: AppDatabase by inject()
|
||||
|
||||
/**
|
||||
* Called if InstanceID token is updated. This may occur if the security of
|
||||
|
@ -72,7 +73,7 @@ class SMFirebaseMessagingService : FirebaseMessagingService(), KoinComponent {
|
|||
val body = remoteMessage.data["body"]
|
||||
val priority = remoteMessage.data["priority"]
|
||||
|
||||
val room = DBHelpers().getRoomByID(roomID!!)
|
||||
val room = db.getRoomByID(roomID!!)
|
||||
if (room != null) {
|
||||
val tag = room.room?.tag
|
||||
|
||||
|
@ -93,7 +94,7 @@ class SMFirebaseMessagingService : FirebaseMessagingService(), KoinComponent {
|
|||
fullMessage.message = message
|
||||
Logger.d(roomID)
|
||||
Logger.d(sender)
|
||||
val usersDB = DBHelpers().getUsersByRoomIDAndMXIDFromCache(
|
||||
val usersDB = db.getUsersByRoomIDAndMXIDFromCache(
|
||||
message.roomID!!,
|
||||
message.userID!!
|
||||
)
|
||||
|
|
|
@ -24,14 +24,15 @@ import android.widget.ExpandableListAdapter
|
|||
import android.widget.ExpandableListView
|
||||
import android.widget.TextView
|
||||
import androidx.appcompat.app.AppCompatActivity
|
||||
import blog.nordgedanken.simplematrix.data.db.AppDatabase
|
||||
import blog.nordgedanken.simplematrix.data.view.User
|
||||
import blog.nordgedanken.simplematrix.utils.HeaderInfo
|
||||
import blog.nordgedanken.simplematrix.utils.UserDataListAdapter
|
||||
import blog.nordgedanken.simplematrix.utils.db.DBHelpers
|
||||
import com.google.android.material.appbar.CollapsingToolbarLayout
|
||||
import com.google.android.material.snackbar.Snackbar
|
||||
import kotlinx.android.synthetic.main.activity_user_profile.*
|
||||
import org.jetbrains.anko.doAsync
|
||||
import org.koin.android.ext.android.inject
|
||||
|
||||
/**
|
||||
*
|
||||
|
@ -42,6 +43,7 @@ import org.jetbrains.anko.doAsync
|
|||
*/
|
||||
class UserProfile : AppCompatActivity() {
|
||||
private val groupsList = ArrayList<HeaderInfo>()
|
||||
private val db: AppDatabase by inject()
|
||||
|
||||
private var listAdapter: UserDataListAdapter? = null
|
||||
private var expandableListView: ExpandableListView? = null
|
||||
|
@ -61,7 +63,7 @@ class UserProfile : AppCompatActivity() {
|
|||
val mxid = intent.getStringExtra("MXID")
|
||||
val roomID = intent.getStringExtra("roomID")
|
||||
doAsync {
|
||||
user = DBHelpers().getUserByMXIDRoomIDFromCache(mxid, roomID)
|
||||
user = db.getUserByMXIDRoomIDFromCache(mxid, roomID)
|
||||
|
||||
runOnUiThread {
|
||||
setupStrings()
|
||||
|
|
|
@ -24,6 +24,7 @@ import blog.nordgedanken.simplematrix.R
|
|||
import blog.nordgedanken.simplematrix.State
|
||||
import blog.nordgedanken.simplematrix.chatView.recyclerView.MessageList
|
||||
import blog.nordgedanken.simplematrix.chatView.recyclerView.PagingMessageController
|
||||
import blog.nordgedanken.simplematrix.data.db.AppDatabase
|
||||
import blog.nordgedanken.simplematrix.data.matrix.MatrixClient
|
||||
import blog.nordgedanken.simplematrix.data.matrix.sync.processing.JoinedRooms
|
||||
import blog.nordgedanken.simplematrix.data.view.Message
|
||||
|
@ -32,7 +33,6 @@ import blog.nordgedanken.simplematrix.data.view.MessageViewModel
|
|||
import blog.nordgedanken.simplematrix.data.view.RoomFull
|
||||
import blog.nordgedanken.simplematrix.databinding.ActivityChatRoomBinding
|
||||
import blog.nordgedanken.simplematrix.utils.MediaStoreHelper
|
||||
import blog.nordgedanken.simplematrix.utils.db.DBHelpers
|
||||
import blog.nordgedanken.simplematrix.utils.ui.SlideAnimations
|
||||
import com.airbnb.deeplinkdispatch.DeepLink
|
||||
import com.google.gson.JsonObject
|
||||
|
@ -70,6 +70,7 @@ class ChatRoom : AppCompatActivity() {
|
|||
private lateinit var recyclerView: MessageList
|
||||
private val pagingController: PagingMessageController by lazy { PagingMessageController(this@ChatRoom) }
|
||||
private val viewModel: MessageViewModel by inject()
|
||||
private val db: AppDatabase by inject()
|
||||
|
||||
private val slideAnimationsUtil = SlideAnimations()
|
||||
|
||||
|
@ -124,8 +125,8 @@ class ChatRoom : AppCompatActivity() {
|
|||
doAsync {
|
||||
rawRoom = MatrixClient.client?.getRoom(roomID)
|
||||
|
||||
if (DBHelpers().doesRoomExistInDB(roomID)) {
|
||||
room = DBHelpers().getRoomByID(roomID)!!
|
||||
if (db.doesRoomExistInDB(roomID)) {
|
||||
room = db.getRoomByID(roomID)!!
|
||||
|
||||
runOnUiThread {
|
||||
activityBinding.room = room
|
||||
|
@ -151,7 +152,7 @@ class ChatRoom : AppCompatActivity() {
|
|||
val joinError = rawRoom?.tryJoin(viaServer)
|
||||
Logger.d(joinError)
|
||||
if (joinError?.isEmpty!!) {
|
||||
room = DBHelpers().getRoomByID(roomID)!!
|
||||
room = db.getRoomByID(roomID)!!
|
||||
|
||||
runOnUiThread {
|
||||
activityBinding.room = room
|
||||
|
@ -165,7 +166,7 @@ class ChatRoom : AppCompatActivity() {
|
|||
|
||||
}
|
||||
|
||||
if (DBHelpers().getMessageCountByRoomIDFromDB(room.room?.id!!) <= 10) {
|
||||
if (db.getMessageCountByRoomIDFromDB(room.room?.id!!) <= 10) {
|
||||
doAsync {
|
||||
loadMore()
|
||||
}
|
||||
|
@ -237,16 +238,16 @@ class ChatRoom : AppCompatActivity() {
|
|||
val rooms = mutableListOf<RoomFull>()
|
||||
|
||||
room.room?.prevBatchToken = response?.endToken!!
|
||||
val messages = DBHelpers().getMessagesByRoomIDFromDB(room.room?.id!!).toMutableList()
|
||||
val messages = db.getMessagesByRoomIDFromDB(room.room?.id!!).toMutableList()
|
||||
|
||||
val timelineEvents = response.events
|
||||
if (timelineEvents.isNotEmpty()) {
|
||||
// We can save assume that the room exists in the DB
|
||||
JoinedRooms().processTimeline(timelineEvents, room, messages)
|
||||
DBHelpers().saveMessagesToDB(messages.filterNotNull())
|
||||
db.saveMessagesToDB(messages.filterNotNull())
|
||||
rooms.add(room)
|
||||
DBHelpers().saveRoomToDB(rooms)
|
||||
room = DBHelpers().getRoomByID(room.room?.id!!)!!
|
||||
db.saveRoomToDB(rooms)
|
||||
room = db.getRoomByID(room.room?.id!!)!!
|
||||
}
|
||||
}
|
||||
state.loadingBacklogOf.remove(room.room?.id!!)
|
||||
|
@ -367,7 +368,7 @@ class ChatRoom : AppCompatActivity() {
|
|||
|
||||
doAsync {
|
||||
val messages = arrayListOf(messageFull)
|
||||
DBHelpers().saveMessagesToDB(messages)
|
||||
db.saveMessagesToDB(messages)
|
||||
|
||||
val info = JsonObject()
|
||||
info.add("size", JsonPrimitive(MediaStoreHelper.getFileSize(this@ChatRoom, targetUri)))
|
||||
|
@ -383,8 +384,8 @@ class ChatRoom : AppCompatActivity() {
|
|||
|
||||
val id = rawRoom?.sendEvent("m.room.message", event)
|
||||
|
||||
DBHelpers().updateMessageIDInDB(messageFull.message?.id!!, id!!)
|
||||
DBHelpers().updateMessageSendingStatusInDB(id, false)
|
||||
db.updateMessageIDInDB(messageFull.message?.id!!, id!!)
|
||||
db.updateMessageSendingStatusInDB(id, false)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -411,11 +412,11 @@ class ChatRoom : AppCompatActivity() {
|
|||
|
||||
doAsync {
|
||||
val messages = arrayListOf(messageFull)
|
||||
DBHelpers().saveMessagesToDB(messages)
|
||||
db.saveMessagesToDB(messages)
|
||||
val id = rawRoom?.sendFormattedText(parsedText, textS)
|
||||
|
||||
DBHelpers().updateMessageIDInDB(messageFull.message?.id!!, id!!)
|
||||
DBHelpers().updateMessageSendingStatusInDB(id, false)
|
||||
db.updateMessageIDInDB(messageFull.message?.id!!, id!!)
|
||||
db.updateMessageSendingStatusInDB(id, false)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -19,6 +19,7 @@
|
|||
package blog.nordgedanken.simplematrix.data.db
|
||||
|
||||
import android.content.Context
|
||||
import android.database.sqlite.SQLiteConstraintException
|
||||
import androidx.room.Database
|
||||
import androidx.room.Room
|
||||
import androidx.room.RoomDatabase
|
||||
|
@ -29,6 +30,7 @@ import blog.nordgedanken.simplematrix.data.matrix.Sync
|
|||
import blog.nordgedanken.simplematrix.data.matrix.SyncDao
|
||||
import blog.nordgedanken.simplematrix.data.view.*
|
||||
import blog.nordgedanken.simplematrix.utils.converters.Converters
|
||||
import com.orhanobut.logger.Logger
|
||||
|
||||
/**
|
||||
* Created by MTRNord on 01.09.2018.
|
||||
|
@ -99,4 +101,153 @@ abstract class AppDatabase : RoomDatabase() {
|
|||
return sInstance!!
|
||||
}
|
||||
}
|
||||
|
||||
// Helper
|
||||
fun doesRoomExistInDB(roomID: String): Boolean {
|
||||
return this.roomDao().exists(roomID)
|
||||
}
|
||||
|
||||
fun removeRoomFromDB(roomID: String) {
|
||||
this.roomDao().deleteByID(roomID)
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* Saves an [ArrayList] of [RoomFull] to the Database.
|
||||
* It also handles to check if they are new Rooms or existing ones.
|
||||
*
|
||||
*/
|
||||
fun saveRoomToDB(rooms: List<RoomFull>) {
|
||||
val workMapDB = this.roomDao().allIDs()
|
||||
val new: Iterable<blog.nordgedanken.simplematrix.data.view.Room> = rooms.asSequence().filter { it.room?.id !in workMapDB }.mapNotNull { it.room }.asIterable()
|
||||
val update: Iterable<blog.nordgedanken.simplematrix.data.view.Room> = rooms.asSequence().filter { it.room?.id in workMapDB }.mapNotNull { it.room }.asIterable()
|
||||
if (new.firstOrNull() != null) this.roomDao().insertAll(new)
|
||||
if (update.firstOrNull() != null) this.roomDao().updateAll(update)
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* Saves an [List] of [User] to the Database.
|
||||
* It also handles to check if they are new Users or existing ones.
|
||||
*
|
||||
*/
|
||||
fun saveUserToDB(users: List<User>) {
|
||||
val new: Iterable<User> = users.asSequence().filter { it.id == null }.asIterable()
|
||||
val update: Iterable<User> = users.asSequence().filter { it.id != null }.asIterable()
|
||||
if (new.firstOrNull() != null) this.userDao().insertAll(new)
|
||||
if (update.firstOrNull() != null) this.userDao().updateAll(update)
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* Saves an [List] of [MessageFull] to the Database.
|
||||
* It also handles to check if they are new Messages or existing ones.
|
||||
*
|
||||
*/
|
||||
fun saveMessagesToDB(messages: List<MessageFull>) {
|
||||
val workMapDB = this.messageDao().allIDs()
|
||||
val new: Iterable<Message> = messages.asSequence().filter { it.message?.id !in workMapDB }.mapNotNull { it.message }.asIterable()
|
||||
val update: Iterable<Message> = messages.asSequence().filter { it.message?.id in workMapDB }.mapNotNull { it.message }.asIterable()
|
||||
|
||||
try {
|
||||
this.messageDao().insertAll(new)
|
||||
} catch (e: SQLiteConstraintException) {
|
||||
Logger.e("Failed to save new Message to DB: ${e.message}")
|
||||
}
|
||||
|
||||
try {
|
||||
this.messageDao().updateAll(update)
|
||||
} catch (e: SQLiteConstraintException) {
|
||||
Logger.e("Failed to update Message to DB: ${e.message}")
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
fun getMessagesByRoomIDFromDB(roomID: String): List<MessageFull?> {
|
||||
return this.messageDao().loadAllByRoomId(roomID)
|
||||
}
|
||||
|
||||
|
||||
fun getMessageCountByRoomIDFromDB(roomID: String): Int {
|
||||
return this.messageDao().countByRoomID(roomID)
|
||||
}
|
||||
|
||||
/**
|
||||
* Changes the ID of a Message in the Database
|
||||
*/
|
||||
fun updateMessageIDInDB(oldID: String, newID: String) {
|
||||
this.messageDao().updateMessageID(oldID, newID)
|
||||
}
|
||||
|
||||
/**
|
||||
* Changes the sending status of a Message in the Database
|
||||
*/
|
||||
fun updateMessageSendingStatusInDB(id: String, status: Boolean) {
|
||||
this.messageDao().updateMessageSendingStatus(id, status)
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* Checks if a room does already exist in the DB
|
||||
*
|
||||
* @return A [Boolean] indicating if it exists
|
||||
*
|
||||
*/
|
||||
fun roomExistsInCache(roomID: String): Boolean {
|
||||
var exists = false
|
||||
val room = this.roomDao().roomIDByID(roomID)
|
||||
if (room == roomID) {
|
||||
exists = true
|
||||
}
|
||||
return exists
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* Gets a room by it's ID
|
||||
*
|
||||
* @return [RoomFull]
|
||||
*
|
||||
*/
|
||||
fun getRoomByID(roomID: String): RoomFull? {
|
||||
return this.roomDao().roomByID(roomID)
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* Gets Users from the cache.
|
||||
* @return a [List] of [User]
|
||||
*
|
||||
*/
|
||||
fun getUsersFromCache(): List<User> {
|
||||
return this.userDao().all()
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* Gets Users from the cache.
|
||||
* @return a [List] of [User]
|
||||
*
|
||||
*/
|
||||
fun getUserByMXIDRoomIDFromCache(userID: String, roomID: String): User? {
|
||||
return this.userDao().userForMessage(roomID, userID)
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* Gets Users from the cache by the attached roomID.
|
||||
* @return a [List] of [User]
|
||||
*/
|
||||
fun getUsersByRoomIDFromCache(roomID: String): List<User> {
|
||||
return this.userDao().allByRooom(roomID)
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* Gets Users from the cache by the attached roomID and mxid.
|
||||
* @return a [List] of [User]
|
||||
*/
|
||||
fun getUsersByRoomIDAndMXIDFromCache(roomID: String, mxid: String): List<User> {
|
||||
return this.userDao().allByRoomAndMXID(roomID, mxid)
|
||||
}
|
||||
}
|
|
@ -25,7 +25,6 @@ import blog.nordgedanken.simplematrix.data.db.AppDatabase
|
|||
import blog.nordgedanken.simplematrix.data.matrix.sync.SyncStarter
|
||||
import blog.nordgedanken.simplematrix.roomView.RoomsActivity
|
||||
import blog.nordgedanken.simplematrix.utils.AppUtils
|
||||
import blog.nordgedanken.simplematrix.utils.db.DBHelpers
|
||||
import com.facebook.stetho.okhttp3.StethoInterceptor
|
||||
import com.orhanobut.logger.Logger
|
||||
import io.kamax.matrix._MatrixID
|
||||
|
@ -38,13 +37,16 @@ import io.sentry.Sentry
|
|||
import io.sentry.event.BreadcrumbBuilder
|
||||
import okhttp3.OkHttpClient
|
||||
import org.jetbrains.anko.runOnUiThread
|
||||
import org.koin.standalone.KoinComponent
|
||||
import org.koin.standalone.inject
|
||||
import java.net.MalformedURLException
|
||||
import java.net.URL
|
||||
import java.net.UnknownHostException
|
||||
import java.util.concurrent.TimeUnit
|
||||
|
||||
|
||||
class MatrixClient {
|
||||
class MatrixClient : KoinComponent {
|
||||
private val db: AppDatabase by inject()
|
||||
|
||||
enum class LoginResult {
|
||||
Success, UnknownError, ServerNotFound, WrongPasswordOrUserNotFound, WellKnownURLNotFound
|
||||
|
@ -189,7 +191,7 @@ class MatrixClient {
|
|||
// Setup user if empty
|
||||
val knownUser = MatrixClient.client?.user?.get()
|
||||
id = knownUser?.id
|
||||
val user = DBHelpers().getUsersFromCache().firstOrNull { user -> user.MXID == id }
|
||||
val user = db.getUsersFromCache().firstOrNull { user -> user.MXID == id }
|
||||
name = user?.name
|
||||
avatar_url = user?.avatar
|
||||
var mxUser: _MatrixUser? = null
|
||||
|
|
|
@ -89,7 +89,7 @@ class BackgroundSyncService : IntentService("BG_Sync_Service") {
|
|||
val sync = db.syncDao().all[0]
|
||||
val token = syncData.nextBatchToken()!!
|
||||
sync.syncToken = token
|
||||
db.syncDao().insertOne(sync)
|
||||
db.syncDao().updateOne(sync)
|
||||
} else {
|
||||
val sync = Sync()
|
||||
val token = syncData.nextBatchToken()!!
|
||||
|
|
|
@ -1,10 +1,12 @@
|
|||
package blog.nordgedanken.simplematrix.data.matrix.sync.processing
|
||||
|
||||
import blog.nordgedanken.simplematrix.data.db.AppDatabase
|
||||
import blog.nordgedanken.simplematrix.data.view.Room
|
||||
import blog.nordgedanken.simplematrix.data.view.RoomFull
|
||||
import blog.nordgedanken.simplematrix.utils.db.DBHelpers
|
||||
import io.kamax.matrix.event._MatrixAccountDataEvent
|
||||
import io.kamax.matrix.json.event.MatrixJsonDirectEvent
|
||||
import org.koin.standalone.KoinComponent
|
||||
import org.koin.standalone.inject
|
||||
|
||||
/**
|
||||
* GlobalAccountData processes any global account_data from the sync
|
||||
|
@ -16,7 +18,8 @@ import io.kamax.matrix.json.event.MatrixJsonDirectEvent
|
|||
* and needs to be a [List] of type [io.kamax.matrix.event._MatrixAccountDataEvent]
|
||||
*
|
||||
*/
|
||||
class GlobalAccountData(data: List<_MatrixAccountDataEvent>) {
|
||||
class GlobalAccountData(data: List<_MatrixAccountDataEvent>) : KoinComponent {
|
||||
private val db: AppDatabase by inject()
|
||||
/**
|
||||
* init is the entry of this class and gets used to split the data into work chunks
|
||||
*/
|
||||
|
@ -54,7 +57,7 @@ class GlobalAccountData(data: List<_MatrixAccountDataEvent>) {
|
|||
|
||||
for (roomIDJSON in roomArray) {
|
||||
val roomId = roomIDJSON.asString
|
||||
if (!DBHelpers().roomExistsInCache(roomId)) {
|
||||
if (!db.roomExistsInCache(roomId)) {
|
||||
val roomPOJO = RoomFull()
|
||||
roomPOJO.room = Room()
|
||||
roomPOJO.room?.id = roomId
|
||||
|
@ -62,7 +65,7 @@ class GlobalAccountData(data: List<_MatrixAccountDataEvent>) {
|
|||
}
|
||||
}
|
||||
}
|
||||
DBHelpers().saveRoomToDB(rooms)
|
||||
db.saveRoomToDB(rooms)
|
||||
|
||||
// Actual processing
|
||||
rooms.clear()
|
||||
|
@ -74,11 +77,11 @@ class GlobalAccountData(data: List<_MatrixAccountDataEvent>) {
|
|||
val roomID = roomIDJSON.asString
|
||||
|
||||
// We can save assume that the room exists in the DB
|
||||
val room: RoomFull = DBHelpers().getRoomByID(roomID)!!
|
||||
val room: RoomFull = db.getRoomByID(roomID)!!
|
||||
room.room?.tag = "direct"
|
||||
rooms.add(room)
|
||||
}
|
||||
}
|
||||
DBHelpers().saveRoomToDB(rooms)
|
||||
db.saveRoomToDB(rooms)
|
||||
}
|
||||
}
|
|
@ -1,16 +1,18 @@
|
|||
package blog.nordgedanken.simplematrix.data.matrix.sync.processing
|
||||
|
||||
import blog.nordgedanken.simplematrix.data.db.AppDatabase
|
||||
import blog.nordgedanken.simplematrix.data.matrix.additionaltypes.messages.MatrixJsonRoomImageMessageEvent
|
||||
import blog.nordgedanken.simplematrix.data.view.Message
|
||||
import blog.nordgedanken.simplematrix.data.view.MessageFull
|
||||
import blog.nordgedanken.simplematrix.data.view.RoomFull
|
||||
import blog.nordgedanken.simplematrix.data.view.User
|
||||
import blog.nordgedanken.simplematrix.utils.db.DBHelpers
|
||||
import com.orhanobut.logger.Logger
|
||||
import io.kamax.matrix.client._SyncData
|
||||
import io.kamax.matrix.event._MatrixAccountDataEvent
|
||||
import io.kamax.matrix.event._MatrixPersistentEvent
|
||||
import io.kamax.matrix.json.event.*
|
||||
import org.koin.standalone.KoinComponent
|
||||
import org.koin.standalone.inject
|
||||
import java.util.*
|
||||
|
||||
/**
|
||||
|
@ -22,8 +24,9 @@ import java.util.*
|
|||
* @param data All the data of joined rooms which came using the Sync
|
||||
* and needs to be a [Set] of type [io.kamax.matrix.client._SyncData.JoinedRoom]
|
||||
*/
|
||||
class JoinedRooms(data: Set<_SyncData.JoinedRoom>? = null) {
|
||||
class JoinedRooms(data: Set<_SyncData.JoinedRoom>? = null) : KoinComponent{
|
||||
private val rooms = mutableListOf<RoomFull>()
|
||||
private val db: AppDatabase by inject()
|
||||
|
||||
/**
|
||||
* The entry of this class and gets used to split the data into work chunks.
|
||||
|
@ -33,7 +36,7 @@ class JoinedRooms(data: Set<_SyncData.JoinedRoom>? = null) {
|
|||
for (chunk in data) {
|
||||
// TODO more types available
|
||||
val stateEvents = chunk.state.events
|
||||
val currentRoom = DBHelpers().getRoomByID(chunk.id)!!
|
||||
val currentRoom = db.getRoomByID(chunk.id)!!
|
||||
currentRoom.room?.prevBatchToken = chunk.timeline.previousBatchToken ?: ""
|
||||
val messages = mutableListOf<MessageFull?>()
|
||||
|
||||
|
@ -49,10 +52,10 @@ class JoinedRooms(data: Set<_SyncData.JoinedRoom>? = null) {
|
|||
if (accountDataEvents.isNotEmpty()) {
|
||||
processAccountData(accountDataEvents, currentRoom)
|
||||
}
|
||||
DBHelpers().saveMessagesToDB(messages.filterNotNull())
|
||||
db.saveMessagesToDB(messages.filterNotNull())
|
||||
rooms.add(currentRoom)
|
||||
}
|
||||
DBHelpers().saveRoomToDB(rooms)
|
||||
db.saveRoomToDB(rooms)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -82,7 +85,7 @@ class JoinedRooms(data: Set<_SyncData.JoinedRoom>? = null) {
|
|||
is MatrixJsonRoomMessageEvent -> messages.add(processMessages(event, room))
|
||||
}
|
||||
}
|
||||
DBHelpers().saveUserToDB(users.filterNotNull())
|
||||
db.saveUserToDB(users.filterNotNull())
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -110,7 +113,7 @@ class JoinedRooms(data: Set<_SyncData.JoinedRoom>? = null) {
|
|||
is MatrixJsonRoomAvatarEvent -> processAvatar(event, room)
|
||||
}
|
||||
}
|
||||
DBHelpers().saveUserToDB(users.filterNotNull())
|
||||
db.saveUserToDB(users.filterNotNull())
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -260,7 +263,7 @@ class JoinedRooms(data: Set<_SyncData.JoinedRoom>? = null) {
|
|||
val membershipType = event.membership
|
||||
val id = event.invitee
|
||||
|
||||
val dbUser = DBHelpers().getUsersByRoomIDFromCache(room.room?.id!!)
|
||||
val dbUser = db.getUsersByRoomIDFromCache(room.room?.id!!)
|
||||
val userFromCache = dbUser.firstOrNull { it.MXID == id.id }
|
||||
return if (userFromCache != null) {
|
||||
// We seem to already know the User. We just need to update it.
|
||||
|
|
|
@ -1,7 +1,9 @@
|
|||
package blog.nordgedanken.simplematrix.data.matrix.sync.processing
|
||||
|
||||
import blog.nordgedanken.simplematrix.utils.db.DBHelpers
|
||||
import blog.nordgedanken.simplematrix.data.db.AppDatabase
|
||||
import io.kamax.matrix.client._SyncData
|
||||
import org.koin.standalone.KoinComponent
|
||||
import org.koin.standalone.inject
|
||||
|
||||
/**
|
||||
* LeftRooms processes any left rooms from the Sync
|
||||
|
@ -12,14 +14,15 @@ import io.kamax.matrix.client._SyncData
|
|||
* @param data Data means all the data of left rooms which came using the Sync
|
||||
* and needs to be a [Set] of type [io.kamax.matrix.client._SyncData.LeftRoom]
|
||||
*/
|
||||
class LeftRooms(data: Set<_SyncData.LeftRoom>) {
|
||||
class LeftRooms(data: Set<_SyncData.LeftRoom>) : KoinComponent {
|
||||
private val db: AppDatabase by inject()
|
||||
|
||||
/**
|
||||
* init is the entry of this class and gets used to split the data into work chunks
|
||||
*/
|
||||
init {
|
||||
for (chunk in data) {
|
||||
DBHelpers().removeRoomFromDB(chunk.id)
|
||||
/**
|
||||
* init is the entry of this class and gets used to split the data into work chunks
|
||||
*/
|
||||
init {
|
||||
for (chunk in data) {
|
||||
db.removeRoomFromDB(chunk.id)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,10 +1,12 @@
|
|||
package blog.nordgedanken.simplematrix.data.matrix.sync.processing
|
||||
|
||||
import blog.nordgedanken.simplematrix.data.db.AppDatabase
|
||||
import blog.nordgedanken.simplematrix.data.view.Room
|
||||
import blog.nordgedanken.simplematrix.data.view.RoomFull
|
||||
import blog.nordgedanken.simplematrix.utils.db.DBHelpers
|
||||
import com.orhanobut.logger.Logger
|
||||
import io.kamax.matrix.client._SyncData
|
||||
import org.koin.standalone.KoinComponent
|
||||
import org.koin.standalone.inject
|
||||
|
||||
/**
|
||||
* The SyncProcessing class handles the processing from
|
||||
|
@ -17,7 +19,8 @@ import io.kamax.matrix.client._SyncData
|
|||
* @param data Data means all the Data which came using the Sync
|
||||
* and needs to be of type [io.kamax.matrix.client._SyncData]
|
||||
*/
|
||||
class SyncProcessing(data: _SyncData) {
|
||||
class SyncProcessing(data: _SyncData) : KoinComponent{
|
||||
private val db: AppDatabase by inject()
|
||||
|
||||
/**
|
||||
* This checks if either of the possible sync streams exists and than delegates the chunk
|
||||
|
@ -30,14 +33,14 @@ class SyncProcessing(data: _SyncData) {
|
|||
val rooms = mutableListOf<RoomFull>()
|
||||
for (room in joined) {
|
||||
val roomId = room.id
|
||||
if (!DBHelpers().roomExistsInCache(roomId)) {
|
||||
if (!db.roomExistsInCache(roomId)) {
|
||||
val roomPOJO = RoomFull()
|
||||
roomPOJO.room = Room()
|
||||
roomPOJO.room?.id = roomId
|
||||
rooms.add(roomPOJO)
|
||||
}
|
||||
}
|
||||
DBHelpers().saveRoomToDB(rooms)
|
||||
db.saveRoomToDB(rooms)
|
||||
JoinedRooms(joined)
|
||||
}
|
||||
|
||||
|
|
|
@ -36,7 +36,6 @@ import blog.nordgedanken.simplematrix.data.db.AppDatabase
|
|||
import blog.nordgedanken.simplematrix.data.view.MessageFull
|
||||
import blog.nordgedanken.simplematrix.data.view.Notification
|
||||
import blog.nordgedanken.simplematrix.roomView.RoomsActivity
|
||||
import blog.nordgedanken.simplematrix.utils.db.DBHelpers
|
||||
import com.orhanobut.logger.Logger
|
||||
import org.koin.standalone.KoinComponent
|
||||
import org.koin.standalone.inject
|
||||
|
@ -62,8 +61,8 @@ class Notification : KoinComponent {
|
|||
message: MessageFull
|
||||
) {
|
||||
val id = Date().time.toInt()
|
||||
val room = DBHelpers().getRoomByID(message.message?.roomID!!)!!
|
||||
val ownUser = DBHelpers().getUserByMXIDRoomIDFromCache(
|
||||
val room = db.getRoomByID(message.message?.roomID!!)!!
|
||||
val ownUser = db.getUserByMXIDRoomIDFromCache(
|
||||
account.client.context?.user?.get()?.id!!,
|
||||
message.message?.roomID!!
|
||||
)
|
||||
|
@ -76,7 +75,7 @@ class Notification : KoinComponent {
|
|||
style.isGroupConversation = true
|
||||
}
|
||||
|
||||
//FIXME use sharedPreference as db is too slow
|
||||
// FIXME use sharedPreference as db is too slow
|
||||
val oldNotification = db.notificationDao().notificationByRoomID(message.message?.roomID!!)
|
||||
if (oldNotification != null) {
|
||||
val messages = db.messageDao().loadAllByIds(oldNotification.messageIDs)
|
||||
|
|
|
@ -1,166 +0,0 @@
|
|||
package blog.nordgedanken.simplematrix.utils.db
|
||||
|
||||
import android.database.sqlite.SQLiteConstraintException
|
||||
import blog.nordgedanken.simplematrix.data.db.AppDatabase
|
||||
import blog.nordgedanken.simplematrix.data.view.*
|
||||
import com.orhanobut.logger.Logger
|
||||
import org.koin.standalone.KoinComponent
|
||||
import org.koin.standalone.inject
|
||||
|
||||
/**
|
||||
* Helps to do common Database tasks by minifying boilerplate code in other classes.
|
||||
*
|
||||
* @author Marcel Radzio
|
||||
*
|
||||
*/
|
||||
class DBHelpers : KoinComponent {
|
||||
val db: AppDatabase by inject()
|
||||
|
||||
fun doesRoomExistInDB(roomID: String): Boolean {
|
||||
return db.roomDao().exists(roomID)
|
||||
}
|
||||
|
||||
fun removeRoomFromDB(roomID: String) {
|
||||
db.roomDao().deleteByID(roomID)
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* Saves an [ArrayList] of [RoomFull] to the Database.
|
||||
* It also handles to check if they are new Rooms or existing ones.
|
||||
*
|
||||
*/
|
||||
fun saveRoomToDB(rooms: List<RoomFull>) {
|
||||
val workMapDB = db.roomDao().allIDs()
|
||||
val new: Iterable<Room> = rooms.asSequence().filter { it.room?.id !in workMapDB }.mapNotNull { it.room }.asIterable()
|
||||
val update: Iterable<Room> = rooms.asSequence().filter { it.room?.id in workMapDB }.mapNotNull { it.room }.asIterable()
|
||||
if (new.firstOrNull() != null) db.roomDao().insertAll(new)
|
||||
if (update.firstOrNull() != null) db.roomDao().updateAll(update)
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* Saves an [List] of [User] to the Database.
|
||||
* It also handles to check if they are new Users or existing ones.
|
||||
*
|
||||
*/
|
||||
fun saveUserToDB(users: List<User>) {
|
||||
val new: Iterable<User> = users.asSequence().filter { it.id == null }.asIterable()
|
||||
val update: Iterable<User> = users.asSequence().filter { it.id != null }.asIterable()
|
||||
if (new.firstOrNull() != null) db.userDao().insertAll(new)
|
||||
if (update.firstOrNull() != null) db.userDao().updateAll(update)
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* Saves an [List] of [MessageFull] to the Database.
|
||||
* It also handles to check if they are new Messages or existing ones.
|
||||
*
|
||||
*/
|
||||
fun saveMessagesToDB(messages: List<MessageFull>) {
|
||||
val workMapDB = db.messageDao().allIDs()
|
||||
val new: Iterable<Message> = messages.asSequence().filter { it.message?.id !in workMapDB }.mapNotNull { it.message }.asIterable()
|
||||
val update: Iterable<Message> = messages.asSequence().filter { it.message?.id in workMapDB }.mapNotNull { it.message }.asIterable()
|
||||
|
||||
try {
|
||||
db.messageDao().insertAll(new)
|
||||
} catch (e: SQLiteConstraintException) {
|
||||
Logger.e("Failed to save new Message to DB: ${e.message}")
|
||||
}
|
||||
|
||||
try {
|
||||
db.messageDao().updateAll(update)
|
||||
} catch (e: SQLiteConstraintException) {
|
||||
Logger.e("Failed to update Message to DB: ${e.message}")
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
fun getMessagesByRoomIDFromDB(roomID: String): List<MessageFull?> {
|
||||
return db.messageDao().loadAllByRoomId(roomID)
|
||||
}
|
||||
|
||||
|
||||
fun getMessageCountByRoomIDFromDB(roomID: String): Int {
|
||||
return db.messageDao().countByRoomID(roomID)
|
||||
}
|
||||
|
||||
/**
|
||||
* Changes the ID of a Message in the Database
|
||||
*/
|
||||
fun updateMessageIDInDB(oldID: String, newID: String) {
|
||||
db.messageDao().updateMessageID(oldID, newID)
|
||||
}
|
||||
|
||||
/**
|
||||
* Changes the sending status of a Message in the Database
|
||||
*/
|
||||
fun updateMessageSendingStatusInDB(id: String, status: Boolean) {
|
||||
db.messageDao().updateMessageSendingStatus(id, status)
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* Checks if a room does already exist in the DB
|
||||
*
|
||||
* @return A [Boolean] indicating if it exists
|
||||
*
|
||||
*/
|
||||
fun roomExistsInCache(roomID: String): Boolean {
|
||||
var exists = false
|
||||
val room = db.roomDao().roomIDByID(roomID)
|
||||
if (room == roomID) {
|
||||
exists = true
|
||||
}
|
||||
return exists
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* Gets a room by it's ID
|
||||
*
|
||||
* @return [RoomFull]
|
||||
*
|
||||
*/
|
||||
fun getRoomByID(roomID: String): RoomFull? {
|
||||
return db.roomDao().roomByID(roomID)
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* Gets Users from the cache.
|
||||
* @return a [List] of [User]
|
||||
*
|
||||
*/
|
||||
fun getUsersFromCache(): List<User> {
|
||||
return db.userDao().all()
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* Gets Users from the cache.
|
||||
* @return a [List] of [User]
|
||||
*
|
||||
*/
|
||||
fun getUserByMXIDRoomIDFromCache(userID: String, roomID: String): User? {
|
||||
return db.userDao().userForMessage(roomID, userID)
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* Gets Users from the cache by the attached roomID.
|
||||
* @return a [List] of [User]
|
||||
*/
|
||||
fun getUsersByRoomIDFromCache(roomID: String): List<User> {
|
||||
return db.userDao().allByRooom(roomID)
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* Gets Users from the cache by the attached roomID and mxid.
|
||||
* @return a [List] of [User]
|
||||
*/
|
||||
fun getUsersByRoomIDAndMXIDFromCache(roomID: String, mxid: String): List<User> {
|
||||
return db.userDao().allByRoomAndMXID(roomID, mxid)
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue