MenuItemConfigPreference implementation

This commit is contained in:
veliymolfar 2020-04-08 15:54:25 +03:00
parent ceb534c6d2
commit 3d8f5777e6
8 changed files with 194 additions and 240 deletions

View file

@ -552,25 +552,14 @@ public class ContextMenuAdapter {
}
}
public void initDefaultOrders(@NonNull List<ContextMenuItem> items) {
private void initDefaultOrders(@NonNull List<ContextMenuItem> items) {
for (int i = 0; i < items.size(); i++) {
items.get(i).setOrder(i);
}
}
public List<AdapterItem> getItemsForRearrangeAdapter(@NonNull ScreenType screenType, @Nullable List<String> hiddenItemsIds, @Nullable HashMap<String, Integer> itemsOrderIds, boolean hidden) {
String idScheme = "";
switch (screenType) {
case DRAWER:
idScheme = DRAWER_ITEM_ID_SCHEME;
break;
case CONFIGURE_MAP:
idScheme = CONFIGURE_MAP_ITEM_ID_SCHEME;
break;
case CONTEXT_MENU_ACTIONS:
idScheme = MAP_CONTEXT_MENU_ACTIONS;
break;
}
public List<AdapterItem> getItemsForRearrangeAdapter(@Nullable List<String> hiddenItemsIds, @Nullable HashMap<String, Integer> itemsOrderIds, boolean hidden) {
String idScheme = getIdScheme();
if (itemsOrderIds == null || itemsOrderIds.isEmpty()) {
initDefaultOrders(items);
} else {
@ -593,19 +582,8 @@ public class ContextMenuAdapter {
return hidden ? hiddenItems : visibleItems;
}
public List<ContextMenuItem> getDefaultItems(ScreenType screenType) {
String idScheme = "";
switch (screenType) {
case DRAWER:
idScheme = DRAWER_ITEM_ID_SCHEME;
break;
case CONFIGURE_MAP:
idScheme = CONFIGURE_MAP_ITEM_ID_SCHEME;
break;
case CONTEXT_MENU_ACTIONS:
idScheme = MAP_CONTEXT_MENU_ACTIONS;
break;
}
public List<ContextMenuItem> getDefaultItems() {
String idScheme = getIdScheme();
List<ContextMenuItem> items = new ArrayList<>();
for (ContextMenuItem item : this.items) {
String id = item.getId();
@ -616,6 +594,26 @@ public class ContextMenuAdapter {
return items;
}
private String getIdScheme() {
String idScheme = "";
for (ContextMenuItem item : items) {
String id = item.getId();
if (id != null) {
if (id.startsWith(DRAWER_ITEM_ID_SCHEME)) {
idScheme = DRAWER_ITEM_ID_SCHEME;
break;
} else if (id.startsWith(CONFIGURE_MAP_ITEM_ID_SCHEME)) {
idScheme = CONFIGURE_MAP_ITEM_ID_SCHEME;
break;
} else if (id.startsWith(MAP_CONTEXT_MENU_ACTIONS)) {
idScheme = MAP_CONTEXT_MENU_ACTIONS;
break;
}
}
}
return idScheme;
}
public void reorderMenuItems(@NonNull List<ContextMenuItem> defaultItems, @NonNull HashMap<String, Integer> itemsOrder) {
for (ContextMenuItem item : defaultItems) {
Integer order = itemsOrder.get(item.getId());
@ -641,100 +639,28 @@ public class ContextMenuAdapter {
return result;
}
public void resetMenuItems(@NonNull OsmandApplication app, @NonNull ScreenType screenType) {
saveHiddenItemsIds(app, screenType, null);
saveItemsIdsOrder(app, screenType, null);
}
@NonNull
public List<String> getHiddenItemsIds(@NonNull OsmandApplication app, @NonNull ScreenType type) {
List<String> hiddenItemsIds = null;
switch (type) {
case DRAWER:
hiddenItemsIds = app.getSettings().HIDDEN_DRAWER_ITEMS.getStringsList();
break;
case CONFIGURE_MAP:
hiddenItemsIds = app.getSettings().HIDDEN_CONFIGURE_MAP_ITEMS.getStringsList();
break;
case CONTEXT_MENU_ACTIONS:
hiddenItemsIds = app.getSettings().HIDDEN_CONTEXT_MENU_ACTIONS_ITEMS.getStringsList();
break;
public void initItemsCustomOrder(@NonNull OsmandApplication app) {
OsmandSettings.MenuItemConfigPreference preference = null;
for (ContextMenuItem item : items) {
String id = item.getId();
if (id != null) {
if (id.startsWith(DRAWER_ITEM_ID_SCHEME)) {
preference = app.getSettings().DRAWER_ITEMS;
break;
} else if (id.startsWith(CONFIGURE_MAP_ITEM_ID_SCHEME)) {
preference = app.getSettings().CONFIGURE_MAP_ITEMS;
break;
} else if (id.startsWith(MAP_CONTEXT_MENU_ACTIONS)) {
preference = app.getSettings().CONTEXT_MENU_ACTIONS_ITEMS;
break;
}
}
}
return hiddenItemsIds != null ? new ArrayList<>(hiddenItemsIds) : new ArrayList<String>();
}
@NonNull
public List<String> getItemsIdsOrder(@NonNull OsmandApplication app, @NonNull ScreenType type) {
List<String> hiddenItemsIds = null;
switch (type) {
case DRAWER:
hiddenItemsIds = app.getSettings().DRAWER_ITEMS_ORDER.getStringsList();
break;
case CONFIGURE_MAP:
hiddenItemsIds = app.getSettings().CONFIGURE_MAP_ITEMS_ORDER.getStringsList();
break;
case CONTEXT_MENU_ACTIONS:
hiddenItemsIds = app.getSettings().CONTEXT_MENU_ACTIONS_ITEMS_ORDER.getStringsList();
break;
if (preference == null) {
return;
}
return hiddenItemsIds != null ? new ArrayList<>(hiddenItemsIds) : new ArrayList<String>();
}
public void saveHiddenItemsIds(@NonNull OsmandApplication app, @NonNull ScreenType type, @Nullable List<String> hiddenItemsIds) {
switch (type) {
case DRAWER:
app.getSettings().HIDDEN_DRAWER_ITEMS.setStringsList(hiddenItemsIds);
break;
case CONFIGURE_MAP:
app.getSettings().HIDDEN_CONFIGURE_MAP_ITEMS.setStringsList(hiddenItemsIds);
break;
case CONTEXT_MENU_ACTIONS:
app.getSettings().HIDDEN_CONTEXT_MENU_ACTIONS_ITEMS.setStringsList(hiddenItemsIds);
break;
}
}
public void saveItemsIdsOrder(@NonNull OsmandApplication app, @NonNull ScreenType type, @Nullable List<String> itemsIdsOrder) {
switch (type) {
case DRAWER:
app.getSettings().DRAWER_ITEMS_ORDER.setStringsList(itemsIdsOrder);
break;
case CONFIGURE_MAP:
app.getSettings().CONFIGURE_MAP_ITEMS_ORDER.setStringsList(itemsIdsOrder);
break;
case CONTEXT_MENU_ACTIONS:
app.getSettings().CONTEXT_MENU_ACTIONS_ITEMS_ORDER.setStringsList(itemsIdsOrder);
break;
}
}
public String getPrefIdOrder(@NonNull OsmandApplication app, @NonNull ScreenType type) {
switch (type) {
case DRAWER:
return app.getSettings().DRAWER_ITEMS_ORDER.getId();
case CONFIGURE_MAP:
return app.getSettings().CONFIGURE_MAP_ITEMS_ORDER.getId();
case CONTEXT_MENU_ACTIONS:
return app.getSettings().CONTEXT_MENU_ACTIONS_ITEMS_ORDER.get();
}
return "";
}
public String getPrefIdHidden(@NonNull OsmandApplication app, @NonNull ScreenType type) {
switch (type) {
case DRAWER:
return app.getSettings().HIDDEN_DRAWER_ITEMS.getId();
case CONFIGURE_MAP:
return app.getSettings().HIDDEN_CONFIGURE_MAP_ITEMS.getId();
case CONTEXT_MENU_ACTIONS:
return app.getSettings().HIDDEN_CONTEXT_MENU_ACTIONS_ITEMS.get();
}
return "";
}
public void initItemsCustomOrder(@NonNull OsmandApplication app, @NonNull ScreenType type) {
List<String> savedOrder = getItemsIdsOrder(app, type);
List<String> hiddenItems = getHiddenItemsIds(app, type);
List<String> savedOrder = preference.getOrderIds();
List<String> hiddenItems = preference.getHiddenIds();
if (!savedOrder.isEmpty()) {
reorderMenuItems(items, getMenuItemsOrder(savedOrder));

View file

@ -25,6 +25,7 @@ import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import net.osmand.IndexConstants;
import net.osmand.PlatformUtil;
import net.osmand.StateChangedListener;
import net.osmand.ValueHolder;
import net.osmand.aidl.OsmandAidlApi;
@ -56,6 +57,8 @@ import net.osmand.plus.voice.CommandPlayer;
import net.osmand.render.RenderingRulesStorage;
import net.osmand.util.Algorithms;
import org.apache.commons.logging.Log;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
@ -79,6 +82,8 @@ import java.util.StringTokenizer;
public class OsmandSettings {
private static final Log LOG = PlatformUtil.getLog(OsmandSettings.class.getName());
public static final int VERSION = 1;
public interface OsmandPreference<T> {
@ -412,11 +417,6 @@ public class OsmandSettings {
((BooleanPreference) preference).setModeValue(mode, (Boolean) value);
return true;
}
} else if (preference instanceof ListStringPreference) {
if (value instanceof List) {
((ListStringPreference) preference).setModeValues(mode, (List<String>) value);
return true;
}
} else if (preference instanceof StringPreference) {
if (value instanceof String) {
((StringPreference) preference).setModeValue(mode, (String) value);
@ -1103,6 +1103,75 @@ public class OsmandSettings {
}
}
public class MenuItemConfigPreference extends StringPreference {
private static final String HIDDEN = "hidden";
private static final String ORDER = "order";
private MenuItemConfigPreference(String id, String defaultValue) {
super(id, defaultValue);
}
private void addIdsToJsonArray(@NonNull JSONArray jsonArray, List<String> ids) {
if (ids != null && !ids.isEmpty()) {
for (String id : ids) {
jsonArray.put(id);
}
}
}
private List<String> getIdValues(String itemName) {
List<String> ids = new ArrayList<>();
String itemsString = get();
if (itemsString == null) {
return ids;
}
try {
JSONObject json = new JSONObject(itemsString);
JSONObject items = json.optJSONObject(getId());
JSONArray idsArray = items.optJSONArray(itemName);
if (idsArray != null) {
for (int i = 0; i < idsArray.length(); i++) {
String id = idsArray.optString(i);
ids.add(id);
}
}
} catch (JSONException e) {
LOG.error("Error converting to json string: " + e);
}
return ids;
}
public String convertToJsonString(List<String> hidden, List<String> order) {
try {
JSONObject json = new JSONObject();
JSONObject items = new JSONObject();
JSONArray hiddenItems = new JSONArray();
JSONArray orderItems = new JSONArray();
addIdsToJsonArray(hiddenItems, hidden);
addIdsToJsonArray(orderItems, order);
items.put(HIDDEN, hiddenItems);
items.put(ORDER, orderItems);
json.put(getId(), items);
return json.toString();
} catch (JSONException e) {
LOG.error("Error converting to json string: " + e);
}
return "";
}
public boolean setIdValues(List<String> hidden, List<String> order) {
return set(convertToJsonString(hidden, order));
}
public List<String> getHiddenIds() {
return getIdValues(HIDDEN);
}
public List<String> getOrderIds() {
return getIdValues(ORDER);
}
}
public class EnumIntPreference<E extends Enum<E>> extends CommonPreference<E> {
private final E[] values;
@ -3410,30 +3479,21 @@ public class OsmandSettings {
SELECTED_POI_FILTER_FOR_MAP.set(android.text.TextUtils.join(",", poiFilters));
}
public final ListStringPreference HIDDEN_CONTEXT_MENU_ACTIONS_ITEMS = (ListStringPreference)
new ListStringPreference("hidden_context_menu_actions_items", null, ",,").makeProfile().cache();
public final ListStringPreference CONTEXT_MENU_ACTIONS_ITEMS_ORDER = (ListStringPreference)
new ListStringPreference("context_menu_actions_items_order", null, ",,").makeProfile().cache();
public final ListStringPreference HIDDEN_CONFIGURE_MAP_ITEMS = (ListStringPreference)
new ListStringPreference("hidden_configure_map_items", null, ",,").makeProfile().cache();
public final ListStringPreference CONFIGURE_MAP_ITEMS_ORDER = (ListStringPreference)
new ListStringPreference("configure_map_items_order", null, ",,").makeProfile().cache();
public final ListStringPreference DRAWER_ITEMS_ORDER = (ListStringPreference)
new ListStringPreference("drawer_items_order", null, ",,").makeProfile().cache();
public final ListStringPreference HIDDEN_DRAWER_ITEMS = (ListStringPreference)
new ListStringPreference("hidden_drawer_items", null, ",,").makeProfile().cache();
public final ListStringPreference POI_FILTERS_ORDER = (ListStringPreference)
new ListStringPreference("poi_filters_order", null, ",,").makeProfile().cache();
public final ListStringPreference INACTIVE_POI_FILTERS = (ListStringPreference)
new ListStringPreference("inactive_poi_filters", null, ",,").makeProfile().cache();
public final MenuItemConfigPreference DRAWER_ITEMS =
(MenuItemConfigPreference) new MenuItemConfigPreference("drawer_items", null).makeProfile().cache();
public final MenuItemConfigPreference CONFIGURE_MAP_ITEMS =
(MenuItemConfigPreference) new MenuItemConfigPreference("configure_map_items", null).makeProfile().cache();
public final MenuItemConfigPreference CONTEXT_MENU_ACTIONS_ITEMS =
(MenuItemConfigPreference) new MenuItemConfigPreference("context_menu_actions_items", null).makeProfile().cache();
public static final String VOICE_PROVIDER_NOT_USE = "VOICE_PROVIDER_NOT_USE";
public static final String[] TTS_AVAILABLE_VOICES = new String[]{

View file

@ -1194,7 +1194,7 @@ public class MapActivityActions implements DialogProvider {
}
menuItemsListView.setDivider(null);
final ContextMenuAdapter contextMenuAdapter = createMainOptionsMenu();
contextMenuAdapter.initItemsCustomOrder(getMyApplication(), ScreenType.DRAWER);
contextMenuAdapter.initItemsCustomOrder(getMyApplication());
contextMenuAdapter.setDefaultLayoutId(R.layout.simple_list_menu_item);
final ArrayAdapter<ContextMenuItem> simpleListAdapter = contextMenuAdapter.createListAdapter(mapActivity,
!nightMode);

View file

@ -698,7 +698,7 @@ public class DashboardOnMap implements ObservableScrollViewCallbacks, IRouteInfo
cm = mapActivity.getMapLayers().getMapWidgetRegistry().getViewConfigureMenuAdapter(mapActivity);
} else if (visibleType == DashboardType.CONFIGURE_MAP) {
cm = new ConfigureMapMenu().createListAdapter(mapActivity);
cm.initItemsCustomOrder(getMyApplication(), ScreenType.CONFIGURE_MAP);
cm.initItemsCustomOrder(getMyApplication());
} else if (visibleType == DashboardType.LIST_MENU) {
cm = mapActivity.getMapActions().createMainOptionsMenu();
} else if (visibleType == DashboardType.ROUTE_PREFERENCES) {

View file

@ -122,8 +122,8 @@ public class ConfigureMenuItemsFragment extends BaseOsmAndFragment
hiddenMenuItems = savedInstanceState.getStringArrayList(HIDDEN_ITEMS_KEY);
menuItemsOrder = (HashMap<String, Integer>) savedInstanceState.getSerializable(ITEMS_ORDER_KEY);
} else {
hiddenMenuItems = contextMenuAdapter.getHiddenItemsIds(app, screenType);
menuItemsOrder = contextMenuAdapter.getMenuItemsOrder(contextMenuAdapter.getItemsIdsOrder(app, screenType));
hiddenMenuItems = getSettingForScreen(app, screenType).getHiddenIds();
menuItemsOrder = contextMenuAdapter.getMenuItemsOrder(getSettingForScreen(app, screenType).getOrderIds());
}
}
@ -218,34 +218,32 @@ public class ConfigureMenuItemsFragment extends BaseOsmAndFragment
applyButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
HashMap<String, Serializable> prefsMap = new HashMap<>();
prefsMap.put(contextMenuAdapter.getPrefIdHidden(app, screenType), (Serializable) hiddenMenuItems);
List<ContextMenuItem> defItems = contextMenuAdapter.getDefaultItems(screenType);
contextMenuAdapter.reorderMenuItems(defItems, menuItemsOrder);
List<String> ids = new ArrayList<>();
for (ContextMenuItem item : defItems) {
ids.add(item.getId());
}
prefsMap.put(contextMenuAdapter.getPrefIdOrder(app, screenType), (Serializable) ids);
FragmentManager fm = getFragmentManager();
if (fm != null) {
ChangeGeneralProfilesPrefBottomSheet.showInstance(
fm,
prefsMap,
getTargetFragment(),
false,
appMode,
new ChangeGeneralProfilesPrefBottomSheet.OnChangeSettingListener() {
@Override
public void onApplied() {
dismissFragment();
}
List<ContextMenuItem> defItems = contextMenuAdapter.getDefaultItems();
contextMenuAdapter.reorderMenuItems(defItems, menuItemsOrder);
List<String> ids = new ArrayList<>();
for (ContextMenuItem item : defItems) {
ids.add(item.getId());
}
FragmentManager fm = getFragmentManager();
String stringToSave = getSettingForScreen(app, screenType).convertToJsonString(hiddenMenuItems, ids);
if (fm != null) {
ChangeGeneralProfilesPrefBottomSheet.showInstance(fm,
getSettingForScreen(app, screenType).getId(),
stringToSave,
getTargetFragment(),
false,
appMode,
new ChangeGeneralProfilesPrefBottomSheet.OnChangeSettingListener() {
@Override
public void onApplied() {
dismissFragment();
}
@Override
public void onDiscard() {
@Override
public void onDiscard() {
}
});
}
});
}
}
});
@ -259,8 +257,8 @@ public class ConfigureMenuItemsFragment extends BaseOsmAndFragment
List<AdapterItem> items = new ArrayList<>();
items.add(new AdapterItem(DESCRIPTION, screenType));
List<AdapterItem> visible = contextMenuAdapter.getItemsForRearrangeAdapter(screenType, hiddenMenuItems, wasReset ? null : menuItemsOrder, false);
List<AdapterItem> hiddenItems = contextMenuAdapter.getItemsForRearrangeAdapter(screenType, hiddenMenuItems, wasReset ? null : menuItemsOrder, true);
List<AdapterItem> visible = contextMenuAdapter.getItemsForRearrangeAdapter(hiddenMenuItems, wasReset ? null : menuItemsOrder, false);
List<AdapterItem> hiddenItems = contextMenuAdapter.getItemsForRearrangeAdapter(hiddenMenuItems, wasReset ? null : menuItemsOrder, true);
if (screenType == ScreenType.CONTEXT_MENU_ACTIONS) {
List<AdapterItem> main = new ArrayList<>();
int actionsIndex = 3;
@ -360,22 +358,22 @@ public class ConfigureMenuItemsFragment extends BaseOsmAndFragment
public void copyAppModePrefs(ApplicationMode appMode) {
if (appMode != null) {
List<OsmandSettings.OsmandPreference> prefs = new ArrayList<>();
switch (screenType) {
case DRAWER:
prefs.add(app.getSettings().DRAWER_ITEMS_ORDER);
prefs.add(app.getSettings().HIDDEN_DRAWER_ITEMS);
break;
case CONFIGURE_MAP:
prefs.add(app.getSettings().CONFIGURE_MAP_ITEMS_ORDER);
prefs.add(app.getSettings().HIDDEN_CONFIGURE_MAP_ITEMS);
break;
case CONTEXT_MENU_ACTIONS:
prefs.add(app.getSettings().CONTEXT_MENU_ACTIONS_ITEMS_ORDER);
prefs.add(app.getSettings().HIDDEN_CONTEXT_MENU_ACTIONS_ITEMS);
break;
}
prefs.add(getSettingForScreen(app, screenType));
app.getSettings().copyProfilePreferences(appMode, this.appMode, prefs);
dismissFragment();
}
}
public static OsmandSettings.MenuItemConfigPreference getSettingForScreen(OsmandApplication app, ScreenType screenType) {
switch (screenType) {
case DRAWER:
return app.getSettings().DRAWER_ITEMS;
case CONFIGURE_MAP:
return app.getSettings().CONFIGURE_MAP_ITEMS;
case CONTEXT_MENU_ACTIONS:
return app.getSettings().CONTEXT_MENU_ACTIONS_ITEMS;
default:
throw new IllegalArgumentException("Unsupported screen type");
}
}
}

View file

@ -37,6 +37,7 @@ import net.osmand.plus.activities.PluginsActivity;
import net.osmand.plus.base.BaseOsmAndFragment;
import net.osmand.plus.dialogs.ConfigureMapMenu;
import net.osmand.plus.helpers.FontCache;
import net.osmand.plus.mapcontextmenu.MapContextMenu;
import net.osmand.plus.widgets.style.CustomTypefaceSpan;
@ -222,12 +223,12 @@ public class ConfigureMenuRootFragment extends BaseOsmAndFragment {
contextMenuAdapter = configureMapMenu.createListAdapter((MapActivity) activity);
break;
case CONTEXT_MENU_ACTIONS:
ConfigureMapMenu configureaMapMenu = new ConfigureMapMenu();
contextMenuAdapter = configureaMapMenu.createListAdapter((MapActivity) activity);
MapContextMenu menu = ((MapActivity) activity).getContextMenu();
contextMenuAdapter = menu.getAdapter();
break;
}
int hiddenCount = contextMenuAdapter.getHiddenItemsIds(app, type).size();
int allCount = contextMenuAdapter.getDefaultItems(type).size();
int hiddenCount = ConfigureMenuItemsFragment.getSettingForScreen(app, type).getHiddenIds().size();
int allCount = contextMenuAdapter.getDefaultItems().size();
String amount = getString(R.string.n_items_of_z, String.valueOf(allCount - hiddenCount), String.valueOf(allCount));
return getString(R.string.ltr_or_rtl_combine_via_colon, getString(R.string.shared_string_items), amount);
}

View file

@ -46,6 +46,7 @@ public class RearrangeMenuItemsAdapter extends RecyclerView.Adapter<RecyclerView
private List<AdapterItem> items;
private MenuItemsAdapterListener listener;
private int activeColorRes;
private int textColorRes;
public RearrangeMenuItemsAdapter(OsmandApplication app,
@ -57,6 +58,9 @@ public class RearrangeMenuItemsAdapter extends RecyclerView.Adapter<RecyclerView
activeColorRes = nightMode
? R.color.active_color_primary_dark
: R.color.active_color_primary_light;
textColorRes = nightMode
? R.color.text_color_primary_dark
: R.color.text_color_primary_light;
}
@Override
@ -146,6 +150,7 @@ public class RearrangeMenuItemsAdapter extends RecyclerView.Adapter<RecyclerView
h.actionIcon.setVisibility(View.VISIBLE);
}
h.title.setText(menuItem.getTitle());
h.title.setTextColor(app.getResources().getColor(textColorRes));
h.description.setText(String.valueOf(menuItem.getOrder()));
h.divider.setVisibility(View.GONE);
h.moveIcon.setVisibility(View.VISIBLE);

View file

@ -33,17 +33,9 @@ public class ChangeGeneralProfilesPrefBottomSheet extends BasePreferenceBottomSh
private static final String NEW_VALUE_KEY = "new_value_key";
private static final String PREFS_MAP_KEY = "prefs_map_key";
@Nullable
private Serializable newValue;
@Nullable
private String prefId;
@Nullable
private HashMap<String, Serializable> prefsMap;
@Nullable
private OnChangeSettingListener listener;
@ -58,12 +50,11 @@ public class ChangeGeneralProfilesPrefBottomSheet extends BasePreferenceBottomSh
if (app == null || args == null) {
return;
}
prefId = args.getString(PREFERENCE_ID);
final String prefId = args.getString(PREFERENCE_ID);
newValue = args.getSerializable(NEW_VALUE_KEY);
prefsMap = (HashMap<String, Serializable>) args.getSerializable(PREFS_MAP_KEY);
// if (newValue == null || prefId == null) {
// return;
// }
if (newValue == null || prefId == null) {
return;
}
items.add(new TitleItem(getString(R.string.change_default_settings)));
items.add(new LongDescriptionItem(getString(R.string.apply_preference_to_all_profiles)));
@ -75,11 +66,7 @@ public class ChangeGeneralProfilesPrefBottomSheet extends BasePreferenceBottomSh
.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (prefsMap != null) {
savePrefs(app, prefsMap, true);
} else if (newValue != null || prefId != null) {
app.getSettings().setPreferenceForAllModes(prefId, newValue);
}
app.getSettings().setPreferenceForAllModes(prefId, newValue);
updateTargetSettings(false, true);
if (listener != null) {
listener.onApplied();
@ -99,11 +86,7 @@ public class ChangeGeneralProfilesPrefBottomSheet extends BasePreferenceBottomSh
.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (prefsMap != null) {
savePrefs(app, prefsMap, false);
} else if (newValue != null || prefId != null) {
app.getSettings().setPreference(prefId, newValue, getAppMode());
}
app.getSettings().setPreference(prefId, newValue, getAppMode());
updateTargetSettings(false, false);
if (listener != null) {
listener.onApplied();
@ -143,18 +126,6 @@ public class ChangeGeneralProfilesPrefBottomSheet extends BasePreferenceBottomSh
outState.putSerializable(NEW_VALUE_KEY, newValue);
}
private void savePrefs(@NonNull OsmandApplication app,
@NonNull HashMap<String, Serializable> prefsMap, boolean toAllProfiles) {
List<String> ids = new ArrayList<>(prefsMap.keySet());
for (String id : ids) {
if (toAllProfiles) {
app.getSettings().setPreferenceForAllModes(id, prefsMap.get(id));
} else {
app.getSettings().setPreference(id, prefsMap.get(id), getAppMode());
}
}
}
private void updateTargetSettings(boolean discard, boolean appliedToAllProfiles) {
BaseSettingsFragment target = (BaseSettingsFragment) getTargetFragment();
if (target != null) {
@ -184,22 +155,22 @@ public class ChangeGeneralProfilesPrefBottomSheet extends BasePreferenceBottomSh
Fragment target,
boolean usedOnMap,
@Nullable ApplicationMode appMode) {
showFragmentInstance(fm, prefId, newValue, null, target, usedOnMap, appMode, null);
showFragmentInstance(fm, prefId, newValue, target, usedOnMap, appMode, null);
}
public static void showInstance(@NonNull FragmentManager fm,
@Nullable HashMap<String, Serializable> prefs,
@Nullable String prefId,
@Nullable Serializable newValue,
Fragment target,
boolean usedOnMap,
@Nullable ApplicationMode appMode,
@Nullable OnChangeSettingListener listener) {
showFragmentInstance(fm, null, null, prefs, target, usedOnMap, appMode, listener);
showFragmentInstance(fm, prefId, newValue, target, usedOnMap, appMode, listener);
}
private static void showFragmentInstance(@NonNull FragmentManager fm,
@Nullable String prefId,
@Nullable Serializable newValue,
@Nullable HashMap<String, Serializable> prefs,
Fragment target,
boolean usedOnMap,
@Nullable ApplicationMode appMode,
@ -207,15 +178,8 @@ public class ChangeGeneralProfilesPrefBottomSheet extends BasePreferenceBottomSh
try {
if (fm.findFragmentByTag(ChangeGeneralProfilesPrefBottomSheet.TAG) == null) {
Bundle args = new Bundle();
if (prefId != null) {
args.putString(PREFERENCE_ID, prefId);
}
if (newValue != null) {
args.putSerializable(NEW_VALUE_KEY, newValue);
}
if (prefs != null) {
args.putSerializable(PREFS_MAP_KEY, prefs);
}
args.putString(PREFERENCE_ID, prefId);
args.putSerializable(NEW_VALUE_KEY, newValue);
ChangeGeneralProfilesPrefBottomSheet fragment = new ChangeGeneralProfilesPrefBottomSheet();
fragment.setArguments(args);