show dynamic colors setting only if available

This commit is contained in:
Daniel Gultsch 2023-02-21 09:26:07 +01:00
parent 7d42da8c34
commit 1f22c5f534
No known key found for this signature in database
GPG key ID: F43D18AD2A0982C2

View file

@ -1,10 +1,9 @@
package im.conversations.android.ui.fragment.settings; package im.conversations.android.ui.fragment.settings;
import android.os.Bundle; import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.preference.Preference;
import androidx.preference.PreferenceFragmentCompat; import androidx.preference.PreferenceFragmentCompat;
import com.google.android.material.color.DynamicColors;
import im.conversations.android.Conversations; import im.conversations.android.Conversations;
import im.conversations.android.R; import im.conversations.android.R;
import im.conversations.android.ui.activity.SettingsActivity; import im.conversations.android.ui.activity.SettingsActivity;
@ -16,7 +15,10 @@ public class InterfaceSettingsFragment extends PreferenceFragmentCompat {
setPreferencesFromResource(R.xml.preferences_interface, rootKey); setPreferencesFromResource(R.xml.preferences_interface, rootKey);
final var themePreference = findPreference("theme"); final var themePreference = findPreference("theme");
final var dynamicColors = findPreference("dynamic_colors"); final var dynamicColors = findPreference("dynamic_colors");
if (themePreference != null) { if (themePreference == null || dynamicColors == null) {
throw new IllegalStateException(
"The preference resource file did not contain theme or color preferences");
}
themePreference.setOnPreferenceChangeListener( themePreference.setOnPreferenceChangeListener(
(preference, newValue) -> { (preference, newValue) -> {
if (newValue instanceof String) { if (newValue instanceof String) {
@ -26,20 +28,13 @@ public class InterfaceSettingsFragment extends PreferenceFragmentCompat {
} }
return true; return true;
}); });
} dynamicColors.setVisible(DynamicColors.isDynamicColorAvailable());
if (dynamicColors != null) {
dynamicColors.setOnPreferenceChangeListener( dynamicColors.setOnPreferenceChangeListener(
new Preference.OnPreferenceChangeListener() { (preference, newValue) -> {
@Override requireSettingsActivity().setDynamicColors(Boolean.TRUE.equals(newValue));
public boolean onPreferenceChange(
@NonNull Preference preference, Object newValue) {
requireSettingsActivity()
.setDynamicColors(Boolean.TRUE.equals(newValue));
return true; return true;
}
}); });
} }
}
public SettingsActivity requireSettingsActivity() { public SettingsActivity requireSettingsActivity() {
final var activity = requireActivity(); final var activity = requireActivity();