forked from upstream-mirrors/quik
Merge pull request #529 from Inhishonor/sync
Refactor sync code to use an async transaction/separate looper thread
This commit is contained in:
@@ -21,6 +21,8 @@ package dev.octoshrimpy.quik.repository
|
|||||||
import android.content.ContentResolver
|
import android.content.ContentResolver
|
||||||
import android.content.ContentUris
|
import android.content.ContentUris
|
||||||
import android.net.Uri
|
import android.net.Uri
|
||||||
|
import android.os.Handler
|
||||||
|
import android.os.HandlerThread
|
||||||
import android.provider.Telephony
|
import android.provider.Telephony
|
||||||
import com.f2prateek.rx.preferences2.RxSharedPreferences
|
import com.f2prateek.rx.preferences2.RxSharedPreferences
|
||||||
import dev.octoshrimpy.quik.extensions.forEach
|
import dev.octoshrimpy.quik.extensions.forEach
|
||||||
@@ -49,6 +51,7 @@ import io.reactivex.subjects.Subject
|
|||||||
import io.realm.Realm
|
import io.realm.Realm
|
||||||
import io.realm.RealmList
|
import io.realm.RealmList
|
||||||
import io.realm.Sort
|
import io.realm.Sort
|
||||||
|
import timber.log.Timber
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
import javax.inject.Singleton
|
import javax.inject.Singleton
|
||||||
|
|
||||||
@@ -73,146 +76,171 @@ class SyncRepositoryImpl @Inject constructor(
|
|||||||
|
|
||||||
override fun syncMessages() {
|
override fun syncMessages() {
|
||||||
|
|
||||||
|
val oldBlockedSenders = rxPrefs.getStringSet("pref_key_blocked_senders")
|
||||||
|
|
||||||
// If the sync is already running, don't try to do another one
|
// If the sync is already running, don't try to do another one
|
||||||
if (syncProgress.blockingFirst() is SyncRepository.SyncProgress.Running) return
|
if (syncProgress.blockingFirst() is SyncRepository.SyncProgress.Running) return
|
||||||
syncProgress.onNext(SyncRepository.SyncProgress.Running(0, 0, true))
|
syncProgress.onNext(SyncRepository.SyncProgress.Running(0, 0, true))
|
||||||
|
|
||||||
val realm = Realm.getDefaultInstance()
|
val handlerThread = HandlerThread("RealmSyncThread")
|
||||||
realm.beginTransaction()
|
handlerThread.start()
|
||||||
|
Handler(handlerThread.looper).post {
|
||||||
|
Realm.getDefaultInstance().executeTransactionAsync(
|
||||||
|
{ realm ->
|
||||||
|
// Prepare existing conversation data
|
||||||
|
val persistedData = realm.copyFromRealm(
|
||||||
|
realm.where(Conversation::class.java)
|
||||||
|
.beginGroup()
|
||||||
|
.equalTo("archived", true)
|
||||||
|
.or()
|
||||||
|
.equalTo("blocked", true)
|
||||||
|
.or()
|
||||||
|
.equalTo("pinned", true)
|
||||||
|
.or()
|
||||||
|
.isNotEmpty("name")
|
||||||
|
.or()
|
||||||
|
.isNotNull("blockingClient")
|
||||||
|
.or()
|
||||||
|
.isNotEmpty("blockReason")
|
||||||
|
.endGroup()
|
||||||
|
.findAll()
|
||||||
|
).associateBy { conversation -> conversation.id }.toMutableMap()
|
||||||
|
|
||||||
val persistedData = realm.copyFromRealm(realm.where(Conversation::class.java)
|
removeOldMessages(realm)
|
||||||
.beginGroup()
|
|
||||||
.equalTo("archived", true)
|
|
||||||
.or()
|
|
||||||
.equalTo("blocked", true)
|
|
||||||
.or()
|
|
||||||
.equalTo("pinned", true)
|
|
||||||
.or()
|
|
||||||
.isNotEmpty("name")
|
|
||||||
.or()
|
|
||||||
.isNotNull("blockingClient")
|
|
||||||
.or()
|
|
||||||
.isNotEmpty("blockReason")
|
|
||||||
.endGroup()
|
|
||||||
.findAll())
|
|
||||||
.associateBy { conversation -> conversation.id }
|
|
||||||
.toMutableMap()
|
|
||||||
|
|
||||||
realm.delete(Contact::class.java)
|
keys.reset()
|
||||||
realm.delete(ContactGroup::class.java)
|
|
||||||
realm.delete(Conversation::class.java)
|
|
||||||
realm.delete(Message::class.java)
|
|
||||||
realm.delete(MmsPart::class.java)
|
|
||||||
realm.delete(Recipient::class.java)
|
|
||||||
|
|
||||||
keys.reset()
|
val partsCursor = cursorToPart.getPartsCursor()
|
||||||
|
val messageCursor = cursorToMessage.getMessagesCursor()
|
||||||
|
val conversationCursor = cursorToConversation.getConversationsCursor()
|
||||||
|
val recipientCursor = cursorToRecipient.getRecipientCursor()
|
||||||
|
|
||||||
val partsCursor = cursorToPart.getPartsCursor()
|
val max = (partsCursor?.count ?: 0) +
|
||||||
val messageCursor = cursorToMessage.getMessagesCursor()
|
(messageCursor?.count ?: 0) +
|
||||||
val conversationCursor = cursorToConversation.getConversationsCursor()
|
(conversationCursor?.count ?: 0) +
|
||||||
val recipientCursor = cursorToRecipient.getRecipientCursor()
|
(recipientCursor?.count ?: 0)
|
||||||
|
|
||||||
val max = (partsCursor?.count ?: 0) +
|
var progress = 0
|
||||||
(messageCursor?.count ?: 0) +
|
|
||||||
(conversationCursor?.count ?: 0) +
|
|
||||||
(recipientCursor?.count ?: 0)
|
|
||||||
|
|
||||||
var progress = 0
|
// Sync message parts
|
||||||
|
partsCursor?.use {
|
||||||
// Sync message parts
|
partsCursor.forEach { cursor ->
|
||||||
partsCursor?.use {
|
tryOrNull {
|
||||||
partsCursor.forEach {
|
val part = cursorToPart.map(partsCursor)
|
||||||
tryOrNull {
|
realm.insertOrUpdate(part)
|
||||||
progress++
|
progress++
|
||||||
val part = cursorToPart.map(partsCursor)
|
}
|
||||||
realm.insertOrUpdate(part)
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Sync messages
|
// Sync messages
|
||||||
messageCursor?.use {
|
messageCursor?.use {
|
||||||
val messageColumns = CursorToMessage.MessageColumns(messageCursor)
|
val messageColumns = CursorToMessage.MessageColumns(messageCursor)
|
||||||
messageCursor.forEach { cursor ->
|
messageCursor.forEach { cursor ->
|
||||||
tryOrNull {
|
tryOrNull {
|
||||||
progress++
|
syncProgress.onNext(
|
||||||
syncProgress.onNext(SyncRepository.SyncProgress.Running(max, progress, false))
|
SyncRepository.SyncProgress.Running(
|
||||||
val message = cursorToMessage.map(Pair(cursor, messageColumns)).apply {
|
max,
|
||||||
if (isMms()) {
|
++progress,
|
||||||
parts = RealmList<MmsPart>().apply {
|
false
|
||||||
addAll(realm.where(MmsPart::class.java)
|
)
|
||||||
.equalTo("messageId", contentId)
|
)
|
||||||
.findAll())
|
val message = cursorToMessage.map(Pair(cursor, messageColumns)).apply {
|
||||||
|
if (isMms()) {
|
||||||
|
parts = RealmList<MmsPart>().apply {
|
||||||
|
addAll(
|
||||||
|
realm.where(MmsPart::class.java)
|
||||||
|
.equalTo("messageId", contentId)
|
||||||
|
.findAll()
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
realm.insertOrUpdate(message)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
realm.insertOrUpdate(message)
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Migrate blocked conversations from 2.7.3
|
// Migrate blocked conversations from 2.7.3
|
||||||
val oldBlockedSenders = rxPrefs.getStringSet("pref_key_blocked_senders")
|
oldBlockedSenders.get()
|
||||||
oldBlockedSenders.get()
|
.map { threadIdString -> threadIdString.toLong() }
|
||||||
.map { threadIdString -> threadIdString.toLong() }
|
.filter { threadId -> !persistedData.contains(threadId) }
|
||||||
.filter { threadId -> !persistedData.contains(threadId) }
|
.forEach { threadId ->
|
||||||
.forEach { threadId -> persistedData[threadId] = Conversation(id = threadId, blocked = true) }
|
persistedData[threadId] = Conversation(id = threadId, blocked = true)
|
||||||
|
|
||||||
// Sync conversations
|
|
||||||
conversationCursor?.use {
|
|
||||||
conversationCursor.forEach { cursor ->
|
|
||||||
tryOrNull {
|
|
||||||
progress++
|
|
||||||
syncProgress.onNext(SyncRepository.SyncProgress.Running(max, progress, false))
|
|
||||||
val conversation = cursorToConversation.map(cursor).apply {
|
|
||||||
persistedData[id]?.let { persistedConversation ->
|
|
||||||
archived = persistedConversation.archived
|
|
||||||
blocked = persistedConversation.blocked
|
|
||||||
pinned = persistedConversation.pinned
|
|
||||||
name = persistedConversation.name
|
|
||||||
blockingClient = persistedConversation.blockingClient
|
|
||||||
blockReason = persistedConversation.blockReason
|
|
||||||
}
|
|
||||||
lastMessage = realm.where(Message::class.java)
|
|
||||||
.sort("date", Sort.DESCENDING)
|
|
||||||
.equalTo("threadId", id)
|
|
||||||
.findFirst()
|
|
||||||
}
|
}
|
||||||
realm.insertOrUpdate(conversation)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Sync recipients
|
// Sync conversations
|
||||||
recipientCursor?.use {
|
conversationCursor?.use {
|
||||||
val contacts = realm.copyToRealmOrUpdate(getContacts())
|
conversationCursor.forEach { cursor ->
|
||||||
recipientCursor.forEach { cursor ->
|
tryOrNull {
|
||||||
tryOrNull {
|
syncProgress.onNext(
|
||||||
progress++
|
SyncRepository.SyncProgress.Running(
|
||||||
syncProgress.onNext(SyncRepository.SyncProgress.Running(max, progress, false))
|
max,
|
||||||
val recipient = cursorToRecipient.map(cursor).apply {
|
++progress,
|
||||||
contact = contacts.firstOrNull { contact ->
|
false
|
||||||
contact.numbers.any { phoneNumberUtils.compare(address, it.address) }
|
)
|
||||||
|
)
|
||||||
|
val conversation = cursorToConversation.map(cursor).apply {
|
||||||
|
persistedData[id]?.let { persistedConversation ->
|
||||||
|
archived = persistedConversation.archived
|
||||||
|
blocked = persistedConversation.blocked
|
||||||
|
pinned = persistedConversation.pinned
|
||||||
|
name = persistedConversation.name
|
||||||
|
blockingClient = persistedConversation.blockingClient
|
||||||
|
blockReason = persistedConversation.blockReason
|
||||||
|
}
|
||||||
|
lastMessage = realm.where(Message::class.java)
|
||||||
|
.sort("date", Sort.DESCENDING)
|
||||||
|
.equalTo("threadId", id)
|
||||||
|
.findFirst()
|
||||||
|
}
|
||||||
|
realm.insertOrUpdate(conversation)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
realm.insertOrUpdate(recipient)
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
// Sync recipients
|
||||||
|
val contacts = realm.copyToRealmOrUpdate(getContacts())
|
||||||
|
recipientCursor?.use {
|
||||||
|
recipientCursor.forEach { cursor ->
|
||||||
|
tryOrNull {
|
||||||
|
syncProgress.onNext(
|
||||||
|
SyncRepository.SyncProgress.Running(
|
||||||
|
max,
|
||||||
|
++progress,
|
||||||
|
false
|
||||||
|
)
|
||||||
|
)
|
||||||
|
val rec = cursorToRecipient.map(cursor).apply {
|
||||||
|
contact = contacts.firstOrNull { c ->
|
||||||
|
c.numbers.any { num ->
|
||||||
|
phoneNumberUtils.compare(
|
||||||
|
address,
|
||||||
|
num.address
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
realm.insertOrUpdate(rec)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
syncProgress.onNext(SyncRepository.SyncProgress.Running(0, 0, true))
|
||||||
|
|
||||||
|
realm.insert(SyncLog())
|
||||||
|
}, {
|
||||||
|
handlerThread.quitSafely()
|
||||||
|
oldBlockedSenders.delete()
|
||||||
|
syncProgress.onNext(SyncRepository.SyncProgress.Idle)
|
||||||
|
},
|
||||||
|
{ error ->
|
||||||
|
handlerThread.quitSafely()
|
||||||
|
Timber.e(error, "syncMessages Failed")
|
||||||
|
syncProgress.onNext(SyncRepository.SyncProgress.Idle)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
syncProgress.onNext(SyncRepository.SyncProgress.Running(0, 0, true))
|
|
||||||
|
|
||||||
|
|
||||||
realm.insert(SyncLog())
|
|
||||||
realm.commitTransaction()
|
|
||||||
realm.close()
|
|
||||||
|
|
||||||
// Only delete this after the sync has successfully completed
|
|
||||||
oldBlockedSenders.delete()
|
|
||||||
|
|
||||||
syncProgress.onNext(SyncRepository.SyncProgress.Idle)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun syncMessage(uri: Uri): Message? {
|
override fun syncMessage(uri: Uri): Message? {
|
||||||
|
|
||||||
// If we don't have a valid type, return null
|
// If we don't have a valid type, return null
|
||||||
@@ -345,4 +373,13 @@ class SyncRepositoryImpl @Inject constructor(
|
|||||||
return groups
|
return groups
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun removeOldMessages(realm: Realm) {
|
||||||
|
realm.delete(Contact::class.java)
|
||||||
|
realm.delete(ContactGroup::class.java)
|
||||||
|
realm.delete(Conversation::class.java)
|
||||||
|
realm.delete(Message::class.java)
|
||||||
|
realm.delete(MmsPart::class.java)
|
||||||
|
realm.delete(Recipient::class.java)
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user