forked from upstream-mirrors/quik
Show determinate progress when syncing
This commit is contained in:
@@ -26,7 +26,6 @@ import android.telephony.PhoneNumberUtils
|
|||||||
import com.f2prateek.rx.preferences2.RxSharedPreferences
|
import com.f2prateek.rx.preferences2.RxSharedPreferences
|
||||||
import com.moez.QKSMS.extensions.insertOrUpdate
|
import com.moez.QKSMS.extensions.insertOrUpdate
|
||||||
import com.moez.QKSMS.extensions.map
|
import com.moez.QKSMS.extensions.map
|
||||||
import com.moez.QKSMS.filter.PhoneNumberFilter
|
|
||||||
import com.moez.QKSMS.manager.KeyManager
|
import com.moez.QKSMS.manager.KeyManager
|
||||||
import com.moez.QKSMS.mapper.CursorToContact
|
import com.moez.QKSMS.mapper.CursorToContact
|
||||||
import com.moez.QKSMS.mapper.CursorToConversation
|
import com.moez.QKSMS.mapper.CursorToConversation
|
||||||
@@ -55,7 +54,6 @@ class SyncRepositoryImpl @Inject constructor(
|
|||||||
private val cursorToRecipient: CursorToRecipient,
|
private val cursorToRecipient: CursorToRecipient,
|
||||||
private val cursorToContact: CursorToContact,
|
private val cursorToContact: CursorToContact,
|
||||||
private val keys: KeyManager,
|
private val keys: KeyManager,
|
||||||
private val phoneNumberFilter: PhoneNumberFilter,
|
|
||||||
private val rxPrefs: RxSharedPreferences
|
private val rxPrefs: RxSharedPreferences
|
||||||
) : SyncRepository {
|
) : SyncRepository {
|
||||||
|
|
||||||
@@ -75,7 +73,7 @@ class SyncRepositoryImpl @Inject constructor(
|
|||||||
|
|
||||||
// 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(0f))
|
syncProgress.onNext(SyncRepository.SyncProgress.Running(0, 0, true))
|
||||||
|
|
||||||
val realm = Realm.getDefaultInstance()
|
val realm = Realm.getDefaultInstance()
|
||||||
realm.beginTransaction()
|
realm.beginTransaction()
|
||||||
@@ -101,11 +99,25 @@ class SyncRepositoryImpl @Inject constructor(
|
|||||||
|
|
||||||
keys.reset()
|
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
|
// Sync messages
|
||||||
cursorToMessage.getMessagesCursor()?.use { messageCursor ->
|
messageCursor?.use {
|
||||||
val messageColumns = CursorToMessage.MessageColumns(messageCursor)
|
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)
|
realm.insertOrUpdate(messages)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -117,9 +129,13 @@ class SyncRepositoryImpl @Inject constructor(
|
|||||||
.map { threadId -> PersistedData(threadId, false, true, false, "") }
|
.map { threadId -> PersistedData(threadId, false, true, false, "") }
|
||||||
|
|
||||||
// Sync conversations
|
// Sync conversations
|
||||||
cursorToConversation.getConversationsCursor()?.use { conversationCursor ->
|
conversationCursor?.use {
|
||||||
val conversations = conversationCursor
|
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 ->
|
persistedData.forEach { data ->
|
||||||
val conversation = conversations.firstOrNull { conversation -> conversation.id == data.id }
|
val conversation = conversations.firstOrNull { conversation -> conversation.id == data.id }
|
||||||
@@ -145,20 +161,23 @@ class SyncRepositoryImpl @Inject constructor(
|
|||||||
|
|
||||||
|
|
||||||
// Sync recipients
|
// Sync recipients
|
||||||
cursorToRecipient.getRecipientCursor()?.use { recipientCursor ->
|
recipientCursor?.use {
|
||||||
val contacts = realm.copyToRealm(getContacts())
|
val contacts = realm.copyToRealm(getContacts())
|
||||||
val recipients = recipientCursor
|
val recipients = recipientCursor
|
||||||
.map { cursor -> cursorToRecipient.map(cursor) }
|
.map { cursor ->
|
||||||
.map { recipient ->
|
progress++
|
||||||
recipient.apply {
|
syncProgress.onNext(SyncRepository.SyncProgress.Running(max, progress, false))
|
||||||
|
cursorToRecipient.map(cursor).apply {
|
||||||
contact = contacts.firstOrNull { contact ->
|
contact = contacts.firstOrNull { contact ->
|
||||||
contact.numbers.any { PhoneNumberUtils.compare(recipient.address, it.address) }
|
contact.numbers.any { PhoneNumberUtils.compare(address, it.address) }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
realm.insertOrUpdate(recipients)
|
realm.insertOrUpdate(recipients)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
syncProgress.onNext(SyncRepository.SyncProgress.Running(0, 0, true))
|
||||||
|
|
||||||
|
|
||||||
realm.insert(SyncLog())
|
realm.insert(SyncLog())
|
||||||
realm.commitTransaction()
|
realm.commitTransaction()
|
||||||
|
|||||||
@@ -26,7 +26,7 @@ interface SyncRepository {
|
|||||||
|
|
||||||
sealed class SyncProgress {
|
sealed class SyncProgress {
|
||||||
class Idle : 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>
|
val syncProgress: Observable<SyncProgress>
|
||||||
|
|||||||
@@ -19,6 +19,7 @@
|
|||||||
package com.moez.QKSMS.feature.main
|
package com.moez.QKSMS.feature.main
|
||||||
|
|
||||||
import android.Manifest
|
import android.Manifest
|
||||||
|
import android.animation.ObjectAnimator
|
||||||
import android.app.AlertDialog
|
import android.app.AlertDialog
|
||||||
import android.content.res.ColorStateList
|
import android.content.res.ColorStateList
|
||||||
import android.os.Bundle
|
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 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 toggle by lazy { ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.main_drawer_open_cd, 0) }
|
||||||
private val itemTouchHelper by lazy { ItemTouchHelper(itemTouchCallback) }
|
private val itemTouchHelper by lazy { ItemTouchHelper(itemTouchCallback) }
|
||||||
|
private val progressAnimator by lazy { ObjectAnimator.ofInt(syncingProgress, "progress", 0, 0) }
|
||||||
private val archiveSnackbar by lazy {
|
private val archiveSnackbar by lazy {
|
||||||
Snackbar.make(drawerLayout, R.string.toast_archived, Snackbar.LENGTH_LONG).apply {
|
Snackbar.make(drawerLayout, R.string.toast_archived, Snackbar.LENGTH_LONG).apply {
|
||||||
setAction(R.string.button_undo) { undoArchiveIntent.onNext(Unit) }
|
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)
|
if (drawerLayout.isDrawerOpen(Gravity.START) && !state.drawerOpen) drawerLayout.closeDrawer(Gravity.START)
|
||||||
else if (!drawerLayout.isDrawerVisible(Gravity.START) && state.drawerOpen) drawerLayout.openDrawer(Gravity.START)
|
else if (!drawerLayout.isDrawerVisible(Gravity.START) && state.drawerOpen) drawerLayout.openDrawer(Gravity.START)
|
||||||
|
|
||||||
syncing.setVisible(state.syncing is SyncRepository.SyncProgress.Running)
|
when (state.syncing) {
|
||||||
snackbar.setVisible(state.syncing is SyncRepository.SyncProgress.Idle
|
is SyncRepository.SyncProgress.Idle -> {
|
||||||
&& !state.defaultSms || !state.smsPermission || !state.contactPermission)
|
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 {
|
when {
|
||||||
!state.smsPermission -> {
|
!state.smsPermission -> {
|
||||||
|
|||||||
Reference in New Issue
Block a user