Show SnackBar when preference change p.4

This commit is contained in:
Nazar-Kutz 2020-04-10 10:05:36 +03:00
parent ba7d1fa7e7
commit b7e42c1646
8 changed files with 47 additions and 67 deletions

View file

@ -21,7 +21,6 @@ import net.osmand.plus.myplaces.FavoritesActivity;
import net.osmand.plus.profiles.SelectCopyAppModeBottomSheet;
import net.osmand.plus.profiles.SelectCopyAppModeBottomSheet.CopyAppModePrefsListener;
import net.osmand.plus.settings.BaseSettingsFragment;
import net.osmand.plus.settings.bottomsheets.ChangeGeneralProfilesPrefBottomSheet;
import net.osmand.plus.settings.bottomsheets.ResetProfilePrefsBottomSheet;
import net.osmand.plus.settings.bottomsheets.ResetProfilePrefsBottomSheet.ResetAppModePrefsListener;
import net.osmand.plus.settings.bottomsheets.SingleSelectPreferenceBottomSheet;
@ -29,7 +28,6 @@ import net.osmand.plus.settings.preferences.ListPreferenceEx;
import net.osmand.plus.settings.preferences.SwitchPreferenceEx;
import net.osmand.plus.widgets.style.CustomTypefaceSpan;
import java.io.Serializable;
import java.util.HashMap;
import java.util.LinkedHashMap;
@ -288,25 +286,19 @@ public class MonitoringSettingsFragment extends BaseSettingsFragment
}
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
String prefId = preference.getKey();
OsmandSettings.OsmandPreference pref = settings.getPreference(prefId);
public void onApplyPreferenceChange(String prefId, boolean applyToAllProfiles, Object newValue) {
if (SAVE_GLOBAL_TRACK_INTERVAL.equals(prefId)) {
OsmandSettings.OsmandPreference pref = settings.getPreference(prefId);
if (newValue instanceof Boolean) {
prefId = settings.SAVE_GLOBAL_TRACK_REMEMBER.getId();
newValue = Boolean.FALSE;
}
if (pref instanceof OsmandSettings.CommonPreference && !((OsmandSettings.CommonPreference) pref).hasDefaultValueForMode(getSelectedAppMode())) {
FragmentManager fragmentManager = getFragmentManager();
if (fragmentManager != null && newValue instanceof Serializable) {
ChangeGeneralProfilesPrefBottomSheet.showInstance(fragmentManager, prefId,
(Serializable) newValue, this, false, getSelectedAppMode());
}
return false;
applyPreference(settings.SAVE_GLOBAL_TRACK_REMEMBER.getId(), applyToAllProfiles, false);
} else if (pref instanceof OsmandSettings.CommonPreference
&& !((OsmandSettings.CommonPreference) pref).hasDefaultValueForMode(getSelectedAppMode())) {
applyPreference(SAVE_GLOBAL_TRACK_INTERVAL, applyToAllProfiles, newValue);
applyPreference(settings.SAVE_GLOBAL_TRACK_REMEMBER.getId(), applyToAllProfiles, true);
}
} else {
super.onApplyPreferenceChange(prefId, applyToAllProfiles, newValue);
}
return super.onPreferenceChange(preference, newValue);
}
@Override
@ -343,16 +335,4 @@ public class MonitoringSettingsFragment extends BaseSettingsFragment
updateAllSettings();
}
}
@Override
public void onSettingApplied(String prefId, boolean appliedToAllProfiles) {
if (settings.SAVE_GLOBAL_TRACK_INTERVAL.getId().equals(prefId)) {
if (appliedToAllProfiles) {
app.getSettings().setPreferenceForAllModes(settings.SAVE_GLOBAL_TRACK_REMEMBER.getId(), true);
} else {
app.getSettings().setPreference(settings.SAVE_GLOBAL_TRACK_REMEMBER.getId(), true, getSelectedAppMode());
}
}
}
}

View file

@ -47,6 +47,7 @@ import androidx.preference.TwoStatePreference;
import androidx.recyclerview.widget.RecyclerView;
import com.google.android.material.appbar.AppBarLayout;
import com.google.android.material.snackbar.BaseTransientBottomBar;
import com.google.android.material.snackbar.Snackbar;
import net.osmand.AndroidUtils;
@ -318,7 +319,7 @@ public abstract class BaseSettingsFragment extends PreferenceFragmentCompat impl
}
@Override
public boolean onConfirmPreferenceChange(String prefId, Object newValue, ApplyQueryType applyQueryType) {
public final boolean onConfirmPreferenceChange(String prefId, Object newValue, ApplyQueryType applyQueryType) {
if (applyQueryType != null && newValue instanceof Serializable) {
OsmandSettings.OsmandPreference pref = settings.getPreference(prefId);
if (pref instanceof CommonPreference) {
@ -605,11 +606,12 @@ public abstract class BaseSettingsFragment extends PreferenceFragmentCompat impl
updateAllSettings();
}
public void onSettingApplied(String prefId, boolean appliedToAllProfiles) {
public void onApplyPreferenceChange(String prefId, boolean applyToAllProfiles, Object newValue) {
applyPreference(prefId, applyToAllProfiles, newValue);
}
public void onApplyPreferenceChange(String prefId, boolean appliedToAllProfiles, Object newValue) {
if (appliedToAllProfiles) {
protected final void applyPreference(String prefId, boolean applyToAllProfiles, Object newValue) {
if (applyToAllProfiles) {
app.getSettings().setPreferenceForAllModes(prefId, newValue);
} else {
app.getSettings().setPreference(prefId, newValue, getSelectedAppMode());
@ -922,6 +924,11 @@ public abstract class BaseSettingsFragment extends PreferenceFragmentCompat impl
}
});
UiUtilities.setupSnackbarVerticalLayout(snackbar);
try {
snackbar.setAnimationMode(BaseTransientBottomBar.ANIMATION_MODE_FADE);
} catch (Throwable e) {
}
snackbar.show();
}
}

View file

@ -328,18 +328,18 @@ public class GeneralProfileSettingsFragment extends BaseSettingsFragment impleme
}
@Override
public void onApplyPreferenceChange(String prefId, boolean appliedToAllProfiles, Object newValue) {
public void onApplyPreferenceChange(String prefId, boolean applyToAllProfiles, Object newValue) {
if (settings.DRIVING_REGION.getId().equals(prefId)) {
ApplicationMode selectedMode = getSelectedAppMode();
if (newValue == null) {
super.onApplyPreferenceChange(settings.DRIVING_REGION_AUTOMATIC.getId(), appliedToAllProfiles, true);
MapViewTrackingUtilities mapViewTrackingUtilities = getMyApplication().getMapViewTrackingUtilities();
applyPreference(settings.DRIVING_REGION_AUTOMATIC.getId(), applyToAllProfiles, true);
MapViewTrackingUtilities mapViewTrackingUtilities = requireMyApplication().getMapViewTrackingUtilities();
if (mapViewTrackingUtilities != null) {
mapViewTrackingUtilities.resetDrivingRegionUpdate();
}
} else if (newValue instanceof OsmandSettings.DrivingRegion) {
super.onApplyPreferenceChange(settings.DRIVING_REGION_AUTOMATIC.getId(), appliedToAllProfiles, false);
if (appliedToAllProfiles) {
applyPreference(settings.DRIVING_REGION_AUTOMATIC.getId(), applyToAllProfiles, false);
if (applyToAllProfiles) {
for (ApplicationMode appMode : ApplicationMode.allPossibleValues()) {
settings.DRIVING_REGION.setModeValue(appMode, (OsmandSettings.DrivingRegion) newValue);
}
@ -349,7 +349,7 @@ public class GeneralProfileSettingsFragment extends BaseSettingsFragment impleme
}
updateAllSettings();
} else {
super.onApplyPreferenceChange(prefId, appliedToAllProfiles, newValue);
applyPreference(prefId, applyToAllProfiles, newValue);
}
}

View file

@ -108,20 +108,20 @@ public class MapDuringNavigationFragment extends BaseSettingsFragment {
}
@Override
public void onApplyPreferenceChange(String prefId, boolean appliedToAllProfiles, Object newValue) {
public void onApplyPreferenceChange(String prefId, boolean applyToAllProfiles, Object newValue) {
if (settings.AUTO_ZOOM_MAP.getId().equals(prefId)) {
if (newValue instanceof Integer) {
int position = (int) newValue;
if (position == 0) {
super.onApplyPreferenceChange(settings.AUTO_ZOOM_MAP.getId(), appliedToAllProfiles, false);
applyPreference(settings.AUTO_ZOOM_MAP.getId(), applyToAllProfiles, false);
} else {
super.onApplyPreferenceChange(settings.AUTO_ZOOM_MAP.getId(), appliedToAllProfiles, true);
super.onApplyPreferenceChange(settings.AUTO_ZOOM_MAP_SCALE.getId(),
appliedToAllProfiles, OsmandSettings.AutoZoomMap.values()[position - 1]);
applyPreference(settings.AUTO_ZOOM_MAP.getId(), applyToAllProfiles, true);
applyPreference(settings.AUTO_ZOOM_MAP_SCALE.getId(),
applyToAllProfiles, OsmandSettings.AutoZoomMap.values()[position - 1]);
}
}
} else {
super.onApplyPreferenceChange(prefId, appliedToAllProfiles, newValue);
super.onApplyPreferenceChange(prefId, applyToAllProfiles, newValue);
}
}
}

View file

@ -92,12 +92,12 @@ public class NavigationFragment extends BaseSettingsFragment {
}
@Override
public void onApplyPreferenceChange(String prefId, boolean appliedToAllProfiles, Object newValue) {
public void onApplyPreferenceChange(String prefId, boolean applyToAllProfiles, Object newValue) {
if (settings.VOICE_MUTE.getId().equals(prefId) && newValue instanceof Boolean) {
super.onApplyPreferenceChange(prefId, appliedToAllProfiles, !(Boolean) newValue);
applyPreference(prefId, applyToAllProfiles, !(Boolean) newValue);
updateMenu();
} else {
super.onApplyPreferenceChange(prefId, appliedToAllProfiles, newValue);
applyPreference(prefId, applyToAllProfiles, newValue);
}
}

View file

@ -411,7 +411,7 @@ public class RouteParametersFragment extends BaseSettingsFragment implements OnP
}
@Override
public void onApplyPreferenceChange(String prefId, boolean appliedToAllProfiles, Object newValue) {
public void onApplyPreferenceChange(String prefId, boolean applyToAllProfiles, Object newValue) {
if ((RELIEF_SMOOTHNESS_FACTOR.equals(prefId) || DRIVING_STYLE.equals(prefId)) && newValue instanceof String) {
ApplicationMode appMode = getSelectedAppMode();
String selectedParameterId = (String) newValue;
@ -422,11 +422,7 @@ public class RouteParametersFragment extends BaseSettingsFragment implements OnP
}
recalculateRoute();
} else if (ROUTING_SHORT_WAY.equals(prefId) && newValue instanceof Boolean) {
if (appliedToAllProfiles) {
settings.setPreferenceForAllModes(settings.FAST_ROUTE_MODE.getId(), !(Boolean) newValue);
} else {
settings.setPreference(settings.FAST_ROUTE_MODE.getId(), !(Boolean) newValue, getSelectedAppMode());
}
applyPreference(settings.FAST_ROUTE_MODE.getId(), applyToAllProfiles, !(Boolean) newValue);
} else if (ROUTING_RECALC_DISTANCE.equals(prefId)) {
boolean enabled = false;
float valueToSave = DISABLE_MODE;
@ -437,11 +433,11 @@ public class RouteParametersFragment extends BaseSettingsFragment implements OnP
valueToSave = (float) newValue;
enabled = valueToSave != DISABLE_MODE;
}
super.onApplyPreferenceChange(ROUTING_RECALC_DISTANCE, appliedToAllProfiles, valueToSave);
super.onApplyPreferenceChange(settings.DISABLE_OFFROUTE_RECALC.getId(), appliedToAllProfiles, !enabled);
applyPreference(ROUTING_RECALC_DISTANCE, applyToAllProfiles, valueToSave);
applyPreference(settings.DISABLE_OFFROUTE_RECALC.getId(), applyToAllProfiles, !enabled);
updateRouteRecalcDistancePref();
} else {
super.onApplyPreferenceChange(prefId, appliedToAllProfiles, newValue);
super.onApplyPreferenceChange(prefId, applyToAllProfiles, newValue);
}
}

View file

@ -291,15 +291,15 @@ public class VoiceAnnouncesFragment extends BaseSettingsFragment {
}
@Override
public void onApplyPreferenceChange(String prefId, boolean appliedToAllProfiles, Object newValue) {
public void onApplyPreferenceChange(String prefId, boolean applyToAllProfiles, Object newValue) {
if (prefId.equals(settings.VOICE_PROVIDER.getId()) && newValue instanceof String) {
if (VOICE_PROVIDER_NOT_USE.equals(newValue)) {
super.onApplyPreferenceChange(settings.VOICE_MUTE.getId(), appliedToAllProfiles, true);
applyPreference(settings.VOICE_MUTE.getId(), applyToAllProfiles, true);
updateToolbar();
}
super.onApplyPreferenceChange(settings.VOICE_PROVIDER.getId(), appliedToAllProfiles, newValue);
applyPreference(settings.VOICE_PROVIDER.getId(), applyToAllProfiles, newValue);
app.initVoiceCommandPlayer(getActivity(), getSelectedAppMode(),
false, null, true, false, appliedToAllProfiles);
false, null, true, false, applyToAllProfiles);
} else if (prefId.equals(settings.AUDIO_MANAGER_STREAM.getId())) {
// Sync DEFAULT value with CAR value, as we have other way to set it for now
@ -310,7 +310,7 @@ public class VoiceAnnouncesFragment extends BaseSettingsFragment {
}
settings.AUDIO_USAGE.setModeValue(ApplicationMode.DEFAULT, settings.AUDIO_USAGE.getModeValue(ApplicationMode.CAR));
} else {
super.onApplyPreferenceChange(prefId, appliedToAllProfiles, newValue);
super.onApplyPreferenceChange(prefId, applyToAllProfiles, newValue);
}
}
}

View file

@ -55,7 +55,6 @@ public class ChangeGeneralProfilesPrefBottomSheet extends BasePreferenceBottomSh
.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
app.getSettings().setPreferenceForAllModes(prefId, newValue);
updateTargetSettings(false, true);
dismiss();
}
@ -72,7 +71,6 @@ public class ChangeGeneralProfilesPrefBottomSheet extends BasePreferenceBottomSh
.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
app.getSettings().setPreference(prefId, newValue, getAppMode());
updateTargetSettings(false, false);
dismiss();
}
@ -106,12 +104,11 @@ public class ChangeGeneralProfilesPrefBottomSheet extends BasePreferenceBottomSh
outState.putSerializable(NEW_VALUE_KEY, newValue);
}
private void updateTargetSettings(boolean discard, boolean appliedToAllProfiles) {
private void updateTargetSettings(boolean discard, boolean applyToAllProfiles) {
BaseSettingsFragment target = (BaseSettingsFragment) getTargetFragment();
if (target != null) {
if (!discard) {
target.onSettingApplied(getPrefId(), appliedToAllProfiles);
target.onApplyPreferenceChange(getPrefId(), appliedToAllProfiles, newValue);
target.onApplyPreferenceChange(getPrefId(), applyToAllProfiles, newValue);
}
target.updateSetting(getPrefId());
if (!discard) {