add settings entry

This commit is contained in:
Bnyro 2022-09-18 12:49:57 +02:00
parent 26ec53f654
commit 9fa7bb9c6d
3 changed files with 65 additions and 12 deletions

View File

@ -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<String>
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()
}

View File

@ -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<String>
private lateinit var createFile: ActivityResultLauncher<String>
// backup and restore prefs
private lateinit var getPrefFile: ActivityResultLauncher<String>
private lateinit var createPrefFile: ActivityResultLauncher<String>
// backup and restore database
private lateinit var getBackupFile: ActivityResultLauncher<String>
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<Preference>(PreferenceKeys.BACKUP_SETTINGS)
backupSettings?.setOnPreferenceClickListener {
createFile.launch("preferences.xml")
createPrefFile.launch("preferences.xml")
true
}
val restoreSettings = findPreference<Preference>(PreferenceKeys.RESTORE_SETTINGS)
restoreSettings?.setOnPreferenceClickListener {
getContent.launch("*/*")
getPrefFile.launch("*/*")
// reset the token
PreferenceHelper.setToken("")
activity?.recreate()
true
}
val advancesBackup = findPreference<Preference>("backup")
advancesBackup?.setOnPreferenceClickListener {
BackupDialog()
.show(childFragmentManager, null)
true
}
val restoreAdvancedBackup = findPreference<Preference>("restore")
restoreAdvancedBackup?.setOnPreferenceClickListener {
getBackupFile.launch("application/json")
true
}
}
private fun showResetDialog() {

View File

@ -32,7 +32,7 @@
</PreferenceCategory>
<PreferenceCategory app:title="@string/backup_restore">
<PreferenceCategory app:title="@string/preferences">
<Preference
android:icon="@drawable/ic_backup"
@ -46,4 +46,18 @@
</PreferenceCategory>
<PreferenceCategory app:title="@string/backup_restore">
<Preference
android:icon="@drawable/ic_backup"
app:key="backup"
app:title="@string/backup" />
<Preference
android:icon="@drawable/ic_restore"
app:key="restore"
app:title="@string/restore" />
</PreferenceCategory>
</PreferenceScreen>