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

View file

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

View file

@ -1141,8 +1141,8 @@ public class OsmandSettings {
@NonNull
private String idScheme;
private ContextMenuItemsPreference(String id, @NonNull String idScheme) {
super(id, new ContextMenuItemsSettings());
private ContextMenuItemsPreference(String id, @NonNull String idScheme, @NonNull ContextMenuItemsSettings defValue) {
super(id, defValue);
this.idScheme = idScheme;
}
@ -1163,7 +1163,7 @@ public class OsmandSettings {
}
private ContextMenuItemsSettings readValue(String s) {
ContextMenuItemsSettings value = new ContextMenuItemsSettings();
ContextMenuItemsSettings value = getDefaultValue().newInstance();
value.readFromJsonString(s, idScheme);
return value;
}
@ -1175,12 +1175,10 @@ public class OsmandSettings {
}
public static class ContextMenuItemsSettings implements Serializable {
public static final String HIDDEN = "hidden";
public static final String ORDER = "order";
public static final String MAIN_ACTIONS = "main_actions";
private static final String HIDDEN = "hidden";
private static final String ORDER = "order";
private List<String> hiddenIds = new ArrayList<>();
private List<String> orderIds = new ArrayList<>();
private List<String> mainActionIds = new ArrayList<>();
public ContextMenuItemsSettings() {
@ -1191,10 +1189,8 @@ public class OsmandSettings {
this.orderIds = orderIds;
}
public ContextMenuItemsSettings(@NonNull List<String> mainActionIds, @NonNull List<String> hiddenIds, @NonNull List<String> orderIds) {
this.mainActionIds = mainActionIds;
this.hiddenIds = hiddenIds;
this.orderIds = orderIds;
public ContextMenuItemsSettings newInstance() {
return new ContextMenuItemsSettings();
}
public void readFromJsonString(String jsonString, @NonNull String idScheme) {
@ -1203,26 +1199,23 @@ public class OsmandSettings {
}
try {
JSONObject json = new JSONObject(jsonString);
hiddenIds = readIdsList(json.optJSONArray(HIDDEN), idScheme);
orderIds = readIdsList(json.optJSONArray(ORDER), idScheme);
if (idScheme.equals(MAP_CONTEXT_MENU_ACTIONS)) {
mainActionIds = readIdsList(json.optJSONArray(MAIN_ACTIONS), idScheme);
}
readFromJson(json, idScheme);
} catch (JSONException 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<>();
if (jsonArray != null) {
for (int i = 0; i < jsonArray.length(); i++) {
String id = jsonArray.optString(i);
if (id.startsWith(AIDL_LAYERS_PREFIX)) {
list.add(id);
} else {
list.add(idScheme + id);
}
list.add(idScheme + id);
}
}
return list;
@ -1231,11 +1224,7 @@ public class OsmandSettings {
public String writeToJsonString(@NonNull String idScheme) {
try {
JSONObject json = new JSONObject();
json.put(HIDDEN, getJsonArray(hiddenIds, idScheme));
json.put(ORDER, getJsonArray(orderIds, idScheme));
if (idScheme.equals(MAP_CONTEXT_MENU_ACTIONS)) {
json.put(MAIN_ACTIONS, getJsonArray(mainActionIds, idScheme));
}
writeToJson(json, idScheme);
return json.toString();
} catch (JSONException e) {
LOG.error("Error converting to json string: " + e);
@ -1243,7 +1232,12 @@ public class OsmandSettings {
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();
if (ids != null && !ids.isEmpty()) {
for (String id : ids) {
@ -1260,6 +1254,37 @@ public class OsmandSettings {
public List<String> getOrderIds() {
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() {
return Collections.unmodifiableList(mainActionIds);
@ -3592,21 +3617,21 @@ public class OsmandSettings {
new ListStringPreference("inactive_poi_filters", null, ",,").makeProfile().cache();
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 =
(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 =
(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);
@Nullable
public ContextMenuItemsPreference getContextMenuItemsPreference(@NonNull String id) {
if (id.startsWith(AIDL_LAYERS_PREFIX)) {
return CONFIGURE_MAP_ITEMS;
}
for (ContextMenuItemsPreference preference : CONTEXT_MENU_ITEMS_PREFERENCES) {
if (id.startsWith(preference.idScheme)) {
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)
.setId(MAP_CONTEXT_MENU_ADD_ID)
.setIcon(R.drawable.map_action_fav_dark)
.setOrder(0)
.setOrder(10)
.createItem());
adapter.addItem(itemBuilder
.setTitleId(R.string.shared_string_marker, mapActivity)
.setId(MAP_CONTEXT_MENU_MARKER_ID)
.setIcon(R.drawable.map_action_flag_dark)
.setOrder(1)
.setOrder(20)
.createItem());
adapter.addItem(itemBuilder
.setTitleId(R.string.shared_string_share, mapActivity)
.setId(MAP_CONTEXT_MENU_SHARE_ID)
.setIcon(R.drawable.map_action_gshare_dark)
.setOrder(2)
.setOrder(30)
.createItem());
adapter.addItem(itemBuilder
.setTitleId(R.string.shared_string_actions, mapActivity)
.setId(MAP_CONTEXT_MENU_MORE_ID)
.setIcon(R.drawable.map_overflow_menu_white)
.setOrder(3)
.setOrder(40)
.createItem());
adapter.addItem(itemBuilder
@ -435,8 +435,6 @@ public class MapActivityActions implements DialogProvider {
.setIcon(R.drawable.ic_action_alert)
.setOrder(AVOID_ROAD_ITEM_ORDER)
.createItem());
adapter.sortItemsByOrder();
}
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);
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)
.setId(DRAWER_CONFIGURE_SCREEN_ID)
.setIcon(R.drawable.ic_configure_screen_dark)
@ -1027,22 +1029,6 @@ public class MapActivityActions implements DialogProvider {
//////////// Others
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;
}

View file

@ -911,7 +911,6 @@ public class ConfigureMapMenu {
.setCategory(true).setLayout(R.layout.list_group_title_with_switch).createItem());
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"};

View file

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

View file

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