Merge pull request #8754 from osmandapp/PreferenceChangeSnackBar

Show SnackBar when preference change
This commit is contained in:
vshcherb 2020-04-03 14:45:06 +02:00 committed by GitHub
commit bf87faf22e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 88 additions and 34 deletions

View file

@ -11,6 +11,7 @@
Thx - Hardy
-->
<string name="changes_applied_to_profile">Changes applied to %1$s profile.</string>
<string name="custom_osmand_plugin">Custom OsmAnd plugin</string>
<string name="app_mode_ski_snowmobile">Snowmobile</string>
<string name="app_mode_ski_touring">Ski touring</string>

View file

@ -306,7 +306,7 @@ public class MonitoringSettingsFragment extends BaseSettingsFragment
return false;
}
}
return true;
return super.onPreferenceChange(preference, newValue);
}
@Override

View file

@ -4,10 +4,13 @@ import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.res.ColorStateList;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.LayerDrawable;
import android.os.Build;
import android.os.Bundle;
import android.text.SpannableString;
import android.text.style.StyleSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
@ -44,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.Snackbar;
import net.osmand.AndroidUtils;
import net.osmand.PlatformUtil;
@ -51,6 +55,7 @@ import net.osmand.access.AccessibilitySettingsFragment;
import net.osmand.plus.ApplicationMode;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.OsmandSettings;
import net.osmand.plus.OsmandSettings.CommonPreference;
import net.osmand.plus.R;
import net.osmand.plus.UiUtilities;
import net.osmand.plus.activities.MapActivity;
@ -63,6 +68,7 @@ import net.osmand.plus.osmedit.OsmEditingFragment;
import net.osmand.plus.profiles.SelectAppModesBottomSheetDialogFragment;
import net.osmand.plus.profiles.SelectAppModesBottomSheetDialogFragment.AppModeChangedListener;
import net.osmand.plus.settings.bottomsheets.BooleanPreferenceBottomSheet;
import net.osmand.plus.settings.bottomsheets.ChangeGeneralProfilesPrefBottomSheet;
import net.osmand.plus.settings.bottomsheets.EditTextPreferenceBottomSheet;
import net.osmand.plus.settings.bottomsheets.MultiSelectPreferencesBottomSheet;
import net.osmand.plus.settings.bottomsheets.SingleSelectPreferenceBottomSheet;
@ -72,6 +78,8 @@ import net.osmand.plus.settings.preferences.SwitchPreferenceEx;
import org.apache.commons.logging.Log;
import java.io.Serializable;
import static net.osmand.aidlapi.OsmAndCustomizationConstants.DRAWER_SETTINGS_ID;
public abstract class BaseSettingsFragment extends PreferenceFragmentCompat implements OnPreferenceChangeListener,
@ -101,42 +109,48 @@ public abstract class BaseSettingsFragment extends PreferenceFragmentCompat impl
public enum SettingsScreenType {
MAIN_SETTINGS(MainSettingsFragment.TAG, false, R.xml.settings_main_screen, R.layout.global_preference_toolbar),
GLOBAL_SETTINGS(GlobalSettingsFragment.class.getName(), false, R.xml.global_settings, R.layout.global_preference_toolbar),
CONFIGURE_PROFILE(ConfigureProfileFragment.class.getName(), true, R.xml.configure_profile, R.layout.profile_preference_toolbar_with_switch),
PROXY_SETTINGS(ProxySettingsFragment.class.getName(), false, R.xml.proxy_preferences, R.layout.global_preferences_toolbar_with_switch),
GENERAL_PROFILE(GeneralProfileSettingsFragment.class.getName(), true, R.xml.general_profile_settings, R.layout.profile_preference_toolbar),
NAVIGATION(NavigationFragment.class.getName(), true, R.xml.navigation_settings_new, R.layout.profile_preference_toolbar),
COORDINATES_FORMAT(CoordinatesFormatFragment.class.getName(), true, R.xml.coordinates_format, R.layout.profile_preference_toolbar),
ROUTE_PARAMETERS(RouteParametersFragment.class.getName(), true, R.xml.route_parameters, R.layout.profile_preference_toolbar),
SCREEN_ALERTS(ScreenAlertsFragment.class.getName(), true, R.xml.screen_alerts, R.layout.profile_preference_toolbar_with_switch),
VOICE_ANNOUNCES(VoiceAnnouncesFragment.class.getName(), true, R.xml.voice_announces, R.layout.profile_preference_toolbar_with_switch),
VEHICLE_PARAMETERS(VehicleParametersFragment.class.getName(), true, R.xml.vehicle_parameters, R.layout.profile_preference_toolbar),
MAP_DURING_NAVIGATION(MapDuringNavigationFragment.class.getName(), true, R.xml.map_during_navigation, R.layout.profile_preference_toolbar),
TURN_SCREEN_ON(TurnScreenOnFragment.class.getName(), true, R.xml.turn_screen_on, R.layout.profile_preference_toolbar_with_switch),
DATA_STORAGE(DataStorageFragment.class.getName(), false, R.xml.data_storage, R.layout.global_preference_toolbar),
DIALOGS_AND_NOTIFICATIONS_SETTINGS(DialogsAndNotificationsSettingsFragment.class.getName(), false, R.xml.dialogs_and_notifications_preferences, R.layout.global_preferences_toolbar_with_switch),
PROFILE_APPEARANCE(ProfileAppearanceFragment.TAG, true, R.xml.profile_appearance, R.layout.profile_preference_toolbar),
OPEN_STREET_MAP_EDITING(OsmEditingFragment.class.getName(), false, R.xml.osm_editing, R.layout.global_preference_toolbar),
MULTIMEDIA_NOTES(MultimediaNotesFragment.class.getName(), true, R.xml.multimedia_notes, R.layout.profile_preference_toolbar),
MONITORING_SETTINGS(MonitoringSettingsFragment.class.getName(), true, R.xml.monitoring_settings, R.layout.profile_preference_toolbar),
LIVE_MONITORING(LiveMonitoringFragment.class.getName(), false, R.xml.live_monitoring, R.layout.global_preferences_toolbar_with_switch),
ACCESSIBILITY_SETTINGS(AccessibilitySettingsFragment.class.getName(), true, R.xml.accessibility_settings, R.layout.profile_preference_toolbar),
DEVELOPMENT_SETTINGS(DevelopmentSettingsFragment.class.getName(), false, R.xml.development_settings, R.layout.global_preference_toolbar);
MAIN_SETTINGS(MainSettingsFragment.TAG, false, null, R.xml.settings_main_screen, R.layout.global_preference_toolbar),
GLOBAL_SETTINGS(GlobalSettingsFragment.class.getName(), false, null, R.xml.global_settings, R.layout.global_preference_toolbar),
CONFIGURE_PROFILE(ConfigureProfileFragment.class.getName(), true, null, R.xml.configure_profile, R.layout.profile_preference_toolbar_with_switch),
PROXY_SETTINGS(ProxySettingsFragment.class.getName(), false, null, R.xml.proxy_preferences, R.layout.global_preferences_toolbar_with_switch),
GENERAL_PROFILE(GeneralProfileSettingsFragment.class.getName(), true, MessageType.BOTTOM_SHEET, R.xml.general_profile_settings, R.layout.profile_preference_toolbar),
NAVIGATION(NavigationFragment.class.getName(), true, MessageType.SNACK_BAR, R.xml.navigation_settings_new, R.layout.profile_preference_toolbar),
COORDINATES_FORMAT(CoordinatesFormatFragment.class.getName(), true, MessageType.BOTTOM_SHEET, R.xml.coordinates_format, R.layout.profile_preference_toolbar),
ROUTE_PARAMETERS(RouteParametersFragment.class.getName(), true, MessageType.SNACK_BAR, R.xml.route_parameters, R.layout.profile_preference_toolbar),
SCREEN_ALERTS(ScreenAlertsFragment.class.getName(), true, MessageType.SNACK_BAR, R.xml.screen_alerts, R.layout.profile_preference_toolbar_with_switch),
VOICE_ANNOUNCES(VoiceAnnouncesFragment.class.getName(), true, MessageType.SNACK_BAR, R.xml.voice_announces, R.layout.profile_preference_toolbar_with_switch),
VEHICLE_PARAMETERS(VehicleParametersFragment.class.getName(), true, MessageType.SNACK_BAR, R.xml.vehicle_parameters, R.layout.profile_preference_toolbar),
MAP_DURING_NAVIGATION(MapDuringNavigationFragment.class.getName(), true, MessageType.SNACK_BAR, R.xml.map_during_navigation, R.layout.profile_preference_toolbar),
TURN_SCREEN_ON(TurnScreenOnFragment.class.getName(), true, MessageType.SNACK_BAR, R.xml.turn_screen_on, R.layout.profile_preference_toolbar_with_switch),
DATA_STORAGE(DataStorageFragment.class.getName(), false, null, R.xml.data_storage, R.layout.global_preference_toolbar),
DIALOGS_AND_NOTIFICATIONS_SETTINGS(DialogsAndNotificationsSettingsFragment.class.getName(), false, null, R.xml.dialogs_and_notifications_preferences, R.layout.global_preferences_toolbar_with_switch),
PROFILE_APPEARANCE(ProfileAppearanceFragment.TAG, true, null, R.xml.profile_appearance, R.layout.profile_preference_toolbar),
OPEN_STREET_MAP_EDITING(OsmEditingFragment.class.getName(), false, null, R.xml.osm_editing, R.layout.global_preference_toolbar),
MULTIMEDIA_NOTES(MultimediaNotesFragment.class.getName(), true, MessageType.SNACK_BAR, R.xml.multimedia_notes, R.layout.profile_preference_toolbar),
MONITORING_SETTINGS(MonitoringSettingsFragment.class.getName(), true, MessageType.SNACK_BAR, R.xml.monitoring_settings, R.layout.profile_preference_toolbar),
LIVE_MONITORING(LiveMonitoringFragment.class.getName(), false, null, R.xml.live_monitoring, R.layout.global_preferences_toolbar_with_switch),
ACCESSIBILITY_SETTINGS(AccessibilitySettingsFragment.class.getName(), true, MessageType.SNACK_BAR, R.xml.accessibility_settings, R.layout.profile_preference_toolbar),
DEVELOPMENT_SETTINGS(DevelopmentSettingsFragment.class.getName(), false, null, R.xml.development_settings, R.layout.global_preference_toolbar);
public final String fragmentName;
public final boolean profileDependent;
public final MessageType onChangeMessageType;
public final int preferencesResId;
public final int toolbarResId;
SettingsScreenType(String fragmentName, boolean profileDependent, int preferencesResId, int toolbarResId) {
SettingsScreenType(String fragmentName, boolean profileDependent, MessageType onChangeMessageType, int preferencesResId, int toolbarResId) {
this.fragmentName = fragmentName;
this.profileDependent = profileDependent;
this.onChangeMessageType = onChangeMessageType;
this.preferencesResId = preferencesResId;
this.toolbarResId = toolbarResId;
}
}
private enum MessageType {
SNACK_BAR, BOTTOM_SHEET
}
@Override
public void onCreate(Bundle savedInstanceState) {
app = requireMyApplication();
@ -304,6 +318,23 @@ public abstract class BaseSettingsFragment extends PreferenceFragmentCompat impl
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
MessageType messageType = getMessageType();
if (messageType != null) {
String prefId = preference.getKey();
OsmandSettings.OsmandPreference pref = settings.getPreference(prefId);
if (pref instanceof CommonPreference && !((CommonPreference) pref).hasDefaultValueForMode(getSelectedAppMode())) {
FragmentManager fragmentManager = getFragmentManager();
if (newValue instanceof Serializable) {
if (messageType == MessageType.SNACK_BAR) {
applyChangeAndSuggestApplyToAllProfiles(prefId, (Serializable) newValue);
} else if (messageType == MessageType.BOTTOM_SHEET && fragmentManager != null) {
ChangeGeneralProfilesPrefBottomSheet.showInstance(fragmentManager, prefId,
(Serializable) newValue, this, false, getSelectedAppMode());
}
}
return false;
}
}
return true;
}
@ -316,6 +347,10 @@ public abstract class BaseSettingsFragment extends PreferenceFragmentCompat impl
return currentScreenType != null && currentScreenType.profileDependent;
}
public MessageType getMessageType() {
return currentScreenType != null ? currentScreenType.onChangeMessageType : null;
}
@Override
public void onDisplayPreferenceDialog(Preference preference) {
FragmentManager fragmentManager = getFragmentManager();
@ -859,4 +894,25 @@ public abstract class BaseSettingsFragment extends PreferenceFragmentCompat impl
mapActivity.getMapRouteInfoMenu().updateMenu();
}
}
protected void applyChangeAndSuggestApplyToAllProfiles(final String prefId, final Serializable newValue) {
app.getSettings().setPreference(prefId, newValue, getSelectedAppMode());
onSettingApplied(prefId, false);
updateSetting(prefId);
View containerView = getView();
if (containerView != null) {
String modeName = appMode.toHumanString();
String text = app.getString(R.string.changes_applied_to_profile, modeName);
SpannableString message = UiUtilities.createSpannableString(text, modeName, new StyleSpan(Typeface.BOLD));
Snackbar snackbar = Snackbar.make(containerView, message, Snackbar.LENGTH_LONG)
.setAction(R.string.apply_to_all_profiles, new View.OnClickListener() {
@Override
public void onClick(View view) {
app.getSettings().setPreferenceForAllModes(prefId, newValue);
onSettingApplied(prefId, true);
}
});
snackbar.show();
}
}
}

View file

@ -354,18 +354,11 @@ public class GeneralProfileSettingsFragment extends BaseSettingsFragment impleme
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
String prefId = preference.getKey();
OsmandSettings.OsmandPreference pref = settings.getPreference(prefId);
if (pref instanceof CommonPreference && !((CommonPreference) pref).hasDefaultValueForMode(getSelectedAppMode())) {
FragmentManager fragmentManager = getFragmentManager();
if (fragmentManager != null && newValue instanceof Serializable) {
ChangeGeneralProfilesPrefBottomSheet.showInstance(fragmentManager, prefId,
(Serializable) newValue, this, false, getSelectedAppMode());
}
if (prefId.equals(settings.ROTATE_MAP.getId()) && newValue instanceof Serializable) {
applyChangeAndSuggestApplyToAllProfiles(prefId, (Serializable) newValue);
return false;
}
return true;
return super.onPreferenceChange(preference, newValue);
}
@Override

View file

@ -21,6 +21,7 @@ import net.osmand.router.GeneralRouter;
import net.osmand.router.RoutingConfiguration;
import net.osmand.util.Algorithms;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

View file

@ -56,6 +56,7 @@ public class ScreenAlertsFragment extends BaseSettingsFragment {
ApplicationMode selectedMode = getSelectedAppMode();
boolean checked = !settings.SHOW_ROUTING_ALARMS.getModeValue(selectedMode);
settings.SHOW_ROUTING_ALARMS.setModeValue(selectedMode, checked);
applyChangeAndSuggestApplyToAllProfiles(settings.SHOW_ROUTING_ALARMS.getId(), checked);
updateToolbarSwitch();
enableDisablePreferences(checked);
}

View file

@ -44,6 +44,7 @@ public class TurnScreenOnFragment extends BaseSettingsFragment {
ApplicationMode selectedMode = getSelectedAppMode();
boolean checked = !settings.TURN_SCREEN_ON_ENABLED.getModeValue(selectedMode);
settings.TURN_SCREEN_ON_ENABLED.setModeValue(selectedMode, checked);
applyChangeAndSuggestApplyToAllProfiles(settings.TURN_SCREEN_ON_ENABLED.getId(), checked);
updateToolbarSwitch();
enableDisablePreferences(checked);
}

View file

@ -51,6 +51,7 @@ public class VoiceAnnouncesFragment extends BaseSettingsFragment {
ApplicationMode selectedMode = getSelectedAppMode();
boolean checked = !settings.VOICE_MUTE.getModeValue(selectedMode);
settings.VOICE_MUTE.setModeValue(selectedMode, checked);
applyChangeAndSuggestApplyToAllProfiles(settings.VOICE_MUTE.getId(), checked);
updateToolbarSwitch();
enableDisablePreferences(!checked);
updateMenu();