Add ability to stop restore

This commit is contained in:
Moez Bhatti
2018-09-01 02:48:54 -04:00
parent 8c008acd9c
commit dc3e54a85a
6 changed files with 26 additions and 13 deletions
@@ -83,6 +83,8 @@ class BackupRepositoryImpl @Inject constructor(
private val backupProgress: Subject<BackupRepository.Progress> = BehaviorSubject.createDefault(BackupRepository.Progress.Idle()) private val backupProgress: Subject<BackupRepository.Progress> = BehaviorSubject.createDefault(BackupRepository.Progress.Idle())
private val restoreProgress: Subject<BackupRepository.Progress> = BehaviorSubject.createDefault(BackupRepository.Progress.Idle()) private val restoreProgress: Subject<BackupRepository.Progress> = BehaviorSubject.createDefault(BackupRepository.Progress.Idle())
@Volatile private var stopFlag: Boolean = false
override fun performBackup() { override fun performBackup() {
// If a backup or restore is already running, don't do anything // If a backup or restore is already running, don't do anything
if (isBackupOrRestoreRunning()) return if (isBackupOrRestoreRunning()) return
@@ -175,6 +177,12 @@ class BackupRepositoryImpl @Inject constructor(
val messageCount = backup?.messages?.size ?: 0 val messageCount = backup?.messages?.size ?: 0
backup?.messages?.forEachIndexed { index, message -> backup?.messages?.forEachIndexed { index, message ->
if (stopFlag) {
stopFlag = false
restoreProgress.onNext(BackupRepository.Progress.Idle())
return
}
// Update the progress // Update the progress
restoreProgress.onNext(BackupRepository.Progress.Running(messageCount, index)) restoreProgress.onNext(BackupRepository.Progress.Running(messageCount, index))
@@ -202,6 +210,10 @@ class BackupRepositoryImpl @Inject constructor(
Timer().schedule(1000) { restoreProgress.onNext(BackupRepository.Progress.Idle()) } Timer().schedule(1000) { restoreProgress.onNext(BackupRepository.Progress.Idle()) }
} }
override fun stopRestore() {
stopFlag = true
}
override fun getRestoreProgress(): Observable<BackupRepository.Progress> = restoreProgress override fun getRestoreProgress(): Observable<BackupRepository.Progress> = restoreProgress
private fun isBackupOrRestoreRunning(): Boolean { private fun isBackupOrRestoreRunning(): Boolean {
@@ -43,6 +43,8 @@ interface BackupRepository {
fun performRestore(filePath: String) fun performRestore(filePath: String)
fun stopRestore()
fun getRestoreProgress(): Observable<Progress> fun getRestoreProgress(): Observable<Progress>
} }
@@ -115,6 +115,7 @@ class BackupController : QkController<BackupView, BackupState, BackupPresenter>(
progressTitle.setText(R.string.backup_backing_up) progressTitle.setText(R.string.backup_backing_up)
progressSummary.text = state.backupProgress.getLabel(activity!!) progressSummary.text = state.backupProgress.getLabel(activity!!)
progressSummary.isVisible = progressSummary.text.isNotEmpty() progressSummary.isVisible = progressSummary.text.isNotEmpty()
progressCancel.isVisible = false
val running = (state.backupProgress as? BackupRepository.Progress.Running) val running = (state.backupProgress as? BackupRepository.Progress.Running)
progressBar.isVisible = state.backupProgress.indeterminate || running?.max ?: 0 > 0 progressBar.isVisible = state.backupProgress.indeterminate || running?.max ?: 0 > 0
progressBar.isIndeterminate = state.backupProgress.indeterminate progressBar.isIndeterminate = state.backupProgress.indeterminate
@@ -129,6 +130,7 @@ class BackupController : QkController<BackupView, BackupState, BackupPresenter>(
progressTitle.setText(R.string.backup_restoring) progressTitle.setText(R.string.backup_restoring)
progressSummary.text = state.restoreProgress.getLabel(activity!!) progressSummary.text = state.restoreProgress.getLabel(activity!!)
progressSummary.isVisible = progressSummary.text.isNotEmpty() progressSummary.isVisible = progressSummary.text.isNotEmpty()
progressCancel.isVisible = true
val running = (state.restoreProgress as? BackupRepository.Progress.Running) val running = (state.restoreProgress as? BackupRepository.Progress.Running)
progressBar.isVisible = state.restoreProgress.indeterminate || running?.max ?: 0 > 0 progressBar.isVisible = state.restoreProgress.indeterminate || running?.max ?: 0 > 0
progressBar.isIndeterminate = state.restoreProgress.indeterminate progressBar.isIndeterminate = state.restoreProgress.indeterminate
@@ -103,7 +103,7 @@ class BackupPresenter @Inject constructor(
view.stopRestoreConfirmed() view.stopRestoreConfirmed()
.autoDisposable(view.scope()) .autoDisposable(view.scope())
.subscribe() .subscribe { backupRepo.stopRestore() }
view.fabClicks() view.fabClicks()
.withLatestFrom(billingManager.upgradeStatus) { _, upgraded -> upgraded } .withLatestFrom(billingManager.upgradeStatus) { _, upgraded -> upgraded }
@@ -86,19 +86,15 @@ class RestoreBackupService : Service() {
when (progress) { when (progress) {
is BackupRepository.Progress.Idle -> stop() is BackupRepository.Progress.Idle -> stop()
is BackupRepository.Progress.Running -> { is BackupRepository.Progress.Running -> notification
notification .setProgress(progress.max, progress.count, progress.indeterminate)
.setProgress(progress.max, progress.count, progress.indeterminate) .setContentText(progress.getLabel(this))
.setContentText(progress.getLabel(this)) .let { notificationManager.notify(NOTIFICATION_ID, it.build()) }
notificationManager.notify(NOTIFICATION_ID, notification.build())
}
else -> { else -> notification
notification .setProgress(0, 0, progress.indeterminate)
.setProgress(0, 0, progress.indeterminate) .setContentText(progress.getLabel(this))
.setContentText(progress.getLabel(this)) .let { notificationManager.notify(NOTIFICATION_ID, it.build()) }
notificationManager.notify(NOTIFICATION_ID, notification.build())
}
} }
} }
@@ -101,6 +101,7 @@
android:layout_marginEnd="16dp" android:layout_marginEnd="16dp"
android:layout_marginTop="8dp" android:layout_marginTop="8dp"
android:indeterminate="true" android:indeterminate="true"
android:visibility="gone"
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="@id/progressTitle" app:layout_constraintStart_toStartOf="@id/progressTitle"
app:layout_constraintTop_toBottomOf="@id/progressSummary" /> app:layout_constraintTop_toBottomOf="@id/progressSummary" />