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