Show determinate progress when syncing

This commit is contained in:
Moez Bhatti
2018-09-08 03:13:43 -04:00
parent 8ba008ef48
commit 34ac72b5a1
3 changed files with 48 additions and 16 deletions
@@ -26,7 +26,6 @@ import android.telephony.PhoneNumberUtils
import com.f2prateek.rx.preferences2.RxSharedPreferences
import com.moez.QKSMS.extensions.insertOrUpdate
import com.moez.QKSMS.extensions.map
import com.moez.QKSMS.filter.PhoneNumberFilter
import com.moez.QKSMS.manager.KeyManager
import com.moez.QKSMS.mapper.CursorToContact
import com.moez.QKSMS.mapper.CursorToConversation
@@ -55,7 +54,6 @@ class SyncRepositoryImpl @Inject constructor(
private val cursorToRecipient: CursorToRecipient,
private val cursorToContact: CursorToContact,
private val keys: KeyManager,
private val phoneNumberFilter: PhoneNumberFilter,
private val rxPrefs: RxSharedPreferences
) : SyncRepository {
@@ -75,7 +73,7 @@ class SyncRepositoryImpl @Inject constructor(
// If the sync is already running, don't try to do another one
if (syncProgress.blockingFirst() is SyncRepository.SyncProgress.Running) return
syncProgress.onNext(SyncRepository.SyncProgress.Running(0f))
syncProgress.onNext(SyncRepository.SyncProgress.Running(0, 0, true))
val realm = Realm.getDefaultInstance()
realm.beginTransaction()
@@ -101,11 +99,25 @@ class SyncRepositoryImpl @Inject constructor(
keys.reset()
val messageCursor = cursorToMessage.getMessagesCursor()
val conversationCursor = cursorToConversation.getConversationsCursor()
val recipientCursor = cursorToRecipient.getRecipientCursor()
val max = (messageCursor?.count ?: 0) +
(conversationCursor?.count ?: 0) +
(recipientCursor?.count ?: 0)
var progress = 0
// Sync messages
cursorToMessage.getMessagesCursor()?.use { messageCursor ->
messageCursor?.use {
val messageColumns = CursorToMessage.MessageColumns(messageCursor)
val messages = messageCursor.map { cursor -> cursorToMessage.map(Pair(cursor, messageColumns)) }
val messages = messageCursor.map { cursor ->
progress++
syncProgress.onNext(SyncRepository.SyncProgress.Running(max, progress, false))
cursorToMessage.map(Pair(cursor, messageColumns))
}
realm.insertOrUpdate(messages)
}
@@ -117,9 +129,13 @@ class SyncRepositoryImpl @Inject constructor(
.map { threadId -> PersistedData(threadId, false, true, false, "") }
// Sync conversations
cursorToConversation.getConversationsCursor()?.use { conversationCursor ->
conversationCursor?.use {
val conversations = conversationCursor
.map { cursor -> cursorToConversation.map(cursor) }
.map { cursor ->
progress++
syncProgress.onNext(SyncRepository.SyncProgress.Running(max, progress, false))
cursorToConversation.map(cursor)
}
persistedData.forEach { data ->
val conversation = conversations.firstOrNull { conversation -> conversation.id == data.id }
@@ -145,20 +161,23 @@ class SyncRepositoryImpl @Inject constructor(
// Sync recipients
cursorToRecipient.getRecipientCursor()?.use { recipientCursor ->
recipientCursor?.use {
val contacts = realm.copyToRealm(getContacts())
val recipients = recipientCursor
.map { cursor -> cursorToRecipient.map(cursor) }
.map { recipient ->
recipient.apply {
.map { cursor ->
progress++
syncProgress.onNext(SyncRepository.SyncProgress.Running(max, progress, false))
cursorToRecipient.map(cursor).apply {
contact = contacts.firstOrNull { contact ->
contact.numbers.any { PhoneNumberUtils.compare(recipient.address, it.address) }
contact.numbers.any { PhoneNumberUtils.compare(address, it.address) }
}
}
}
realm.insertOrUpdate(recipients)
}
syncProgress.onNext(SyncRepository.SyncProgress.Running(0, 0, true))
realm.insert(SyncLog())
realm.commitTransaction()
@@ -26,7 +26,7 @@ interface SyncRepository {
sealed class SyncProgress {
class Idle : SyncProgress()
class Running(progress: Float) : SyncProgress()
data class Running(val max: Int, val progress: Int, val indeterminate: Boolean) : SyncProgress()
}
val syncProgress: Observable<SyncProgress>
@@ -19,6 +19,7 @@
package com.moez.QKSMS.feature.main
import android.Manifest
import android.animation.ObjectAnimator
import android.app.AlertDialog
import android.content.res.ColorStateList
import android.os.Bundle
@@ -103,6 +104,7 @@ class MainActivity : QkThemedActivity(), MainView {
private val viewModel by lazy { ViewModelProviders.of(this, viewModelFactory)[MainViewModel::class.java] }
private val toggle by lazy { ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.main_drawer_open_cd, 0) }
private val itemTouchHelper by lazy { ItemTouchHelper(itemTouchCallback) }
private val progressAnimator by lazy { ObjectAnimator.ofInt(syncingProgress, "progress", 0, 0) }
private val archiveSnackbar by lazy {
Snackbar.make(drawerLayout, R.string.toast_archived, Snackbar.LENGTH_LONG).apply {
setAction(R.string.button_undo) { undoArchiveIntent.onNext(Unit) }
@@ -246,9 +248,20 @@ class MainActivity : QkThemedActivity(), MainView {
if (drawerLayout.isDrawerOpen(Gravity.START) && !state.drawerOpen) drawerLayout.closeDrawer(Gravity.START)
else if (!drawerLayout.isDrawerVisible(Gravity.START) && state.drawerOpen) drawerLayout.openDrawer(Gravity.START)
syncing.setVisible(state.syncing is SyncRepository.SyncProgress.Running)
snackbar.setVisible(state.syncing is SyncRepository.SyncProgress.Idle
&& !state.defaultSms || !state.smsPermission || !state.contactPermission)
when (state.syncing) {
is SyncRepository.SyncProgress.Idle -> {
syncing.isVisible = false
snackbar.isVisible = !state.defaultSms || !state.smsPermission || !state.contactPermission
}
is SyncRepository.SyncProgress.Running -> {
syncing.isVisible = true
syncingProgress.max = state.syncing.max
progressAnimator.apply { setIntValues(syncingProgress.progress, state.syncing.progress) }.start()
syncingProgress.isIndeterminate = state.syncing.indeterminate
snackbar.isVisible = false
}
}
when {
!state.smsPermission -> {