From 9fa7bb9c6da228075bae733bcc5c602eb1caac66 Mon Sep 17 00:00:00 2001 From: Bnyro Date: Sun, 18 Sep 2022 12:49:57 +0200 Subject: [PATCH] add settings entry --- .../github/libretube/dialogs/BackupDialog.kt | 21 +++++++++- .../libretube/preferences/AdvancedSettings.kt | 40 ++++++++++++++----- app/src/main/res/xml/advanced_settings.xml | 16 +++++++- 3 files changed, 65 insertions(+), 12 deletions(-) diff --git a/app/src/main/java/com/github/libretube/dialogs/BackupDialog.kt b/app/src/main/java/com/github/libretube/dialogs/BackupDialog.kt index 48dd885b0..1dcabc176 100644 --- a/app/src/main/java/com/github/libretube/dialogs/BackupDialog.kt +++ b/app/src/main/java/com/github/libretube/dialogs/BackupDialog.kt @@ -1,7 +1,10 @@ package com.github.libretube.dialogs import android.app.Dialog +import android.net.Uri import android.os.Bundle +import androidx.activity.result.ActivityResultLauncher +import androidx.activity.result.contract.ActivityResultContracts import androidx.fragment.app.DialogFragment import androidx.recyclerview.widget.LinearLayoutManager import com.github.libretube.R @@ -9,10 +12,24 @@ import com.github.libretube.adapters.BackupOptionsAdapter import com.github.libretube.databinding.DialogBackupBinding import com.github.libretube.db.DatabaseHolder import com.github.libretube.obj.BackupFile +import com.github.libretube.util.BackupHelper import com.google.android.material.dialog.MaterialAlertDialogBuilder class BackupDialog() : DialogFragment() { private lateinit var binding: DialogBackupBinding + private lateinit var createBackupFile: ActivityResultLauncher + + val backupFile = BackupFile() + + override fun onCreate(savedInstanceState: Bundle?) { + createBackupFile = registerForActivityResult( + ActivityResultContracts.CreateDocument("application/json") + ) { uri: Uri? -> + BackupHelper(requireContext()).advancedBackup(uri, backupFile) + } + + super.onCreate(savedInstanceState) + } override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { val backupOptions = listOf( @@ -25,8 +42,6 @@ class BackupDialog() : DialogFragment() { val selected = mutableListOf(false, false, false, false, false) - val backupFile = BackupFile() - binding = DialogBackupBinding.inflate(layoutInflater) binding.backupOptionsRecycler.layoutManager = LinearLayoutManager(context) binding.backupOptionsRecycler.adapter = BackupOptionsAdapter(backupOptions) { position, isChecked -> @@ -43,6 +58,8 @@ class BackupDialog() : DialogFragment() { if (selected[2]) backupFile.searchHistory = DatabaseHolder.db.searchHistoryDao().getAll() if (selected[3]) backupFile.localSubscriptions = DatabaseHolder.db.localSubscriptionDao().getAll() if (selected[4]) backupFile.customInstances = DatabaseHolder.db.customInstanceDao().getAll() + + createBackupFile.launch("application/json") } .create() } diff --git a/app/src/main/java/com/github/libretube/preferences/AdvancedSettings.kt b/app/src/main/java/com/github/libretube/preferences/AdvancedSettings.kt index 1b17b4894..91f0745b7 100644 --- a/app/src/main/java/com/github/libretube/preferences/AdvancedSettings.kt +++ b/app/src/main/java/com/github/libretube/preferences/AdvancedSettings.kt @@ -10,6 +10,7 @@ import androidx.preference.Preference import com.github.libretube.R import com.github.libretube.activities.SettingsActivity import com.github.libretube.constants.PreferenceKeys +import com.github.libretube.dialogs.BackupDialog import com.github.libretube.util.BackupHelper import com.github.libretube.util.ImageHelper import com.github.libretube.util.PreferenceHelper @@ -18,24 +19,32 @@ import com.google.android.material.dialog.MaterialAlertDialogBuilder class AdvancedSettings : MaterialPreferenceFragment() { - /** - * result listeners for importing and exporting subscriptions - */ - private lateinit var getContent: ActivityResultLauncher - private lateinit var createFile: ActivityResultLauncher + // backup and restore prefs + private lateinit var getPrefFile: ActivityResultLauncher + private lateinit var createPrefFile: ActivityResultLauncher + + // backup and restore database + private lateinit var getBackupFile: ActivityResultLauncher override fun onCreate(savedInstanceState: Bundle?) { - getContent = + getPrefFile = registerForActivityResult( ActivityResultContracts.GetContent() ) { uri: Uri? -> BackupHelper(requireContext()).restoreSharedPreferences(uri) } - createFile = registerForActivityResult( + createPrefFile = registerForActivityResult( CreateDocument("application/json") ) { uri: Uri? -> BackupHelper(requireContext()).backupSharedPreferences(uri) } + + getBackupFile = + registerForActivityResult( + ActivityResultContracts.GetContent() + ) { uri: Uri? -> + BackupHelper(requireContext()).restoreAdvancedBackup(uri) + } super.onCreate(savedInstanceState) } @@ -59,18 +68,31 @@ class AdvancedSettings : MaterialPreferenceFragment() { val backupSettings = findPreference(PreferenceKeys.BACKUP_SETTINGS) backupSettings?.setOnPreferenceClickListener { - createFile.launch("preferences.xml") + createPrefFile.launch("preferences.xml") true } val restoreSettings = findPreference(PreferenceKeys.RESTORE_SETTINGS) restoreSettings?.setOnPreferenceClickListener { - getContent.launch("*/*") + getPrefFile.launch("*/*") // reset the token PreferenceHelper.setToken("") activity?.recreate() true } + + val advancesBackup = findPreference("backup") + advancesBackup?.setOnPreferenceClickListener { + BackupDialog() + .show(childFragmentManager, null) + true + } + + val restoreAdvancedBackup = findPreference("restore") + restoreAdvancedBackup?.setOnPreferenceClickListener { + getBackupFile.launch("application/json") + true + } } private fun showResetDialog() { diff --git a/app/src/main/res/xml/advanced_settings.xml b/app/src/main/res/xml/advanced_settings.xml index bd0bc574e..a596e7237 100644 --- a/app/src/main/res/xml/advanced_settings.xml +++ b/app/src/main/res/xml/advanced_settings.xml @@ -32,7 +32,7 @@ - + + + + + + + + + \ No newline at end of file