Display progress when backing up or restoring

This commit is contained in:
Moez Bhatti
2018-08-23 10:41:12 -04:00
parent 72bd507a6c
commit adb6d50cad
10 changed files with 316 additions and 7 deletions
@@ -18,20 +18,65 @@
*/
package com.moez.QKSMS.repository
import android.animation.ObjectAnimator
import android.os.Environment
import androidx.core.animation.addListener
import com.moez.QKSMS.model.Backup
import com.moez.QKSMS.util.QkFileObserver
import io.reactivex.Observable
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.subjects.BehaviorSubject
import io.reactivex.subjects.Subject
import java.io.File
import java.util.concurrent.TimeUnit
import javax.inject.Inject
import javax.inject.Singleton
@Singleton
class BackupRepositoryImpl @Inject constructor() : BackupRepository {
companion object {
private val BACKUP_LOCATION = Environment.getExternalStorageDirectory().toString() + "/QKSMS/Backups"
private val BACKUP_DIRECTORY = Environment.getExternalStorageDirectory().toString() + "/QKSMS/Backups"
}
override fun getBackups(): Observable<List<Backup>> = QkFileObserver(BACKUP_LOCATION)
data class BackupMessage(
val protocol: Int,
val address: String,
val date: Long,
val type: Int,
val body: String,
val service_center: String?,
val read: Boolean,
val status: Int,
val locked: Boolean,
val date_sent: Long)
private val backupProgress: Subject<BackupRepository.Progress> = BehaviorSubject.createDefault(BackupRepository.Progress.Idle())
private val restoreProgress: Subject<BackupRepository.Progress> = BehaviorSubject.createDefault(BackupRepository.Progress.Idle())
override fun performBackup() {
// If a backup or restore is already running, don't do anything
if (isBackupOrRestoreRunning()) return
backupProgress.onNext(BackupRepository.Progress.Running(0))
Thread.sleep(1000)
val animator = ObjectAnimator.ofInt(0, 100)
animator.duration = 3000
animator.addUpdateListener {
val remaining = TimeUnit.MILLISECONDS.toSeconds(it.duration - it.currentPlayTime) + 1
backupProgress.onNext(BackupRepository.Progress.Running(it.animatedValue as Int, "$remaining seconds remaining"))
}
animator.addListener(onEnd = {
backupProgress.onNext(BackupRepository.Progress.Idle())
})
AndroidSchedulers.mainThread().scheduleDirect { animator.start() }
}
override fun getBackupProgress(): Observable<BackupRepository.Progress> = backupProgress
override fun getBackups(): Observable<List<Backup>> = QkFileObserver(BACKUP_DIRECTORY)
.observable
.map { path -> File(path).listFiles() }
.map { files ->
@@ -42,4 +87,31 @@ class BackupRepositoryImpl @Inject constructor() : BackupRepository {
}
}
override fun performRestore() {
// If a backup or restore is already running, don't do anything
if (isBackupOrRestoreRunning()) return
restoreProgress.onNext(BackupRepository.Progress.Running(0))
Thread.sleep(1000)
val animator = ObjectAnimator.ofInt(0, 100)
animator.duration = 3000
animator.addUpdateListener {
val remaining = TimeUnit.MILLISECONDS.toSeconds(it.duration - it.currentPlayTime) + 1
restoreProgress.onNext(BackupRepository.Progress.Running(it.animatedValue as Int, "$remaining seconds remaining"))
}
animator.addListener(onEnd = {
restoreProgress.onNext(BackupRepository.Progress.Idle())
})
AndroidSchedulers.mainThread().scheduleDirect { animator.start() }
}
override fun getRestoreProgress(): Observable<BackupRepository.Progress> = restoreProgress
private fun isBackupOrRestoreRunning(): Boolean {
return backupProgress.blockingFirst() is BackupRepository.Progress.Running
|| restoreProgress.blockingFirst() is BackupRepository.Progress.Running
}
}