add preference to clear preferences for each screen
This commit is contained in:
parent
cefa55f37f
commit
7d0d3af036
17 changed files with 137 additions and 28 deletions
|
|
@ -1,12 +1,17 @@
|
|||
package com.deniscerri.ytdl.ui.more.settings
|
||||
|
||||
import android.content.SharedPreferences
|
||||
import android.view.inputmethod.InputMethodManager
|
||||
import androidx.appcompat.app.AppCompatActivity
|
||||
import androidx.preference.EditTextPreference
|
||||
import androidx.preference.ListPreference
|
||||
import androidx.preference.MultiSelectListPreference
|
||||
import androidx.preference.Preference
|
||||
import androidx.preference.PreferenceCategory
|
||||
import androidx.preference.PreferenceFragmentCompat
|
||||
import androidx.preference.PreferenceGroup
|
||||
import androidx.preference.PreferenceManager
|
||||
import androidx.preference.PreferenceScreen
|
||||
import com.deniscerri.ytdl.R
|
||||
import com.deniscerri.ytdl.databinding.TextinputBinding
|
||||
import com.google.android.material.dialog.MaterialAlertDialogBuilder
|
||||
|
|
@ -21,6 +26,27 @@ abstract class BaseSettingsFragment : PreferenceFragmentCompat() {
|
|||
(activity as? SettingsActivity)?.changeTopAppbarTitle(getString(title))
|
||||
}
|
||||
|
||||
fun getPreferences(p: Preference, list: MutableList<Preference>) : List<Preference> {
|
||||
if (p is PreferenceCategory || p is PreferenceScreen) {
|
||||
val pGroup: PreferenceGroup = p as PreferenceGroup
|
||||
val pCount: Int = pGroup.preferenceCount
|
||||
for (i in 0 until pCount) {
|
||||
getPreferences(pGroup.getPreference(i), list) // recursive call
|
||||
}
|
||||
} else {
|
||||
list.add(p)
|
||||
}
|
||||
return list
|
||||
}
|
||||
|
||||
fun resetPreferences(editor: SharedPreferences.Editor, key: Int) {
|
||||
getPreferences(preferenceScreen, mutableListOf()).forEach {
|
||||
editor.remove(it.key)
|
||||
}
|
||||
editor.apply()
|
||||
PreferenceManager.setDefaultValues(requireActivity().applicationContext, key, true)
|
||||
}
|
||||
|
||||
//Thanks libretube
|
||||
override fun onDisplayPreferenceDialog(preference: Preference) {
|
||||
when (preference) {
|
||||
|
|
|
|||
|
|
@ -263,6 +263,14 @@ class DownloadSettingsFragment : BaseSettingsFragment() {
|
|||
true
|
||||
}
|
||||
}
|
||||
|
||||
findPreference<Preference>("reset_preferences")?.setOnPreferenceClickListener {
|
||||
UiUtil.showGenericConfirmDialog(requireContext(), getString(R.string.reset), getString(R.string.reset_preferences_in_screen)) {
|
||||
resetPreferences(preferences.edit(), R.xml.downloading_preferences)
|
||||
requireActivity().recreate()
|
||||
}
|
||||
true
|
||||
}
|
||||
}
|
||||
|
||||
private var archivePathResultLauncher = registerForActivityResult(
|
||||
|
|
|
|||
|
|
@ -1,20 +1,22 @@
|
|||
package com.deniscerri.ytdl.ui.more.settings
|
||||
|
||||
import android.app.Activity
|
||||
import android.content.Context.MODE_PRIVATE
|
||||
import android.content.Intent
|
||||
import android.content.SharedPreferences
|
||||
import android.net.Uri
|
||||
import android.os.Build
|
||||
import android.os.Build.VERSION
|
||||
import android.os.Bundle
|
||||
import android.os.Environment
|
||||
import android.provider.Settings
|
||||
import androidx.activity.result.contract.ActivityResultContracts
|
||||
import androidx.annotation.RequiresApi
|
||||
import androidx.lifecycle.ViewModelProvider
|
||||
import androidx.lifecycle.lifecycleScope
|
||||
import androidx.preference.Preference
|
||||
import androidx.preference.PreferenceCategory
|
||||
import androidx.preference.PreferenceGroup
|
||||
import androidx.preference.PreferenceManager
|
||||
import androidx.preference.PreferenceScreen
|
||||
import androidx.preference.SwitchPreferenceCompat
|
||||
import androidx.work.ExistingWorkPolicy
|
||||
import androidx.work.OneTimeWorkRequestBuilder
|
||||
|
|
@ -74,16 +76,16 @@ class FolderSettingsFragment : BaseSettingsFragment() {
|
|||
moveCache = findPreference("move_cache")
|
||||
|
||||
if (preferences.getString("music_path", "")!!.isEmpty()) {
|
||||
editor.putString("music_path", FileUtil.getDefaultAudioPath())
|
||||
editor.putString("music_path", FileUtil.getDefaultAudioPath()).apply()
|
||||
}
|
||||
if (preferences.getString("video_path", "")!!.isEmpty()) {
|
||||
editor.putString("video_path", FileUtil.getDefaultVideoPath())
|
||||
editor.putString("video_path", FileUtil.getDefaultVideoPath()).apply()
|
||||
}
|
||||
if (preferences.getString("command_path", "")!!.isEmpty()) {
|
||||
editor.putString("command_path", FileUtil.getDefaultCommandPath())
|
||||
editor.putString("command_path", FileUtil.getDefaultCommandPath()).apply()
|
||||
}
|
||||
if (preferences.getString("cache_path", "")!!.isEmpty()) {
|
||||
editor.putString("cache_path", FileUtil.getCachePath(requireContext()))
|
||||
editor.putString("cache_path", FileUtil.getCachePath(requireContext())).apply()
|
||||
}
|
||||
|
||||
if (FileUtil.hasAllFilesAccess()) {
|
||||
|
|
@ -125,7 +127,7 @@ class FolderSettingsFragment : BaseSettingsFragment() {
|
|||
true
|
||||
}
|
||||
|
||||
cachePath!!.summary = FileUtil.formatPath(preferences.getString("cache_path", "")!!)
|
||||
cachePath!!.summary = FileUtil.formatPath(preferences.getString("cache_path", FileUtil.getCachePath(requireContext()))!!)
|
||||
cachePath!!.onPreferenceClickListener =
|
||||
Preference.OnPreferenceClickListener {
|
||||
UiUtil.showGenericConfirmDialog(requireContext(), getString(R.string.cache_directory), getString(R.string.cache_directory_warning)) {
|
||||
|
|
@ -234,6 +236,14 @@ class FolderSettingsFragment : BaseSettingsFragment() {
|
|||
}
|
||||
|
||||
|
||||
findPreference<Preference>("reset_preferences")?.setOnPreferenceClickListener {
|
||||
UiUtil.showGenericConfirmDialog(requireContext(), getString(R.string.reset), getString(R.string.reset_preferences_in_screen)) {
|
||||
resetPreferences(editor, R.xml.folders_preference)
|
||||
requireActivity().recreate()
|
||||
}
|
||||
true
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
override fun onResume() {
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@ import android.content.DialogInterface
|
|||
import android.content.Intent
|
||||
import android.content.SharedPreferences
|
||||
import android.content.pm.PackageManager
|
||||
import android.content.res.Resources.Theme
|
||||
import android.net.Uri
|
||||
import android.os.Bundle
|
||||
import android.os.PowerManager
|
||||
|
|
@ -68,12 +69,13 @@ class GeneralSettingsFragment : BaseSettingsFragment() {
|
|||
}
|
||||
|
||||
findPreference<ListPreference>("app_language")?.apply {
|
||||
if (value == null) {
|
||||
if (value.isNullOrBlank()) {
|
||||
value = Locale.getDefault().language
|
||||
summary = Locale.getDefault().displayLanguage
|
||||
}
|
||||
summary = Locale.getDefault().displayLanguage
|
||||
setOnPreferenceChangeListener { _, newValue ->
|
||||
AppCompatDelegate.setApplicationLocales(LocaleListCompat.forLanguageTags(newValue.toString()))
|
||||
summary = Locale.getDefault().displayLanguage
|
||||
true
|
||||
}
|
||||
}
|
||||
|
|
@ -366,6 +368,14 @@ class GeneralSettingsFragment : BaseSettingsFragment() {
|
|||
true
|
||||
}
|
||||
}
|
||||
|
||||
findPreference<Preference>("reset_preferences")?.setOnPreferenceClickListener {
|
||||
UiUtil.showGenericConfirmDialog(requireContext(), getString(R.string.reset), getString(R.string.reset_preferences_in_screen)) {
|
||||
resetPreferences(editor, R.xml.general_preferences)
|
||||
ThemeUtil.updateThemes()
|
||||
}
|
||||
true
|
||||
}
|
||||
}
|
||||
|
||||
override fun onResume() {
|
||||
|
|
|
|||
|
|
@ -1,22 +1,12 @@
|
|||
package com.deniscerri.ytdl.ui.more.settings
|
||||
|
||||
import android.annotation.SuppressLint
|
||||
import android.content.DialogInterface
|
||||
import android.graphics.Typeface
|
||||
import android.os.Bundle
|
||||
import android.widget.LinearLayout
|
||||
import android.widget.TextView
|
||||
import androidx.preference.EditTextPreference
|
||||
import androidx.preference.Preference
|
||||
import androidx.preference.PreferenceManager
|
||||
import androidx.recyclerview.widget.ItemTouchHelper
|
||||
import androidx.recyclerview.widget.LinearLayoutManager
|
||||
import androidx.recyclerview.widget.RecyclerView
|
||||
import com.afollestad.materialdialogs.utils.MDUtil.getStringArray
|
||||
import com.deniscerri.ytdl.R
|
||||
import com.deniscerri.ytdl.ui.adapter.SortableTextItemAdapter
|
||||
import com.deniscerri.ytdl.util.UiUtil
|
||||
import com.google.android.material.dialog.MaterialAlertDialogBuilder
|
||||
|
||||
|
||||
class ProcessingSettingsFragment : BaseSettingsFragment() {
|
||||
|
|
@ -82,5 +72,13 @@ class ProcessingSettingsFragment : BaseSettingsFragment() {
|
|||
true
|
||||
}
|
||||
}
|
||||
|
||||
findPreference<Preference>("reset_preferences")?.setOnPreferenceClickListener {
|
||||
UiUtil.showGenericConfirmDialog(requireContext(), getString(R.string.reset), getString(R.string.reset_preferences_in_screen)) {
|
||||
resetPreferences(editor, R.xml.processing_preferences)
|
||||
requireActivity().recreate()
|
||||
}
|
||||
true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -81,7 +81,7 @@ class UpdateSettingsFragment : BaseSettingsFragment() {
|
|||
ytdlpViewModel = ViewModelProvider(this)[YTDLPViewModel::class.java]
|
||||
|
||||
ytdlSource?.apply {
|
||||
summary = preferences.getString("ytdlp_source_label", "")
|
||||
summary = preferences.getString("ytdlp_source_label", "")!!.ifEmpty { getString(R.string.update_ytdl_stable) }
|
||||
setOnPreferenceClickListener {
|
||||
UiUtil.showYTDLSourceBottomSheet(requireActivity(), preferences) { t, r ->
|
||||
summary = t
|
||||
|
|
@ -95,15 +95,14 @@ class UpdateSettingsFragment : BaseSettingsFragment() {
|
|||
|
||||
ytdlVersion?.apply {
|
||||
summary = preferences.getString("ytdl-version", "")
|
||||
if (summary?.isBlank() == true) {
|
||||
setYTDLPVersion()
|
||||
}
|
||||
setOnPreferenceClickListener {
|
||||
initYTDLUpdate()
|
||||
true
|
||||
}
|
||||
}
|
||||
ytdlVersion!!.setOnPreferenceClickListener {
|
||||
initYTDLUpdate()
|
||||
true
|
||||
}
|
||||
|
||||
updateYTDL!!.onPreferenceClickListener =
|
||||
Preference.OnPreferenceClickListener {
|
||||
|
|
@ -122,7 +121,7 @@ class UpdateSettingsFragment : BaseSettingsFragment() {
|
|||
|
||||
version = findPreference("version")
|
||||
val nativeLibraryDir = context?.applicationInfo?.nativeLibraryDir
|
||||
version!!.summary = "${BuildConfig.VERSION_NAME} [${nativeLibraryDir?.split("/lib/")?.get(1)}]"
|
||||
version!!.summary = "${BuildConfig.VERSION_NAME} (${nativeLibraryDir?.split("/lib/")?.get(1)})"
|
||||
version!!.onPreferenceClickListener =
|
||||
Preference.OnPreferenceClickListener {
|
||||
lifecycleScope.launch{
|
||||
|
|
@ -140,6 +139,14 @@ class UpdateSettingsFragment : BaseSettingsFragment() {
|
|||
true
|
||||
}
|
||||
|
||||
|
||||
findPreference<Preference>("reset_preferences")?.setOnPreferenceClickListener {
|
||||
UiUtil.showGenericConfirmDialog(requireContext(), getString(R.string.reset), getString(R.string.reset_preferences_in_screen)) {
|
||||
resetPreferences(preferences.edit(), R.xml.updating_preferences)
|
||||
requireActivity().recreate()
|
||||
}
|
||||
true
|
||||
}
|
||||
}
|
||||
|
||||
private fun setYTDLPVersion() {
|
||||
|
|
|
|||
|
|
@ -80,6 +80,14 @@ class AdvancedSettingsFragment : BaseSettingsFragment() {
|
|||
}
|
||||
}
|
||||
|
||||
findPreference<Preference>("reset_preferences")?.setOnPreferenceClickListener {
|
||||
UiUtil.showGenericConfirmDialog(requireContext(), getString(R.string.reset), getString(R.string.reset_preferences_in_screen)) {
|
||||
resetPreferences(editor, R.xml.downloading_preferences)
|
||||
requireActivity().recreate()
|
||||
}
|
||||
true
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
5
app/src/main/res/drawable/baseline_reset_tv_24.xml
Normal file
5
app/src/main/res/drawable/baseline_reset_tv_24.xml
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
<vector xmlns:android="http://schemas.android.com/apk/res/android" android:height="24dp" android:tint="?android:colorAccent" android:viewportHeight="24" android:viewportWidth="24" android:width="24dp">
|
||||
|
||||
<path android:fillColor="@android:color/white" android:pathData="M21,10h-8.01V7L9,11l3.99,4v-3H21v5H3V5h18v3h2V5c0,-1.1 -0.9,-2 -2,-2H3c-1.1,0 -2,0.9 -2,2v12c0,1.1 0.9,2 2,2h5v2h8v-2h5c1.1,0 1.99,-0.9 1.99,-2v-5H23c0,-1.1 -0.9,-2 -2,-2z"/>
|
||||
|
||||
</vector>
|
||||
|
|
@ -2,7 +2,6 @@
|
|||
<com.google.android.material.card.MaterialCardView android:id="@+id/sampleCustomSource"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginTop="10dp"
|
||||
android:checkable="true"
|
||||
android:clickable="true"
|
||||
android:focusable="true"
|
||||
|
|
@ -17,6 +16,7 @@
|
|||
|
||||
<androidx.constraintlayout.widget.ConstraintLayout
|
||||
android:layout_width="match_parent"
|
||||
android:paddingBottom="5dp"
|
||||
android:layout_height="wrap_content">
|
||||
|
||||
<RadioButton
|
||||
|
|
|
|||
|
|
@ -59,7 +59,7 @@
|
|||
|
||||
<androidx.core.widget.NestedScrollView
|
||||
android:layout_width="match_parent"
|
||||
android:padding="15dp"
|
||||
android:padding="10dp"
|
||||
android:layout_height="wrap_content">
|
||||
|
||||
<LinearLayout
|
||||
|
|
|
|||
|
|
@ -466,4 +466,5 @@
|
|||
<string name="app_icon_change">Icon could change and Application will close</string>
|
||||
<string name="use_format_sorting">Use Format Importance Ordering</string>
|
||||
<string name="use_code_highlighter">Use Code Color Highlighter</string>
|
||||
<string name="reset_preferences_in_screen">Reset all preferences in this screen</string>
|
||||
</resources>
|
||||
|
|
@ -65,4 +65,10 @@
|
|||
android:title="@string/disable_write_info_json" />
|
||||
</PreferenceCategory>
|
||||
|
||||
<Preference
|
||||
app:icon="@drawable/baseline_reset_tv_24"
|
||||
app:key="reset_preferences"
|
||||
android:summary="@string/reset_preferences_in_screen"
|
||||
app:title="@string/reset" />
|
||||
|
||||
</PreferenceScreen>
|
||||
|
|
@ -228,5 +228,11 @@
|
|||
android:key="useragent_header"
|
||||
app:isPreferenceVisible="false" />
|
||||
|
||||
<Preference
|
||||
app:icon="@drawable/baseline_reset_tv_24"
|
||||
app:key="reset_preferences"
|
||||
android:summary="@string/reset_preferences_in_screen"
|
||||
app:title="@string/reset" />
|
||||
|
||||
|
||||
</PreferenceScreen>
|
||||
|
|
@ -95,4 +95,10 @@
|
|||
android:summary="@string/clear_temporary_files_summary"
|
||||
app:title="@string/clear_temporary_files" />
|
||||
|
||||
<Preference
|
||||
app:icon="@drawable/baseline_reset_tv_24"
|
||||
app:key="reset_preferences"
|
||||
android:summary="@string/reset_preferences_in_screen"
|
||||
app:title="@string/reset" />
|
||||
|
||||
</PreferenceScreen>
|
||||
|
|
@ -9,7 +9,6 @@
|
|||
android:defaultValue=""
|
||||
android:icon="@drawable/baseline_translate_24"
|
||||
app:key="app_language"
|
||||
app:useSimpleSummaryProvider="true"
|
||||
app:summary=""
|
||||
app:title="@string/language" />
|
||||
|
||||
|
|
@ -216,6 +215,13 @@
|
|||
app:icon="@drawable/ic_battery"
|
||||
app:key="ignore_battery"
|
||||
app:title="@string/ignore_battery_optimization" />
|
||||
|
||||
<Preference
|
||||
app:icon="@drawable/baseline_reset_tv_24"
|
||||
app:key="reset_preferences"
|
||||
android:summary="@string/reset_preferences_in_screen"
|
||||
app:title="@string/reset" />
|
||||
|
||||
</PreferenceCategory>
|
||||
|
||||
</PreferenceScreen>
|
||||
|
|
@ -288,4 +288,10 @@
|
|||
|
||||
</PreferenceCategory>
|
||||
|
||||
<Preference
|
||||
app:icon="@drawable/baseline_reset_tv_24"
|
||||
app:key="reset_preferences"
|
||||
android:summary="@string/reset_preferences_in_screen"
|
||||
app:title="@string/reset" />
|
||||
|
||||
</PreferenceScreen>
|
||||
|
|
@ -94,4 +94,10 @@
|
|||
app:title="@string/update_formats" />
|
||||
</PreferenceCategory>
|
||||
|
||||
<Preference
|
||||
app:icon="@drawable/baseline_reset_tv_24"
|
||||
app:key="reset_preferences"
|
||||
android:summary="@string/reset_preferences_in_screen"
|
||||
app:title="@string/reset" />
|
||||
|
||||
</PreferenceScreen>
|
||||
Loading…
Reference in a new issue