forked from upstream-mirrors/quik
Move syncing code to SyncManager to prevent multiple syncs from happening at the same time
This commit is contained in:
@@ -0,0 +1,86 @@
|
|||||||
|
package com.moez.QKSMS.common.util
|
||||||
|
|
||||||
|
import android.content.Context
|
||||||
|
import com.moez.QKSMS.common.util.extensions.map
|
||||||
|
import com.moez.QKSMS.common.util.extensions.mapWhile
|
||||||
|
import com.moez.QKSMS.data.mapper.CursorToConversation
|
||||||
|
import com.moez.QKSMS.data.mapper.CursorToMessage
|
||||||
|
import com.moez.QKSMS.data.mapper.CursorToRecipient
|
||||||
|
import com.moez.QKSMS.data.model.Conversation
|
||||||
|
import com.moez.QKSMS.data.model.Message
|
||||||
|
import com.moez.QKSMS.data.model.Recipient
|
||||||
|
import com.moez.QKSMS.data.model.SyncLog
|
||||||
|
import io.realm.Realm
|
||||||
|
import javax.inject.Inject
|
||||||
|
import javax.inject.Singleton
|
||||||
|
|
||||||
|
@Singleton
|
||||||
|
class SyncManager @Inject constructor(
|
||||||
|
context: Context,
|
||||||
|
private val cursorToConversation: CursorToConversation,
|
||||||
|
private val cursorToMessage: CursorToMessage,
|
||||||
|
private val cursorToRecipient: CursorToRecipient) {
|
||||||
|
|
||||||
|
sealed class Status {
|
||||||
|
class Idle : Status()
|
||||||
|
class Running(progress: Int = 0) : Status()
|
||||||
|
}
|
||||||
|
|
||||||
|
private val contentResolver = context.contentResolver
|
||||||
|
private var status: Status = Status.Idle()
|
||||||
|
|
||||||
|
// TODO: This needs to be substantially faster
|
||||||
|
fun performSync(fullSync: Boolean = false) {
|
||||||
|
|
||||||
|
// If the sync is already running, don't try to do another one
|
||||||
|
if (status is Status.Running) return
|
||||||
|
status = Status.Running()
|
||||||
|
|
||||||
|
val realm = Realm.getDefaultInstance()
|
||||||
|
realm.beginTransaction()
|
||||||
|
|
||||||
|
if (fullSync) {
|
||||||
|
realm.delete(Conversation::class.java)
|
||||||
|
realm.delete(Message::class.java)
|
||||||
|
realm.delete(Recipient::class.java)
|
||||||
|
realm.delete(SyncLog::class.java)
|
||||||
|
}
|
||||||
|
|
||||||
|
val lastSync = realm.where(SyncLog::class.java)?.max("date")?.toLong() ?: 0
|
||||||
|
realm.insert(SyncLog())
|
||||||
|
|
||||||
|
// Sync conversations
|
||||||
|
val conversationCursor = contentResolver.query(
|
||||||
|
CursorToConversation.URI,
|
||||||
|
CursorToConversation.PROJECTION,
|
||||||
|
"date >= ?", arrayOf(lastSync.toString()),
|
||||||
|
"date desc")
|
||||||
|
val conversations = conversationCursor.map { cursor -> cursorToConversation.map(cursor) }
|
||||||
|
realm.insertOrUpdate(conversations)
|
||||||
|
conversationCursor.close()
|
||||||
|
|
||||||
|
|
||||||
|
// Sync messages
|
||||||
|
val messageCursor = contentResolver.query(CursorToMessage.URI, CursorToMessage.PROJECTION, null, null, "normalized_date desc")
|
||||||
|
val messageColumns = CursorToMessage.MessageColumns(messageCursor)
|
||||||
|
val messages = messageCursor.mapWhile(
|
||||||
|
{ cursor -> cursorToMessage.map(Pair(cursor, messageColumns)) },
|
||||||
|
{ message -> message.date >= lastSync })
|
||||||
|
realm.insertOrUpdate(messages)
|
||||||
|
messageCursor.close()
|
||||||
|
|
||||||
|
|
||||||
|
// Sync recipients
|
||||||
|
val recipientCursor = contentResolver.query(CursorToRecipient.URI, null, null, null, null)
|
||||||
|
val recipients = recipientCursor.map { cursor -> cursorToRecipient.map(cursor) }
|
||||||
|
realm.insertOrUpdate(recipients)
|
||||||
|
recipientCursor.close()
|
||||||
|
|
||||||
|
|
||||||
|
realm.commitTransaction()
|
||||||
|
realm.close()
|
||||||
|
|
||||||
|
status = Status.Idle()
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -16,6 +16,27 @@ fun Cursor.forEach(closeOnComplete: Boolean = true, method: (Cursor) -> Unit = {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun <T> Cursor.map(map: (Cursor) -> T): List<T> {
|
||||||
|
return List(count, { position ->
|
||||||
|
moveToPosition(position)
|
||||||
|
map(this)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
fun <T> Cursor.mapWhile(map: (Cursor) -> T, predicate: (T) -> Boolean): ArrayList<T> {
|
||||||
|
val result = ArrayList<T>()
|
||||||
|
|
||||||
|
moveToPosition(-1)
|
||||||
|
while (moveToNext()) {
|
||||||
|
val item = map(this)
|
||||||
|
|
||||||
|
if (!predicate(item)) break
|
||||||
|
|
||||||
|
result.add(item)
|
||||||
|
}
|
||||||
|
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* We're using this simple implementation with .range() because of the
|
* We're using this simple implementation with .range() because of the
|
||||||
|
|||||||
@@ -1,35 +1,19 @@
|
|||||||
package com.moez.QKSMS.domain.interactor
|
package com.moez.QKSMS.domain.interactor
|
||||||
|
|
||||||
import android.content.Context
|
import com.moez.QKSMS.common.util.SyncManager
|
||||||
import com.moez.QKSMS.data.mapper.CursorToConversation
|
|
||||||
import com.moez.QKSMS.data.mapper.CursorToMessage
|
|
||||||
import com.moez.QKSMS.data.mapper.CursorToRecipient
|
|
||||||
import com.moez.QKSMS.data.model.Conversation
|
|
||||||
import com.moez.QKSMS.data.model.Message
|
|
||||||
import com.moez.QKSMS.data.model.Recipient
|
|
||||||
import com.moez.QKSMS.data.model.SyncLog
|
|
||||||
import io.reactivex.Flowable
|
import io.reactivex.Flowable
|
||||||
import io.realm.Realm
|
import timber.log.Timber
|
||||||
|
import java.util.concurrent.TimeUnit
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
|
|
||||||
class FullSync @Inject constructor(
|
class FullSync @Inject constructor(private val syncManager: SyncManager) : Interactor<Unit, Long>() {
|
||||||
context: Context,
|
|
||||||
cursorToConversation: CursorToConversation,
|
|
||||||
cursorToMessage: CursorToMessage,
|
|
||||||
cursorToRecipient: CursorToRecipient)
|
|
||||||
: PartialSync(context, cursorToConversation, cursorToMessage, cursorToRecipient) {
|
|
||||||
|
|
||||||
override fun buildObservable(params: Unit): Flowable<Long> {
|
override fun buildObservable(params: Unit): Flowable<Long> {
|
||||||
val realm = Realm.getDefaultInstance()
|
return Flowable.just(System.currentTimeMillis())
|
||||||
realm.executeTransaction {
|
.doOnNext { syncManager.performSync(true) }
|
||||||
realm.delete(Conversation::class.java)
|
.map { startTime -> System.currentTimeMillis() - startTime }
|
||||||
realm.delete(Message::class.java)
|
.map { elapsed -> TimeUnit.MILLISECONDS.toSeconds(elapsed) }
|
||||||
realm.delete(Recipient::class.java)
|
.doOnNext { seconds -> Timber.v("Completed sync in $seconds seconds") }
|
||||||
realm.delete(SyncLog::class.java)
|
|
||||||
}
|
|
||||||
realm.close()
|
|
||||||
|
|
||||||
return super.buildObservable(params)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -1,79 +1,19 @@
|
|||||||
package com.moez.QKSMS.domain.interactor
|
package com.moez.QKSMS.domain.interactor
|
||||||
|
|
||||||
import android.content.Context
|
import com.moez.QKSMS.common.util.SyncManager
|
||||||
import com.moez.QKSMS.common.util.extensions.asFlowable
|
|
||||||
import com.moez.QKSMS.data.mapper.CursorToConversation
|
|
||||||
import com.moez.QKSMS.data.mapper.CursorToMessage
|
|
||||||
import com.moez.QKSMS.data.mapper.CursorToRecipient
|
|
||||||
import com.moez.QKSMS.data.model.SyncLog
|
|
||||||
import io.reactivex.Flowable
|
import io.reactivex.Flowable
|
||||||
import io.realm.Realm
|
|
||||||
import timber.log.Timber
|
import timber.log.Timber
|
||||||
import java.util.concurrent.TimeUnit
|
import java.util.concurrent.TimeUnit
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
|
|
||||||
// TODO: This needs to be substantially faster
|
open class PartialSync @Inject constructor(private val syncManager: SyncManager) : Interactor<Unit, Long>() {
|
||||||
open class PartialSync @Inject constructor(
|
|
||||||
private val context: Context,
|
|
||||||
private val cursorToConversation: CursorToConversation,
|
|
||||||
private val cursorToMessage: CursorToMessage,
|
|
||||||
private val cursorToRecipient: CursorToRecipient)
|
|
||||||
: Interactor<Unit, Long>() {
|
|
||||||
|
|
||||||
override fun buildObservable(params: Unit): Flowable<Long> {
|
override fun buildObservable(params: Unit): Flowable<Long> {
|
||||||
val contentResolver = context.contentResolver
|
return Flowable.just(System.currentTimeMillis())
|
||||||
var realm: Realm? = null
|
.doOnNext { syncManager.performSync() }
|
||||||
|
.map { startTime -> System.currentTimeMillis() - startTime }
|
||||||
var lastSync = 0L
|
.map { elapsed -> TimeUnit.MILLISECONDS.toSeconds(elapsed) }
|
||||||
var startTime = 0L
|
.doOnNext { seconds -> Timber.v("Completed sync in $seconds seconds") }
|
||||||
|
|
||||||
return Flowable.just(params)
|
|
||||||
.doOnNext {
|
|
||||||
Timber.v("Starting sync")
|
|
||||||
startTime = System.currentTimeMillis()
|
|
||||||
|
|
||||||
// We need to set up realm on the io thread, and doOnSubscribe doesn't support setting a custom Scheduler
|
|
||||||
realm = Realm.getDefaultInstance()
|
|
||||||
realm?.beginTransaction()
|
|
||||||
|
|
||||||
lastSync = realm?.where(SyncLog::class.java)?.max("date")?.toLong() ?: 0
|
|
||||||
|
|
||||||
// Add a log entry for this sync
|
|
||||||
realm?.insert(SyncLog())
|
|
||||||
}
|
|
||||||
.flatMap {
|
|
||||||
// Sync conversations
|
|
||||||
contentResolver.query(CursorToConversation.URI, CursorToConversation.PROJECTION,
|
|
||||||
"date >= ?", arrayOf(lastSync.toString()), "date desc")
|
|
||||||
.asFlowable()
|
|
||||||
.map { cursor -> cursorToConversation.map(cursor) }
|
|
||||||
.toList().toFlowable()
|
|
||||||
.doOnNext { conversations -> realm?.insertOrUpdate(conversations) }
|
|
||||||
}
|
|
||||||
.flatMap {
|
|
||||||
// Sync messages
|
|
||||||
val messageCursor = contentResolver.query(CursorToMessage.URI, CursorToMessage.PROJECTION, null, null, "normalized_date desc")
|
|
||||||
val messageColumns = CursorToMessage.MessageColumns(messageCursor)
|
|
||||||
messageCursor.asFlowable()
|
|
||||||
.map { cursor -> cursorToMessage.map(Pair(cursor, messageColumns)) }
|
|
||||||
.takeWhile { message -> message.date >= lastSync }
|
|
||||||
.filter { message -> message.type == "sms" || message.type == "mms" }
|
|
||||||
.toList().toFlowable()
|
|
||||||
.doOnNext { messages -> realm?.insertOrUpdate(messages) }
|
|
||||||
}
|
|
||||||
.flatMap {
|
|
||||||
// Sync recipients
|
|
||||||
contentResolver.query(CursorToRecipient.URI, null, null, null, null).asFlowable()
|
|
||||||
.map { cursor -> cursorToRecipient.map(cursor) }
|
|
||||||
.toList().toFlowable()
|
|
||||||
.doOnNext { recipients -> realm?.insertOrUpdate(recipients) }
|
|
||||||
}
|
|
||||||
.doOnNext {
|
|
||||||
realm?.commitTransaction()
|
|
||||||
realm?.close()
|
|
||||||
Timber.v("Completed sync in ${TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis() - startTime)} seconds")
|
|
||||||
}
|
|
||||||
.map { 0L }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user