From 286ad614c6f2ab69bde5a2f11090e0e2c42baf12 Mon Sep 17 00:00:00 2001 From: veliymolfar Date: Tue, 14 Apr 2020 19:33:49 +0300 Subject: [PATCH] refactor ContextMenuItemsPreference --- .../net/osmand/plus/ContextMenuAdapter.java | 41 +++--------------- .../src/net/osmand/plus/OsmandSettings.java | 43 ++++++++----------- 2 files changed, 24 insertions(+), 60 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/ContextMenuAdapter.java b/OsmAnd/src/net/osmand/plus/ContextMenuAdapter.java index e6ba0a0b54..2c65976608 100644 --- a/OsmAnd/src/net/osmand/plus/ContextMenuAdapter.java +++ b/OsmAnd/src/net/osmand/plus/ContextMenuAdapter.java @@ -24,7 +24,6 @@ import androidx.annotation.DrawableRes; import androidx.annotation.IdRes; import androidx.annotation.LayoutRes; import androidx.annotation.NonNull; -import androidx.annotation.Nullable; import androidx.appcompat.widget.AppCompatImageView; import androidx.core.content.ContextCompat; import androidx.fragment.app.FragmentActivity; @@ -36,7 +35,6 @@ import net.osmand.plus.activities.actions.AppModeDialog; 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; @@ -46,7 +44,6 @@ import java.io.File; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; -import java.util.HashMap; import java.util.Iterator; import java.util.LinkedHashSet; import java.util.List; @@ -56,7 +53,6 @@ import static net.osmand.aidlapi.OsmAndCustomizationConstants.APP_PROFILES_ID; import static net.osmand.aidlapi.OsmAndCustomizationConstants.CONFIGURE_MAP_ITEM_ID_SCHEME; import static net.osmand.aidlapi.OsmAndCustomizationConstants.DRAWER_ITEM_ID_SCHEME; import static net.osmand.aidlapi.OsmAndCustomizationConstants.MAP_CONTEXT_MENU_ACTIONS; -import static net.osmand.plus.settings.RearrangeMenuItemsAdapter.AdapterItemType.MENU_ITEM; public class ContextMenuAdapter { private static final Log LOG = PlatformUtil.getLog(ContextMenuAdapter.class); @@ -74,8 +70,6 @@ public class ContextMenuAdapter { private boolean nightMode; private ConfigureMapMenu.OnClickListener changeAppModeListener = null; private OsmandApplication app; - private HashMap ordersMap; - private ContextMenuItemsPreference contextMenuItemsPreference; public ContextMenuAdapter(OsmandApplication app) { this.app = app; @@ -98,7 +92,7 @@ public class ContextMenuAdapter { String id = item.getId(); if (id != null) { item.setHidden(isItemHidden(id)); - item.setOrder(getItemCustomOrder(id, item.getOrder())); + item.setOrder(getItemOrder(id, item.getOrder())); } if (item.isHidden()) { hiddenItems.add(item); @@ -166,12 +160,10 @@ public class ContextMenuAdapter { } private boolean isItemHidden(@NonNull String id) { - if (contextMenuItemsPreference == null) { - contextMenuItemsPreference = getPreference(id); + ContextMenuItemsPreference contextMenuItemsPreference = app.getSettings().getContextMenuItemsPreference(id); if (contextMenuItemsPreference == null) { return false; } - } List hiddenIds = contextMenuItemsPreference.getHiddenIds(); if (!Algorithms.isEmpty(hiddenIds)) { return hiddenIds.contains(id); @@ -179,23 +171,15 @@ public class ContextMenuAdapter { return false; } - private int getItemCustomOrder(@NonNull String id, int defaultOrder) { - if (contextMenuItemsPreference == null) { - contextMenuItemsPreference = getPreference(id); + private int getItemOrder(@NonNull String id, int defaultOrder) { + ContextMenuItemsPreference contextMenuItemsPreference = app.getSettings().getContextMenuItemsPreference(id); if (contextMenuItemsPreference == null) { return defaultOrder; } - } List orderIds = contextMenuItemsPreference.getOrderIds(); if (!Algorithms.isEmpty(orderIds)) { - if (ordersMap == null) { - ordersMap = new HashMap<>(); - for (int i = 0; i < orderIds.size(); i++) { - ordersMap.put(orderIds.get(i), i); - } - } - Integer order = ordersMap.get(id); - if (order != null) { + int order = orderIds.indexOf(id); + if (order != -1) { return order; } } @@ -642,17 +626,4 @@ public class ContextMenuAdapter { } return idScheme; } - - private ContextMenuItemsPreference getPreference(@NonNull String id) { - if (app != null) { - if (id.startsWith(DRAWER_ITEM_ID_SCHEME)) { - return app.getSettings().DRAWER_ITEMS; - } else if (id.startsWith(CONFIGURE_MAP_ITEM_ID_SCHEME)) { - return app.getSettings().CONFIGURE_MAP_ITEMS; - } else if (id.startsWith(MAP_CONTEXT_MENU_ACTIONS)) { - return app.getSettings().CONTEXT_MENU_ACTIONS_ITEMS; - } - } - return null; - } } \ No newline at end of file diff --git a/OsmAnd/src/net/osmand/plus/OsmandSettings.java b/OsmAnd/src/net/osmand/plus/OsmandSettings.java index 855c17b5f1..cf08deae47 100644 --- a/OsmAnd/src/net/osmand/plus/OsmandSettings.java +++ b/OsmAnd/src/net/osmand/plus/OsmandSettings.java @@ -1132,9 +1132,6 @@ public class OsmandSettings { } public class ContextMenuItemsPreference extends StringPreference { - public static final String DRAWER_ITEMS_KEY = "drawer_items"; - public static final String CONTEXT_MENU_ITEMS_KEY = "context_menu_items"; - public static final String CONFIGURE_MAP_ITEMS_KEY = "configure_map_items"; public static final String HIDDEN = "hidden"; public static final String ORDER = "order"; private List hiddenIds; @@ -1142,9 +1139,9 @@ public class OsmandSettings { private Object cachedPreference; private String idScheme; - private ContextMenuItemsPreference(String id, String defaultValue) { - super(id, defaultValue); - idScheme = getIdScheme(id); + private ContextMenuItemsPreference(String id, String idScheme) { + super(id, ""); + this.idScheme = idScheme; } private void readValues() { @@ -1174,22 +1171,6 @@ public class OsmandSettings { } } - private String getIdScheme(String id) { - String idScheme = ""; - switch (id) { - case DRAWER_ITEMS_KEY: - idScheme = DRAWER_ITEM_ID_SCHEME; - break; - case CONFIGURE_MAP_ITEMS_KEY: - idScheme = CONFIGURE_MAP_ITEM_ID_SCHEME; - break; - case CONTEXT_MENU_ITEMS_KEY: - idScheme = MAP_CONTEXT_MENU_ACTIONS; - break; - } - return idScheme; - } - public String convertToJsonString(List hidden, List order, String id) { try { JSONObject json = new JSONObject(); @@ -3556,13 +3537,25 @@ public class OsmandSettings { new ListStringPreference("inactive_poi_filters", null, ",,").makeProfile().cache(); public final ContextMenuItemsPreference DRAWER_ITEMS = - (ContextMenuItemsPreference) new ContextMenuItemsPreference(ContextMenuItemsPreference.DRAWER_ITEMS_KEY, null).makeProfile().cache(); + (ContextMenuItemsPreference) new ContextMenuItemsPreference("drawer_items", DRAWER_ITEM_ID_SCHEME).makeProfile().cache(); public final ContextMenuItemsPreference CONFIGURE_MAP_ITEMS = - (ContextMenuItemsPreference) new ContextMenuItemsPreference(ContextMenuItemsPreference.CONFIGURE_MAP_ITEMS_KEY, null).makeProfile().cache(); + (ContextMenuItemsPreference) new ContextMenuItemsPreference("context_menu_items", CONFIGURE_MAP_ITEM_ID_SCHEME).makeProfile().cache(); public final ContextMenuItemsPreference CONTEXT_MENU_ACTIONS_ITEMS = - (ContextMenuItemsPreference) new ContextMenuItemsPreference(ContextMenuItemsPreference.CONTEXT_MENU_ITEMS_KEY, null).makeProfile().cache(); + (ContextMenuItemsPreference) new ContextMenuItemsPreference("configure_map_items", MAP_CONTEXT_MENU_ACTIONS).makeProfile().cache(); + + public final List contextMenuItemsPreferences = Arrays.asList(DRAWER_ITEMS, CONFIGURE_MAP_ITEMS, CONTEXT_MENU_ACTIONS_ITEMS); + + @Nullable + public ContextMenuItemsPreference getContextMenuItemsPreference(@NonNull String id) { + for (ContextMenuItemsPreference preference : contextMenuItemsPreferences) { + if (id.startsWith(preference.idScheme)) { + return preference; + } + } + return null; + } public static final String VOICE_PROVIDER_NOT_USE = "VOICE_PROVIDER_NOT_USE";