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
}
}
@@ -0,0 +1,34 @@
/*
* Copyright (C) 2017 Moez Bhatti <moez.bhatti@gmail.com>
*
* This file is part of QKSMS.
*
* QKSMS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* QKSMS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with QKSMS. If not, see <http://www.gnu.org/licenses/>.
*/
package com.moez.QKSMS.interactor
import com.moez.QKSMS.repository.BackupRepository
import io.reactivex.Flowable
import javax.inject.Inject
class PerformBackup @Inject constructor(
private val backupRepo: BackupRepository
) : Interactor<Unit>() {
override fun buildObservable(params: Unit): Flowable<*> {
return Flowable.just(params)
.doOnNext { backupRepo.performBackup() }
}
}
@@ -0,0 +1,34 @@
/*
* Copyright (C) 2017 Moez Bhatti <moez.bhatti@gmail.com>
*
* This file is part of QKSMS.
*
* QKSMS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* QKSMS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with QKSMS. If not, see <http://www.gnu.org/licenses/>.
*/
package com.moez.QKSMS.interactor
import com.moez.QKSMS.repository.BackupRepository
import io.reactivex.Flowable
import javax.inject.Inject
class PerformRestore @Inject constructor(
private val backupRepo: BackupRepository
) : Interactor<Unit>() {
override fun buildObservable(params: Unit): Flowable<*> {
return Flowable.just(params)
.doOnNext { backupRepo.performRestore() }
}
}
@@ -23,9 +23,22 @@ import io.reactivex.Observable
interface BackupRepository {
sealed class Progress {
class Idle : Progress()
class Running(val progress: Int, val status: String? = null) : Progress()
}
fun performBackup()
fun getBackupProgress(): Observable<Progress>
/**
* Returns a list of all local backups
*/
fun getBackups(): Observable<List<Backup>>
fun performRestore()
fun getRestoreProgress(): Observable<Progress>
}
@@ -22,6 +22,7 @@ import android.app.AlertDialog
import android.graphics.Typeface
import android.view.View
import androidx.core.view.children
import androidx.core.view.isVisible
import com.jakewharton.rxbinding2.view.clicks
import com.moez.QKSMS.R
import com.moez.QKSMS.common.base.QkController
@@ -31,6 +32,7 @@ import com.moez.QKSMS.common.util.extensions.setTint
import com.moez.QKSMS.common.widget.PreferenceView
import com.moez.QKSMS.injection.appComponent
import com.moez.QKSMS.model.Backup
import com.moez.QKSMS.repository.BackupRepository
import io.reactivex.Observable
import kotlinx.android.synthetic.main.backup_controller.*
import kotlinx.android.synthetic.main.backup_list_dialog.view.*
@@ -82,6 +84,35 @@ class BackupController : QkController<BackupView, BackupState, BackupPresenter>(
}
override fun render(state: BackupState) {
when {
state.backupProgress is BackupRepository.Progress.Running -> {
progressIcon.setImageResource(R.drawable.ic_file_upload_black_24dp)
progressTitle.setText(R.string.backup_backing_up)
progressSummary.text = state.backupProgress.status
progressSummary.isVisible = progressSummary.text.isNotEmpty()
progressBar.isIndeterminate = state.backupProgress.progress == 0
progressBar.progress = state.backupProgress.progress
progress.isVisible = true
fab.isVisible = false
}
state.restoreProgress is BackupRepository.Progress.Running -> {
progressIcon.setImageResource(R.drawable.ic_file_download_black_24dp)
progressTitle.setText(R.string.backup_restoring)
progressSummary.text = state.restoreProgress.status
progressSummary.isVisible = progressSummary.text.isNotEmpty()
progressBar.isIndeterminate = state.restoreProgress.progress == 0
progressBar.progress = state.restoreProgress.progress
progress.isVisible = true
fab.isVisible = false
}
else -> {
progress.isVisible = false
fab.isVisible = true
}
}
backup.summary = when (state.lastBackup) {
null -> activity?.getString(R.string.backup_never)
else -> dateFormatter.getDetailedTimestamp(state.lastBackup)
@@ -105,6 +136,7 @@ class BackupController : QkController<BackupView, BackupState, BackupPresenter>(
override fun restoreClicks(): Observable<*> = restore.clicks()
override fun restoreFileSelected(): Observable<Backup> = adapter.backupSelected
.doOnNext { dialog.dismiss() }
override fun fabClicks(): Observable<*> = fab.clicks()
@@ -21,6 +21,8 @@ package com.moez.QKSMS.feature.backup
import com.moez.QKSMS.common.Navigator
import com.moez.QKSMS.common.base.QkPresenter
import com.moez.QKSMS.common.util.BillingManager
import com.moez.QKSMS.interactor.PerformBackup
import com.moez.QKSMS.interactor.PerformRestore
import com.moez.QKSMS.repository.BackupRepository
import com.uber.autodispose.kotlin.autoDisposable
import io.reactivex.rxkotlin.plusAssign
@@ -30,10 +32,18 @@ import javax.inject.Inject
class BackupPresenter @Inject constructor(
backupRepo: BackupRepository,
private val billingManager: BillingManager,
private val navigator: Navigator
private val navigator: Navigator,
private val performBackup: PerformBackup,
private val performRestore: PerformRestore
) : QkPresenter<BackupView, BackupState>(BackupState()) {
init {
disposables += backupRepo.getBackupProgress()
.subscribe { progress -> newState { copy(backupProgress = progress) } }
disposables += backupRepo.getRestoreProgress()
.subscribe { progress -> newState { copy(restoreProgress = progress) } }
disposables += backupRepo.getBackups()
.subscribe { backups -> newState { copy(lastBackup = backups.map { it.date }.max(), backups = backups) } }
@@ -50,14 +60,14 @@ class BackupPresenter @Inject constructor(
view.restoreFileSelected()
.autoDisposable(view.scope())
.subscribe()
.subscribe { performRestore.execute(Unit) }
view.fabClicks()
.withLatestFrom(billingManager.upgradeStatus) { _, upgraded -> upgraded }
.autoDisposable(view.scope())
.subscribe { upgraded ->
when (upgraded) {
true -> navigator.showQksmsPlusActivity("backup_fab")
true -> performBackup.execute(Unit)
false -> navigator.showQksmsPlusActivity("backup_fab")
}
}
@@ -19,9 +19,12 @@
package com.moez.QKSMS.feature.backup
import com.moez.QKSMS.model.Backup
import com.moez.QKSMS.repository.BackupRepository
data class BackupState(
val upgraded: Boolean = false,
val backupProgress: BackupRepository.Progress = BackupRepository.Progress.Idle(),
val restoreProgress: BackupRepository.Progress = BackupRepository.Progress.Idle(),
val lastBackup: Long? = null,
val backups: List<Backup> = listOf()
val backups: List<Backup> = listOf(),
val upgraded: Boolean = false
)
@@ -0,0 +1,27 @@
<!--
~ Copyright (C) 2017 Moez Bhatti <moez.bhatti@gmail.com>
~
~ This file is part of QKSMS.
~
~ QKSMS is free software: you can redistribute it and/or modify
~ it under the terms of the GNU General Public License as published by
~ the Free Software Foundation, either version 3 of the License, or
~ (at your option) any later version.
~
~ QKSMS is distributed in the hope that it will be useful,
~ but WITHOUT ANY WARRANTY; without even the implied warranty of
~ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
~ GNU General Public License for more details.
~
~ You should have received a copy of the GNU General Public License
~ along with QKSMS. If not, see <http://www.gnu.org/licenses/>.
-->
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="#FF000000"
android:pathData="M19,6.41L17.59,5 12,10.59 6.41,5 5,6.41 10.59,12 5,17.59 6.41,19 12,13.41 17.59,19 19,17.59 13.41,12z"/>
</vector>
@@ -33,8 +33,90 @@
android:id="@+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:animateLayoutChanges="true"
android:orientation="vertical">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/progress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:animateLayoutChanges="true"
android:visibility="gone"
tools:visibility="visible">
<ImageView
android:id="@+id/progressIcon"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_marginStart="16dp"
android:tint="?android:attr/textColorSecondary"
app:layout_constraintBottom_toBottomOf="@id/progressSummary"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@id/progressTitle"
tools:src="@drawable/ic_file_download_black_24dp" />
<com.moez.QKSMS.common.widget.QkTextView
android:id="@+id/progressTitle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="24dp"
android:layout_marginTop="16dp"
android:textColor="?android:attr/textColorPrimary"
android:textStyle="bold"
app:layout_constraintEnd_toStartOf="@id/progressCancel"
app:layout_constraintStart_toEndOf="@id/progressIcon"
app:layout_constraintTop_toTopOf="parent"
app:textSize="primary"
tools:text="Restoring from backup" />
<com.moez.QKSMS.common.widget.QkTextView
android:id="@+id/progressSummary"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:textColor="?android:attr/textColorTertiary"
app:layout_constraintEnd_toEndOf="@id/progressTitle"
app:layout_constraintStart_toStartOf="@id/progressTitle"
app:layout_constraintTop_toBottomOf="@id/progressTitle"
app:textSize="secondary"
tools:text="8 minutes remaining" />
<ImageView
android:id="@+id/progressCancel"
android:layout_width="56dp"
android:layout_height="56dp"
android:background="?android:attr/selectableItemBackground"
android:padding="16dp"
android:src="@drawable/ic_close_black_24dp"
android:tint="?android:attr/textColorSecondary"
app:layout_constraintBottom_toBottomOf="@id/progressSummary"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="@id/progressTitle" />
<ProgressBar
android:id="@+id/progressBar"
style="@style/Widget.AppCompat.ProgressBar.Horizontal"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_marginTop="8dp"
android:indeterminate="true"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="@id/progressTitle"
app:layout_constraintTop_toBottomOf="@id/progressSummary" />
<View
android:layout_width="0dp"
android:layout_height="1dp"
android:layout_marginTop="16dp"
android:background="?android:attr/divider"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/progressBar" />
</androidx.constraintlayout.widget.ConstraintLayout>
<com.moez.QKSMS.common.widget.PreferenceView
android:id="@+id/backup"
android:layout_width="match_parent"
@@ -146,6 +146,8 @@
<string name="gallery_toast_saved">Saved to gallery</string>
<string name="backup_title">Backup and restore</string>
<string name="backup_backing_up">Backing up messages</string>
<string name="backup_restoring">Restoring from backup</string>
<string name="backup_last">Last backup</string>
<string name="backup_never">Never</string>
<string name="backup_restore_title">Restore</string>