From 1f22c5f53434ef4e67d8e088dd88e8184e561eaa Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Tue, 21 Feb 2023 09:26:07 +0100 Subject: [PATCH] show dynamic colors setting only if available --- .../settings/InterfaceSettingsFragment.java | 43 ++++++++----------- 1 file changed, 19 insertions(+), 24 deletions(-) diff --git a/app/src/main/java/im/conversations/android/ui/fragment/settings/InterfaceSettingsFragment.java b/app/src/main/java/im/conversations/android/ui/fragment/settings/InterfaceSettingsFragment.java index 623d58906..d06baa9df 100644 --- a/app/src/main/java/im/conversations/android/ui/fragment/settings/InterfaceSettingsFragment.java +++ b/app/src/main/java/im/conversations/android/ui/fragment/settings/InterfaceSettingsFragment.java @@ -1,10 +1,9 @@ package im.conversations.android.ui.fragment.settings; import android.os.Bundle; -import androidx.annotation.NonNull; import androidx.annotation.Nullable; -import androidx.preference.Preference; import androidx.preference.PreferenceFragmentCompat; +import com.google.android.material.color.DynamicColors; import im.conversations.android.Conversations; import im.conversations.android.R; import im.conversations.android.ui.activity.SettingsActivity; @@ -16,29 +15,25 @@ public class InterfaceSettingsFragment extends PreferenceFragmentCompat { setPreferencesFromResource(R.xml.preferences_interface, rootKey); final var themePreference = findPreference("theme"); final var dynamicColors = findPreference("dynamic_colors"); - if (themePreference != null) { - themePreference.setOnPreferenceChangeListener( - (preference, newValue) -> { - if (newValue instanceof String) { - final String theme = (String) newValue; - final int desiredNightMode = Conversations.getDesiredNightMode(theme); - requireSettingsActivity().setDesiredNightMode(desiredNightMode); - } - return true; - }); - } - if (dynamicColors != null) { - dynamicColors.setOnPreferenceChangeListener( - new Preference.OnPreferenceChangeListener() { - @Override - public boolean onPreferenceChange( - @NonNull Preference preference, Object newValue) { - requireSettingsActivity() - .setDynamicColors(Boolean.TRUE.equals(newValue)); - return true; - } - }); + if (themePreference == null || dynamicColors == null) { + throw new IllegalStateException( + "The preference resource file did not contain theme or color preferences"); } + themePreference.setOnPreferenceChangeListener( + (preference, newValue) -> { + if (newValue instanceof String) { + final String theme = (String) newValue; + final int desiredNightMode = Conversations.getDesiredNightMode(theme); + requireSettingsActivity().setDesiredNightMode(desiredNightMode); + } + return true; + }); + dynamicColors.setVisible(DynamicColors.isDynamicColorAvailable()); + dynamicColors.setOnPreferenceChangeListener( + (preference, newValue) -> { + requireSettingsActivity().setDynamicColors(Boolean.TRUE.equals(newValue)); + return true; + }); } public SettingsActivity requireSettingsActivity() {