mirror of
https://github.com/quik-sms/quik.git
synced 2026-11-22 05:38:52 +01:00
Added support for partial sync
This commit is contained in:
@@ -0,0 +1,9 @@
|
|||||||
|
package com.moez.QKSMS.data.model
|
||||||
|
|
||||||
|
import io.realm.RealmObject
|
||||||
|
|
||||||
|
open class SyncLog : RealmObject() {
|
||||||
|
|
||||||
|
var date: Long = System.currentTimeMillis()
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,9 +1,6 @@
|
|||||||
package com.moez.QKSMS.domain.interactor
|
package com.moez.QKSMS.domain.interactor
|
||||||
|
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.net.Uri
|
|
||||||
import android.provider.Telephony
|
|
||||||
import com.moez.QKSMS.common.util.extensions.asFlowable
|
|
||||||
import com.moez.QKSMS.data.mapper.CursorToConversation
|
import com.moez.QKSMS.data.mapper.CursorToConversation
|
||||||
import com.moez.QKSMS.data.mapper.CursorToMessage
|
import com.moez.QKSMS.data.mapper.CursorToMessage
|
||||||
import io.reactivex.Flowable
|
import io.reactivex.Flowable
|
||||||
@@ -11,43 +8,17 @@ import io.realm.Realm
|
|||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
|
|
||||||
class FullSync @Inject constructor(
|
class FullSync @Inject constructor(
|
||||||
private val context: Context,
|
context: Context,
|
||||||
private val cursorToConversation: CursorToConversation,
|
cursorToConversation: CursorToConversation,
|
||||||
private val cursorToMessage: CursorToMessage)
|
cursorToMessage: CursorToMessage)
|
||||||
: Interactor<Long, Unit>() {
|
: PartialSync(context, cursorToConversation, cursorToMessage) {
|
||||||
|
|
||||||
override fun buildObservable(params: Unit): Flowable<Long> {
|
override fun buildObservable(params: Unit): Flowable<Long> {
|
||||||
val contentResolver = context.contentResolver
|
val realm = Realm.getDefaultInstance()
|
||||||
val conversationsCursor = contentResolver.query(CursorToConversation.URI, CursorToConversation.PROJECTION, null, null, "date desc")
|
realm.executeTransaction { realm.deleteAll() }
|
||||||
|
realm.close()
|
||||||
|
|
||||||
var realm: Realm? = null
|
return super.buildObservable(params)
|
||||||
|
|
||||||
return Flowable.just(conversationsCursor)
|
|
||||||
.doOnNext {
|
|
||||||
// We need to set up realm on the io thread, and doOnSubscribe doesn't support setting a custom Scheduler
|
|
||||||
realm = Realm.getDefaultInstance()
|
|
||||||
realm?.beginTransaction()
|
|
||||||
realm?.deleteAll()
|
|
||||||
}
|
|
||||||
.flatMap { cursor -> cursor.asFlowable().map { cursorToConversation.map(cursor) } }
|
|
||||||
.distinct { conversation -> conversation.id }
|
|
||||||
.doOnNext { conversation -> realm?.insertOrUpdate(conversation) }
|
|
||||||
.map { conversation -> conversation.id }
|
|
||||||
.map { threadId -> Uri.withAppendedPath(Telephony.MmsSms.CONTENT_CONVERSATIONS_URI, threadId.toString()) }
|
|
||||||
.map { threadUri -> contentResolver.query(threadUri, CursorToMessage.CURSOR_PROJECTION, null, null, "date desc") }
|
|
||||||
.flatMap { cursor ->
|
|
||||||
val columnsMap = CursorToMessage.MessageColumns(cursor)
|
|
||||||
cursor.asFlowable().map { cursorToMessage.map(Pair(cursor, columnsMap)) }
|
|
||||||
}
|
|
||||||
.filter { message -> message.type == "sms" || message.type == "mms" }
|
|
||||||
.distinct { message -> message.id }
|
|
||||||
.doOnNext { message -> realm?.insertOrUpdate(message) }
|
|
||||||
.count()
|
|
||||||
.toFlowable()
|
|
||||||
.doOnNext {
|
|
||||||
realm?.commitTransaction()
|
|
||||||
realm?.close()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,67 @@
|
|||||||
|
package com.moez.QKSMS.domain.interactor
|
||||||
|
|
||||||
|
import android.content.Context
|
||||||
|
import android.net.Uri
|
||||||
|
import android.provider.Telephony
|
||||||
|
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.model.SyncLog
|
||||||
|
import io.reactivex.Flowable
|
||||||
|
import io.realm.Realm
|
||||||
|
import timber.log.Timber
|
||||||
|
import java.util.concurrent.TimeUnit
|
||||||
|
import javax.inject.Inject
|
||||||
|
|
||||||
|
open class PartialSync @Inject constructor(
|
||||||
|
private val context: Context,
|
||||||
|
private val cursorToConversation: CursorToConversation,
|
||||||
|
private val cursorToMessage: CursorToMessage)
|
||||||
|
: Interactor<Long, Unit>() {
|
||||||
|
|
||||||
|
override fun buildObservable(params: Unit): Flowable<Long> {
|
||||||
|
val contentResolver = context.contentResolver
|
||||||
|
val conversationsCursor = contentResolver.query(CursorToConversation.URI, CursorToConversation.PROJECTION, null, null, "date desc")
|
||||||
|
|
||||||
|
var realm: Realm? = null
|
||||||
|
|
||||||
|
var lastSync = 0L
|
||||||
|
var startTime = 0L
|
||||||
|
|
||||||
|
return Flowable.just(conversationsCursor)
|
||||||
|
.doOnNext {
|
||||||
|
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 { cursor -> cursor.asFlowable().map { cursorToConversation.map(it) } }
|
||||||
|
.distinct { conversation -> conversation.id }
|
||||||
|
.doOnNext { conversation -> realm?.insertOrUpdate(conversation) }
|
||||||
|
.map { conversation -> conversation.id }
|
||||||
|
.map { threadId -> Uri.withAppendedPath(Telephony.MmsSms.CONTENT_CONVERSATIONS_URI, threadId.toString()) }
|
||||||
|
.map { threadUri -> contentResolver.query(threadUri, CursorToMessage.CURSOR_PROJECTION, null, null, "date desc") }
|
||||||
|
.flatMap { cursor ->
|
||||||
|
val columnsMap = CursorToMessage.MessageColumns(cursor)
|
||||||
|
cursor.asFlowable().map { cursorToMessage.map(Pair(cursor, columnsMap)) }
|
||||||
|
}
|
||||||
|
.takeWhile { message -> message.date >= lastSync }
|
||||||
|
.filter { message -> message.type == "sms" || message.type == "mms" }
|
||||||
|
.distinct { message -> message.id }
|
||||||
|
.doOnNext { message -> realm?.insertOrUpdate(message) }
|
||||||
|
.count()
|
||||||
|
.toFlowable()
|
||||||
|
.doOnNext {
|
||||||
|
realm?.commitTransaction()
|
||||||
|
realm?.close()
|
||||||
|
Timber.v("Synced $it messages in ${TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis() - startTime)} seconds")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,9 +1,12 @@
|
|||||||
package com.moez.QKSMS.presentation.main
|
package com.moez.QKSMS.presentation.main
|
||||||
|
|
||||||
|
import android.content.Context
|
||||||
|
import android.provider.Telephony
|
||||||
import com.moez.QKSMS.common.di.AppComponentManager
|
import com.moez.QKSMS.common.di.AppComponentManager
|
||||||
import com.moez.QKSMS.data.repository.MessageRepository
|
import com.moez.QKSMS.data.repository.MessageRepository
|
||||||
import com.moez.QKSMS.domain.interactor.MarkAllSeen
|
import com.moez.QKSMS.domain.interactor.MarkAllSeen
|
||||||
import com.moez.QKSMS.domain.interactor.MarkArchived
|
import com.moez.QKSMS.domain.interactor.MarkArchived
|
||||||
|
import com.moez.QKSMS.domain.interactor.PartialSync
|
||||||
import com.moez.QKSMS.presentation.Navigator
|
import com.moez.QKSMS.presentation.Navigator
|
||||||
import com.moez.QKSMS.presentation.base.QkViewModel
|
import com.moez.QKSMS.presentation.base.QkViewModel
|
||||||
import io.reactivex.rxkotlin.plusAssign
|
import io.reactivex.rxkotlin.plusAssign
|
||||||
@@ -11,10 +14,12 @@ import javax.inject.Inject
|
|||||||
|
|
||||||
class MainViewModel : QkViewModel<MainView, MainState>(MainState()) {
|
class MainViewModel : QkViewModel<MainView, MainState>(MainState()) {
|
||||||
|
|
||||||
|
@Inject lateinit var context: Context
|
||||||
@Inject lateinit var navigator: Navigator
|
@Inject lateinit var navigator: Navigator
|
||||||
@Inject lateinit var messageRepo: MessageRepository
|
@Inject lateinit var messageRepo: MessageRepository
|
||||||
@Inject lateinit var markAllSeen: MarkAllSeen
|
@Inject lateinit var markAllSeen: MarkAllSeen
|
||||||
@Inject lateinit var markArchived: MarkArchived
|
@Inject lateinit var markArchived: MarkArchived
|
||||||
|
@Inject lateinit var partialSync: PartialSync
|
||||||
|
|
||||||
init {
|
init {
|
||||||
AppComponentManager.appComponent.inject(this)
|
AppComponentManager.appComponent.inject(this)
|
||||||
@@ -23,6 +28,10 @@ class MainViewModel : QkViewModel<MainView, MainState>(MainState()) {
|
|||||||
|
|
||||||
newState { it.copy(page = MainPage.INBOX, adapter = ConversationsAdapter(messageRepo.getConversations())) }
|
newState { it.copy(page = MainPage.INBOX, adapter = ConversationsAdapter(messageRepo.getConversations())) }
|
||||||
|
|
||||||
|
if (Telephony.Sms.getDefaultSmsPackage(context) != context.packageName) {
|
||||||
|
partialSync.execute(Unit)
|
||||||
|
}
|
||||||
|
|
||||||
markAllSeen.execute(Unit)
|
markAllSeen.execute(Unit)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user