Add check if pref was set for app mode

This commit is contained in:
Chumva 2019-09-19 13:19:54 +03:00
parent 73af3931e8
commit eb1b61d8bf
3 changed files with 26 additions and 18 deletions

View file

@ -81,6 +81,8 @@ public class OsmandSettings {
void removeListener(StateChangedListener<T> listener); void removeListener(StateChangedListener<T> listener);
boolean isSet(); boolean isSet();
boolean isSetForMode(ApplicationMode m);
} }
private abstract class PreferenceWithListener<T> implements OsmandPreference<T> { private abstract class PreferenceWithListener<T> implements OsmandPreference<T> {
@ -326,13 +328,12 @@ public class OsmandSettings {
// this value string is synchronized with settings_pref.xml preference name // this value string is synchronized with settings_pref.xml preference name
public final OsmandPreference<ApplicationMode> APPLICATION_MODE = new PreferenceWithListener<ApplicationMode>() { public final OsmandPreference<ApplicationMode> APPLICATION_MODE = new PreferenceWithListener<ApplicationMode>() {
@Override @Override
public String getId() { public String getId() {
return "application_mode"; return "application_mode";
} }
;
@Override @Override
public ApplicationMode get() { public ApplicationMode get() {
return currentMode; return currentMode;
@ -348,14 +349,15 @@ public class OsmandSettings {
set(ApplicationMode.DEFAULT); set(ApplicationMode.DEFAULT);
} }
;
@Override @Override
public boolean isSet() { public boolean isSet() {
return true; return true;
} }
; @Override
public boolean isSetForMode(ApplicationMode m) {
return true;
}
@Override @Override
public boolean set(ApplicationMode val) { public boolean set(ApplicationMode val) {
@ -582,6 +584,9 @@ public class OsmandSettings {
return settingsAPI.contains(getPreferences(), getId()); return settingsAPI.contains(getPreferences(), getId());
} }
public boolean isSetForMode(ApplicationMode mode) {
return settingsAPI.contains(getProfilePreferences(mode), getId());
}
} }
public class BooleanPreference extends CommonPreference<Boolean> { public class BooleanPreference extends CommonPreference<Boolean> {

View file

@ -279,10 +279,15 @@ public class GeneralProfileSettingsFragment extends BaseSettingsFragment {
@Override @Override
public boolean onPreferenceChange(Preference preference, Object newValue) { public boolean onPreferenceChange(Preference preference, Object newValue) {
FragmentManager fragmentManager = getFragmentManager(); OsmandSettings.OsmandPreference pref = settings.getPreference(preference.getKey());
if (fragmentManager != null) { if (pref != null && !pref.isSetForMode(getSelectedAppMode())) {
ChangeGeneralProfilesPrefBottomSheet.showInstance(fragmentManager, preference.getKey(), newValue, this, false); FragmentManager fragmentManager = getFragmentManager();
if (fragmentManager != null) {
ChangeGeneralProfilesPrefBottomSheet.showInstance(fragmentManager, preference.getKey(), newValue, this, false);
}
return false;
} }
return false;
return true;
} }
} }

View file

@ -41,9 +41,11 @@ public class ChangeGeneralProfilesPrefBottomSheet extends BasePreferenceBottomSh
if (app == null || args == null || newValue == null || !args.containsKey(PREFERENCE_ID)) { if (app == null || args == null || newValue == null || !args.containsKey(PREFERENCE_ID)) {
return; return;
} }
final String prefId = args.getString(PREFERENCE_ID); final String prefId = args.getString(PREFERENCE_ID);
CommonPreference pref = getPreference(prefId); CommonPreference pref = getPreference(prefId);
if (pref == null) { OsmandPreference osmandPref = app.getSettings().getPreference(prefId);
if (pref == null || osmandPref == null) {
return; return;
} }
@ -51,20 +53,16 @@ public class ChangeGeneralProfilesPrefBottomSheet extends BasePreferenceBottomSh
StringBuilder builder = new StringBuilder(); StringBuilder builder = new StringBuilder();
final List<ApplicationMode> values = ApplicationMode.values(app); final List<ApplicationMode> values = ApplicationMode.values(app);
List<ApplicationMode> appModesSameValue = new ArrayList<>(); List<ApplicationMode> appModesDefaultValue = new ArrayList<>();
for (int i = 0; i < values.size(); i++) { for (int i = 0; i < values.size(); i++) {
ApplicationMode mode = values.get(i); ApplicationMode mode = values.get(i);
Object modeValue = pref.getModeValue(mode); if (!osmandPref.isSetForMode(mode)) {
if (modeValue instanceof Enum) { appModesDefaultValue.add(mode);
modeValue = ((Enum) modeValue).ordinal();
}
if (modeValue.equals(newValue)) {
appModesSameValue.add(mode);
} }
} }
Iterator<ApplicationMode> iterator = appModesSameValue.iterator(); Iterator<ApplicationMode> iterator = appModesDefaultValue.iterator();
while (iterator.hasNext()) { while (iterator.hasNext()) {
builder.append(iterator.next().toHumanString(app)); builder.append(iterator.next().toHumanString(app));
builder.append(iterator.hasNext() ? ", " : '.'); builder.append(iterator.hasNext() ? ", " : '.');