forked from upstream-mirrors/quik
Migrate backup & restore feature to use SAF (#1900)
* Migrate backup & restore feature to use SAF * Remove unused imports * Remove unused rx subject * Remove unused dateFormatter * Open picker to current dir * Improve exception messages when handling DocumentFile
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
package com.moez.QKSMS.common.util
|
||||
|
||||
import android.app.Activity
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.net.Uri
|
||||
import android.os.Build
|
||||
import android.provider.DocumentsContract
|
||||
import androidx.activity.result.contract.ActivityResultContract
|
||||
|
||||
class QkActivityResultContracts {
|
||||
|
||||
data class OpenDocumentParams(
|
||||
val mimeTypes: List<String>,
|
||||
val initialUri: Uri? = null
|
||||
)
|
||||
|
||||
class OpenDocument : ActivityResultContract<OpenDocumentParams, Uri>() {
|
||||
override fun createIntent(context: Context, input: OpenDocumentParams): Intent {
|
||||
val intent = Intent(Intent.ACTION_OPEN_DOCUMENT)
|
||||
.putExtra(Intent.EXTRA_MIME_TYPES, input.mimeTypes.toTypedArray())
|
||||
.setType("*/*")
|
||||
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||
intent.putExtra(DocumentsContract.EXTRA_INITIAL_URI, input.initialUri)
|
||||
}
|
||||
|
||||
return intent
|
||||
}
|
||||
|
||||
override fun parseResult(resultCode: Int, intent: Intent?): Uri? {
|
||||
if (intent == null || resultCode != Activity.RESULT_OK) {
|
||||
return null
|
||||
}
|
||||
|
||||
return intent.data
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
+9
-1
@@ -32,4 +32,12 @@ fun AlertDialog.Builder.setNegativeButton(@StringRes textId: Int, subject: Subje
|
||||
|
||||
fun AlertDialog.Builder.setNeutralButton(@StringRes textId: Int, subject: Subject<Unit>): AlertDialog.Builder {
|
||||
return setNeutralButton(textId) { _, _ -> subject.onNext(Unit) }
|
||||
}
|
||||
}
|
||||
|
||||
fun AlertDialog.setShowing(show: Boolean) {
|
||||
if (isShowing && !show) {
|
||||
dismiss()
|
||||
} else if (!isShowing && show) {
|
||||
show()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,61 +0,0 @@
|
||||
/*
|
||||
* 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.feature.backup
|
||||
|
||||
import android.content.Context
|
||||
import android.text.format.Formatter
|
||||
import android.view.LayoutInflater
|
||||
import android.view.ViewGroup
|
||||
import com.moez.QKSMS.R
|
||||
import com.moez.QKSMS.common.base.FlowableAdapter
|
||||
import com.moez.QKSMS.common.base.QkViewHolder
|
||||
import com.moez.QKSMS.common.util.DateFormatter
|
||||
import com.moez.QKSMS.model.BackupFile
|
||||
import io.reactivex.subjects.PublishSubject
|
||||
import io.reactivex.subjects.Subject
|
||||
import kotlinx.android.synthetic.main.backup_list_item.*
|
||||
import javax.inject.Inject
|
||||
|
||||
class BackupAdapter @Inject constructor(
|
||||
private val context: Context,
|
||||
private val dateFormatter: DateFormatter
|
||||
) : FlowableAdapter<BackupFile>() {
|
||||
|
||||
val backupSelected: Subject<BackupFile> = PublishSubject.create()
|
||||
|
||||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): QkViewHolder {
|
||||
val layoutInflater = LayoutInflater.from(parent.context)
|
||||
val view = layoutInflater.inflate(R.layout.backup_list_item, parent, false)
|
||||
|
||||
return QkViewHolder(view).apply {
|
||||
view.setOnClickListener { backupSelected.onNext(getItem(adapterPosition)) }
|
||||
}
|
||||
}
|
||||
|
||||
override fun onBindViewHolder(holder: QkViewHolder, position: Int) {
|
||||
val backup = getItem(position)
|
||||
|
||||
val count = backup.messages
|
||||
|
||||
holder.title.text = dateFormatter.getDetailedTimestamp(backup.date)
|
||||
holder.messages.text = context.resources.getQuantityString(R.plurals.backup_message_count, count, count)
|
||||
holder.size.text = Formatter.formatFileSize(context, backup.size)
|
||||
}
|
||||
|
||||
}
|
||||
@@ -18,78 +18,112 @@
|
||||
*/
|
||||
package com.moez.QKSMS.feature.backup
|
||||
|
||||
import android.Manifest
|
||||
import android.app.Activity
|
||||
import android.content.Context
|
||||
import android.content.res.ColorStateList
|
||||
import android.graphics.Typeface
|
||||
import android.net.Uri
|
||||
import android.view.View
|
||||
import androidx.activity.result.ActivityResultLauncher
|
||||
import androidx.activity.result.contract.ActivityResultContracts
|
||||
import androidx.appcompat.app.AlertDialog
|
||||
import androidx.core.app.ActivityCompat
|
||||
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
|
||||
import com.moez.QKSMS.common.util.DateFormatter
|
||||
import com.moez.QKSMS.common.util.QkActivityResultContracts
|
||||
import com.moez.QKSMS.common.util.extensions.getLabel
|
||||
import com.moez.QKSMS.common.util.extensions.setBackgroundTint
|
||||
import com.moez.QKSMS.common.util.extensions.setNegativeButton
|
||||
import com.moez.QKSMS.common.util.extensions.setPositiveButton
|
||||
import com.moez.QKSMS.common.util.extensions.setShowing
|
||||
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.BackupFile
|
||||
import com.moez.QKSMS.repository.BackupRepository
|
||||
import io.reactivex.Observable
|
||||
import io.reactivex.subjects.PublishSubject
|
||||
import io.reactivex.subjects.Subject
|
||||
import kotlinx.android.synthetic.main.backup_controller.*
|
||||
import kotlinx.android.synthetic.main.backup_list_dialog.view.*
|
||||
import kotlinx.android.synthetic.main.preference_view.view.*
|
||||
import javax.inject.Inject
|
||||
|
||||
class BackupController : QkController<BackupView, BackupState, BackupPresenter>(), BackupView {
|
||||
|
||||
@Inject lateinit var adapter: BackupAdapter
|
||||
@Inject lateinit var dateFormatter: DateFormatter
|
||||
@Inject override lateinit var presenter: BackupPresenter
|
||||
|
||||
private val activityVisibleSubject: Subject<Unit> = PublishSubject.create()
|
||||
private val confirmRestoreSubject: Subject<Unit> = PublishSubject.create()
|
||||
private val stopRestoreSubject: Subject<Unit> = PublishSubject.create()
|
||||
private val selectFolderCancelSubject: Subject<Unit> = PublishSubject.create()
|
||||
private val selectFolderConfirmSubject: Subject<Unit> = PublishSubject.create()
|
||||
|
||||
private val backupFilesDialog by lazy {
|
||||
val view = View.inflate(activity, R.layout.backup_list_dialog, null)
|
||||
.apply { files.adapter = adapter.apply { emptyView = empty } }
|
||||
private val restoreErrorConfirmSubject: Subject<Unit> = PublishSubject.create()
|
||||
|
||||
AlertDialog.Builder(activity!!)
|
||||
.setView(view)
|
||||
.setCancelable(true)
|
||||
.create()
|
||||
}
|
||||
private val confirmRestoreCancelSubject: Subject<Unit> = PublishSubject.create()
|
||||
private val confirmRestoreConfirmSubject: Subject<Unit> = PublishSubject.create()
|
||||
|
||||
private val confirmRestoreDialog by lazy {
|
||||
AlertDialog.Builder(activity!!)
|
||||
.setTitle(R.string.backup_restore_confirm_title)
|
||||
.setMessage(R.string.backup_restore_confirm_message)
|
||||
.setPositiveButton(R.string.backup_restore_title, confirmRestoreSubject)
|
||||
.setNegativeButton(R.string.button_cancel, null)
|
||||
.create()
|
||||
}
|
||||
private val stopRestoreConfirmSubject: Subject<Unit> = PublishSubject.create()
|
||||
private val stopRestoreCancelSubject: Subject<Unit> = PublishSubject.create()
|
||||
|
||||
private val documentTreeSelectedSubject: Subject<Uri> = PublishSubject.create()
|
||||
private val documentSelectedSubject: Subject<Uri> = PublishSubject.create()
|
||||
|
||||
private val stopRestoreDialog by lazy {
|
||||
AlertDialog.Builder(activity!!)
|
||||
.setTitle(R.string.backup_restore_stop_title)
|
||||
.setMessage(R.string.backup_restore_stop_message)
|
||||
.setPositiveButton(R.string.button_stop, stopRestoreSubject)
|
||||
.setNegativeButton(R.string.button_cancel, null)
|
||||
.setPositiveButton(R.string.button_stop, stopRestoreConfirmSubject)
|
||||
.setNegativeButton(R.string.button_cancel, stopRestoreCancelSubject)
|
||||
.setCancelable(false)
|
||||
.create()
|
||||
}
|
||||
|
||||
private val selectLocationRationaleDialog by lazy {
|
||||
AlertDialog.Builder(activity!!)
|
||||
.setTitle(R.string.backup_select_location_rationale_title)
|
||||
.setMessage(R.string.backup_select_location_rationale_message)
|
||||
.setPositiveButton(R.string.button_continue, selectFolderConfirmSubject)
|
||||
.setNegativeButton(R.string.button_cancel, selectFolderCancelSubject)
|
||||
.setCancelable(false)
|
||||
.create()
|
||||
}
|
||||
|
||||
private val selectedBackupErrorDialog by lazy {
|
||||
AlertDialog.Builder(activity!!)
|
||||
.setTitle(R.string.backup_selected_backup_error_title)
|
||||
.setMessage(R.string.backup_selected_backup_error_message)
|
||||
.setPositiveButton(R.string.button_continue, restoreErrorConfirmSubject)
|
||||
.setCancelable(false)
|
||||
.create()
|
||||
}
|
||||
|
||||
private val selectedBackupDetailsDialog by lazy {
|
||||
AlertDialog.Builder(activity!!)
|
||||
.setTitle(R.string.backup_selected_backup_details_title)
|
||||
.setPositiveButton(R.string.backup_restore_title, confirmRestoreConfirmSubject)
|
||||
.setNegativeButton(R.string.button_cancel, confirmRestoreCancelSubject)
|
||||
.setCancelable(false)
|
||||
.create()
|
||||
}
|
||||
|
||||
private lateinit var openDirectory: ActivityResultLauncher<Uri>
|
||||
private lateinit var openDocument: ActivityResultLauncher<QkActivityResultContracts.OpenDocumentParams>
|
||||
|
||||
init {
|
||||
appComponent.inject(this)
|
||||
layoutRes = R.layout.backup_controller
|
||||
}
|
||||
|
||||
override fun onContextAvailable(context: Context) {
|
||||
// Init activity result contracts
|
||||
openDirectory = themedActivity!!.registerForActivityResult(
|
||||
ActivityResultContracts.OpenDocumentTree(),
|
||||
documentTreeSelectedSubject::onNext)
|
||||
|
||||
openDocument = themedActivity!!.registerForActivityResult(
|
||||
QkActivityResultContracts.OpenDocument(),
|
||||
documentSelectedSubject::onNext)
|
||||
}
|
||||
|
||||
override fun onAttach(view: View) {
|
||||
super.onAttach(view)
|
||||
presenter.bindIntents(this)
|
||||
@@ -115,11 +149,6 @@ class BackupController : QkController<BackupView, BackupState, BackupPresenter>(
|
||||
.forEach { it.setTypeface(it.typeface, Typeface.BOLD) }
|
||||
}
|
||||
|
||||
override fun onActivityResumed(activity: Activity) {
|
||||
super.onActivityResumed(activity)
|
||||
activityVisibleSubject.onNext(Unit)
|
||||
}
|
||||
|
||||
override fun render(state: BackupState) {
|
||||
when {
|
||||
state.backupProgress.running -> {
|
||||
@@ -158,9 +187,14 @@ class BackupController : QkController<BackupView, BackupState, BackupPresenter>(
|
||||
}
|
||||
}
|
||||
|
||||
backup.summary = state.lastBackup
|
||||
selectLocationRationaleDialog.setShowing(state.showLocationRationale)
|
||||
|
||||
adapter.data = state.backups
|
||||
selectedBackupErrorDialog.setShowing(state.showSelectedBackupError)
|
||||
|
||||
selectedBackupDetailsDialog.setMessage(state.selectedBackupDetails)
|
||||
selectedBackupDetailsDialog.setShowing(state.selectedBackupDetails != null)
|
||||
|
||||
stopRestoreDialog.setShowing(state.showStopRestoreDialog)
|
||||
|
||||
fabIcon.setImageResource(when (state.upgraded) {
|
||||
true -> R.drawable.ic_file_upload_black_24dp
|
||||
@@ -173,29 +207,40 @@ class BackupController : QkController<BackupView, BackupState, BackupPresenter>(
|
||||
})
|
||||
}
|
||||
|
||||
override fun activityVisible(): Observable<*> = activityVisibleSubject
|
||||
override fun setBackupLocationClicks(): Observable<*> = location.clicks()
|
||||
|
||||
override fun restoreClicks(): Observable<*> = restore.clicks()
|
||||
|
||||
override fun restoreFileSelected(): Observable<BackupFile> = adapter.backupSelected
|
||||
.doOnNext { backupFilesDialog.dismiss() }
|
||||
override fun locationRationaleConfirmClicks(): Observable<*> = selectFolderConfirmSubject
|
||||
|
||||
override fun restoreConfirmed(): Observable<*> = confirmRestoreSubject
|
||||
override fun locationRationaleCancelClicks(): Observable<*> = selectFolderCancelSubject
|
||||
|
||||
override fun selectedBackupErrorClicks(): Observable<*> = restoreErrorConfirmSubject
|
||||
|
||||
override fun confirmRestoreBackupConfirmClicks(): Observable<*> = confirmRestoreConfirmSubject
|
||||
|
||||
override fun confirmRestoreBackupCancelClicks(): Observable<*> = confirmRestoreCancelSubject
|
||||
|
||||
override fun stopRestoreClicks(): Observable<*> = progressCancel.clicks()
|
||||
|
||||
override fun stopRestoreConfirmed(): Observable<*> = stopRestoreSubject
|
||||
override fun stopRestoreConfirmed(): Observable<*> = stopRestoreConfirmSubject
|
||||
|
||||
override fun fabClicks(): Observable<*> = fab.clicks()
|
||||
override fun stopRestoreCancel(): Observable<*> = stopRestoreCancelSubject
|
||||
|
||||
override fun requestStoragePermission() {
|
||||
ActivityCompat.requestPermissions(activity!!, arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE), 0)
|
||||
override fun backupClicks(): Observable<*> = fab.clicks()
|
||||
|
||||
override fun documentTreeSelected(): Observable<Uri> = documentTreeSelectedSubject
|
||||
|
||||
override fun documentSelected(): Observable<Uri> = documentSelectedSubject
|
||||
|
||||
override fun selectFolder(initialUri: Uri) {
|
||||
openDirectory.launch(initialUri)
|
||||
}
|
||||
|
||||
override fun selectFile() = backupFilesDialog.show()
|
||||
override fun selectFile(initialUri: Uri) {
|
||||
openDocument.launch(QkActivityResultContracts.OpenDocumentParams(
|
||||
mimeTypes = listOf("application/json"),
|
||||
initialUri = initialUri))
|
||||
}
|
||||
|
||||
override fun confirmRestore() = confirmRestoreDialog.show()
|
||||
|
||||
override fun stopRestore() = stopRestoreDialog.show()
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,6 +19,7 @@
|
||||
package com.moez.QKSMS.feature.backup
|
||||
|
||||
import android.content.Context
|
||||
import androidx.core.net.toUri
|
||||
import com.moez.QKSMS.R
|
||||
import com.moez.QKSMS.common.Navigator
|
||||
import com.moez.QKSMS.common.base.QkPresenter
|
||||
@@ -26,14 +27,14 @@ import com.moez.QKSMS.common.util.DateFormatter
|
||||
import com.moez.QKSMS.common.util.extensions.makeToast
|
||||
import com.moez.QKSMS.interactor.PerformBackup
|
||||
import com.moez.QKSMS.manager.BillingManager
|
||||
import com.moez.QKSMS.manager.PermissionManager
|
||||
import com.moez.QKSMS.repository.BackupRepository
|
||||
import com.uber.autodispose.android.lifecycle.scope
|
||||
import com.uber.autodispose.autoDisposable
|
||||
import io.reactivex.android.schedulers.AndroidSchedulers
|
||||
import io.reactivex.rxkotlin.plusAssign
|
||||
import io.reactivex.rxkotlin.withLatestFrom
|
||||
import io.reactivex.subjects.BehaviorSubject
|
||||
import io.reactivex.subjects.Subject
|
||||
import io.reactivex.schedulers.Schedulers
|
||||
import timber.log.Timber
|
||||
import java.util.concurrent.TimeUnit
|
||||
import javax.inject.Inject
|
||||
|
||||
@@ -43,12 +44,9 @@ class BackupPresenter @Inject constructor(
|
||||
private val context: Context,
|
||||
private val dateFormatter: DateFormatter,
|
||||
private val navigator: Navigator,
|
||||
private val performBackup: PerformBackup,
|
||||
private val permissionManager: PermissionManager
|
||||
private val performBackup: PerformBackup
|
||||
) : QkPresenter<BackupView, BackupState>(BackupState()) {
|
||||
|
||||
private val storagePermissionSubject: Subject<Boolean> = BehaviorSubject.createDefault(permissionManager.hasStorage())
|
||||
|
||||
init {
|
||||
disposables += backupRepo.getBackupProgress()
|
||||
.sample(16, TimeUnit.MILLISECONDS)
|
||||
@@ -60,20 +58,6 @@ class BackupPresenter @Inject constructor(
|
||||
.distinctUntilChanged()
|
||||
.subscribe { progress -> newState { copy(restoreProgress = progress) } }
|
||||
|
||||
disposables += storagePermissionSubject
|
||||
.distinctUntilChanged()
|
||||
.switchMap { backupRepo.getBackups() }
|
||||
.doOnNext { backups -> newState { copy(backups = backups) } }
|
||||
.map { backups -> backups.maxOfOrNull { it.date } ?: 0L }
|
||||
.map { lastBackup ->
|
||||
when (lastBackup) {
|
||||
0L -> context.getString(R.string.backup_never)
|
||||
else -> dateFormatter.getDetailedTimestamp(lastBackup)
|
||||
}
|
||||
}
|
||||
.startWith(context.getString(R.string.backup_loading))
|
||||
.subscribe { lastBackup -> newState { copy(lastBackup = lastBackup) } }
|
||||
|
||||
disposables += billingManager.upgradeStatus
|
||||
.subscribe { upgraded -> newState { copy(upgraded = upgraded) } }
|
||||
}
|
||||
@@ -81,10 +65,10 @@ class BackupPresenter @Inject constructor(
|
||||
override fun bindIntents(view: BackupView) {
|
||||
super.bindIntents(view)
|
||||
|
||||
view.activityVisible()
|
||||
.map { permissionManager.hasStorage() }
|
||||
view.setBackupLocationClicks()
|
||||
.observeOn(AndroidSchedulers.mainThread())
|
||||
.autoDisposable(view.scope())
|
||||
.subscribe(storagePermissionSubject)
|
||||
.subscribe { view.selectFolder(backupRepo.getBackupPathUriForPicker()) }
|
||||
|
||||
view.restoreClicks()
|
||||
.withLatestFrom(
|
||||
@@ -96,40 +80,80 @@ class BackupPresenter @Inject constructor(
|
||||
!upgraded -> context.makeToast(R.string.backup_restore_error_plus)
|
||||
backupProgress.running -> context.makeToast(R.string.backup_restore_error_backup)
|
||||
restoreProgress.running -> context.makeToast(R.string.backup_restore_error_restore)
|
||||
!permissionManager.hasStorage() -> view.requestStoragePermission()
|
||||
else -> view.selectFile()
|
||||
else -> view.selectFile(backupRepo.getBackupPathUriForPicker())
|
||||
}
|
||||
}
|
||||
.autoDisposable(view.scope())
|
||||
.subscribe()
|
||||
|
||||
view.restoreFileSelected()
|
||||
.autoDisposable(view.scope())
|
||||
.subscribe { view.confirmRestore() }
|
||||
|
||||
view.restoreConfirmed()
|
||||
.withLatestFrom(view.restoreFileSelected()) { _, backup -> backup }
|
||||
.autoDisposable(view.scope())
|
||||
.subscribe { backup -> RestoreBackupService.start(context, backup.path) }
|
||||
|
||||
view.stopRestoreClicks()
|
||||
.autoDisposable(view.scope())
|
||||
.subscribe { view.stopRestore() }
|
||||
|
||||
view.stopRestoreConfirmed()
|
||||
.autoDisposable(view.scope())
|
||||
.subscribe { backupRepo.stopRestore() }
|
||||
|
||||
view.fabClicks()
|
||||
view.backupClicks()
|
||||
.withLatestFrom(billingManager.upgradeStatus) { _, upgraded -> upgraded }
|
||||
.autoDisposable(view.scope())
|
||||
.subscribe { upgraded ->
|
||||
when {
|
||||
backupRepo.getBackupDocumentTree() == null -> {
|
||||
newState { copy(showLocationRationale = true) }
|
||||
}
|
||||
!upgraded -> navigator.showQksmsPlusActivity("backup_fab")
|
||||
!permissionManager.hasStorage() -> view.requestStoragePermission()
|
||||
upgraded -> performBackup.execute(Unit)
|
||||
}
|
||||
}
|
||||
|
||||
view.locationRationaleConfirmClicks()
|
||||
.doOnNext { newState { copy(showLocationRationale = false) } }
|
||||
.autoDisposable(view.scope())
|
||||
.subscribe { view.selectFolder(backupRepo.getBackupPathUriForPicker()) }
|
||||
|
||||
view.locationRationaleCancelClicks()
|
||||
.doOnNext { newState { copy(showLocationRationale = false) } }
|
||||
.autoDisposable(view.scope())
|
||||
.subscribe()
|
||||
|
||||
view.selectedBackupErrorClicks()
|
||||
.autoDisposable(view.scope())
|
||||
.subscribe { newState { copy(showSelectedBackupError = false) } }
|
||||
|
||||
view.confirmRestoreBackupConfirmClicks()
|
||||
.doOnNext { newState { copy(selectedBackupDetails = null) } }
|
||||
.withLatestFrom(view.documentSelected()) { _, backup -> backup }
|
||||
.autoDisposable(view.scope())
|
||||
.subscribe { backup -> RestoreBackupService.start(context, backup) }
|
||||
|
||||
view.confirmRestoreBackupCancelClicks()
|
||||
.doOnNext { newState { copy(selectedBackupDetails = null) } }
|
||||
.autoDisposable(view.scope())
|
||||
.subscribe()
|
||||
|
||||
view.stopRestoreClicks()
|
||||
.autoDisposable(view.scope())
|
||||
.subscribe { newState { copy(showStopRestoreDialog = true) } }
|
||||
|
||||
view.stopRestoreConfirmed()
|
||||
.doOnNext { newState { copy(showStopRestoreDialog = false) } }
|
||||
.autoDisposable(view.scope())
|
||||
.subscribe { backupRepo.stopRestore() }
|
||||
|
||||
view.stopRestoreCancel()
|
||||
.autoDisposable(view.scope())
|
||||
.subscribe { newState { copy(showStopRestoreDialog = false) } }
|
||||
|
||||
view.documentTreeSelected()
|
||||
.autoDisposable(view.scope())
|
||||
.subscribe { uri -> backupRepo.persistBackupDirectory(uri) }
|
||||
|
||||
view.documentSelected()
|
||||
.observeOn(Schedulers.io())
|
||||
.autoDisposable(view.scope())
|
||||
.subscribe { uri ->
|
||||
try {
|
||||
val backupFile = backupRepo.parseBackup(uri)
|
||||
val date = dateFormatter.getDetailedTimestamp(backupFile.date)
|
||||
val details = context.getString(R.string.backup_details, date, backupFile.messages)
|
||||
newState { copy(selectedBackupDetails = details) }
|
||||
} catch (e: Exception) {
|
||||
newState { copy(showSelectedBackupError = true) }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -18,13 +18,16 @@
|
||||
*/
|
||||
package com.moez.QKSMS.feature.backup
|
||||
|
||||
import com.moez.QKSMS.model.BackupFile
|
||||
import com.moez.QKSMS.repository.BackupRepository
|
||||
|
||||
data class BackupState(
|
||||
val backupProgress: BackupRepository.Progress = BackupRepository.Progress.Idle(),
|
||||
val restoreProgress: BackupRepository.Progress = BackupRepository.Progress.Idle(),
|
||||
val lastBackup: String = "",
|
||||
val backups: List<BackupFile> = listOf(),
|
||||
|
||||
val showLocationRationale: Boolean = false,
|
||||
val showSelectedBackupError: Boolean = false,
|
||||
val selectedBackupDetails: String? = null,
|
||||
val showStopRestoreDialog: Boolean = false,
|
||||
|
||||
val upgraded: Boolean = false
|
||||
)
|
||||
)
|
||||
|
||||
@@ -18,23 +18,31 @@
|
||||
*/
|
||||
package com.moez.QKSMS.feature.backup
|
||||
|
||||
import android.net.Uri
|
||||
import com.moez.QKSMS.common.base.QkViewContract
|
||||
import com.moez.QKSMS.model.BackupFile
|
||||
import io.reactivex.Observable
|
||||
|
||||
interface BackupView : QkViewContract<BackupState> {
|
||||
|
||||
fun activityVisible(): Observable<*>
|
||||
fun setBackupLocationClicks(): Observable<*>
|
||||
fun restoreClicks(): Observable<*>
|
||||
fun restoreFileSelected(): Observable<BackupFile>
|
||||
fun restoreConfirmed(): Observable<*>
|
||||
|
||||
fun backupClicks(): Observable<*>
|
||||
|
||||
fun locationRationaleConfirmClicks(): Observable<*>
|
||||
fun locationRationaleCancelClicks(): Observable<*>
|
||||
|
||||
fun selectedBackupErrorClicks(): Observable<*>
|
||||
|
||||
fun confirmRestoreBackupConfirmClicks(): Observable<*>
|
||||
fun confirmRestoreBackupCancelClicks(): Observable<*>
|
||||
|
||||
fun stopRestoreClicks(): Observable<*>
|
||||
fun stopRestoreConfirmed(): Observable<*>
|
||||
fun fabClicks(): Observable<*>
|
||||
fun stopRestoreCancel(): Observable<*>
|
||||
|
||||
fun requestStoragePermission()
|
||||
fun selectFile()
|
||||
fun confirmRestore()
|
||||
fun stopRestore()
|
||||
fun documentTreeSelected(): Observable<Uri>
|
||||
fun documentSelected(): Observable<Uri>
|
||||
|
||||
}
|
||||
fun selectFolder(initialUri: Uri)
|
||||
fun selectFile(initialUri: Uri)
|
||||
}
|
||||
|
||||
@@ -22,6 +22,7 @@ import android.annotation.SuppressLint
|
||||
import android.app.Service
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.net.Uri
|
||||
import android.os.IBinder
|
||||
import androidx.core.app.NotificationManagerCompat
|
||||
import androidx.core.content.ContextCompat
|
||||
@@ -43,12 +44,12 @@ class RestoreBackupService : Service() {
|
||||
|
||||
private const val ACTION_START = "com.moez.QKSMS.ACTION_START"
|
||||
private const val ACTION_STOP = "com.moez.QKSMS.ACTION_STOP"
|
||||
private const val EXTRA_FILE_PATH = "com.moez.QKSMS.EXTRA_FILE_PATH"
|
||||
private const val EXTRA_FILE_URI = "com.moez.QKSMS.EXTRA_FILE_URI"
|
||||
|
||||
fun start(context: Context, filePath: String) {
|
||||
fun start(context: Context, backupFile: Uri) {
|
||||
val intent = Intent(context, RestoreBackupService::class.java)
|
||||
.setAction(ACTION_START)
|
||||
.putExtra(EXTRA_FILE_PATH, filePath)
|
||||
.putExtra(EXTRA_FILE_URI, backupFile.toString())
|
||||
|
||||
ContextCompat.startForegroundService(context, intent)
|
||||
}
|
||||
@@ -100,7 +101,7 @@ class RestoreBackupService : Service() {
|
||||
|
||||
// Start the restore
|
||||
Observable.just(intent)
|
||||
.map { it.getStringExtra(EXTRA_FILE_PATH) }
|
||||
.map { Uri.parse(it.getStringExtra(EXTRA_FILE_URI)) }
|
||||
.map(backupRepo::performRestore)
|
||||
.subscribeOn(Schedulers.io())
|
||||
.subscribe({}, Timber::w)
|
||||
|
||||
Reference in New Issue
Block a user