refactor ContextMenuItemsPreference, corrections

This commit is contained in:
veliymolfar 2020-05-05 11:38:27 +03:00
parent d80f4c039c
commit 23a7b701a7
9 changed files with 128 additions and 115 deletions

View file

@ -27,6 +27,7 @@ import net.osmand.util.Algorithms;
import java.util.Map; import java.util.Map;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import static net.osmand.aidlapi.OsmAndCustomizationConstants.CONFIGURE_MAP_ITEM_ID_SCHEME;
public class ConnectedApp implements Comparable<ConnectedApp> { public class ConnectedApp implements Comparable<ConnectedApp> {
@ -156,7 +157,7 @@ public class ConnectedApp implements Comparable<ConnectedApp> {
}; };
boolean layersEnabled = layersPref.get(); boolean layersEnabled = layersPref.get();
menuAdapter.addItem(new ContextMenuItem.ItemBuilder() menuAdapter.addItem(new ContextMenuItem.ItemBuilder()
.setId(AIDL_LAYERS_PREFIX + pack) .setId(CONFIGURE_MAP_ITEM_ID_SCHEME + AIDL_LAYERS_PREFIX + pack)
.setTitle(name) .setTitle(name)
.setListener(listener) .setListener(listener)
.setSelected(layersEnabled) .setSelected(layersEnabled)

View file

@ -49,7 +49,6 @@ import java.util.LinkedHashSet;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
import static net.osmand.aidl.ConnectedApp.AIDL_LAYERS_PREFIX;
import static net.osmand.aidlapi.OsmAndCustomizationConstants.APP_PROFILES_ID; import static net.osmand.aidlapi.OsmAndCustomizationConstants.APP_PROFILES_ID;
public class ContextMenuAdapter { public class ContextMenuAdapter {
@ -59,6 +58,7 @@ public class ContextMenuAdapter {
public static final int PROFILES_NORMAL_PROFILE_TAG = 0; public static final int PROFILES_NORMAL_PROFILE_TAG = 0;
public static final int PROFILES_CHOSEN_PROFILE_TAG = 1; public static final int PROFILES_CHOSEN_PROFILE_TAG = 1;
public static final int PROFILES_CONTROL_BUTTON_TAG = 2; public static final int PROFILES_CONTROL_BUTTON_TAG = 2;
private static final int ITEMS_ORDER_STEP = 10;
@LayoutRes @LayoutRes
private int DEFAULT_LAYOUT_ID = R.layout.list_menu_item_native; private int DEFAULT_LAYOUT_ID = R.layout.list_menu_item_native;
@ -85,14 +85,15 @@ public class ContextMenuAdapter {
} }
public void addItem(ContextMenuItem item) { public void addItem(ContextMenuItem item) {
try { String id = item.getId();
String id = item.getId(); if (id != null) {
if (id != null) { item.setHidden(isItemHidden(id));
item.setHidden(isItemHidden(id)); item.setOrder(getItemOrder(id, item.getOrder()));
item.setOrder(getItemOrder(id, item.getOrder())); }
} int pos = item.getPos();
if (pos >= 0 && pos < items.size()) {
items.add(item.getPos(), item); items.add(item.getPos(), item);
} catch (IndexOutOfBoundsException ex) { } else {
items.add(item); items.add(item);
} }
sortItemsByOrder(); sortItemsByOrder();
@ -132,7 +133,7 @@ public class ContextMenuAdapter {
this.changeAppModeListener = changeAppModeListener; this.changeAppModeListener = changeAppModeListener;
} }
public void sortItemsByOrder() { private void sortItemsByOrder() {
Collections.sort(items, new Comparator<ContextMenuItem>() { Collections.sort(items, new Comparator<ContextMenuItem>() {
@Override @Override
public int compare(ContextMenuItem item1, ContextMenuItem item2) { public int compare(ContextMenuItem item1, ContextMenuItem item2) {
@ -162,18 +163,25 @@ public class ContextMenuAdapter {
private int getItemOrder(@NonNull String id, int defaultOrder) { private int getItemOrder(@NonNull String id, int defaultOrder) {
ContextMenuItemsPreference contextMenuItemsPreference = app.getSettings().getContextMenuItemsPreference(id); ContextMenuItemsPreference contextMenuItemsPreference = app.getSettings().getContextMenuItemsPreference(id);
if (contextMenuItemsPreference == null) { if (contextMenuItemsPreference != null) {
return defaultOrder; List<String> orderIds = contextMenuItemsPreference.get().getOrderIds();
} if (!Algorithms.isEmpty(orderIds)) {
List<String> orderIds = contextMenuItemsPreference.get().getOrderIds(); int index = orderIds.indexOf(id);
if (!Algorithms.isEmpty(orderIds)) { if (index != -1) {
int order = orderIds.indexOf(id); return index + ITEMS_ORDER_STEP;
if (order != -1) { }
return order; }
} }
} return getDefaultOrder(defaultOrder);
return defaultOrder == -1 ? items.size() - 1 : defaultOrder; }
}
private int getDefaultOrder(int defaultOrder) {
if (defaultOrder == 0 && !items.isEmpty()) {
return items.get(items.size() - 1).getOrder() + ITEMS_ORDER_STEP;
} else {
return defaultOrder;
}
}
public ArrayAdapter<ContextMenuItem> createListAdapter(final Activity activity, final boolean lightTheme) { public ArrayAdapter<ContextMenuItem> createListAdapter(final Activity activity, final boolean lightTheme) {
final int layoutId = DEFAULT_LAYOUT_ID; final int layoutId = DEFAULT_LAYOUT_ID;
@ -580,7 +588,7 @@ public class ContextMenuAdapter {
List<ContextMenuItem> items = new ArrayList<>(); List<ContextMenuItem> items = new ArrayList<>();
for (ContextMenuItem item : this.items) { for (ContextMenuItem item : this.items) {
String id = item.getId(); String id = item.getId();
if (id != null && (id.startsWith(idScheme) && !APP_PROFILES_ID.equals(id) || id.startsWith(AIDL_LAYERS_PREFIX))) { if (id != null && (id.startsWith(idScheme) && !APP_PROFILES_ID.equals(id))) {
items.add(item); items.add(item);
} }
} }
@ -600,4 +608,14 @@ public class ContextMenuAdapter {
} }
return idScheme; return idScheme;
} }
public List<ContextMenuItem> getVisibleItems() {
List<ContextMenuItem> visible = new ArrayList<>();
for (ContextMenuItem item : items) {
if (!item.isHidden()) {
visible.add(item);
}
}
return visible;
}
} }

View file

@ -268,7 +268,7 @@ public class ContextMenuItem {
private boolean mIsCategory = false; private boolean mIsCategory = false;
private boolean mIsClickable = true; private boolean mIsClickable = true;
private int mPosition = -1; private int mPosition = -1;
private int mOrder = -1; private int mOrder = 0;
private String mDescription = null; private String mDescription = null;
private ContextMenuAdapter.ItemClickListener mItemClickListener = null; private ContextMenuAdapter.ItemClickListener mItemClickListener = null;
private ContextMenuAdapter.OnIntegerValueChangedListener mIntegerListener = null; private ContextMenuAdapter.OnIntegerValueChangedListener mIntegerListener = null;

View file

@ -1141,8 +1141,8 @@ public class OsmandSettings {
@NonNull @NonNull
private String idScheme; private String idScheme;
private ContextMenuItemsPreference(String id, @NonNull String idScheme) { private ContextMenuItemsPreference(String id, @NonNull String idScheme, @NonNull ContextMenuItemsSettings defValue) {
super(id, new ContextMenuItemsSettings()); super(id, defValue);
this.idScheme = idScheme; this.idScheme = idScheme;
} }
@ -1163,7 +1163,7 @@ public class OsmandSettings {
} }
private ContextMenuItemsSettings readValue(String s) { private ContextMenuItemsSettings readValue(String s) {
ContextMenuItemsSettings value = new ContextMenuItemsSettings(); ContextMenuItemsSettings value = getDefaultValue().newInstance();
value.readFromJsonString(s, idScheme); value.readFromJsonString(s, idScheme);
return value; return value;
} }
@ -1175,12 +1175,10 @@ public class OsmandSettings {
} }
public static class ContextMenuItemsSettings implements Serializable { public static class ContextMenuItemsSettings implements Serializable {
public static final String HIDDEN = "hidden"; private static final String HIDDEN = "hidden";
public static final String ORDER = "order"; private static final String ORDER = "order";
public static final String MAIN_ACTIONS = "main_actions";
private List<String> hiddenIds = new ArrayList<>(); private List<String> hiddenIds = new ArrayList<>();
private List<String> orderIds = new ArrayList<>(); private List<String> orderIds = new ArrayList<>();
private List<String> mainActionIds = new ArrayList<>();
public ContextMenuItemsSettings() { public ContextMenuItemsSettings() {
@ -1191,10 +1189,8 @@ public class OsmandSettings {
this.orderIds = orderIds; this.orderIds = orderIds;
} }
public ContextMenuItemsSettings(@NonNull List<String> mainActionIds, @NonNull List<String> hiddenIds, @NonNull List<String> orderIds) { public ContextMenuItemsSettings newInstance() {
this.mainActionIds = mainActionIds; return new ContextMenuItemsSettings();
this.hiddenIds = hiddenIds;
this.orderIds = orderIds;
} }
public void readFromJsonString(String jsonString, @NonNull String idScheme) { public void readFromJsonString(String jsonString, @NonNull String idScheme) {
@ -1203,26 +1199,23 @@ public class OsmandSettings {
} }
try { try {
JSONObject json = new JSONObject(jsonString); JSONObject json = new JSONObject(jsonString);
hiddenIds = readIdsList(json.optJSONArray(HIDDEN), idScheme); readFromJson(json, idScheme);
orderIds = readIdsList(json.optJSONArray(ORDER), idScheme);
if (idScheme.equals(MAP_CONTEXT_MENU_ACTIONS)) {
mainActionIds = readIdsList(json.optJSONArray(MAIN_ACTIONS), idScheme);
}
} catch (JSONException e) { } catch (JSONException e) {
LOG.error("Error converting to json string: " + e); LOG.error("Error converting to json string: " + e);
} }
} }
private List<String> readIdsList(JSONArray jsonArray, @NonNull String idScheme) { public void readFromJson(JSONObject json, String idScheme) {
hiddenIds = readIdsList(json.optJSONArray(HIDDEN), idScheme);
orderIds = readIdsList(json.optJSONArray(ORDER), idScheme);
}
protected List<String> readIdsList(JSONArray jsonArray, @NonNull String idScheme) {
List<String> list = new ArrayList<>(); List<String> list = new ArrayList<>();
if (jsonArray != null) { if (jsonArray != null) {
for (int i = 0; i < jsonArray.length(); i++) { for (int i = 0; i < jsonArray.length(); i++) {
String id = jsonArray.optString(i); String id = jsonArray.optString(i);
if (id.startsWith(AIDL_LAYERS_PREFIX)) { list.add(idScheme + id);
list.add(id);
} else {
list.add(idScheme + id);
}
} }
} }
return list; return list;
@ -1231,11 +1224,7 @@ public class OsmandSettings {
public String writeToJsonString(@NonNull String idScheme) { public String writeToJsonString(@NonNull String idScheme) {
try { try {
JSONObject json = new JSONObject(); JSONObject json = new JSONObject();
json.put(HIDDEN, getJsonArray(hiddenIds, idScheme)); writeToJson(json, idScheme);
json.put(ORDER, getJsonArray(orderIds, idScheme));
if (idScheme.equals(MAP_CONTEXT_MENU_ACTIONS)) {
json.put(MAIN_ACTIONS, getJsonArray(mainActionIds, idScheme));
}
return json.toString(); return json.toString();
} catch (JSONException e) { } catch (JSONException e) {
LOG.error("Error converting to json string: " + e); LOG.error("Error converting to json string: " + e);
@ -1243,7 +1232,12 @@ public class OsmandSettings {
return ""; return "";
} }
private JSONArray getJsonArray(List<String> ids, @NonNull String idScheme) { public void writeToJson(JSONObject json, String idScheme) throws JSONException {
json.put(HIDDEN, getJsonArray(hiddenIds, idScheme));
json.put(ORDER, getJsonArray(orderIds, idScheme));
}
protected JSONArray getJsonArray(List<String> ids, @NonNull String idScheme) {
JSONArray jsonArray = new JSONArray(); JSONArray jsonArray = new JSONArray();
if (ids != null && !ids.isEmpty()) { if (ids != null && !ids.isEmpty()) {
for (String id : ids) { for (String id : ids) {
@ -1260,6 +1254,37 @@ public class OsmandSettings {
public List<String> getOrderIds() { public List<String> getOrderIds() {
return Collections.unmodifiableList(orderIds); return Collections.unmodifiableList(orderIds);
} }
}
public static class MapContextMenuItemsSettings extends ContextMenuItemsSettings {
private static final String MAIN_ACTIONS = "main_actions";
private List<String> mainActionIds = new ArrayList<>();
public MapContextMenuItemsSettings() {
}
public MapContextMenuItemsSettings(@NonNull List<String> mainActionIds, @NonNull List<String> hiddenIds, @NonNull List<String> orderIds) {
super(hiddenIds, orderIds);
this.mainActionIds = mainActionIds;
}
@Override
public ContextMenuItemsSettings newInstance() {
return new MapContextMenuItemsSettings();
}
@Override
public void readFromJson(JSONObject json, String idScheme) {
super.readFromJson(json, idScheme);
mainActionIds = readIdsList(json.optJSONArray(MAIN_ACTIONS), idScheme);
}
@Override
public void writeToJson(JSONObject json, String idScheme) throws JSONException {
super.writeToJson(json, idScheme);
json.put(MAIN_ACTIONS, getJsonArray(mainActionIds, idScheme));
}
public List<String> getMainActionIds() { public List<String> getMainActionIds() {
return Collections.unmodifiableList(mainActionIds); return Collections.unmodifiableList(mainActionIds);
@ -3592,21 +3617,21 @@ public class OsmandSettings {
new ListStringPreference("inactive_poi_filters", null, ",,").makeProfile().cache(); new ListStringPreference("inactive_poi_filters", null, ",,").makeProfile().cache();
public final ContextMenuItemsPreference DRAWER_ITEMS = public final ContextMenuItemsPreference DRAWER_ITEMS =
(ContextMenuItemsPreference) new ContextMenuItemsPreference("drawer_items", DRAWER_ITEM_ID_SCHEME).makeProfile().cache(); (ContextMenuItemsPreference) new ContextMenuItemsPreference("drawer_items", DRAWER_ITEM_ID_SCHEME, new ContextMenuItemsSettings())
.makeProfile().cache();
public final ContextMenuItemsPreference CONFIGURE_MAP_ITEMS = public final ContextMenuItemsPreference CONFIGURE_MAP_ITEMS =
(ContextMenuItemsPreference) new ContextMenuItemsPreference("context_menu_items", CONFIGURE_MAP_ITEM_ID_SCHEME).makeProfile().cache(); (ContextMenuItemsPreference) new ContextMenuItemsPreference("context_menu_items", CONFIGURE_MAP_ITEM_ID_SCHEME, new ContextMenuItemsSettings())
.makeProfile().cache();
public final ContextMenuItemsPreference CONTEXT_MENU_ACTIONS_ITEMS = public final ContextMenuItemsPreference CONTEXT_MENU_ACTIONS_ITEMS =
(ContextMenuItemsPreference) new ContextMenuItemsPreference("configure_map_items", MAP_CONTEXT_MENU_ACTIONS).makeProfile().cache(); (ContextMenuItemsPreference) new ContextMenuItemsPreference("configure_map_items", MAP_CONTEXT_MENU_ACTIONS, new MapContextMenuItemsSettings())
.makeProfile().cache();
public final List<ContextMenuItemsPreference> CONTEXT_MENU_ITEMS_PREFERENCES = Arrays.asList(DRAWER_ITEMS, CONFIGURE_MAP_ITEMS, CONTEXT_MENU_ACTIONS_ITEMS); public final List<ContextMenuItemsPreference> CONTEXT_MENU_ITEMS_PREFERENCES = Arrays.asList(DRAWER_ITEMS, CONFIGURE_MAP_ITEMS, CONTEXT_MENU_ACTIONS_ITEMS);
@Nullable @Nullable
public ContextMenuItemsPreference getContextMenuItemsPreference(@NonNull String id) { public ContextMenuItemsPreference getContextMenuItemsPreference(@NonNull String id) {
if (id.startsWith(AIDL_LAYERS_PREFIX)) {
return CONFIGURE_MAP_ITEMS;
}
for (ContextMenuItemsPreference preference : CONTEXT_MENU_ITEMS_PREFERENCES) { for (ContextMenuItemsPreference preference : CONTEXT_MENU_ITEMS_PREFERENCES) {
if (id.startsWith(preference.idScheme)) { if (id.startsWith(preference.idScheme)) {
return preference; return preference;

View file

@ -350,25 +350,25 @@ public class MapActivityActions implements DialogProvider {
.setTitleId(selectedObj instanceof FavouritePoint ? R.string.favourites_context_menu_edit : R.string.shared_string_add, mapActivity) .setTitleId(selectedObj instanceof FavouritePoint ? R.string.favourites_context_menu_edit : R.string.shared_string_add, mapActivity)
.setId(MAP_CONTEXT_MENU_ADD_ID) .setId(MAP_CONTEXT_MENU_ADD_ID)
.setIcon(R.drawable.map_action_fav_dark) .setIcon(R.drawable.map_action_fav_dark)
.setOrder(0) .setOrder(10)
.createItem()); .createItem());
adapter.addItem(itemBuilder adapter.addItem(itemBuilder
.setTitleId(R.string.shared_string_marker, mapActivity) .setTitleId(R.string.shared_string_marker, mapActivity)
.setId(MAP_CONTEXT_MENU_MARKER_ID) .setId(MAP_CONTEXT_MENU_MARKER_ID)
.setIcon(R.drawable.map_action_flag_dark) .setIcon(R.drawable.map_action_flag_dark)
.setOrder(1) .setOrder(20)
.createItem()); .createItem());
adapter.addItem(itemBuilder adapter.addItem(itemBuilder
.setTitleId(R.string.shared_string_share, mapActivity) .setTitleId(R.string.shared_string_share, mapActivity)
.setId(MAP_CONTEXT_MENU_SHARE_ID) .setId(MAP_CONTEXT_MENU_SHARE_ID)
.setIcon(R.drawable.map_action_gshare_dark) .setIcon(R.drawable.map_action_gshare_dark)
.setOrder(2) .setOrder(30)
.createItem()); .createItem());
adapter.addItem(itemBuilder adapter.addItem(itemBuilder
.setTitleId(R.string.shared_string_actions, mapActivity) .setTitleId(R.string.shared_string_actions, mapActivity)
.setId(MAP_CONTEXT_MENU_MORE_ID) .setId(MAP_CONTEXT_MENU_MORE_ID)
.setIcon(R.drawable.map_overflow_menu_white) .setIcon(R.drawable.map_overflow_menu_white)
.setOrder(3) .setOrder(40)
.createItem()); .createItem());
adapter.addItem(itemBuilder adapter.addItem(itemBuilder
@ -435,8 +435,6 @@ public class MapActivityActions implements DialogProvider {
.setIcon(R.drawable.ic_action_alert) .setIcon(R.drawable.ic_action_alert)
.setOrder(AVOID_ROAD_ITEM_ORDER) .setOrder(AVOID_ROAD_ITEM_ORDER)
.createItem()); .createItem());
adapter.sortItemsByOrder();
} }
public void contextMenuPoint(final double latitude, final double longitude, final ContextMenuAdapter iadapter, Object selectedObj) { public void contextMenuPoint(final double latitude, final double longitude, final ContextMenuAdapter iadapter, Object selectedObj) {
@ -941,6 +939,10 @@ public class MapActivityActions implements DialogProvider {
app.getAidlApi().registerNavDrawerItems(mapActivity, optionsMenuHelper); app.getAidlApi().registerNavDrawerItems(mapActivity, optionsMenuHelper);
optionsMenuHelper.addItem(new ItemBuilder().setLayout(R.layout.drawer_divider)
.setId(DRAWER_DIVIDER_ID)
.createItem());
optionsMenuHelper.addItem(new ItemBuilder().setTitleId(R.string.layer_map_appearance, mapActivity) optionsMenuHelper.addItem(new ItemBuilder().setTitleId(R.string.layer_map_appearance, mapActivity)
.setId(DRAWER_CONFIGURE_SCREEN_ID) .setId(DRAWER_CONFIGURE_SCREEN_ID)
.setIcon(R.drawable.ic_configure_screen_dark) .setIcon(R.drawable.ic_configure_screen_dark)
@ -1027,22 +1029,6 @@ public class MapActivityActions implements DialogProvider {
//////////// Others //////////// Others
OsmandPlugin.registerOptionsMenu(mapActivity, optionsMenuHelper); OsmandPlugin.registerOptionsMenu(mapActivity, optionsMenuHelper);
// Place divider between functionality and configuration related menu items
int dividerItemIndex = -1;
for (int i = 0; i < optionsMenuHelper.length(); i++) {
if (optionsMenuHelper.getItem(i).getTitleId() == R.string.layer_map_appearance) {
dividerItemIndex = i;
break;
}
}
ItemBuilder divider = new ItemBuilder().setLayout(R.layout.drawer_divider);
divider.setId(DRAWER_DIVIDER_ID);
int position = dividerItemIndex >= 0 ? dividerItemIndex : 8;
divider.setPosition(position);
divider.setOrder(position);
optionsMenuHelper.addItem(divider.createItem());
return optionsMenuHelper; return optionsMenuHelper;
} }

View file

@ -911,7 +911,6 @@ public class ConfigureMapMenu {
.setCategory(true).setLayout(R.layout.list_group_title_with_switch).createItem()); .setCategory(true).setLayout(R.layout.list_group_title_with_switch).createItem());
createCustomRenderingProperties(adapter, activity, customRules, app, selectedProfileColor, nightMode, themeRes); createCustomRenderingProperties(adapter, activity, customRules, app, selectedProfileColor, nightMode, themeRes);
} }
adapter.sortItemsByOrder();
} }
public static String[] mapNamesIds = new String[]{"", "en", "af", "als", "ar", "az", "be", "ber", "bg", "bn", "bpy", "br", "bs", "ca", "ceb", "cs", "cy", "da", "de", "el", "eo", "es", "et", "eu", "fa", "fi", "fr", "fy", "ga", "gl", "he", "hi", "hsb", "hr", "ht", "hu", "hy", "id", "is", "it", "ja", "ka", "kab", "ko", "ku", "la", "lb", "lo", "lt", "lv", "mk", "ml", "mr", "ms", "nds", "new", "nl", "nn", "no", "nv", "oc", "os", "pl", "pms", "pt", "ro", "ru", "sc", "sh", "sk", "sl", "sq", "sr", "sv", "sw", "ta", "te", "th", "tl", "tr", "uk", "vi", "vo", "zh"}; public static String[] mapNamesIds = new String[]{"", "en", "af", "als", "ar", "az", "be", "ber", "bg", "bn", "bpy", "br", "bs", "ca", "ceb", "cs", "cy", "da", "de", "el", "eo", "es", "et", "eu", "fa", "fi", "fr", "fy", "ga", "gl", "he", "hi", "hsb", "hr", "ht", "hu", "hy", "id", "is", "it", "ja", "ka", "kab", "ko", "ku", "la", "lb", "lo", "lt", "lv", "mk", "ml", "mr", "ms", "nds", "new", "nl", "nn", "no", "nv", "oc", "os", "pl", "pms", "pt", "ro", "ru", "sc", "sh", "sk", "sl", "sq", "sr", "sv", "sw", "ta", "te", "th", "tl", "tr", "uk", "vi", "vo", "zh"};

View file

@ -565,15 +565,9 @@ public class MapContextMenuFragment extends BaseOsmAndFragment implements Downlo
} }
// Action buttons // Action buttons
// TODO refactor section
ContextMenuAdapter adapter = menu.getActionsContextMenuAdapter(false); ContextMenuAdapter adapter = menu.getActionsContextMenuAdapter(false);
List<ContextMenuItem> items = new ArrayList<>(); List<ContextMenuItem> items = adapter.getVisibleItems();
List<String> mainIds = app.getSettings().CONTEXT_MENU_ACTIONS_ITEMS.get().getMainActionIds(); List<String> mainIds = ((OsmandSettings.MapContextMenuItemsSettings) app.getSettings().CONTEXT_MENU_ACTIONS_ITEMS.get()).getMainActionIds();
for (ContextMenuItem item : adapter.getItems()) {
if (!item.isHidden()) {
items.add(item);
}
}
ContextMenuAdapter mainAdapter = new ContextMenuAdapter(requireMyApplication()); ContextMenuAdapter mainAdapter = new ContextMenuAdapter(requireMyApplication());
ContextMenuAdapter additionalAdapter = new ContextMenuAdapter(requireMyApplication()); ContextMenuAdapter additionalAdapter = new ContextMenuAdapter(requireMyApplication());

View file

@ -159,11 +159,13 @@ public class ConfigureMenuItemsFragment extends BaseOsmAndFragment
private void initDefaultMainActions() { private void initDefaultMainActions() {
List<ContextMenuItem> defItems = contextMenuAdapter.getDefaultItems(); List<ContextMenuItem> defItems = contextMenuAdapter.getDefaultItems();
initDefaultOrders(defItems); OsmandSettings.ContextMenuItemsSettings pref = getSettingForScreen(app, screenType).getModeValue(appMode);
mainActionItems = new ArrayList<>(getSettingForScreen(app, screenType).getModeValue(appMode).getMainActionIds()); if (pref instanceof OsmandSettings.MapContextMenuItemsSettings) {
if (mainActionItems.isEmpty()) { mainActionItems = new ArrayList<>(((OsmandSettings.MapContextMenuItemsSettings) pref).getMainActionIds());
for (int i = 0; i < MAIN_BUTTONS_QUANTITY; i++) { if (mainActionItems.isEmpty()) {
mainActionItems.add(defItems.get(i).getId()); for (int i = 0; i < MAIN_BUTTONS_QUANTITY; i++) {
mainActionItems.add(defItems.get(i).getId());
}
} }
} }
} }
@ -240,7 +242,7 @@ public class ConfigureMenuItemsFragment extends BaseOsmAndFragment
FragmentManager fm = getFragmentManager(); FragmentManager fm = getFragmentManager();
OsmandSettings.ContextMenuItemsSettings prefToSave; OsmandSettings.ContextMenuItemsSettings prefToSave;
if (screenType == ScreenType.CONTEXT_MENU_ACTIONS) { if (screenType == ScreenType.CONTEXT_MENU_ACTIONS) {
prefToSave = new OsmandSettings.ContextMenuItemsSettings(mainActionItems, hiddenMenuItems, ids); prefToSave = new OsmandSettings.MapContextMenuItemsSettings(mainActionItems, hiddenMenuItems, ids);
} else { } else {
prefToSave = new OsmandSettings.ContextMenuItemsSettings(hiddenMenuItems, ids); prefToSave = new OsmandSettings.ContextMenuItemsSettings(hiddenMenuItems, ids);
} }
@ -276,10 +278,8 @@ public class ConfigureMenuItemsFragment extends BaseOsmAndFragment
super.onActivityCreated(savedInstanceState); super.onActivityCreated(savedInstanceState);
instantiateContextMenuAdapter(); instantiateContextMenuAdapter();
if (menuItemsOrder.isEmpty()) { if (menuItemsOrder.isEmpty()) {
List<ContextMenuItem> defItems = contextMenuAdapter.getDefaultItems(); for (ContextMenuItem item : contextMenuAdapter.getDefaultItems()) {
initDefaultOrders(defItems); menuItemsOrder.put(item.getId(), item.getOrder());
for (int i = 0; i < defItems.size(); i++) {
menuItemsOrder.put(defItems.get(i).getId(), i);
} }
} }
if (screenType == ScreenType.CONTEXT_MENU_ACTIONS) { if (screenType == ScreenType.CONTEXT_MENU_ACTIONS) {
@ -363,8 +363,8 @@ public class ConfigureMenuItemsFragment extends BaseOsmAndFragment
List<RearrangeMenuAdapterItem> items = new ArrayList<>(); List<RearrangeMenuAdapterItem> items = new ArrayList<>();
items.add(new RearrangeMenuAdapterItem(DESCRIPTION, screenType)); items.add(new RearrangeMenuAdapterItem(DESCRIPTION, screenType));
List<RearrangeMenuAdapterItem> visible = getItemsForRearrangeAdapter(hiddenMenuItems, wasReset ? null : menuItemsOrder, false); List<RearrangeMenuAdapterItem> visible = getItemsForRearrangeAdapter(hiddenMenuItems, menuItemsOrder, false);
List<RearrangeMenuAdapterItem> hiddenItems = getItemsForRearrangeAdapter(hiddenMenuItems, wasReset ? null : menuItemsOrder, true); List<RearrangeMenuAdapterItem> hiddenItems = getItemsForRearrangeAdapter(hiddenMenuItems, menuItemsOrder, true);
if (screenType == ScreenType.CONTEXT_MENU_ACTIONS) { if (screenType == ScreenType.CONTEXT_MENU_ACTIONS) {
int buttonMoreIndex = MAIN_BUTTONS_QUANTITY - 1; int buttonMoreIndex = MAIN_BUTTONS_QUANTITY - 1;
for (int i = 0; i < visible.size(); i++) { for (int i = 0; i < visible.size(); i++) {
@ -468,11 +468,11 @@ public class ConfigureMenuItemsFragment extends BaseOsmAndFragment
wasReset = true; wasReset = true;
isChanged = true; isChanged = true;
getSettingForScreen(app, screenType).resetModeToDefault(appMode); getSettingForScreen(app, screenType).resetModeToDefault(appMode);
instantiateContextMenuAdapter();
if (screenType == ScreenType.CONTEXT_MENU_ACTIONS) { if (screenType == ScreenType.CONTEXT_MENU_ACTIONS) {
mainActionItems.clear(); mainActionItems.clear();
initDefaultMainActions();
} }
instantiateContextMenuAdapter();
initDefaultMainActions();
rearrangeAdapter.updateItems(getAdapterItems()); rearrangeAdapter.updateItems(getAdapterItems());
} }
}); });
@ -513,17 +513,9 @@ public class ConfigureMenuItemsFragment extends BaseOsmAndFragment
} }
} }
private void initDefaultOrders(@NonNull List<ContextMenuItem> items) { private List<RearrangeMenuAdapterItem> getItemsForRearrangeAdapter(List<String> hiddenItemsIds, HashMap<String, Integer> itemsOrderIds, boolean hidden) {
for (int i = 0; i < items.size(); i++) {
items.get(i).setOrder(i);
}
}
public List<RearrangeMenuAdapterItem> getItemsForRearrangeAdapter(@Nullable List<String> hiddenItemsIds, @Nullable HashMap<String, Integer> itemsOrderIds, boolean hidden) {
List<ContextMenuItem> defItems = contextMenuAdapter.getDefaultItems(); List<ContextMenuItem> defItems = contextMenuAdapter.getDefaultItems();
if (itemsOrderIds == null || itemsOrderIds.isEmpty()) { if (!itemsOrderIds.isEmpty()) {
initDefaultOrders(defItems);
} else {
sortByCustomOrder(defItems, itemsOrderIds); sortByCustomOrder(defItems, itemsOrderIds);
} }
List<RearrangeMenuAdapterItem> visibleItems = new ArrayList<>(); List<RearrangeMenuAdapterItem> visibleItems = new ArrayList<>();

View file

@ -278,8 +278,6 @@ public class RearrangeMenuItemsAdapter extends RecyclerView.Adapter<RecyclerView
if (menuItemFrom.getId().startsWith(SHOW_ITEMS_ID_SCHEME) && menuItemTo.getId().startsWith(RENDERING_ITEMS_ID_SCHEME) if (menuItemFrom.getId().startsWith(SHOW_ITEMS_ID_SCHEME) && menuItemTo.getId().startsWith(RENDERING_ITEMS_ID_SCHEME)
|| menuItemFrom.getId().startsWith(RENDERING_ITEMS_ID_SCHEME) && menuItemTo.getId().startsWith(SHOW_ITEMS_ID_SCHEME) || menuItemFrom.getId().startsWith(RENDERING_ITEMS_ID_SCHEME) && menuItemTo.getId().startsWith(SHOW_ITEMS_ID_SCHEME)
|| menuItemFrom.getId().startsWith(RENDERING_ITEMS_ID_SCHEME) && menuItemTo.getId().startsWith(AIDL_LAYERS_PREFIX)
|| menuItemFrom.getId().startsWith(AIDL_LAYERS_PREFIX) && menuItemTo.getId().startsWith(RENDERING_ITEMS_ID_SCHEME)
|| menuItemTo.isHidden()) { || menuItemTo.isHidden()) {
return false; return false;
} }