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,14 +76,20 @@ 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 {
|
||||||
val persistedData = realm.copyFromRealm(realm.where(Conversation::class.java)
|
Realm.getDefaultInstance().executeTransactionAsync(
|
||||||
|
{ realm ->
|
||||||
|
// Prepare existing conversation data
|
||||||
|
val persistedData = realm.copyFromRealm(
|
||||||
|
realm.where(Conversation::class.java)
|
||||||
.beginGroup()
|
.beginGroup()
|
||||||
.equalTo("archived", true)
|
.equalTo("archived", true)
|
||||||
.or()
|
.or()
|
||||||
@@ -94,16 +103,10 @@ class SyncRepositoryImpl @Inject constructor(
|
|||||||
.or()
|
.or()
|
||||||
.isNotEmpty("blockReason")
|
.isNotEmpty("blockReason")
|
||||||
.endGroup()
|
.endGroup()
|
||||||
.findAll())
|
.findAll()
|
||||||
.associateBy { conversation -> conversation.id }
|
).associateBy { conversation -> conversation.id }.toMutableMap()
|
||||||
.toMutableMap()
|
|
||||||
|
|
||||||
realm.delete(Contact::class.java)
|
removeOldMessages(realm)
|
||||||
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()
|
keys.reset()
|
||||||
|
|
||||||
@@ -121,11 +124,11 @@ class SyncRepositoryImpl @Inject constructor(
|
|||||||
|
|
||||||
// Sync message parts
|
// Sync message parts
|
||||||
partsCursor?.use {
|
partsCursor?.use {
|
||||||
partsCursor.forEach {
|
partsCursor.forEach { cursor ->
|
||||||
tryOrNull {
|
tryOrNull {
|
||||||
progress++
|
|
||||||
val part = cursorToPart.map(partsCursor)
|
val part = cursorToPart.map(partsCursor)
|
||||||
realm.insertOrUpdate(part)
|
realm.insertOrUpdate(part)
|
||||||
|
progress++
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -135,14 +138,21 @@ class SyncRepositoryImpl @Inject constructor(
|
|||||||
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(
|
||||||
|
max,
|
||||||
|
++progress,
|
||||||
|
false
|
||||||
|
)
|
||||||
|
)
|
||||||
val message = cursorToMessage.map(Pair(cursor, messageColumns)).apply {
|
val message = cursorToMessage.map(Pair(cursor, messageColumns)).apply {
|
||||||
if (isMms()) {
|
if (isMms()) {
|
||||||
parts = RealmList<MmsPart>().apply {
|
parts = RealmList<MmsPart>().apply {
|
||||||
addAll(realm.where(MmsPart::class.java)
|
addAll(
|
||||||
|
realm.where(MmsPart::class.java)
|
||||||
.equalTo("messageId", contentId)
|
.equalTo("messageId", contentId)
|
||||||
.findAll())
|
.findAll()
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -152,18 +162,24 @@ class SyncRepositoryImpl @Inject constructor(
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 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 -> persistedData[threadId] = Conversation(id = threadId, blocked = true) }
|
.forEach { threadId ->
|
||||||
|
persistedData[threadId] = Conversation(id = threadId, blocked = true)
|
||||||
|
}
|
||||||
|
|
||||||
// Sync conversations
|
// Sync conversations
|
||||||
conversationCursor?.use {
|
conversationCursor?.use {
|
||||||
conversationCursor.forEach { cursor ->
|
conversationCursor.forEach { cursor ->
|
||||||
tryOrNull {
|
tryOrNull {
|
||||||
progress++
|
syncProgress.onNext(
|
||||||
syncProgress.onNext(SyncRepository.SyncProgress.Running(max, progress, false))
|
SyncRepository.SyncProgress.Running(
|
||||||
|
max,
|
||||||
|
++progress,
|
||||||
|
false
|
||||||
|
)
|
||||||
|
)
|
||||||
val conversation = cursorToConversation.map(cursor).apply {
|
val conversation = cursorToConversation.map(cursor).apply {
|
||||||
persistedData[id]?.let { persistedConversation ->
|
persistedData[id]?.let { persistedConversation ->
|
||||||
archived = persistedConversation.archived
|
archived = persistedConversation.archived
|
||||||
@@ -184,35 +200,47 @@ class SyncRepositoryImpl @Inject constructor(
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Sync recipients
|
// Sync recipients
|
||||||
recipientCursor?.use {
|
|
||||||
val contacts = realm.copyToRealmOrUpdate(getContacts())
|
val contacts = realm.copyToRealmOrUpdate(getContacts())
|
||||||
|
recipientCursor?.use {
|
||||||
recipientCursor.forEach { cursor ->
|
recipientCursor.forEach { cursor ->
|
||||||
tryOrNull {
|
tryOrNull {
|
||||||
progress++
|
syncProgress.onNext(
|
||||||
syncProgress.onNext(SyncRepository.SyncProgress.Running(max, progress, false))
|
SyncRepository.SyncProgress.Running(
|
||||||
val recipient = cursorToRecipient.map(cursor).apply {
|
max,
|
||||||
contact = contacts.firstOrNull { contact ->
|
++progress,
|
||||||
contact.numbers.any { phoneNumberUtils.compare(address, it.address) }
|
false
|
||||||
|
)
|
||||||
|
)
|
||||||
|
val rec = cursorToRecipient.map(cursor).apply {
|
||||||
|
contact = contacts.firstOrNull { c ->
|
||||||
|
c.numbers.any { num ->
|
||||||
|
phoneNumberUtils.compare(
|
||||||
|
address,
|
||||||
|
num.address
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
realm.insertOrUpdate(recipient)
|
}
|
||||||
|
realm.insertOrUpdate(rec)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
syncProgress.onNext(SyncRepository.SyncProgress.Running(0, 0, true))
|
syncProgress.onNext(SyncRepository.SyncProgress.Running(0, 0, true))
|
||||||
|
|
||||||
|
|
||||||
realm.insert(SyncLog())
|
realm.insert(SyncLog())
|
||||||
realm.commitTransaction()
|
}, {
|
||||||
realm.close()
|
handlerThread.quitSafely()
|
||||||
|
|
||||||
// Only delete this after the sync has successfully completed
|
|
||||||
oldBlockedSenders.delete()
|
oldBlockedSenders.delete()
|
||||||
|
|
||||||
syncProgress.onNext(SyncRepository.SyncProgress.Idle)
|
syncProgress.onNext(SyncRepository.SyncProgress.Idle)
|
||||||
|
},
|
||||||
|
{ error ->
|
||||||
|
handlerThread.quitSafely()
|
||||||
|
Timber.e(error, "syncMessages Failed")
|
||||||
|
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