use custom dialogs in the preferences

This commit is contained in:
Bnyro 2022-08-07 17:56:57 +02:00
parent 5446d3257f
commit 39a26a9820
12 changed files with 65 additions and 28 deletions

View File

@ -15,9 +15,6 @@ class SettingsActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
ThemeHelper.updateTheme(this)
// apply the theme for the preference dialogs
setTheme(R.style.MaterialAlertDialog)
super.onCreate(savedInstanceState)
binding = ActivitySettingsBinding.inflate(layoutInflater)

View File

@ -2,12 +2,12 @@ package com.github.libretube.preferences
import android.os.Bundle
import androidx.preference.Preference
import androidx.preference.PreferenceFragmentCompat
import com.github.libretube.R
import com.github.libretube.activities.SettingsActivity
import com.github.libretube.views.MaterialPreferenceFragment
import com.google.android.material.dialog.MaterialAlertDialogBuilder
class AdvancedSettings : PreferenceFragmentCompat() {
class AdvancedSettings : MaterialPreferenceFragment() {
val TAG = "AdvancedSettings"
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {

View File

@ -7,15 +7,15 @@ import android.provider.Settings
import android.widget.Toast
import androidx.preference.ListPreference
import androidx.preference.Preference
import androidx.preference.PreferenceFragmentCompat
import androidx.preference.SwitchPreferenceCompat
import com.github.libretube.R
import com.github.libretube.activities.SettingsActivity
import com.github.libretube.dialogs.RequireRestartDialog
import com.github.libretube.util.ThemeHelper
import com.github.libretube.views.MaterialPreferenceFragment
import com.google.android.material.color.DynamicColors
class AppearanceSettings : PreferenceFragmentCompat() {
class AppearanceSettings : MaterialPreferenceFragment() {
private val TAG = "AppearanceSettings"
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
setPreferencesFromResource(R.xml.appearance_settings, rootKey)

View File

@ -2,13 +2,13 @@ package com.github.libretube.preferences
import android.os.Bundle
import androidx.preference.ListPreference
import androidx.preference.PreferenceFragmentCompat
import androidx.preference.SwitchPreferenceCompat
import com.github.libretube.R
import com.github.libretube.activities.SettingsActivity
import com.github.libretube.dialogs.RequireRestartDialog
import com.github.libretube.views.MaterialPreferenceFragment
class GeneralSettings : PreferenceFragmentCompat() {
class GeneralSettings : MaterialPreferenceFragment() {
val TAG = "SettingsFragment"
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {

View File

@ -2,12 +2,12 @@ package com.github.libretube.preferences
import android.os.Bundle
import androidx.preference.Preference
import androidx.preference.PreferenceFragmentCompat
import com.github.libretube.R
import com.github.libretube.activities.SettingsActivity
import com.github.libretube.views.MaterialPreferenceFragment
import com.google.android.material.dialog.MaterialAlertDialogBuilder
class HistorySettings : PreferenceFragmentCompat() {
class HistorySettings : MaterialPreferenceFragment() {
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
setPreferencesFromResource(R.xml.history_settings, rootKey)

View File

@ -11,7 +11,6 @@ import androidx.fragment.app.Fragment
import androidx.lifecycle.lifecycleScope
import androidx.preference.ListPreference
import androidx.preference.Preference
import androidx.preference.PreferenceFragmentCompat
import androidx.preference.SwitchPreferenceCompat
import com.github.libretube.R
import com.github.libretube.activities.SettingsActivity
@ -22,8 +21,9 @@ import com.github.libretube.dialogs.LogoutDialog
import com.github.libretube.util.ImportHelper
import com.github.libretube.util.PermissionHelper
import com.github.libretube.util.RetrofitInstance
import com.github.libretube.views.MaterialPreferenceFragment
class InstanceSettings : PreferenceFragmentCompat() {
class InstanceSettings : MaterialPreferenceFragment() {
val TAG = "InstanceSettings"
/**

View File

@ -3,18 +3,18 @@ package com.github.libretube.preferences
import android.os.Bundle
import androidx.fragment.app.Fragment
import androidx.preference.Preference
import androidx.preference.PreferenceFragmentCompat
import com.github.libretube.BuildConfig
import com.github.libretube.R
import com.github.libretube.activities.SettingsActivity
import com.github.libretube.dialogs.UpdateDialog
import com.github.libretube.update.UpdateChecker
import com.github.libretube.views.MaterialPreferenceFragment
import com.google.android.material.snackbar.Snackbar
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
class MainSettings : PreferenceFragmentCompat() {
class MainSettings : MaterialPreferenceFragment() {
val TAG = "SettingsFragment"
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {

View File

@ -2,14 +2,14 @@ package com.github.libretube.preferences
import android.os.Bundle
import androidx.preference.ListPreference
import androidx.preference.PreferenceFragmentCompat
import androidx.preference.SwitchPreferenceCompat
import androidx.work.ExistingPeriodicWorkPolicy
import com.github.libretube.R
import com.github.libretube.activities.SettingsActivity
import com.github.libretube.util.NotificationHelper
import com.github.libretube.views.MaterialPreferenceFragment
class NotificationSettings : PreferenceFragmentCompat() {
class NotificationSettings : MaterialPreferenceFragment() {
val TAG = "SettingsFragment"
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {

View File

@ -3,13 +3,13 @@ package com.github.libretube.preferences
import android.os.Bundle
import androidx.preference.ListPreference
import androidx.preference.Preference
import androidx.preference.PreferenceFragmentCompat
import androidx.preference.SwitchPreferenceCompat
import com.github.libretube.R
import com.github.libretube.activities.SettingsActivity
import com.github.libretube.views.MaterialPreferenceFragment
import java.util.*
class PlayerSettings : PreferenceFragmentCompat() {
class PlayerSettings : MaterialPreferenceFragment() {
val TAG = "PlayerSettings"
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {

View File

@ -1,11 +1,11 @@
package com.github.libretube.preferences
import android.os.Bundle
import androidx.preference.PreferenceFragmentCompat
import com.github.libretube.R
import com.github.libretube.activities.SettingsActivity
import com.github.libretube.views.MaterialPreferenceFragment
class SponsorBlockSettings : PreferenceFragmentCompat() {
class SponsorBlockSettings : MaterialPreferenceFragment() {
private val TAG = "SponsorBlockSettings"
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {

View File

@ -0,0 +1,47 @@
package com.github.libretube.views
import android.os.Bundle
import androidx.preference.ListPreference
import androidx.preference.Preference
import androidx.preference.PreferenceFragmentCompat
import com.github.libretube.R
import com.google.android.material.dialog.MaterialAlertDialogBuilder
/**
* PreferenceFragmentCompat using the [MaterialAlertDialogBuilder] instead of the old dialog builder
*/
open class MaterialPreferenceFragment : PreferenceFragmentCompat() {
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {}
override fun onDisplayPreferenceDialog(preference: Preference) {
when (preference) {
/**
* Show a [MaterialAlertDialogBuilder] when the preference is a [ListPreference]
*/
is ListPreference -> {
// get the index of the previous selected item
val prefIndex = preference.entryValues.indexOf(preference.value)
MaterialAlertDialogBuilder(requireContext())
.setTitle(preference.title)
.setSingleChoiceItems(preference.entries, prefIndex) { dialog, index ->
// get the new ListPreference value
val newValue = preference.entryValues[index].toString()
// save the new value and call the onPreferenceChange Method
preference.value = newValue
preference.callChangeListener(newValue)
// dismiss the dialog
dialog.dismiss()
}
.setNegativeButton(R.string.cancel, null)
.show()
}
/**
* Otherwise show the normal dialog, dialogs for other preference types are not supported yet
*/
else -> super.onDisplayPreferenceDialog(preference)
}
}
}

View File

@ -15,13 +15,6 @@
</style>
<style name="MaterialAlertDialog">
<item name="alertDialogTheme">@style/ThemeOverlay.Material3.MaterialAlertDialog</item>
<item name="dialogCornerRadius">28dp</item>
</style>
<style name="AboutCard" parent="Widget.Material3.CardView.Elevated">
<item name="android:layout_marginLeft">10dp</item>