From bc4528497b97a2c6ba650aac3757eeef0b30f4dd Mon Sep 17 00:00:00 2001 From: Dima-1 Date: Fri, 29 Nov 2019 12:56:43 +0200 Subject: [PATCH] Implement switch onPreferenceChange --- .../plus/settings/MainSettingsFragment.java | 36 +++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/settings/MainSettingsFragment.java b/OsmAnd/src/net/osmand/plus/settings/MainSettingsFragment.java index 23fca293e3..63acf0bc05 100644 --- a/OsmAnd/src/net/osmand/plus/settings/MainSettingsFragment.java +++ b/OsmAnd/src/net/osmand/plus/settings/MainSettingsFragment.java @@ -7,7 +7,6 @@ import android.support.annotation.ColorRes; import android.support.v7.preference.Preference; import android.support.v7.preference.PreferenceCategory; import android.support.v7.preference.PreferenceViewHolder; -import android.support.v7.preference.SwitchPreferenceCompat; import android.view.View; import net.osmand.AndroidUtils; @@ -15,6 +14,7 @@ import net.osmand.plus.ApplicationMode; import net.osmand.plus.OsmandApplication; import net.osmand.plus.R; import net.osmand.plus.UiUtilities; +import net.osmand.plus.settings.preferences.SwitchPreferenceEx; import java.util.ArrayList; import java.util.LinkedHashSet; @@ -77,6 +77,29 @@ public class MainSettingsFragment extends BaseSettingsFragment { } } + @Override + public boolean onPreferenceChange(Preference preference, Object newValue) { + String key = preference.getKey(); + ApplicationMode applicationMode = getAppMode(key); + if (applicationMode != null) { + if (newValue instanceof Boolean) { + boolean isChecked = (Boolean) newValue; + onProfileSelected(applicationMode, isChecked); + preference.setIcon(getAppProfilesIcon(applicationMode, isChecked)); + } + } + return super.onPreferenceChange(preference, newValue); + } + + ApplicationMode getAppMode(String key) { + for (ApplicationMode applicationMode : allAppModes) { + if (applicationMode.getStringKey().equals(key)) { + return applicationMode; + } + } + return null; + } + private void setupConfigureProfilePref() { ApplicationMode selectedMode = getSelectedAppMode(); String title = selectedMode.toHumanString(getContext()); @@ -107,7 +130,7 @@ public class MainSettingsFragment extends BaseSettingsFragment { } for (ApplicationMode applicationMode : allAppModes) { boolean isAppProfileEnabled = availableAppModes.contains(applicationMode); - SwitchPreferenceCompat pref = new SwitchPreferenceCompat(app); + SwitchPreferenceEx pref = new SwitchPreferenceEx(app); pref.setPersistent(false); pref.setKey(applicationMode.getStringKey()); pref.setIcon(getAppProfilesIcon(applicationMode, isAppProfileEnabled)); @@ -120,6 +143,15 @@ public class MainSettingsFragment extends BaseSettingsFragment { } } + public void onProfileSelected(ApplicationMode item, boolean isChecked) { + if (isChecked) { + availableAppModes.add(item); + } else { + availableAppModes.remove(item); + } + ApplicationMode.changeProfileAvailability(item, isChecked, getMyApplication()); + } + private Drawable getAppProfilesIcon(ApplicationMode applicationMode, boolean appProfileEnabled) { int iconResId = applicationMode.getIconRes(); return appProfileEnabled ? app.getUIUtilities().getIcon(applicationMode.getIconRes(), applicationMode.getIconColorInfo().getColor(isNightMode()))