From 065f281f92198a6128bbac1a9b721675b0a44829 Mon Sep 17 00:00:00 2001 From: veliymolfar Date: Thu, 2 Apr 2020 20:23:48 +0300 Subject: [PATCH] initial rearrange menu items implementation --- OsmAnd/res/layout/profile_edit_list_item.xml | 9 + .../net/osmand/plus/ContextMenuAdapter.java | 103 ++++- .../src/net/osmand/plus/ContextMenuItem.java | 9 + .../src/net/osmand/plus/OsmandSettings.java | 5 + .../plus/activities/MapActivityActions.java | 29 -- .../settings/ConfigureMenuItemsFragment.java | 352 ++++++++---------- .../settings/ConfigureMenuRootFragment.java | 12 +- .../plus/settings/MenuItemsManager.java | 184 --------- ...er.java => RearrangeMenuItemsAdapter.java} | 156 +++----- 9 files changed, 345 insertions(+), 514 deletions(-) delete mode 100644 OsmAnd/src/net/osmand/plus/settings/MenuItemsManager.java rename OsmAnd/src/net/osmand/plus/settings/{MenuItemsAdapter.java => RearrangeMenuItemsAdapter.java} (76%) diff --git a/OsmAnd/res/layout/profile_edit_list_item.xml b/OsmAnd/res/layout/profile_edit_list_item.xml index 3075e66982..08c2880c37 100644 --- a/OsmAnd/res/layout/profile_edit_list_item.xml +++ b/OsmAnd/res/layout/profile_edit_list_item.xml @@ -4,9 +4,18 @@ xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="wrap_content" + android:orientation="vertical" android:background="?attr/list_background_color" android:gravity="center_vertical"> + + items) { + for (int i = 0; i < items.size(); i++) { + items.get(i).setOrder(i); + } + } + + public List getItemsForRearrangeAdapter(@NonNull ScreenType screenType, @Nullable List hiddenItemsIds, @Nullable HashMap 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; + } + if (itemsOrderIds == null || itemsOrderIds.isEmpty()) { + initDefaultOrders(items); + } else { + reorderMenuItems(items, itemsOrderIds); + } + List visibleItems = new ArrayList<>(); + List 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 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; + } + List items = new ArrayList<>(); + for (ContextMenuItem item : this.items) { + String id = item.getId(); + if (id != null && id.startsWith(idScheme) && !APP_PROFILES_ID.equals(id)) { + items.add(item); + } + } + return items; + } + + public void reorderMenuItems(@NonNull List defaultItems, @NonNull HashMap itemsOrder) { + for (ContextMenuItem item : defaultItems) { + Integer order = itemsOrder.get(item.getId()); + if (order != null) { + item.setOrder(order); + } + } + Collections.sort(defaultItems, new Comparator() { + @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 getMenuItemsOrder(@NonNull List ids) { + HashMap result = new HashMap<>(); + for (int i = 0; i < ids.size(); i++) { + result.put(ids.get(i), i); + } + return result; + } + } \ No newline at end of file diff --git a/OsmAnd/src/net/osmand/plus/ContextMenuItem.java b/OsmAnd/src/net/osmand/plus/ContextMenuItem.java index 980d255119..dcdd7894f3 100644 --- a/OsmAnd/src/net/osmand/plus/ContextMenuItem.java +++ b/OsmAnd/src/net/osmand/plus/ContextMenuItem.java @@ -30,6 +30,7 @@ public class ContextMenuItem { private final boolean category; private final boolean clickable; private final boolean skipPaintingWithoutColor; + private boolean hidden; private final int pos; private int order; private String description; @@ -150,6 +151,10 @@ public class ContextMenuItem { return clickable; } + public boolean isHidden() { + return hidden; + } + public int getPos() { return pos; } @@ -182,6 +187,10 @@ public class ContextMenuItem { return hideDivider; } + public void setHidden(boolean hidden) { + this.hidden = hidden; + } + public void setTitle(String title) { this.title = title; } diff --git a/OsmAnd/src/net/osmand/plus/OsmandSettings.java b/OsmAnd/src/net/osmand/plus/OsmandSettings.java index afce0f0e89..2e231954e3 100644 --- a/OsmAnd/src/net/osmand/plus/OsmandSettings.java +++ b/OsmAnd/src/net/osmand/plus/OsmandSettings.java @@ -3389,9 +3389,14 @@ public class OsmandSettings { 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(); diff --git a/OsmAnd/src/net/osmand/plus/activities/MapActivityActions.java b/OsmAnd/src/net/osmand/plus/activities/MapActivityActions.java index ffd819eafe..10463946b8 100644 --- a/OsmAnd/src/net/osmand/plus/activities/MapActivityActions.java +++ b/OsmAnd/src/net/osmand/plus/activities/MapActivityActions.java @@ -65,7 +65,6 @@ import net.osmand.plus.routepreparationmenu.WaypointsFragment; import net.osmand.plus.routing.RouteProvider.GPXRouteParamsBuilder; import net.osmand.plus.routing.RoutingHelper; import net.osmand.plus.settings.BaseSettingsFragment; -import net.osmand.plus.settings.MenuItemsManager; import net.osmand.plus.views.BaseMapLayer; import net.osmand.plus.views.MapControlsLayer; import net.osmand.plus.views.MapTileLayer; @@ -82,7 +81,6 @@ import java.io.File; import java.text.MessageFormat; import java.util.ArrayList; import java.util.Date; -import java.util.LinkedHashMap; import java.util.List; import static net.osmand.aidlapi.OsmAndCustomizationConstants.DRAWER_CONFIGURE_MAP_ID; @@ -988,33 +986,6 @@ public class MapActivityActions implements DialogProvider { divider.setPosition(dividerItemIndex >= 0 ? dividerItemIndex : 8); optionsMenuHelper.addItem(divider.createItem()); - List items = optionsMenuHelper.getItems(); - List newItems = new ArrayList<>(); - List hiddenIds = app.getSettings().HIDDEN_DRAWER_ITEMS.getStringsList(); - - MenuItemsManager manager = new MenuItemsManager(app); - LinkedHashMap order = manager.getDrawerItemsSavedOrder(); - - if (hiddenIds != null){ - for (ContextMenuItem item: items){ - if (!hiddenIds.contains(item.getId())){ - newItems.add(item); - } - } - } else { - newItems.addAll(items); - } - - for (ContextMenuItem item: newItems){ - Integer o = order.get(item.getId()); - if (o == null){ - o = item.getOrder(); - } - item.setOrder(o); - } - - optionsMenuHelper.updateItems(newItems); - optionsMenuHelper.sortItemsByOrder(); return optionsMenuHelper; } diff --git a/OsmAnd/src/net/osmand/plus/settings/ConfigureMenuItemsFragment.java b/OsmAnd/src/net/osmand/plus/settings/ConfigureMenuItemsFragment.java index c9be548385..0ecfe40248 100644 --- a/OsmAnd/src/net/osmand/plus/settings/ConfigureMenuItemsFragment.java +++ b/OsmAnd/src/net/osmand/plus/settings/ConfigureMenuItemsFragment.java @@ -1,10 +1,10 @@ package net.osmand.plus.settings; +import android.app.Activity; import android.content.Context; import android.content.DialogInterface; import android.os.Build; import android.os.Bundle; -import android.os.Parcelable; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; @@ -22,60 +22,59 @@ import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; import net.osmand.AndroidUtils; +import net.osmand.plus.ContextMenuAdapter; +import net.osmand.plus.ContextMenuItem; import net.osmand.plus.OsmandApplication; import net.osmand.plus.R; import net.osmand.plus.UiUtilities; import net.osmand.plus.activities.MapActivity; import net.osmand.plus.activities.MapActivityActions; import net.osmand.plus.base.BaseOsmAndFragment; +import net.osmand.plus.dialogs.ConfigureMapMenu; import net.osmand.plus.settings.ConfigureMenuRootFragment.ScreenType; import net.osmand.plus.views.controls.ReorderItemTouchHelperCallback; -import net.osmand.plus.settings.MenuItemsAdapter.AdapterItem; -import net.osmand.plus.settings.MenuItemsAdapter.HeaderItem; -import net.osmand.plus.settings.MenuItemsAdapter.ButtonItem; -import net.osmand.plus.settings.MenuItemsAdapter.MenuItemBase; -import net.osmand.plus.settings.MenuItemsAdapter.MenuItemsAdapterListener; +import net.osmand.plus.settings.RearrangeMenuItemsAdapter.AdapterItem; +import net.osmand.plus.settings.RearrangeMenuItemsAdapter.MenuItemsAdapterListener; import java.util.ArrayList; import java.util.Collections; -import java.util.Comparator; import java.util.HashMap; import java.util.LinkedHashMap; import java.util.List; -import static net.osmand.plus.settings.MenuItemsAdapter.AdapterItemType.HEADER; -import static net.osmand.plus.settings.MenuItemsAdapter.AdapterItemType.DIVIDER; -import static net.osmand.plus.settings.MenuItemsAdapter.AdapterItemType.DESCRIPTION; -import static net.osmand.plus.settings.MenuItemsAdapter.AdapterItemType.BUTTON; -import static net.osmand.plus.settings.MenuItemsAdapter.AdapterItemType.MENU_ITEM; +import static net.osmand.plus.settings.RearrangeMenuItemsAdapter.AdapterItemType.BUTTON; +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; public class ConfigureMenuItemsFragment extends BaseOsmAndFragment { public static final String TAG = ConfigureMenuItemsFragment.class.getName(); private static final String ITEM_TYPE_KEY = "item_type_key"; - private static String ITEMS_ORDER_KEY = "items_order_key"; - private static String HIDDEN_ITEMS_KEY = "hidden_items_key"; - private static String CONFIGURE_MENU_ITEMS_TAG = "configure_menu_items_tag"; - private OsmandApplication app; - private boolean nightMode; - private ScreenType type; - private LayoutInflater mInflater; - private MenuItemsManager menuItemsManager; + private static final String ITEMS_ORDER_KEY = "items_order_key"; + private static final String HIDDEN_ITEMS_KEY = "hidden_items_key"; + private static final String CONFIGURE_MENU_ITEMS_TAG = "configure_menu_items_tag"; + private RearrangeMenuItemsAdapter rearrangeAdapter; private HashMap menuItemsOrder; + private ContextMenuAdapter contextMenuAdapter; private List hiddenMenuItems; - private MenuItemsAdapter adapter; + private LayoutInflater mInflater; + private OsmandApplication app; + private ScreenType screenType; + private boolean nightMode; + private boolean wasReset = false; @Override public void onSaveInstanceState(@NonNull Bundle outState) { super.onSaveInstanceState(outState); - outState.putSerializable(ITEM_TYPE_KEY, type); outState.putStringArrayList(HIDDEN_ITEMS_KEY, new ArrayList<>(hiddenMenuItems)); outState.putSerializable(ITEMS_ORDER_KEY, menuItemsOrder); + outState.putSerializable(ITEM_TYPE_KEY, screenType); } public static ConfigureMenuItemsFragment showInstance(@NonNull FragmentManager fm, @NonNull ScreenType type) { ConfigureMenuItemsFragment fragment = new ConfigureMenuItemsFragment(); - fragment.setType(type); + fragment.setScreenType(type); fm.beginTransaction() .replace(R.id.fragmentContainer, fragment, TAG) .addToBackStack(CONFIGURE_MENU_ITEMS_TAG) @@ -89,20 +88,31 @@ public class ConfigureMenuItemsFragment extends BaseOsmAndFragment { app = requireMyApplication(); nightMode = !app.getSettings().isLightContent(); mInflater = UiUtilities.getInflater(app, nightMode); - menuItemsManager = new MenuItemsManager(app); -// MapActivityActions mapActivityActions = new MapActivityActions(getActivity()) - + Activity activity = getActivity(); + if (activity instanceof MapActivity) { + switch (screenType) { + case DRAWER: + MapActivityActions mapActivityActions = new MapActivityActions((MapActivity) activity); + contextMenuAdapter = mapActivityActions.createMainOptionsMenu(); + break; + case CONFIGURE_MAP: + ConfigureMapMenu configureMapMenu = new ConfigureMapMenu(); + contextMenuAdapter = configureMapMenu.createListAdapter((MapActivity) activity); + break; + case CONTEXT_MENU_ACTIONS: + break; + } + } if (savedInstanceState != null && savedInstanceState.containsKey(ITEM_TYPE_KEY) && savedInstanceState.containsKey(HIDDEN_ITEMS_KEY) && savedInstanceState.containsKey(ITEMS_ORDER_KEY)) { - type = (ScreenType) savedInstanceState.getSerializable(ITEM_TYPE_KEY); + screenType = (ScreenType) savedInstanceState.getSerializable(ITEM_TYPE_KEY); hiddenMenuItems = savedInstanceState.getStringArrayList(HIDDEN_ITEMS_KEY); - menuItemsOrder = (HashMap) savedInstanceState.getSerializable(ITEMS_ORDER_KEY); + menuItemsOrder = (HashMap) savedInstanceState.getSerializable(ITEMS_ORDER_KEY); } else { - hiddenMenuItems = menuItemsManager.getHiddenItemsIds(type); -// TODO - menuItemsOrder = menuItemsManager.getDrawerItemsSavedOrder(); + hiddenMenuItems = getHiddenItemsIds(screenType); + menuItemsOrder = contextMenuAdapter.getMenuItemsOrder(getItemsIdsOrder(screenType)); } } @@ -121,16 +131,18 @@ public class ConfigureMenuItemsFragment extends BaseOsmAndFragment { ? getResources().getColor(R.color.text_color_primary_dark) : getResources().getColor(R.color.list_background_color_dark)); toolbarButton.setImageDrawable(getPaintedContentIcon(R.drawable.ic_arrow_back, getResources().getColor(R.color.text_color_secondary_light))); - toolbarTitle.setText(type.titleRes); + toolbarTitle.setText(screenType.titleRes); toolbarButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { showExitDialog(); } }); + + + rearrangeAdapter = new RearrangeMenuItemsAdapter(app, getAdapterItems()); recyclerView.setLayoutManager(new LinearLayoutManager(app)); - adapter = new MenuItemsAdapter(app, getAdapterItems()); - final ItemTouchHelper touchHelper = new ItemTouchHelper(new ReorderItemTouchHelperCallback(adapter)); + final ItemTouchHelper touchHelper = new ItemTouchHelper(new ReorderItemTouchHelperCallback(rearrangeAdapter)); touchHelper.attachToRecyclerView(recyclerView); MenuItemsAdapterListener listener = new MenuItemsAdapterListener() { private int fromPosition; @@ -146,32 +158,34 @@ public class ConfigureMenuItemsFragment extends BaseOsmAndFragment { public void onDragOrSwipeEnded(RecyclerView.ViewHolder holder) { toPosition = holder.getAdapterPosition(); if (toPosition >= 0 && fromPosition >= 0 && toPosition != fromPosition) { - adapter.notifyDataSetChanged(); + rearrangeAdapter.notifyDataSetChanged(); } } @Override public void onButtonClicked(int position) { - AdapterItem adapterItem = adapter.getItem(position); - if (adapterItem.getValue() instanceof MenuItemBase) { - MenuItemBase menuItemBase = (MenuItemBase) adapterItem.getValue(); - menuItemBase.toggleHidden(); + AdapterItem adapterItem = rearrangeAdapter.getItem(position); + if (adapterItem.getValue() instanceof ContextMenuItem) { + ContextMenuItem menuItemBase = (ContextMenuItem) adapterItem.getValue(); + menuItemBase.setHidden(!menuItemBase.isHidden()); if (menuItemBase.isHidden()) { hiddenMenuItems.add(menuItemBase.getId()); } else { hiddenMenuItems.remove(menuItemBase.getId()); } - adapter.updateItems(getAdapterItems()); + wasReset = false; + rearrangeAdapter.updateItems(getAdapterItems()); } } @Override public void onItemMoved(String id, int position) { menuItemsOrder.put(id, position); + wasReset = false; } }; - adapter.setListener(listener); - recyclerView.setAdapter(adapter); + rearrangeAdapter.setListener(listener); + recyclerView.setAdapter(rearrangeAdapter); View cancelButton = root.findViewById(R.id.dismiss_button); UiUtilities.setupDialogButton(nightMode, cancelButton, UiUtilities.DialogButtonType.SECONDARY, R.string.shared_string_cancel); cancelButton.setOnClickListener(new View.OnClickListener() { @@ -190,15 +204,18 @@ public class ConfigureMenuItemsFragment extends BaseOsmAndFragment { applyButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { - menuItemsManager.saveHiddenItemsIds(type, hiddenMenuItems); - List defaultItems; - defaultItems = menuItemsManager.getDrawerItemsDefault(); - menuItemsManager.reorderMenuItems(defaultItems, menuItemsOrder); - List ids = new ArrayList<>(); - for (MenuItemBase item : defaultItems) { - ids.add(item.getId()); + if (wasReset) { + resetMenuItems(screenType); + } else { + saveHiddenItemsIds(screenType, hiddenMenuItems); + List defItems = contextMenuAdapter.getDefaultItems(screenType); + contextMenuAdapter.reorderMenuItems(defItems, menuItemsOrder); + List ids = new ArrayList<>(); + for (ContextMenuItem item : defItems) { + ids.add(item.getId()); + } + saveItemsIdsOrder(screenType, ids); } - menuItemsManager.saveItemsIdsOrder(type, ids); dismissFragment(); } }); @@ -208,9 +225,40 @@ public class ConfigureMenuItemsFragment extends BaseOsmAndFragment { return root; } - @Override - public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { - super.onViewCreated(view, savedInstanceState); + private List getAdapterItems() { + List items = new ArrayList<>(); + items.add(new AdapterItem(DESCRIPTION, screenType)); + + List visible = contextMenuAdapter.getItemsForRearrangeAdapter(screenType, hiddenMenuItems, wasReset ? null : menuItemsOrder, false); + List hiddenItems = contextMenuAdapter.getItemsForRearrangeAdapter(screenType, hiddenMenuItems, wasReset ? null : menuItemsOrder, true); + items.addAll(visible); + if (!hiddenItems.isEmpty()) { + items.add(new AdapterItem(HEADER, new RearrangeMenuItemsAdapter.HeaderItem(R.string.shared_string_hidden, R.string.hidden_items_descr))); + items.addAll(hiddenItems); + } + items.add(new AdapterItem(DIVIDER, 1)); + items.add(new AdapterItem(BUTTON, new RearrangeMenuItemsAdapter.ButtonItem( + R.string.reset_to_default, + R.drawable.ic_action_reset_to_default_dark, + new View.OnClickListener() { + @Override + public void onClick(View view) { + hiddenMenuItems.clear(); + menuItemsOrder.clear(); + wasReset = true; + rearrangeAdapter.updateItems(getAdapterItems()); + } + }))); + items.add(new AdapterItem(BUTTON, new RearrangeMenuItemsAdapter.ButtonItem( + R.string.copy_from_other_profile, + R.drawable.ic_action_copy, + new View.OnClickListener() { + @Override + public void onClick(View view) { + + } + }))); + return items; } public void showExitDialog() { @@ -235,170 +283,74 @@ public class ConfigureMenuItemsFragment extends BaseOsmAndFragment { } } - public void setType(@NonNull ScreenType type) { - this.type = type; + private void setScreenType(@NonNull ScreenType screenType) { + this.screenType = screenType; } - private List getAdapterItems() { - List items = new ArrayList<>(); - items.add(new AdapterItem(DESCRIPTION, type)); + private void resetMenuItems(@NonNull ScreenType screenType) { + saveHiddenItemsIds(screenType, null); + saveItemsIdsOrder(screenType, null); + } + + @NonNull + private List getHiddenItemsIds(@NonNull ScreenType type) { + List hiddenItemsIds = null; switch (type) { case DRAWER: - items.addAll(getDrawerAdapterItems()); + hiddenItemsIds = app.getSettings().HIDDEN_DRAWER_ITEMS.getStringsList(); break; case CONFIGURE_MAP: - items.addAll(getConfigureMapAdapterItems()); + hiddenItemsIds = app.getSettings().HIDDEN_CONFIGURE_MAP_ITEMS.getStringsList(); break; case CONTEXT_MENU_ACTIONS: - items.addAll(getContextMenuActionsAdapterItems()); + hiddenItemsIds = app.getSettings().HIDDEN_CONTEXT_MENU_ACTIONS_ITEMS.getStringsList(); break; } - items.add(new AdapterItem(DIVIDER, 1)); - items.add(new AdapterItem(BUTTON, new ButtonItem( - R.string.reset_to_default, - R.drawable.ic_action_reset_to_default_dark, - new View.OnClickListener() { - @Override - public void onClick(View view) { - hiddenMenuItems.clear(); - menuItemsOrder.clear(); - menuItemsManager.resetMenuItems(type); - adapter.updateItems(getAdapterItems()); - } - }))); - items.add(new AdapterItem(BUTTON, new ButtonItem( - R.string.copy_from_other_profile, - R.drawable.ic_action_copy, - new View.OnClickListener() { - @Override - public void onClick(View view) { - //TODO - } - }))); - return items; + return hiddenItemsIds != null ? new ArrayList<>(hiddenItemsIds) : new ArrayList(); } - public List getDrawerItems(boolean hidden) { - - List items = new ArrayList<>(); - List drawerItems = menuItemsManager.getDrawerItemsDefault(); - - for (MenuItemBase menuItemBase : drawerItems) { - Integer order = menuItemsOrder.get(menuItemBase.getId()); - if (order == null) { - order = menuItemBase.getOrder(); - } - menuItemBase.setOrder(order); -// menuItemBase.setHidden(hiddenMenuItems.contains(menuItemBase.getId())); - + @NonNull + private List getItemsIdsOrder(@NonNull ScreenType type) { + List 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; } + return hiddenItemsIds != null ? new ArrayList<>(hiddenItemsIds) : new ArrayList(); + } - Collections.sort(drawerItems, new Comparator() { - @Override - public int compare(MenuItemBase item1, MenuItemBase item2) { - int order1 = item1.getOrder(); - int order2 = item2.getOrder(); - return (order1 < order2) ? -1 : ((order1 == order2) ? 0 : 1); - } - }); - - - for (MenuItemBase drawerItem : drawerItems) { - if (hidden && hiddenMenuItems.contains(drawerItem.getId())) { - drawerItem.setHidden(true); - items.add(drawerItem); - } else if (!hidden && !hiddenMenuItems.contains(drawerItem.getId())) { - drawerItem.setHidden(false); - items.add(drawerItem); - } + private void saveHiddenItemsIds(@NonNull ScreenType type, @Nullable List 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; } - return items; } - private List getDrawerAdapterItems() { - List active = convertToAdapterItems(getDrawerItems(false)); - List hidden = convertToAdapterItems(getDrawerItems(true)); - List items = new ArrayList<>(active); - if (!hidden.isEmpty()) { - items.add(new AdapterItem(HEADER, new HeaderItem( - R.string.shared_string_hidden, - R.string.hidden_items_descr, - false) - )); - items.addAll(hidden); + private void saveItemsIdsOrder(@NonNull ScreenType type, @Nullable List 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; } - return items; } - - private List convertToAdapterItems(List itemBaseList) { - List items = new ArrayList<>(); - for (MenuItemBase menuItem : itemBaseList) { - items.add(new AdapterItem(MENU_ITEM, menuItem)); - } - return items; - } - - private List getConfigureMapAdapterItems() { - List items = new ArrayList<>(); - items.add(new AdapterItem(HEADER, new HeaderItem( - R.string.main_actions, - R.string.main_actions_descr, - false) - )); - - items.add(new AdapterItem(HEADER, new HeaderItem( - R.string.additional_actions, - R.string.additional_actions_descr, - false) - )); - - items.add(new AdapterItem(HEADER, new HeaderItem( - R.string.shared_string_hidden, - R.string.hidden_items_descr, - false) - )); - - return items; - } - - private List getContextMenuActionsAdapterItems() { - List items = new ArrayList<>(); - items.add(new AdapterItem(HEADER, new HeaderItem( - R.string.shared_string_show, - R.string.move_inside_category, - false) - )); - - items.add(new AdapterItem(HEADER, new HeaderItem( - R.string.map_widget_map_rendering, - R.string.move_inside_category, - false) - )); - - items.add(new AdapterItem(HEADER, new HeaderItem( - R.string.shared_string_hidden, - R.string.reset_items_descr, - false) - )); - - return items; - } - -// private List getDrawerItems() { -// List items = new ArrayList<>(); -// -// return items; -// } - -// private List getConfigureMapItems() { -// List items = new ArrayList<>(); -// -// return items; -// } - -// private List getContextMenuActionsItems() { -// List items = new ArrayList<>(); -// -// return items; -// } } diff --git a/OsmAnd/src/net/osmand/plus/settings/ConfigureMenuRootFragment.java b/OsmAnd/src/net/osmand/plus/settings/ConfigureMenuRootFragment.java index 50e3bb2de8..238bb5ac9e 100644 --- a/OsmAnd/src/net/osmand/plus/settings/ConfigureMenuRootFragment.java +++ b/OsmAnd/src/net/osmand/plus/settings/ConfigureMenuRootFragment.java @@ -49,7 +49,6 @@ public class ConfigureMenuRootFragment extends BaseOsmAndFragment { private OsmandApplication app; private LayoutInflater mInflater; private boolean nightMode; - private MenuItemsManager menuItemsManager; @Override public void onCreate(Bundle savedInstanceState) { @@ -57,7 +56,6 @@ public class ConfigureMenuRootFragment extends BaseOsmAndFragment { app = requireMyApplication(); nightMode = !app.getSettings().isLightContent(); mInflater = UiUtilities.getInflater(app, nightMode); - menuItemsManager = new MenuItemsManager(app); } @Nullable @@ -204,18 +202,20 @@ public class ConfigureMenuRootFragment extends BaseOsmAndFragment { } private String getSubTitleText(ScreenType type) { - int allCount = 0; - int hiddenCount = 0; + List hiddenItems = null; switch (type) { case DRAWER: - allCount = menuItemsManager.getDrawerIdsDefaultOrder().size(); - hiddenCount = menuItemsManager.getHiddenItemsIds(type).size(); + hiddenItems = app.getSettings().HIDDEN_DRAWER_ITEMS.getStringsList(); break; case CONFIGURE_MAP: + hiddenItems = app.getSettings().HIDDEN_CONFIGURE_MAP_ITEMS.getStringsList(); break; case CONTEXT_MENU_ACTIONS: + hiddenItems = app.getSettings().HIDDEN_CONTEXT_MENU_ACTIONS_ITEMS.getStringsList(); break; } + int hiddenCount = hiddenItems != null ? hiddenItems.size() : 0; + int allCount = 0; 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); } diff --git a/OsmAnd/src/net/osmand/plus/settings/MenuItemsManager.java b/OsmAnd/src/net/osmand/plus/settings/MenuItemsManager.java deleted file mode 100644 index ba77cbbcab..0000000000 --- a/OsmAnd/src/net/osmand/plus/settings/MenuItemsManager.java +++ /dev/null @@ -1,184 +0,0 @@ -package net.osmand.plus.settings; - - -import androidx.annotation.NonNull; -import androidx.annotation.Nullable; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.Comparator; -import java.util.HashMap; -import java.util.LinkedHashMap; -import java.util.List; - -import net.osmand.plus.OsmandApplication; -import net.osmand.plus.R; -import net.osmand.plus.Version; -import net.osmand.plus.settings.MenuItemsAdapter.MenuItemBase; -import net.osmand.plus.settings.ConfigureMenuRootFragment.ScreenType; -import net.osmand.plus.OsmandSettings.ListStringPreference; - -import static net.osmand.aidlapi.OsmAndCustomizationConstants.DRAWER_BUILDS_ID; -import static net.osmand.aidlapi.OsmAndCustomizationConstants.DRAWER_CONFIGURE_MAP_ID; -import static net.osmand.aidlapi.OsmAndCustomizationConstants.DRAWER_CONFIGURE_SCREEN_ID; -import static net.osmand.aidlapi.OsmAndCustomizationConstants.DRAWER_DASHBOARD_ID; -import static net.osmand.aidlapi.OsmAndCustomizationConstants.DRAWER_DIRECTIONS_ID; -import static net.osmand.aidlapi.OsmAndCustomizationConstants.DRAWER_DIVIDER_ID; -import static net.osmand.aidlapi.OsmAndCustomizationConstants.DRAWER_DOWNLOAD_MAPS_ID; -import static net.osmand.aidlapi.OsmAndCustomizationConstants.DRAWER_HELP_ID; -import static net.osmand.aidlapi.OsmAndCustomizationConstants.DRAWER_MAP_MARKERS_ID; -import static net.osmand.aidlapi.OsmAndCustomizationConstants.DRAWER_MEASURE_DISTANCE_ID; -import static net.osmand.aidlapi.OsmAndCustomizationConstants.DRAWER_MY_PLACES_ID; -import static net.osmand.aidlapi.OsmAndCustomizationConstants.DRAWER_OSMAND_LIVE_ID; -import static net.osmand.aidlapi.OsmAndCustomizationConstants.DRAWER_PLUGINS_ID; -import static net.osmand.aidlapi.OsmAndCustomizationConstants.DRAWER_SEARCH_ID; -import static net.osmand.aidlapi.OsmAndCustomizationConstants.DRAWER_SETTINGS_ID; -import static net.osmand.aidlapi.OsmAndCustomizationConstants.DRAWER_TRAVEL_GUIDES_ID; -import static net.osmand.aidlapi.OsmAndCustomizationConstants.MAP_CONTEXT_MENU_ADD_GPX_WAYPOINT; -import static net.osmand.aidlapi.OsmAndCustomizationConstants.MAP_CONTEXT_MENU_AVOID_ROAD; -import static net.osmand.aidlapi.OsmAndCustomizationConstants.MAP_CONTEXT_MENU_CHANGE_MARKER_POSITION; -import static net.osmand.aidlapi.OsmAndCustomizationConstants.MAP_CONTEXT_MENU_CREATE_POI; -import static net.osmand.aidlapi.OsmAndCustomizationConstants.MAP_CONTEXT_MENU_DIRECTIONS_FROM_ID; -import static net.osmand.aidlapi.OsmAndCustomizationConstants.MAP_CONTEXT_MENU_DOWNLOAD_MAP; -import static net.osmand.aidlapi.OsmAndCustomizationConstants.MAP_CONTEXT_MENU_EDIT_GPX_WP; -import static net.osmand.aidlapi.OsmAndCustomizationConstants.MAP_CONTEXT_MENU_MARK_AS_PARKING_LOC; -import static net.osmand.aidlapi.OsmAndCustomizationConstants.MAP_CONTEXT_MENU_MEASURE_DISTANCE; -import static net.osmand.aidlapi.OsmAndCustomizationConstants.MAP_CONTEXT_MENU_MODIFY_OSM_CHANGE; -import static net.osmand.aidlapi.OsmAndCustomizationConstants.MAP_CONTEXT_MENU_MODIFY_OSM_NOTE; -import static net.osmand.aidlapi.OsmAndCustomizationConstants.MAP_CONTEXT_MENU_MODIFY_POI; -import static net.osmand.aidlapi.OsmAndCustomizationConstants.MAP_CONTEXT_MENU_OPEN_OSM_NOTE; -import static net.osmand.aidlapi.OsmAndCustomizationConstants.MAP_CONTEXT_MENU_SEARCH_NEARBY; -import static net.osmand.aidlapi.OsmAndCustomizationConstants.MAP_CONTEXT_MENU_UPDATE_MAP; - -public class MenuItemsManager { - - private OsmandApplication app; - - public MenuItemsManager(OsmandApplication app) { - this.app = app; - } - - List getDrawerItemsDefault() { - List items = new ArrayList<>(); - items.add(new MenuItemBase(DRAWER_DASHBOARD_ID, R.string.home, R.string.app_name_osmand, R.drawable.map_dashboard, 0, false)); - items.add(new MenuItemBase(DRAWER_MAP_MARKERS_ID, R.string.map_markers, R.string.app_name_osmand, R.drawable.ic_action_flag_dark, 1, false)); - items.add(new MenuItemBase(DRAWER_MY_PLACES_ID, R.string.shared_string_my_places, R.string.app_name_osmand, R.drawable.ic_action_fav_dark, 2, false)); - items.add(new MenuItemBase(DRAWER_SEARCH_ID, R.string.shared_string_search, R.string.app_name_osmand, R.drawable.ic_action_search_dark, 3, false)); - items.add(new MenuItemBase(DRAWER_DIRECTIONS_ID, R.string.get_directions, R.string.app_name_osmand, R.drawable.ic_action_gdirections_dark, 4, false)); - items.add(new MenuItemBase(DRAWER_CONFIGURE_MAP_ID, R.string.configure_map, R.string.app_name_osmand, R.drawable.ic_action_layers, 5, false)); - items.add(new MenuItemBase(DRAWER_DOWNLOAD_MAPS_ID, R.string.shared_string_download_map, R.string.app_name_osmand, R.drawable.ic_type_archive, 6, false)); - items.add(new MenuItemBase(DRAWER_OSMAND_LIVE_ID, R.string.osm_live, R.string.app_name_osmand, R.drawable.ic_action_osm_live, 7, false)); - items.add(new MenuItemBase(DRAWER_TRAVEL_GUIDES_ID, R.string.wikivoyage_travel_guide, R.string.app_name_osmand, R.drawable.ic_action_travel, 8, false)); - items.add(new MenuItemBase(DRAWER_MEASURE_DISTANCE_ID, R.string.measurement_tool, R.string.app_name_osmand, R.drawable.ic_action_ruler, 9, false)); - items.add(new MenuItemBase(DRAWER_DIVIDER_ID, R.string.shared_string_divider, R.string.divider_descr, 0, 10, false)); - items.add(new MenuItemBase(DRAWER_CONFIGURE_SCREEN_ID, R.string.layer_map_appearance, R.string.app_name_osmand, R.drawable.ic_configure_screen_dark, 11, false)); - items.add(new MenuItemBase(DRAWER_PLUGINS_ID, R.string.plugins_screen, R.string.app_name_osmand, R.drawable.ic_extension_dark, 12, false)); - items.add(new MenuItemBase(DRAWER_SETTINGS_ID + ".new", R.string.shared_string_settings, R.string.app_name_osmand, R.drawable.ic_action_settings, 13, false)); - items.add(new MenuItemBase(DRAWER_HELP_ID, R.string.shared_string_help, R.string.app_name_osmand, R.drawable.ic_action_help, 14, false)); - if (Version.isDeveloperVersion(app)) { - items.add(new MenuItemBase(DRAWER_BUILDS_ID, R.string.version_settings, R.string.developer_plugin, R.drawable.ic_action_gabout_dark, 15, false)); - } - return items; - } - - @NonNull - public List getHiddenItemsIds(@NonNull ScreenType type) { - List 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; - } - return hiddenItemsIds != null ? hiddenItemsIds : new ArrayList(); - } - - public void saveHiddenItemsIds(@NonNull ScreenType type, @Nullable List 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 ScreenType type, @Nullable List itemsIdsOrder) { - switch (type) { - case DRAWER: - app.getSettings().DRAWER_ITEMS_ORDER.setStringsList(itemsIdsOrder); - break; - case CONFIGURE_MAP: - break; - case CONTEXT_MENU_ACTIONS: - break; - } - } - - public void reorderMenuItems(@NonNull List defaultItems, @NonNull HashMap itemsOrder) { - for (MenuItemBase item : defaultItems) { - Integer order = itemsOrder.get(item.getId()); - if (order != null) { - item.setOrder(order); - } - } - Collections.sort(defaultItems, new Comparator() { - @Override - public int compare(MenuItemBase item1, MenuItemBase item2) { - int order1 = item1.getOrder(); - int order2 = item2.getOrder(); - return (order1 < order2) ? -1 : ((order1 == order2) ? 0 : 1); - } - }); - } - - public void resetMenuItems(@NonNull ScreenType type) { - switch (type) { - case DRAWER: - app.getSettings().DRAWER_ITEMS_ORDER.setStringsList(null); - app.getSettings().HIDDEN_DRAWER_ITEMS.setStringsList(null); - break; - case CONFIGURE_MAP: - break; - case CONTEXT_MENU_ACTIONS: - break; - } - } - - public List getDrawerIdsDefaultOrder() { - return getMenuItemsIdsDefaultOrder(getDrawerItemsDefault()); - } - - public LinkedHashMap getDrawerItemsSavedOrder() { - return getMenuItemsOrder(app.getSettings().DRAWER_ITEMS_ORDER); - } - - private List getMenuItemsIdsDefaultOrder(List items) { - List itemsIds = new ArrayList<>(); - for (MenuItemBase item : items) { - itemsIds.add(item.getId()); - } - return itemsIds; - } - - private LinkedHashMap getMenuItemsOrder(ListStringPreference preference) { - List ids = preference.getStringsList(); - if (ids == null) { - ids = getDrawerIdsDefaultOrder(); - } - LinkedHashMap result = new LinkedHashMap<>(); - for (int i = 0; i < ids.size(); i++) { - result.put(ids.get(i), i); - } - return result; - } -} diff --git a/OsmAnd/src/net/osmand/plus/settings/MenuItemsAdapter.java b/OsmAnd/src/net/osmand/plus/settings/RearrangeMenuItemsAdapter.java similarity index 76% rename from OsmAnd/src/net/osmand/plus/settings/MenuItemsAdapter.java rename to OsmAnd/src/net/osmand/plus/settings/RearrangeMenuItemsAdapter.java index c8a0dbc0db..ec90689e9d 100644 --- a/OsmAnd/src/net/osmand/plus/settings/MenuItemsAdapter.java +++ b/OsmAnd/src/net/osmand/plus/settings/RearrangeMenuItemsAdapter.java @@ -17,6 +17,7 @@ import androidx.core.view.MotionEventCompat; import androidx.recyclerview.widget.RecyclerView; import net.osmand.AndroidUtils; +import net.osmand.plus.ContextMenuItem; import net.osmand.plus.OsmandApplication; import net.osmand.plus.R; import net.osmand.plus.UiUtilities; @@ -28,8 +29,12 @@ import java.util.Collections; import java.util.List; import static net.osmand.aidlapi.OsmAndCustomizationConstants.DRAWER_DIVIDER_ID; +import static net.osmand.aidlapi.OsmAndCustomizationConstants.MAP_RENDERING_CATEGORY_ID; +import static net.osmand.aidlapi.OsmAndCustomizationConstants.RENDERING_ITEMS_ID_SCHEME; +import static net.osmand.aidlapi.OsmAndCustomizationConstants.SHOW_CATEGORY_ID; +import static net.osmand.aidlapi.OsmAndCustomizationConstants.SHOW_ITEMS_ID_SCHEME; -public class MenuItemsAdapter extends RecyclerView.Adapter +public class RearrangeMenuItemsAdapter extends RecyclerView.Adapter implements ReorderItemTouchHelperCallback.OnItemMoveCallback { private OsmandApplication app; @@ -40,8 +45,8 @@ public class MenuItemsAdapter extends RecyclerView.Adapter items) { + public RearrangeMenuItemsAdapter(OsmandApplication app, + List items) { this.app = app; this.items = items; uiUtilities = app.getUIUtilities(); @@ -95,16 +100,34 @@ public class MenuItemsAdapter extends RecyclerView.Adapter