refactor ContextMenuAdapter
This commit is contained in:
parent
2c7fd3606f
commit
c80c93e57f
5 changed files with 103 additions and 90 deletions
|
@ -37,6 +37,8 @@ import net.osmand.plus.dialogs.ConfigureMapMenu;
|
|||
import net.osmand.plus.dialogs.HelpArticleDialogFragment;
|
||||
import net.osmand.plus.helpers.AndroidUiHelper;
|
||||
import net.osmand.plus.settings.RearrangeMenuItemsAdapter.AdapterItem;
|
||||
import net.osmand.plus.OsmandSettings.ContextMenuItemsPreference;
|
||||
import net.osmand.util.Algorithms;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
|
||||
|
@ -150,6 +152,43 @@ public class ContextMenuAdapter {
|
|||
});
|
||||
}
|
||||
|
||||
public void sortItemsByCustomOrder(OsmandApplication app) {
|
||||
ContextMenuItemsPreference pref = getPreference(app);
|
||||
if (pref == null) {
|
||||
sortItemsByOrder();
|
||||
return;
|
||||
}
|
||||
|
||||
List<String> hiddenIds = pref.getHiddenIds();
|
||||
List<String> orderIds = pref.getOrderIds();
|
||||
|
||||
if (!Algorithms.isEmpty(orderIds)) {
|
||||
HashMap<String, Integer> ordersMap = new HashMap<>();
|
||||
for (int i = 0; i < orderIds.size(); i++) {
|
||||
ordersMap.put(orderIds.get(i), i);
|
||||
}
|
||||
|
||||
for (ContextMenuItem item : items) {
|
||||
Integer order = ordersMap.get(item.getId());
|
||||
if (order != null) {
|
||||
item.setOrder(order);
|
||||
}
|
||||
}
|
||||
sortItemsByOrder();
|
||||
}
|
||||
|
||||
if (!Algorithms.isEmpty(hiddenIds)) {
|
||||
List<ContextMenuItem> filtered = new ArrayList<>();
|
||||
for (ContextMenuItem item : items) {
|
||||
if (!hiddenIds.contains(item.getId())) {
|
||||
filtered.add(item);
|
||||
}
|
||||
}
|
||||
items = filtered;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public ArrayAdapter<ContextMenuItem> createListAdapter(final Activity activity, final boolean lightTheme) {
|
||||
final int layoutId = DEFAULT_LAYOUT_ID;
|
||||
final OsmandApplication app = ((OsmandApplication) activity.getApplication());
|
||||
|
@ -555,36 +594,6 @@ public class ContextMenuAdapter {
|
|||
}
|
||||
}
|
||||
|
||||
private void initDefaultOrders(@NonNull List<ContextMenuItem> items) {
|
||||
for (int i = 0; i < items.size(); i++) {
|
||||
items.get(i).setOrder(i);
|
||||
}
|
||||
}
|
||||
|
||||
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 {
|
||||
reorderMenuItems(items, itemsOrderIds);
|
||||
}
|
||||
List<AdapterItem> visibleItems = new ArrayList<>();
|
||||
List<AdapterItem> hiddenItems = new ArrayList<>();
|
||||
for (ContextMenuItem item : items) {
|
||||
String id = item.getId();
|
||||
if (id != null && id.startsWith(idScheme) && !APP_PROFILES_ID.equals(id)) {
|
||||
if (hiddenItemsIds != null && hiddenItemsIds.contains(id)) {
|
||||
item.setHidden(true);
|
||||
hiddenItems.add(new AdapterItem(MENU_ITEM, item));
|
||||
} else {
|
||||
item.setHidden(false);
|
||||
visibleItems.add(new AdapterItem(MENU_ITEM, item));
|
||||
}
|
||||
}
|
||||
}
|
||||
return hidden ? hiddenItems : visibleItems;
|
||||
}
|
||||
|
||||
public List<ContextMenuItem> getDefaultItems() {
|
||||
String idScheme = getIdScheme();
|
||||
List<ContextMenuItem> items = new ArrayList<>();
|
||||
|
@ -617,66 +626,19 @@ public class ContextMenuAdapter {
|
|||
return idScheme;
|
||||
}
|
||||
|
||||
public void reorderMenuItems(@NonNull List<ContextMenuItem> defaultItems, @NonNull HashMap<String, Integer> itemsOrder) {
|
||||
for (ContextMenuItem item : defaultItems) {
|
||||
Integer order = itemsOrder.get(item.getId());
|
||||
if (order != null) {
|
||||
item.setOrder(order);
|
||||
}
|
||||
}
|
||||
Collections.sort(defaultItems, new Comparator<ContextMenuItem>() {
|
||||
@Override
|
||||
public int compare(ContextMenuItem item1, ContextMenuItem item2) {
|
||||
int order1 = item1.getOrder();
|
||||
int order2 = item2.getOrder();
|
||||
return (order1 < order2) ? -1 : ((order1 == order2) ? 0 : 1);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public HashMap<String, Integer> getMenuItemsOrder(@NonNull List<String> ids) {
|
||||
HashMap<String, Integer> result = new HashMap<>();
|
||||
for (int i = 0; i < ids.size(); i++) {
|
||||
result.put(ids.get(i), i);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public void initItemsCustomOrder(@NonNull OsmandApplication app) {
|
||||
OsmandSettings.ContextMenuItemsPreference preference = null;
|
||||
private ContextMenuItemsPreference getPreference(OsmandApplication app) {
|
||||
for (ContextMenuItem item : items) {
|
||||
String id = item.getId();
|
||||
if (id != null) {
|
||||
if (id.startsWith(DRAWER_ITEM_ID_SCHEME)) {
|
||||
preference = app.getSettings().DRAWER_ITEMS;
|
||||
break;
|
||||
return app.getSettings().DRAWER_ITEMS;
|
||||
} else if (id.startsWith(CONFIGURE_MAP_ITEM_ID_SCHEME)) {
|
||||
preference = app.getSettings().CONFIGURE_MAP_ITEMS;
|
||||
break;
|
||||
return app.getSettings().CONFIGURE_MAP_ITEMS;
|
||||
} else if (id.startsWith(MAP_CONTEXT_MENU_ACTIONS)) {
|
||||
preference = app.getSettings().CONTEXT_MENU_ACTIONS_ITEMS;
|
||||
break;
|
||||
return app.getSettings().CONTEXT_MENU_ACTIONS_ITEMS;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (preference == null) {
|
||||
return;
|
||||
}
|
||||
List<String> savedOrder = preference.getOrderIds();
|
||||
List<String> hiddenItems = preference.getHiddenIds();
|
||||
|
||||
if (!savedOrder.isEmpty()) {
|
||||
reorderMenuItems(items, getMenuItemsOrder(savedOrder));
|
||||
}
|
||||
|
||||
if (!hiddenItems.isEmpty()) {
|
||||
List<ContextMenuItem> filtered = new ArrayList<>();
|
||||
for (ContextMenuItem item : items) {
|
||||
if (!hiddenItems.contains(item.getId())) {
|
||||
filtered.add(item);
|
||||
}
|
||||
}
|
||||
items = filtered;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
|
@ -1182,7 +1182,7 @@ public class MapActivityActions implements DialogProvider {
|
|||
}
|
||||
menuItemsListView.setDivider(null);
|
||||
final ContextMenuAdapter contextMenuAdapter = createMainOptionsMenu();
|
||||
contextMenuAdapter.initItemsCustomOrder(getMyApplication());
|
||||
contextMenuAdapter.sortItemsByCustomOrder(getMyApplication());
|
||||
contextMenuAdapter.setDefaultLayoutId(R.layout.simple_list_menu_item);
|
||||
final ArrayAdapter<ContextMenuItem> simpleListAdapter = contextMenuAdapter.createListAdapter(mapActivity,
|
||||
!nightMode);
|
||||
|
|
|
@ -77,7 +77,6 @@ import net.osmand.plus.rastermaps.OsmandRasterMapsPlugin;
|
|||
import net.osmand.plus.routepreparationmenu.RoutingOptionsHelper.LocalRoutingParameter;
|
||||
import net.osmand.plus.routing.IRouteInformationListener;
|
||||
import net.osmand.plus.routing.RoutingHelper;
|
||||
import net.osmand.plus.settings.ConfigureMenuRootFragment.ScreenType;
|
||||
import net.osmand.plus.srtmplugin.ContourLinesMenu;
|
||||
import net.osmand.plus.srtmplugin.SRTMPlugin;
|
||||
import net.osmand.plus.srtmplugin.TerrainFragment;
|
||||
|
@ -703,7 +702,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());
|
||||
cm.sortItemsByCustomOrder(getMyApplication());
|
||||
} else if (visibleType == DashboardType.LIST_MENU) {
|
||||
cm = mapActivity.getMapActions().createMainOptionsMenu();
|
||||
} else if (visibleType == DashboardType.ROUTE_PREFERENCES) {
|
||||
|
|
|
@ -566,7 +566,7 @@ public class MapContextMenuFragment extends BaseOsmAndFragment implements Downlo
|
|||
// Action buttons
|
||||
// TODO refactor section
|
||||
ContextMenuAdapter adapter = menu.getActionsContextMenuAdapter(false);
|
||||
adapter.initItemsCustomOrder(requireMyApplication());
|
||||
adapter.sortItemsByCustomOrder(requireMyApplication());
|
||||
List<ContextMenuItem> items = adapter.getItems();
|
||||
List<ContextMenuItem> main = new ArrayList<>();
|
||||
List<ContextMenuItem> additional = new ArrayList<>();
|
||||
|
|
|
@ -48,6 +48,8 @@ import org.json.JSONException;
|
|||
import org.json.JSONObject;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
||||
|
@ -58,6 +60,7 @@ import static net.osmand.plus.settings.RearrangeMenuItemsAdapter.AdapterItemType
|
|||
import static net.osmand.plus.settings.RearrangeMenuItemsAdapter.AdapterItemType.DESCRIPTION;
|
||||
import static net.osmand.plus.settings.RearrangeMenuItemsAdapter.AdapterItemType.DIVIDER;
|
||||
import static net.osmand.plus.settings.RearrangeMenuItemsAdapter.AdapterItemType.HEADER;
|
||||
import static net.osmand.plus.settings.RearrangeMenuItemsAdapter.AdapterItemType.MENU_ITEM;
|
||||
|
||||
public class ConfigureMenuItemsFragment extends BaseOsmAndFragment
|
||||
implements SelectCopyAppModeBottomSheet.CopyAppModePrefsListener {
|
||||
|
@ -125,7 +128,11 @@ public class ConfigureMenuItemsFragment extends BaseOsmAndFragment
|
|||
menuItemsOrder = (HashMap<String, Integer>) savedInstanceState.getSerializable(ITEMS_ORDER_KEY);
|
||||
} else {
|
||||
hiddenMenuItems = getSettingForScreen(app, screenType).getHiddenIds();
|
||||
menuItemsOrder = contextMenuAdapter.getMenuItemsOrder(getSettingForScreen(app, screenType).getOrderIds());
|
||||
menuItemsOrder = new HashMap<>();
|
||||
List<String> orderIds = getSettingForScreen(app, screenType).getOrderIds();
|
||||
for (int i = 0; i < orderIds.size(); i++) {
|
||||
menuItemsOrder.put(orderIds.get(i), i);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -241,7 +248,7 @@ public class ConfigureMenuItemsFragment extends BaseOsmAndFragment
|
|||
@Override
|
||||
public void onClick(View v) {
|
||||
List<ContextMenuItem> defItems = contextMenuAdapter.getDefaultItems();
|
||||
contextMenuAdapter.reorderMenuItems(defItems, menuItemsOrder);
|
||||
sortByCustomOrder(defItems, menuItemsOrder);
|
||||
List<String> ids = new ArrayList<>();
|
||||
for (ContextMenuItem item : defItems) {
|
||||
ids.add(item.getId());
|
||||
|
@ -305,8 +312,8 @@ public class ConfigureMenuItemsFragment extends BaseOsmAndFragment
|
|||
List<AdapterItem> items = new ArrayList<>();
|
||||
items.add(new AdapterItem(DESCRIPTION, screenType));
|
||||
|
||||
List<AdapterItem> visible = contextMenuAdapter.getItemsForRearrangeAdapter(hiddenMenuItems, wasReset ? null : menuItemsOrder, false);
|
||||
List<AdapterItem> hiddenItems = contextMenuAdapter.getItemsForRearrangeAdapter(hiddenMenuItems, wasReset ? null : menuItemsOrder, true);
|
||||
List<AdapterItem> visible = getItemsForRearrangeAdapter(hiddenMenuItems, wasReset ? null : menuItemsOrder, false);
|
||||
List<AdapterItem> hiddenItems = getItemsForRearrangeAdapter(hiddenMenuItems, wasReset ? null : menuItemsOrder, true);
|
||||
if (screenType == ScreenType.CONTEXT_MENU_ACTIONS) {
|
||||
for (int i =0; i<visible.size(); i++){
|
||||
ContextMenuItem value = (ContextMenuItem) visible.get(i).getValue();
|
||||
|
@ -431,4 +438,49 @@ public class ConfigureMenuItemsFragment extends BaseOsmAndFragment
|
|||
throw new IllegalArgumentException("Unsupported screen type");
|
||||
}
|
||||
}
|
||||
|
||||
private void initDefaultOrders(@NonNull List<ContextMenuItem> items) {
|
||||
for (int i = 0; i < items.size(); i++) {
|
||||
items.get(i).setOrder(i);
|
||||
}
|
||||
}
|
||||
|
||||
public List<AdapterItem> getItemsForRearrangeAdapter(@Nullable List<String> hiddenItemsIds, @Nullable HashMap<String, Integer> itemsOrderIds, boolean hidden) {
|
||||
List<ContextMenuItem> defItems = contextMenuAdapter.getDefaultItems();
|
||||
if (itemsOrderIds == null || itemsOrderIds.isEmpty()) {
|
||||
initDefaultOrders(defItems);
|
||||
} else {
|
||||
sortByCustomOrder(defItems, itemsOrderIds);
|
||||
}
|
||||
List<AdapterItem> visibleItems = new ArrayList<>();
|
||||
List<AdapterItem> hiddenItems = new ArrayList<>();
|
||||
for (ContextMenuItem item : defItems) {
|
||||
String id = item.getId();
|
||||
if (hiddenItemsIds != null && hiddenItemsIds.contains(id)) {
|
||||
item.setHidden(true);
|
||||
hiddenItems.add(new AdapterItem(MENU_ITEM, item));
|
||||
} else {
|
||||
item.setHidden(false);
|
||||
visibleItems.add(new AdapterItem(MENU_ITEM, item));
|
||||
}
|
||||
}
|
||||
return hidden ? hiddenItems : visibleItems;
|
||||
}
|
||||
|
||||
private void sortByCustomOrder(List<ContextMenuItem> defItems, HashMap<String, Integer> itemsOrderIds) {
|
||||
for (ContextMenuItem item : defItems) {
|
||||
Integer order = itemsOrderIds.get(item.getId());
|
||||
if (order != null) {
|
||||
item.setOrder(order);
|
||||
}
|
||||
}
|
||||
Collections.sort(defItems, new Comparator<ContextMenuItem>() {
|
||||
@Override
|
||||
public int compare(ContextMenuItem item1, ContextMenuItem item2) {
|
||||
int order1 = item1.getOrder();
|
||||
int order2 = item2.getOrder();
|
||||
return (order1 < order2) ? -1 : ((order1 == order2) ? 0 : 1);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue