From d7123bd43d450012dfbede0a7a37814936900024 Mon Sep 17 00:00:00 2001 From: Bnyro Date: Thu, 30 Mar 2023 18:44:41 +0200 Subject: [PATCH] Migrate preferences to Material Design 3 --- .../heckel/ntfy/ui/BasePreferenceFragment.kt | 50 +++++++++++++++++++ .../heckel/ntfy/ui/DetailSettingsActivity.kt | 8 +-- .../io/heckel/ntfy/ui/SettingsActivity.kt | 20 ++++---- .../layout/dialog_edit_text_preference.xml | 19 +++++++ .../main/res/layout/fragment_user_dialog.xml | 1 + .../res/layout/view_preference_switch.xml | 5 ++ app/src/main/res/values-night/styles.xml | 4 +- app/src/main/res/values/styles.xml | 6 +++ app/src/main/res/xml/detail_preferences.xml | 4 +- app/src/main/res/xml/main_preferences.xml | 8 +-- 10 files changed, 104 insertions(+), 21 deletions(-) create mode 100644 app/src/main/java/io/heckel/ntfy/ui/BasePreferenceFragment.kt create mode 100644 app/src/main/res/layout/dialog_edit_text_preference.xml create mode 100644 app/src/main/res/layout/view_preference_switch.xml diff --git a/app/src/main/java/io/heckel/ntfy/ui/BasePreferenceFragment.kt b/app/src/main/java/io/heckel/ntfy/ui/BasePreferenceFragment.kt new file mode 100644 index 0000000..14ab816 --- /dev/null +++ b/app/src/main/java/io/heckel/ntfy/ui/BasePreferenceFragment.kt @@ -0,0 +1,50 @@ +package io.heckel.ntfy.ui + +import androidx.preference.EditTextPreference +import androidx.preference.ListPreference +import androidx.preference.Preference +import androidx.preference.PreferenceFragmentCompat +import com.google.android.material.dialog.MaterialAlertDialogBuilder +import com.google.android.material.textfield.TextInputEditText +import io.heckel.ntfy.R + +abstract class BasePreferenceFragment : PreferenceFragmentCompat() { + /** + * Show [ListPreference] and [EditTextPreference] dialog by [MaterialAlertDialogBuilder] + */ + override fun onDisplayPreferenceDialog(preference: Preference) { + when (preference) { + is ListPreference -> { + val prefIndex = preference.entryValues.indexOf(preference.value) + MaterialAlertDialogBuilder(requireContext()) + .setTitle(preference.title) + .setSingleChoiceItems(preference.entries, prefIndex) { dialog, index -> + val newValue = preference.entryValues[index].toString() + if (preference.callChangeListener(newValue)) { + preference.value = newValue + } + dialog.dismiss() + } + .setNegativeButton(android.R.string.cancel, null) + .show() + } + is EditTextPreference -> { + val view = layoutInflater.inflate(R.layout.dialog_edit_text_preference, null) + val editText = view.findViewById(R.id.editText) + editText.setText(preference.text.toString()) + MaterialAlertDialogBuilder(requireContext()) + .setTitle(preference.title) + .setView(view) + .setPositiveButton(android.R.string.ok) { _, _ -> + val newValue = editText.text.toString() + if (preference.callChangeListener(newValue)) { + preference.text = newValue + } + } + .setNegativeButton(android.R.string.cancel, null) + .show() + } + else -> super.onDisplayPreferenceDialog(preference) + } + } +} diff --git a/app/src/main/java/io/heckel/ntfy/ui/DetailSettingsActivity.kt b/app/src/main/java/io/heckel/ntfy/ui/DetailSettingsActivity.kt index ccc0191..d2e76a8 100644 --- a/app/src/main/java/io/heckel/ntfy/ui/DetailSettingsActivity.kt +++ b/app/src/main/java/io/heckel/ntfy/ui/DetailSettingsActivity.kt @@ -137,7 +137,7 @@ class DetailSettingsActivity : AppCompatActivity() { private fun loadInstantPref() { val appBaseUrl = getString(R.string.app_base_url) val prefId = context?.getString(R.string.detail_settings_notifications_instant_key) ?: return - val pref: SwitchPreference? = findPreference(prefId) + val pref: SwitchPreferenceCompat? = findPreference(prefId) pref?.isVisible = BuildConfig.FIREBASE_AVAILABLE && subscription.baseUrl == appBaseUrl pref?.isChecked = subscription.instant pref?.preferenceDataStore = object : PreferenceDataStore() { @@ -148,7 +148,7 @@ class DetailSettingsActivity : AppCompatActivity() { return subscription.instant } } - pref?.summaryProvider = Preference.SummaryProvider { preference -> + pref?.summaryProvider = Preference.SummaryProvider { preference -> if (preference.isChecked) { getString(R.string.detail_settings_notifications_instant_summary_on) } else { @@ -159,7 +159,7 @@ class DetailSettingsActivity : AppCompatActivity() { private fun loadDedicatedChannelsPrefs() { val prefId = context?.getString(R.string.detail_settings_notifications_dedicated_channels_key) ?: return - val pref: SwitchPreference? = findPreference(prefId) + val pref: SwitchPreferenceCompat? = findPreference(prefId) pref?.isVisible = true pref?.isChecked = subscription.dedicatedChannels pref?.preferenceDataStore = object : PreferenceDataStore() { @@ -176,7 +176,7 @@ class DetailSettingsActivity : AppCompatActivity() { return subscription.dedicatedChannels } } - pref?.summaryProvider = Preference.SummaryProvider { preference -> + pref?.summaryProvider = Preference.SummaryProvider { preference -> if (preference.isChecked) { getString(R.string.detail_settings_notifications_dedicated_channels_summary_on) } else { diff --git a/app/src/main/java/io/heckel/ntfy/ui/SettingsActivity.kt b/app/src/main/java/io/heckel/ntfy/ui/SettingsActivity.kt index 1ce185e..1a9ab9e 100644 --- a/app/src/main/java/io/heckel/ntfy/ui/SettingsActivity.kt +++ b/app/src/main/java/io/heckel/ntfy/ui/SettingsActivity.kt @@ -120,7 +120,7 @@ class SettingsActivity : AppCompatActivity(), PreferenceFragmentCompat.OnPrefere return true } - class SettingsFragment : PreferenceFragmentCompat() { + class SettingsFragment : BasePreferenceFragment() { private lateinit var repository: Repository private lateinit var serviceManager: SubscriberServiceManager private var autoDownloadSelection = AUTO_DOWNLOAD_SELECTION_NOT_SET @@ -203,7 +203,7 @@ class SettingsActivity : AppCompatActivity(), PreferenceFragmentCompat.OnPrefere // Keep alerting for max priority val insistentMaxPriorityPrefId = context?.getString(R.string.settings_notifications_insistent_max_priority_key) ?: return - val insistentMaxPriority: SwitchPreference? = findPreference(insistentMaxPriorityPrefId) + val insistentMaxPriority: SwitchPreferenceCompat? = findPreference(insistentMaxPriorityPrefId) insistentMaxPriority?.isChecked = repository.getInsistentMaxPriorityEnabled() insistentMaxPriority?.preferenceDataStore = object : PreferenceDataStore() { override fun putBoolean(key: String?, value: Boolean) { @@ -213,7 +213,7 @@ class SettingsActivity : AppCompatActivity(), PreferenceFragmentCompat.OnPrefere return repository.getInsistentMaxPriorityEnabled() } } - insistentMaxPriority?.summaryProvider = Preference.SummaryProvider { pref -> + insistentMaxPriority?.summaryProvider = Preference.SummaryProvider { pref -> if (pref.isChecked) { getString(R.string.settings_notifications_insistent_max_priority_summary_enabled) } else { @@ -347,7 +347,7 @@ class SettingsActivity : AppCompatActivity(), PreferenceFragmentCompat.OnPrefere // Broadcast enabled val broadcastEnabledPrefId = context?.getString(R.string.settings_advanced_broadcast_key) ?: return - val broadcastEnabled: SwitchPreference? = findPreference(broadcastEnabledPrefId) + val broadcastEnabled: SwitchPreferenceCompat? = findPreference(broadcastEnabledPrefId) broadcastEnabled?.isChecked = repository.getBroadcastEnabled() broadcastEnabled?.preferenceDataStore = object : PreferenceDataStore() { override fun putBoolean(key: String?, value: Boolean) { @@ -357,7 +357,7 @@ class SettingsActivity : AppCompatActivity(), PreferenceFragmentCompat.OnPrefere return repository.getBroadcastEnabled() } } - broadcastEnabled?.summaryProvider = Preference.SummaryProvider { pref -> + broadcastEnabled?.summaryProvider = Preference.SummaryProvider { pref -> if (pref.isChecked) { getString(R.string.settings_advanced_broadcast_summary_enabled) } else { @@ -367,7 +367,7 @@ class SettingsActivity : AppCompatActivity(), PreferenceFragmentCompat.OnPrefere // Enable UnifiedPush val unifiedPushEnabledPrefId = context?.getString(R.string.settings_advanced_unifiedpush_key) ?: return - val unifiedPushEnabled: SwitchPreference? = findPreference(unifiedPushEnabledPrefId) + val unifiedPushEnabled: SwitchPreferenceCompat? = findPreference(unifiedPushEnabledPrefId) unifiedPushEnabled?.isChecked = repository.getUnifiedPushEnabled() unifiedPushEnabled?.preferenceDataStore = object : PreferenceDataStore() { override fun putBoolean(key: String?, value: Boolean) { @@ -377,7 +377,7 @@ class SettingsActivity : AppCompatActivity(), PreferenceFragmentCompat.OnPrefere return repository.getUnifiedPushEnabled() } } - unifiedPushEnabled?.summaryProvider = Preference.SummaryProvider { pref -> + unifiedPushEnabled?.summaryProvider = Preference.SummaryProvider { pref -> if (pref.isChecked) { getString(R.string.settings_advanced_unifiedpush_summary_enabled) } else { @@ -412,7 +412,7 @@ class SettingsActivity : AppCompatActivity(), PreferenceFragmentCompat.OnPrefere // Record logs val recordLogsPrefId = context?.getString(R.string.settings_advanced_record_logs_key) ?: return - val recordLogsEnabled: SwitchPreference? = findPreference(recordLogsPrefId) + val recordLogsEnabled: SwitchPreferenceCompat? = findPreference(recordLogsPrefId) recordLogsEnabled?.isChecked = Log.getRecord() recordLogsEnabled?.preferenceDataStore = object : PreferenceDataStore() { override fun putBoolean(key: String?, value: Boolean) { @@ -425,7 +425,7 @@ class SettingsActivity : AppCompatActivity(), PreferenceFragmentCompat.OnPrefere return Log.getRecord() } } - recordLogsEnabled?.summaryProvider = Preference.SummaryProvider { pref -> + recordLogsEnabled?.summaryProvider = Preference.SummaryProvider { pref -> if (pref.isChecked) { getString(R.string.settings_advanced_record_logs_summary_enabled) } else { @@ -683,7 +683,7 @@ class SettingsActivity : AppCompatActivity(), PreferenceFragmentCompat.OnPrefere data class NopasteResponse(val url: String) } - class UserSettingsFragment : PreferenceFragmentCompat() { + class UserSettingsFragment : BasePreferenceFragment() { private lateinit var repository: Repository override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) { diff --git a/app/src/main/res/layout/dialog_edit_text_preference.xml b/app/src/main/res/layout/dialog_edit_text_preference.xml new file mode 100644 index 0000000..5ffaf24 --- /dev/null +++ b/app/src/main/res/layout/dialog_edit_text_preference.xml @@ -0,0 +1,19 @@ + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/fragment_user_dialog.xml b/app/src/main/res/layout/fragment_user_dialog.xml index b28ae57..bbffa4f 100644 --- a/app/src/main/res/layout/fragment_user_dialog.xml +++ b/app/src/main/res/layout/fragment_user_dialog.xml @@ -50,6 +50,7 @@ android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="@string/user_dialog_password_hint_add" android:importantForAutofill="no" + android:backgroundTint="?attr/colorPrimary" android:maxLines="1" android:inputType="textPassword" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent" android:layout_marginTop="6dp" app:layout_constraintTop_toBottomOf="@id/user_dialog_username"/> diff --git a/app/src/main/res/layout/view_preference_switch.xml b/app/src/main/res/layout/view_preference_switch.xml new file mode 100644 index 0000000..ba3fd96 --- /dev/null +++ b/app/src/main/res/layout/view_preference_switch.xml @@ -0,0 +1,5 @@ + + \ No newline at end of file diff --git a/app/src/main/res/values-night/styles.xml b/app/src/main/res/values-night/styles.xml index bb89181..11168ed 100644 --- a/app/src/main/res/values-night/styles.xml +++ b/app/src/main/res/values-night/styles.xml @@ -12,7 +12,7 @@ + + + diff --git a/app/src/main/res/xml/detail_preferences.xml b/app/src/main/res/xml/detail_preferences.xml index 38cd18b..4cb105b 100644 --- a/app/src/main/res/xml/detail_preferences.xml +++ b/app/src/main/res/xml/detail_preferences.xml @@ -3,7 +3,7 @@ - @@ -35,7 +35,7 @@ app:entryValues="@array/detail_settings_notifications_insistent_max_priority_values" app:defaultValue="-1" app:isPreferenceVisible="false"/> - diff --git a/app/src/main/res/xml/main_preferences.xml b/app/src/main/res/xml/main_preferences.xml index 0be2442..589ae82 100644 --- a/app/src/main/res/xml/main_preferences.xml +++ b/app/src/main/res/xml/main_preferences.xml @@ -25,7 +25,7 @@ app:entries="@array/settings_notifications_auto_delete_entries" app:entryValues="@array/settings_notifications_auto_delete_values" app:defaultValue="2592000"/> - @@ -72,15 +72,15 @@ app:entries="@array/settings_advanced_connection_protocol_entries" app:entryValues="@array/settings_advanced_connection_protocol_values" app:defaultValue="jsonhttp"/> - - -