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,29 +15,25 @@ 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) {
themePreference.setOnPreferenceChangeListener( throw new IllegalStateException(
(preference, newValue) -> { "The preference resource file did not contain theme or color preferences");
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;
}
});
} }
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() { public SettingsActivity requireSettingsActivity() {