|
| 1 | +package com.simplemobiletools.notes.pro.dialogs |
| 2 | + |
| 3 | +import android.view.ViewGroup |
| 4 | +import androidx.appcompat.app.AlertDialog |
| 5 | +import com.simplemobiletools.commons.dialogs.FilePickerDialog |
| 6 | +import com.simplemobiletools.commons.extensions.* |
| 7 | +import com.simplemobiletools.commons.helpers.ensureBackgroundThread |
| 8 | +import com.simplemobiletools.notes.pro.R |
| 9 | +import com.simplemobiletools.notes.pro.activities.SimpleActivity |
| 10 | +import com.simplemobiletools.notes.pro.extensions.config |
| 11 | +import kotlinx.android.synthetic.main.dialog_manage_automatic_backups.view.backup_notes_filename |
| 12 | +import kotlinx.android.synthetic.main.dialog_manage_automatic_backups.view.backup_notes_filename_hint |
| 13 | +import kotlinx.android.synthetic.main.dialog_manage_automatic_backups.view.backup_notes_folder |
| 14 | +import java.io.File |
| 15 | + |
| 16 | +class ManageAutoBackupsDialog(private val activity: SimpleActivity, onSuccess: () -> Unit) { |
| 17 | + private val view = (activity.layoutInflater.inflate(R.layout.dialog_manage_automatic_backups, null) as ViewGroup) |
| 18 | + private val config = activity.config |
| 19 | + private var backupFolder = config.autoBackupFolder |
| 20 | + |
| 21 | + init { |
| 22 | + view.apply { |
| 23 | + backup_notes_folder.setText(activity.humanizePath(backupFolder)) |
| 24 | + val filename = config.autoBackupFilename.ifEmpty { |
| 25 | + "${activity.getString(R.string.notes)}_%Y%M%D_%h%m%s" |
| 26 | + } |
| 27 | + |
| 28 | + backup_notes_filename.setText(filename) |
| 29 | + backup_notes_filename_hint.setEndIconOnClickListener { |
| 30 | + DateTimePatternInfoDialog(activity) |
| 31 | + } |
| 32 | + |
| 33 | + backup_notes_filename_hint.setEndIconOnLongClickListener { |
| 34 | + DateTimePatternInfoDialog(activity) |
| 35 | + true |
| 36 | + } |
| 37 | + |
| 38 | + backup_notes_folder.setOnClickListener { |
| 39 | + selectBackupFolder() |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + activity.getAlertDialogBuilder() |
| 44 | + .setPositiveButton(R.string.ok, null) |
| 45 | + .setNegativeButton(R.string.cancel, null) |
| 46 | + .apply { |
| 47 | + activity.setupDialogStuff(view, this, R.string.manage_automatic_backups) { dialog -> |
| 48 | + dialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener { |
| 49 | + val filename = view.backup_notes_filename.value |
| 50 | + when { |
| 51 | + filename.isEmpty() -> activity.toast(R.string.empty_name) |
| 52 | + filename.isAValidFilename() -> { |
| 53 | + val file = File(backupFolder, "$filename.json") |
| 54 | + if (file.exists() && !file.canWrite()) { |
| 55 | + activity.toast(R.string.name_taken) |
| 56 | + return@setOnClickListener |
| 57 | + } |
| 58 | + |
| 59 | + ensureBackgroundThread { |
| 60 | + config.apply { |
| 61 | + autoBackupFolder = backupFolder |
| 62 | + autoBackupFilename = filename |
| 63 | + } |
| 64 | + |
| 65 | + activity.runOnUiThread { |
| 66 | + onSuccess() |
| 67 | + } |
| 68 | + |
| 69 | + dialog.dismiss() |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + else -> activity.toast(R.string.invalid_name) |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + private fun selectBackupFolder() { |
| 81 | + activity.hideKeyboard(view.backup_notes_filename) |
| 82 | + FilePickerDialog(activity, backupFolder, false, showFAB = true) { path -> |
| 83 | + activity.handleSAFDialog(path) { grantedSAF -> |
| 84 | + if (!grantedSAF) { |
| 85 | + return@handleSAFDialog |
| 86 | + } |
| 87 | + |
| 88 | + activity.handleSAFDialogSdk30(path) { grantedSAF30 -> |
| 89 | + if (!grantedSAF30) { |
| 90 | + return@handleSAFDialogSdk30 |
| 91 | + } |
| 92 | + |
| 93 | + backupFolder = path |
| 94 | + view.backup_notes_folder.setText(activity.humanizePath(path)) |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | +} |
0 commit comments