Migrate preferences to Material Design 3

This commit is contained in:
Bnyro 2023-03-30 18:44:41 +02:00
parent ed8128ba87
commit d7123bd43d
10 changed files with 104 additions and 21 deletions

View file

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

View file

@ -137,7 +137,7 @@ class DetailSettingsActivity : AppCompatActivity() {
private fun loadInstantPref() { private fun loadInstantPref() {
val appBaseUrl = getString(R.string.app_base_url) val appBaseUrl = getString(R.string.app_base_url)
val prefId = context?.getString(R.string.detail_settings_notifications_instant_key) ?: return 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?.isVisible = BuildConfig.FIREBASE_AVAILABLE && subscription.baseUrl == appBaseUrl
pref?.isChecked = subscription.instant pref?.isChecked = subscription.instant
pref?.preferenceDataStore = object : PreferenceDataStore() { pref?.preferenceDataStore = object : PreferenceDataStore() {
@ -148,7 +148,7 @@ class DetailSettingsActivity : AppCompatActivity() {
return subscription.instant return subscription.instant
} }
} }
pref?.summaryProvider = Preference.SummaryProvider<SwitchPreference> { preference -> pref?.summaryProvider = Preference.SummaryProvider<SwitchPreferenceCompat> { preference ->
if (preference.isChecked) { if (preference.isChecked) {
getString(R.string.detail_settings_notifications_instant_summary_on) getString(R.string.detail_settings_notifications_instant_summary_on)
} else { } else {
@ -159,7 +159,7 @@ class DetailSettingsActivity : AppCompatActivity() {
private fun loadDedicatedChannelsPrefs() { private fun loadDedicatedChannelsPrefs() {
val prefId = context?.getString(R.string.detail_settings_notifications_dedicated_channels_key) ?: return 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?.isVisible = true
pref?.isChecked = subscription.dedicatedChannels pref?.isChecked = subscription.dedicatedChannels
pref?.preferenceDataStore = object : PreferenceDataStore() { pref?.preferenceDataStore = object : PreferenceDataStore() {
@ -176,7 +176,7 @@ class DetailSettingsActivity : AppCompatActivity() {
return subscription.dedicatedChannels return subscription.dedicatedChannels
} }
} }
pref?.summaryProvider = Preference.SummaryProvider<SwitchPreference> { preference -> pref?.summaryProvider = Preference.SummaryProvider<SwitchPreferenceCompat> { preference ->
if (preference.isChecked) { if (preference.isChecked) {
getString(R.string.detail_settings_notifications_dedicated_channels_summary_on) getString(R.string.detail_settings_notifications_dedicated_channels_summary_on)
} else { } else {

View file

@ -120,7 +120,7 @@ class SettingsActivity : AppCompatActivity(), PreferenceFragmentCompat.OnPrefere
return true return true
} }
class SettingsFragment : PreferenceFragmentCompat() { class SettingsFragment : BasePreferenceFragment() {
private lateinit var repository: Repository private lateinit var repository: Repository
private lateinit var serviceManager: SubscriberServiceManager private lateinit var serviceManager: SubscriberServiceManager
private var autoDownloadSelection = AUTO_DOWNLOAD_SELECTION_NOT_SET private var autoDownloadSelection = AUTO_DOWNLOAD_SELECTION_NOT_SET
@ -203,7 +203,7 @@ class SettingsActivity : AppCompatActivity(), PreferenceFragmentCompat.OnPrefere
// Keep alerting for max priority // Keep alerting for max priority
val insistentMaxPriorityPrefId = context?.getString(R.string.settings_notifications_insistent_max_priority_key) ?: return 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?.isChecked = repository.getInsistentMaxPriorityEnabled()
insistentMaxPriority?.preferenceDataStore = object : PreferenceDataStore() { insistentMaxPriority?.preferenceDataStore = object : PreferenceDataStore() {
override fun putBoolean(key: String?, value: Boolean) { override fun putBoolean(key: String?, value: Boolean) {
@ -213,7 +213,7 @@ class SettingsActivity : AppCompatActivity(), PreferenceFragmentCompat.OnPrefere
return repository.getInsistentMaxPriorityEnabled() return repository.getInsistentMaxPriorityEnabled()
} }
} }
insistentMaxPriority?.summaryProvider = Preference.SummaryProvider<SwitchPreference> { pref -> insistentMaxPriority?.summaryProvider = Preference.SummaryProvider<SwitchPreferenceCompat> { pref ->
if (pref.isChecked) { if (pref.isChecked) {
getString(R.string.settings_notifications_insistent_max_priority_summary_enabled) getString(R.string.settings_notifications_insistent_max_priority_summary_enabled)
} else { } else {
@ -347,7 +347,7 @@ class SettingsActivity : AppCompatActivity(), PreferenceFragmentCompat.OnPrefere
// Broadcast enabled // Broadcast enabled
val broadcastEnabledPrefId = context?.getString(R.string.settings_advanced_broadcast_key) ?: return 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?.isChecked = repository.getBroadcastEnabled()
broadcastEnabled?.preferenceDataStore = object : PreferenceDataStore() { broadcastEnabled?.preferenceDataStore = object : PreferenceDataStore() {
override fun putBoolean(key: String?, value: Boolean) { override fun putBoolean(key: String?, value: Boolean) {
@ -357,7 +357,7 @@ class SettingsActivity : AppCompatActivity(), PreferenceFragmentCompat.OnPrefere
return repository.getBroadcastEnabled() return repository.getBroadcastEnabled()
} }
} }
broadcastEnabled?.summaryProvider = Preference.SummaryProvider<SwitchPreference> { pref -> broadcastEnabled?.summaryProvider = Preference.SummaryProvider<SwitchPreferenceCompat> { pref ->
if (pref.isChecked) { if (pref.isChecked) {
getString(R.string.settings_advanced_broadcast_summary_enabled) getString(R.string.settings_advanced_broadcast_summary_enabled)
} else { } else {
@ -367,7 +367,7 @@ class SettingsActivity : AppCompatActivity(), PreferenceFragmentCompat.OnPrefere
// Enable UnifiedPush // Enable UnifiedPush
val unifiedPushEnabledPrefId = context?.getString(R.string.settings_advanced_unifiedpush_key) ?: return 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?.isChecked = repository.getUnifiedPushEnabled()
unifiedPushEnabled?.preferenceDataStore = object : PreferenceDataStore() { unifiedPushEnabled?.preferenceDataStore = object : PreferenceDataStore() {
override fun putBoolean(key: String?, value: Boolean) { override fun putBoolean(key: String?, value: Boolean) {
@ -377,7 +377,7 @@ class SettingsActivity : AppCompatActivity(), PreferenceFragmentCompat.OnPrefere
return repository.getUnifiedPushEnabled() return repository.getUnifiedPushEnabled()
} }
} }
unifiedPushEnabled?.summaryProvider = Preference.SummaryProvider<SwitchPreference> { pref -> unifiedPushEnabled?.summaryProvider = Preference.SummaryProvider<SwitchPreferenceCompat> { pref ->
if (pref.isChecked) { if (pref.isChecked) {
getString(R.string.settings_advanced_unifiedpush_summary_enabled) getString(R.string.settings_advanced_unifiedpush_summary_enabled)
} else { } else {
@ -412,7 +412,7 @@ class SettingsActivity : AppCompatActivity(), PreferenceFragmentCompat.OnPrefere
// Record logs // Record logs
val recordLogsPrefId = context?.getString(R.string.settings_advanced_record_logs_key) ?: return 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?.isChecked = Log.getRecord()
recordLogsEnabled?.preferenceDataStore = object : PreferenceDataStore() { recordLogsEnabled?.preferenceDataStore = object : PreferenceDataStore() {
override fun putBoolean(key: String?, value: Boolean) { override fun putBoolean(key: String?, value: Boolean) {
@ -425,7 +425,7 @@ class SettingsActivity : AppCompatActivity(), PreferenceFragmentCompat.OnPrefere
return Log.getRecord() return Log.getRecord()
} }
} }
recordLogsEnabled?.summaryProvider = Preference.SummaryProvider<SwitchPreference> { pref -> recordLogsEnabled?.summaryProvider = Preference.SummaryProvider<SwitchPreferenceCompat> { pref ->
if (pref.isChecked) { if (pref.isChecked) {
getString(R.string.settings_advanced_record_logs_summary_enabled) getString(R.string.settings_advanced_record_logs_summary_enabled)
} else { } else {
@ -683,7 +683,7 @@ class SettingsActivity : AppCompatActivity(), PreferenceFragmentCompat.OnPrefere
data class NopasteResponse(val url: String) data class NopasteResponse(val url: String)
} }
class UserSettingsFragment : PreferenceFragmentCompat() { class UserSettingsFragment : BasePreferenceFragment() {
private lateinit var repository: Repository private lateinit var repository: Repository
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) { override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {

View file

@ -0,0 +1,19 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="?dialogPreferredPadding">
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="?dialogPreferredPadding">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</com.google.android.material.textfield.TextInputLayout>
</LinearLayout>

View file

@ -50,6 +50,7 @@
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:hint="@string/user_dialog_password_hint_add" android:layout_height="wrap_content" android:hint="@string/user_dialog_password_hint_add"
android:importantForAutofill="no" android:importantForAutofill="no"
android:backgroundTint="?attr/colorPrimary"
android:maxLines="1" android:inputType="textPassword" android:maxLines="1" android:inputType="textPassword"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent"
android:layout_marginTop="6dp" app:layout_constraintTop_toBottomOf="@id/user_dialog_username"/> android:layout_marginTop="6dp" app:layout_constraintTop_toBottomOf="@id/user_dialog_username"/>

View file

@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<com.google.android.material.materialswitch.MaterialSwitch xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/switchWidget"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

View file

@ -12,7 +12,7 @@
<style name="AppTheme" parent="Theme.Material3.DayNight"> <style name="AppTheme" parent="Theme.Material3.DayNight">
<item name="colorPrimary">@color/teal_light</item> <item name="colorPrimary">@color/teal_light</item>
<item name="colorAccent">@color/teal_light</item> <!-- checkboxes, text fields --> <item name="colorSecondary">@color/teal_light</item> <!-- checkboxes, text fields -->
<item name="android:colorBackground">@color/black_900</item> <!-- background --> <item name="android:colorBackground">@color/black_900</item> <!-- background -->
<item name="android:statusBarColor">@color/black_900</item> <item name="android:statusBarColor">@color/black_900</item>
<item name="actionModeBackground">@color/black_900</item> <item name="actionModeBackground">@color/black_900</item>
@ -20,6 +20,8 @@
<!-- Action bar background & text color --> <!-- Action bar background & text color -->
<item name="colorSurface">@color/black_800b</item> <item name="colorSurface">@color/black_800b</item>
<item name="colorOnSurface">@color/white</item> <item name="colorOnSurface">@color/white</item>
<item name="switchPreferenceCompatStyle">@style/MaterialSwitch</item>
</style> </style>
<style name="DangerText" parent="@android:style/TextAppearance"> <style name="DangerText" parent="@android:style/TextAppearance">

View file

@ -6,6 +6,7 @@
<item name="android:colorBackground">@color/white</item> <!-- background --> <item name="android:colorBackground">@color/white</item> <!-- background -->
<item name="android:statusBarColor">@color/teal</item> <item name="android:statusBarColor">@color/teal</item>
<item name="actionModeBackground">@color/teal_dark</item> <item name="actionModeBackground">@color/teal_dark</item>
<item name="switchPreferenceCompatStyle">@style/MaterialSwitch</item>
</style> </style>
<style name="DangerText" parent="@android:style/TextAppearance"> <style name="DangerText" parent="@android:style/TextAppearance">
@ -23,4 +24,9 @@
<item name="cornerFamily">rounded</item> <item name="cornerFamily">rounded</item>
<item name="cornerSize">5dp</item> <item name="cornerSize">5dp</item>
</style> </style>
<!-- Material Design 3 switches in the preferences -->
<style name="MaterialSwitch" parent="@style/Preference.SwitchPreferenceCompat.Material">
<item name="widgetLayout">@layout/view_preference_switch</item>
</style>
</resources> </resources>

View file

@ -3,7 +3,7 @@
<PreferenceCategory <PreferenceCategory
app:key="@string/detail_settings_notifications_header_key" app:key="@string/detail_settings_notifications_header_key"
app:title="@string/settings_notifications_header"> app:title="@string/settings_notifications_header">
<SwitchPreference <SwitchPreferenceCompat
app:key="@string/detail_settings_notifications_instant_key" app:key="@string/detail_settings_notifications_instant_key"
app:title="@string/detail_settings_notifications_instant_title" app:title="@string/detail_settings_notifications_instant_title"
app:isPreferenceVisible="false"/> app:isPreferenceVisible="false"/>
@ -35,7 +35,7 @@
app:entryValues="@array/detail_settings_notifications_insistent_max_priority_values" app:entryValues="@array/detail_settings_notifications_insistent_max_priority_values"
app:defaultValue="-1" app:defaultValue="-1"
app:isPreferenceVisible="false"/> <!-- Same as Repository.INSISTENT_MAX_PRIORITY_USE_GLOBAL --> app:isPreferenceVisible="false"/> <!-- Same as Repository.INSISTENT_MAX_PRIORITY_USE_GLOBAL -->
<SwitchPreference <SwitchPreferenceCompat
app:key="@string/detail_settings_notifications_dedicated_channels_key" app:key="@string/detail_settings_notifications_dedicated_channels_key"
app:title="@string/detail_settings_notifications_dedicated_channels_title" app:title="@string/detail_settings_notifications_dedicated_channels_title"
app:isPreferenceVisible="false"/> app:isPreferenceVisible="false"/>

View file

@ -25,7 +25,7 @@
app:entries="@array/settings_notifications_auto_delete_entries" app:entries="@array/settings_notifications_auto_delete_entries"
app:entryValues="@array/settings_notifications_auto_delete_values" app:entryValues="@array/settings_notifications_auto_delete_values"
app:defaultValue="2592000"/> app:defaultValue="2592000"/>
<SwitchPreference <SwitchPreferenceCompat
app:key="@string/settings_notifications_insistent_max_priority_key" app:key="@string/settings_notifications_insistent_max_priority_key"
app:title="@string/settings_notifications_insistent_max_priority_title" app:title="@string/settings_notifications_insistent_max_priority_title"
app:defaultValue="false"/> app:defaultValue="false"/>
@ -72,15 +72,15 @@
app:entries="@array/settings_advanced_connection_protocol_entries" app:entries="@array/settings_advanced_connection_protocol_entries"
app:entryValues="@array/settings_advanced_connection_protocol_values" app:entryValues="@array/settings_advanced_connection_protocol_values"
app:defaultValue="jsonhttp"/> app:defaultValue="jsonhttp"/>
<SwitchPreference <SwitchPreferenceCompat
app:key="@string/settings_advanced_broadcast_key" app:key="@string/settings_advanced_broadcast_key"
app:title="@string/settings_advanced_broadcast_title" app:title="@string/settings_advanced_broadcast_title"
app:enabled="true"/> app:enabled="true"/>
<SwitchPreference <SwitchPreferenceCompat
app:key="@string/settings_advanced_unifiedpush_key" app:key="@string/settings_advanced_unifiedpush_key"
app:title="@string/settings_advanced_unifiedpush_title" app:title="@string/settings_advanced_unifiedpush_title"
app:enabled="true"/> app:enabled="true"/>
<SwitchPreference <SwitchPreferenceCompat
app:key="@string/settings_advanced_record_logs_key" app:key="@string/settings_advanced_record_logs_key"
app:title="@string/settings_advanced_record_logs_title" app:title="@string/settings_advanced_record_logs_title"
app:enabled="true"/> app:enabled="true"/>