mirror of
https://github.com/mihonapp/mihon.git
synced 2025-11-16 14:07:28 +01:00
Fully move backup progress/created dialog logic to notifications
This commit is contained in:
@@ -182,7 +182,9 @@ class SettingsBackupController : SettingsController() {
|
||||
|
||||
val file = UniFile.fromUri(activity, uri)
|
||||
|
||||
notifier.showBackupNotification()
|
||||
activity.toast(R.string.creating_backup)
|
||||
notifier.showBackupProgress()
|
||||
|
||||
BackupCreateService.makeBackup(activity, file.uri, backupFlags)
|
||||
}
|
||||
CODE_BACKUP_RESTORE -> if (data != null && resultCode == Activity.RESULT_OK) {
|
||||
@@ -247,35 +249,6 @@ class SettingsBackupController : SettingsController() {
|
||||
}
|
||||
}
|
||||
|
||||
class CreatedBackupDialog(bundle: Bundle? = null) : DialogController(bundle) {
|
||||
constructor(uri: Uri) : this(Bundle().apply {
|
||||
putParcelable(KEY_URI, uri)
|
||||
})
|
||||
|
||||
override fun onCreateDialog(savedViewState: Bundle?): Dialog {
|
||||
val activity = activity!!
|
||||
val unifile = UniFile.fromUri(activity, args.getParcelable(KEY_URI))
|
||||
return MaterialDialog.Builder(activity).apply {
|
||||
title(R.string.backup_created)
|
||||
if (unifile.filePath != null) {
|
||||
content(activity.getString(R.string.file_saved, unifile.filePath))
|
||||
}
|
||||
positiveText(R.string.action_close)
|
||||
negativeText(R.string.action_export)
|
||||
onNegative { _, _ ->
|
||||
val sendIntent = Intent(Intent.ACTION_SEND)
|
||||
sendIntent.type = "application/json"
|
||||
sendIntent.putExtra(Intent.EXTRA_STREAM, unifile.uri)
|
||||
startActivity(Intent.createChooser(sendIntent, ""))
|
||||
}
|
||||
}.build()
|
||||
}
|
||||
|
||||
private companion object {
|
||||
const val KEY_URI = "BackupCreatedDialog.uri"
|
||||
}
|
||||
}
|
||||
|
||||
class RestoreBackupDialog(bundle: Bundle? = null) : DialogController(bundle) {
|
||||
constructor(uri: Uri) : this(Bundle().apply {
|
||||
putParcelable(KEY_URI, uri)
|
||||
@@ -392,9 +365,12 @@ class SettingsBackupController : SettingsController() {
|
||||
override fun onReceive(context: Context, intent: Intent) {
|
||||
when (intent.getStringExtra(BackupConst.ACTION)) {
|
||||
BackupConst.ACTION_BACKUP_COMPLETED_DIALOG -> {
|
||||
notifier.dismiss()
|
||||
val uri = Uri.parse(intent.getStringExtra(BackupConst.EXTRA_URI))
|
||||
CreatedBackupDialog(uri).showDialog(router)
|
||||
val unifile = UniFile.fromUri(activity, uri)
|
||||
notifier.showBackupComplete(unifile)
|
||||
}
|
||||
BackupConst.ACTION_ERROR_BACKUP_DIALOG -> {
|
||||
notifier.showBackupError(intent.getStringExtra(BackupConst.EXTRA_ERROR_MESSAGE))
|
||||
}
|
||||
BackupConst.ACTION_SET_PROGRESS_DIALOG -> {
|
||||
val progress = intent.getIntExtra(BackupConst.EXTRA_PROGRESS, 0)
|
||||
@@ -413,10 +389,6 @@ class SettingsBackupController : SettingsController() {
|
||||
RestoredBackupDialog(time, errors, path, file).showDialog(router)
|
||||
}
|
||||
}
|
||||
BackupConst.ACTION_ERROR_BACKUP_DIALOG -> {
|
||||
notifier.dismiss()
|
||||
context.toast(intent.getStringExtra(BackupConst.EXTRA_ERROR_MESSAGE))
|
||||
}
|
||||
BackupConst.ACTION_ERROR_RESTORE_DIALOG -> {
|
||||
router.popControllerWithTag(TAG_RESTORING_BACKUP_DIALOG)
|
||||
context.toast(intent.getStringExtra(BackupConst.EXTRA_ERROR_MESSAGE))
|
||||
|
||||
@@ -3,7 +3,9 @@ package eu.kanade.tachiyomi.ui.setting.backup
|
||||
import android.content.Context
|
||||
import android.graphics.BitmapFactory
|
||||
import androidx.core.app.NotificationCompat
|
||||
import com.hippo.unifile.UniFile
|
||||
import eu.kanade.tachiyomi.R
|
||||
import eu.kanade.tachiyomi.data.notification.NotificationReceiver
|
||||
import eu.kanade.tachiyomi.data.notification.Notifications
|
||||
import eu.kanade.tachiyomi.util.system.notificationBuilder
|
||||
import eu.kanade.tachiyomi.util.system.notificationManager
|
||||
@@ -14,15 +16,11 @@ internal class BackupNotifier(private val context: Context) {
|
||||
setLargeIcon(BitmapFactory.decodeResource(context.resources, R.mipmap.ic_launcher))
|
||||
}
|
||||
|
||||
private fun NotificationCompat.Builder.show(id: Int = Notifications.ID_BACKUP) {
|
||||
private fun NotificationCompat.Builder.show(id: Int) {
|
||||
context.notificationManager.notify(id, build())
|
||||
}
|
||||
|
||||
fun dismiss() {
|
||||
context.notificationManager.cancel(Notifications.ID_BACKUP)
|
||||
}
|
||||
|
||||
fun showBackupNotification() {
|
||||
fun showBackupProgress() {
|
||||
with(notificationBuilder) {
|
||||
setSmallIcon(R.drawable.ic_tachi)
|
||||
setAutoCancel(false)
|
||||
@@ -33,6 +31,48 @@ internal class BackupNotifier(private val context: Context) {
|
||||
setProgress(0, 0, true)
|
||||
}
|
||||
|
||||
notificationBuilder.show()
|
||||
notificationBuilder.show(Notifications.ID_BACKUP)
|
||||
}
|
||||
|
||||
fun showBackupError(error: String) {
|
||||
with(notificationBuilder) {
|
||||
setSmallIcon(R.drawable.ic_tachi)
|
||||
setAutoCancel(false)
|
||||
|
||||
setContentTitle(context.getString(R.string.creating_backup_error))
|
||||
setContentText(error)
|
||||
|
||||
// Remove progress bar
|
||||
setProgress(0, 0, false)
|
||||
}
|
||||
|
||||
notificationBuilder.show(Notifications.ID_BACKUP)
|
||||
}
|
||||
|
||||
fun showBackupComplete(unifile: UniFile) {
|
||||
with(notificationBuilder) {
|
||||
setSmallIcon(R.drawable.ic_tachi)
|
||||
setAutoCancel(false)
|
||||
|
||||
setContentTitle(context.getString(R.string.backup_created))
|
||||
|
||||
if (unifile.filePath != null) {
|
||||
setContentText(context.getString(R.string.file_saved, unifile.filePath))
|
||||
}
|
||||
|
||||
// Remove progress bar
|
||||
setProgress(0, 0, false)
|
||||
|
||||
// Clear old actions if they exist
|
||||
if (mActions.isNotEmpty()) {
|
||||
mActions.clear()
|
||||
}
|
||||
|
||||
addAction(R.drawable.ic_share_24dp,
|
||||
context.getString(R.string.action_share),
|
||||
NotificationReceiver.shareBackup(context, unifile.uri, Notifications.ID_BACKUP))
|
||||
}
|
||||
|
||||
notificationBuilder.show(Notifications.ID_BACKUP)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user