Add ability to reset profile preferences

This commit is contained in:
Vitaliy 2019-12-27 15:19:42 +02:00
parent 36e96c566c
commit e4b624c20e
7 changed files with 47 additions and 25 deletions

View file

@ -78,6 +78,13 @@
android:title="@string/copy_from_other_profile"
tools:icon="@drawable/ic_action_copy" />
<Preference
android:key="reset_to_default"
android:layout="@layout/preference_button"
android:persistent="false"
android:title="@string/reset_to_default"
tools:icon="@drawable/ic_action_reset_to_default_dark" />
<Preference
android:key="export_profile"
android:layout="@layout/preference_button"

View file

@ -306,12 +306,17 @@ public class OsmandSettings {
if (pref instanceof CommonPreference) {
CommonPreference commonPreference = (CommonPreference) pref;
if (!commonPreference.global) {
commonPreference.copyFromMode(modeFrom, modeTo);
Object copiedValue = commonPreference.getModeValue(modeFrom);
commonPreference.setModeValue(modeTo, copiedValue);
}
}
}
}
public boolean resetPreferencesForProfile(ApplicationMode mode) {
return settingsAPI.edit(getProfilePreferences(mode)).clear().commit();
}
public boolean setPreference(String key, Object value) {
return setPreference(key, value, APPLICATION_MODE.get());
}
@ -734,23 +739,6 @@ public class OsmandSettings {
return settingsAPI.contains(getProfilePreferences(mode), getId());
}
public boolean copyFromMode(ApplicationMode modeFrom, ApplicationMode modeTo) {
if (global) {
return false;
}
T copiedValue = getModeValue(modeFrom);
Object profilePrefs = getProfilePreferences(modeTo);
boolean valueSaved = setValue(profilePrefs, copiedValue);
if (valueSaved && cache && cachedPreference == profilePrefs) {
cachedValue = copiedValue;
}
fireEvent(copiedValue);
return valueSaved;
}
@Override
public boolean writeToJson(JSONObject json, ApplicationMode appMode) throws JSONException {
if (appMode != null) {

View file

@ -50,7 +50,13 @@ public class FileSettingsAPIImpl implements SettingsAPI {
modified.put(wrap(pref,key), null);
return this;
}
@Override
public SettingsEditor clear() {
modified.clear();
return this;
}
@Override
public SettingsEditor putString(String key, String value) {
modified.put(wrap(pref,key), value);

View file

@ -13,6 +13,7 @@ public interface SettingsAPI {
public SettingsEditor putInt(String key, int value);
public SettingsEditor putLong(String key, long value);
public SettingsEditor remove(String key);
public SettingsEditor clear();
public boolean commit();
}

View file

@ -28,7 +28,13 @@ public class SettingsAPIImpl implements SettingsAPI {
edit.remove(key);
return this;
}
@Override
public SettingsEditor clear() {
edit.clear();
return this;
}
@Override
public SettingsEditor putString(String key, String value) {
edit.putString(key, value);

View file

@ -71,6 +71,7 @@ public class ConfigureProfileFragment extends BaseSettingsFragment {
private static final String CONFIGURE_MAP = "configure_map";
private static final String CONFIGURE_SCREEN = "configure_screen";
private static final String COPY_FROM_OTHER_PROFILE = "copy_from_other_profile";
private static final String RESET_TO_DEFAULT = "reset_to_default";
private static final String EXPORT_PROFILE = "export_profile";
private static final String DELETE_PROFILE = "delete_profile";
private static final String PROFILE_APPEARANCE = "profile_appearance";
@ -211,6 +212,7 @@ public class ConfigureProfileFragment extends BaseSettingsFragment {
settingsActions.setIconSpaceReserved(false);
setupCopyFromOtherProfilePref();
setupResetToDefaultPref();
setupExportProfilePref();
setupDeleteProfilePref();
}
@ -270,6 +272,12 @@ public class ConfigureProfileFragment extends BaseSettingsFragment {
isNightMode() ? R.color.active_color_primary_dark : R.color.active_color_primary_light));
}
private void setupResetToDefaultPref() {
Preference copyProfilePrefs = findPreference(RESET_TO_DEFAULT);
copyProfilePrefs.setIcon(app.getUIUtilities().getIcon(R.drawable.ic_action_reset_to_default_dark,
isNightMode() ? R.color.active_color_primary_dark : R.color.active_color_primary_light));
}
private void setupExportProfilePref() {
Preference exportProfile = findPreference(EXPORT_PROFILE);
exportProfile.setIcon(app.getUIUtilities().getIcon(R.drawable.ic_action_app_configuration,
@ -359,8 +367,10 @@ public class ConfigureProfileFragment extends BaseSettingsFragment {
}
@Override
public void onAppModeChanged(ApplicationMode appMode) {
settings.copyPreferencesFromProfile(appMode, getSelectedAppMode());
public void onAppModeChanged(final ApplicationMode appMode) {
long start = System.currentTimeMillis();
app.getSettings().copyPreferencesFromProfile(appMode, getSelectedAppMode());
LOG.debug("copyPrefs " + (System.currentTimeMillis() - start));
}
@Override
@ -393,6 +403,8 @@ public class ConfigureProfileFragment extends BaseSettingsFragment {
SelectAppModesBottomSheetDialogFragment.showInstance(fragmentManager,
ConfigureProfileFragment.this, false, getSelectedAppMode(), false);
}
} else if (RESET_TO_DEFAULT.equals(prefId)) {
app.getSettings().resetPreferencesForProfile(getSelectedAppMode());
} else if (EXPORT_PROFILE.equals(prefId)) {
Context ctx = requireContext();
final ApplicationMode profile = getSelectedAppMode();

View file

@ -470,13 +470,15 @@ public class ProfileAppearanceFragment extends BaseSettingsFragment {
.setRoutingProfile(changedProfile.routingProfile)
.setColor(changedProfile.color);
if (ApplicationMode.valueOfStringKey(changedProfile.stringKey, null) == null) {
settings.copyPreferencesFromProfile(changedProfile.parent, getSelectedAppMode());
}
boolean newProfile = ApplicationMode.valueOfStringKey(changedProfile.stringKey, null) == null;
ApplicationMode mode = ApplicationMode.saveProfile(builder, getMyApplication());
if (!ApplicationMode.values(app).contains(mode)) {
ApplicationMode.changeProfileAvailability(mode, true, getMyApplication());
}
if (newProfile) {
app.getSettings().copyPreferencesFromProfile(changedProfile.parent, mode);
}
return true;
}