diff --git a/data/src/main/java/com/moez/QKSMS/repository/BackupRepositoryImpl.kt b/data/src/main/java/com/moez/QKSMS/repository/BackupRepositoryImpl.kt index 4229e481..9ba3b1a5 100644 --- a/data/src/main/java/com/moez/QKSMS/repository/BackupRepositoryImpl.kt +++ b/data/src/main/java/com/moez/QKSMS/repository/BackupRepositoryImpl.kt @@ -83,6 +83,8 @@ class BackupRepositoryImpl @Inject constructor( private val backupProgress: Subject = BehaviorSubject.createDefault(BackupRepository.Progress.Idle()) private val restoreProgress: Subject = 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 = restoreProgress private fun isBackupOrRestoreRunning(): Boolean { diff --git a/domain/src/main/java/com/moez/QKSMS/repository/BackupRepository.kt b/domain/src/main/java/com/moez/QKSMS/repository/BackupRepository.kt index 04bf6a87..7c73a731 100644 --- a/domain/src/main/java/com/moez/QKSMS/repository/BackupRepository.kt +++ b/domain/src/main/java/com/moez/QKSMS/repository/BackupRepository.kt @@ -43,6 +43,8 @@ interface BackupRepository { fun performRestore(filePath: String) + fun stopRestore() + fun getRestoreProgress(): Observable } \ No newline at end of file diff --git a/presentation/src/main/java/com/moez/QKSMS/feature/backup/BackupController.kt b/presentation/src/main/java/com/moez/QKSMS/feature/backup/BackupController.kt index 926234ab..5d6d2815 100644 --- a/presentation/src/main/java/com/moez/QKSMS/feature/backup/BackupController.kt +++ b/presentation/src/main/java/com/moez/QKSMS/feature/backup/BackupController.kt @@ -115,6 +115,7 @@ class BackupController : QkController( 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( 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 diff --git a/presentation/src/main/java/com/moez/QKSMS/feature/backup/BackupPresenter.kt b/presentation/src/main/java/com/moez/QKSMS/feature/backup/BackupPresenter.kt index 9906ce1b..fd8a5bab 100644 --- a/presentation/src/main/java/com/moez/QKSMS/feature/backup/BackupPresenter.kt +++ b/presentation/src/main/java/com/moez/QKSMS/feature/backup/BackupPresenter.kt @@ -103,7 +103,7 @@ class BackupPresenter @Inject constructor( view.stopRestoreConfirmed() .autoDisposable(view.scope()) - .subscribe() + .subscribe { backupRepo.stopRestore() } view.fabClicks() .withLatestFrom(billingManager.upgradeStatus) { _, upgraded -> upgraded } diff --git a/presentation/src/main/java/com/moez/QKSMS/feature/backup/RestoreBackupService.kt b/presentation/src/main/java/com/moez/QKSMS/feature/backup/RestoreBackupService.kt index 9b30e160..49527ef0 100644 --- a/presentation/src/main/java/com/moez/QKSMS/feature/backup/RestoreBackupService.kt +++ b/presentation/src/main/java/com/moez/QKSMS/feature/backup/RestoreBackupService.kt @@ -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()) } } } diff --git a/presentation/src/main/res/layout/backup_controller.xml b/presentation/src/main/res/layout/backup_controller.xml index 944edb1a..ccda7ae1 100644 --- a/presentation/src/main/res/layout/backup_controller.xml +++ b/presentation/src/main/res/layout/backup_controller.xml @@ -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" />