refactor ContextMenuItemsPreference

This commit is contained in:
veliymolfar 2020-04-14 19:33:49 +03:00
parent 0f8c8d8905
commit 286ad614c6
2 changed files with 24 additions and 60 deletions

View file

@ -24,7 +24,6 @@ import androidx.annotation.DrawableRes;
import androidx.annotation.IdRes; import androidx.annotation.IdRes;
import androidx.annotation.LayoutRes; import androidx.annotation.LayoutRes;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatImageView; import androidx.appcompat.widget.AppCompatImageView;
import androidx.core.content.ContextCompat; import androidx.core.content.ContextCompat;
import androidx.fragment.app.FragmentActivity; 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.ConfigureMapMenu;
import net.osmand.plus.dialogs.HelpArticleDialogFragment; import net.osmand.plus.dialogs.HelpArticleDialogFragment;
import net.osmand.plus.helpers.AndroidUiHelper; import net.osmand.plus.helpers.AndroidUiHelper;
import net.osmand.plus.settings.RearrangeMenuItemsAdapter.AdapterItem;
import net.osmand.plus.OsmandSettings.ContextMenuItemsPreference; import net.osmand.plus.OsmandSettings.ContextMenuItemsPreference;
import net.osmand.util.Algorithms; import net.osmand.util.Algorithms;
@ -46,7 +44,6 @@ import java.io.File;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.Comparator; import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator; import java.util.Iterator;
import java.util.LinkedHashSet; import java.util.LinkedHashSet;
import java.util.List; 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.CONFIGURE_MAP_ITEM_ID_SCHEME;
import static net.osmand.aidlapi.OsmAndCustomizationConstants.DRAWER_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.aidlapi.OsmAndCustomizationConstants.MAP_CONTEXT_MENU_ACTIONS;
import static net.osmand.plus.settings.RearrangeMenuItemsAdapter.AdapterItemType.MENU_ITEM;
public class ContextMenuAdapter { public class ContextMenuAdapter {
private static final Log LOG = PlatformUtil.getLog(ContextMenuAdapter.class); private static final Log LOG = PlatformUtil.getLog(ContextMenuAdapter.class);
@ -74,8 +70,6 @@ public class ContextMenuAdapter {
private boolean nightMode; private boolean nightMode;
private ConfigureMapMenu.OnClickListener changeAppModeListener = null; private ConfigureMapMenu.OnClickListener changeAppModeListener = null;
private OsmandApplication app; private OsmandApplication app;
private HashMap<String, Integer> ordersMap;
private ContextMenuItemsPreference contextMenuItemsPreference;
public ContextMenuAdapter(OsmandApplication app) { public ContextMenuAdapter(OsmandApplication app) {
this.app = app; this.app = app;
@ -98,7 +92,7 @@ public class ContextMenuAdapter {
String id = item.getId(); String id = item.getId();
if (id != null) { if (id != null) {
item.setHidden(isItemHidden(id)); item.setHidden(isItemHidden(id));
item.setOrder(getItemCustomOrder(id, item.getOrder())); item.setOrder(getItemOrder(id, item.getOrder()));
} }
if (item.isHidden()) { if (item.isHidden()) {
hiddenItems.add(item); hiddenItems.add(item);
@ -166,12 +160,10 @@ public class ContextMenuAdapter {
} }
private boolean isItemHidden(@NonNull String id) { private boolean isItemHidden(@NonNull String id) {
if (contextMenuItemsPreference == null) { ContextMenuItemsPreference contextMenuItemsPreference = app.getSettings().getContextMenuItemsPreference(id);
contextMenuItemsPreference = getPreference(id);
if (contextMenuItemsPreference == null) { if (contextMenuItemsPreference == null) {
return false; return false;
} }
}
List<String> hiddenIds = contextMenuItemsPreference.getHiddenIds(); List<String> hiddenIds = contextMenuItemsPreference.getHiddenIds();
if (!Algorithms.isEmpty(hiddenIds)) { if (!Algorithms.isEmpty(hiddenIds)) {
return hiddenIds.contains(id); return hiddenIds.contains(id);
@ -179,23 +171,15 @@ public class ContextMenuAdapter {
return false; return false;
} }
private int getItemCustomOrder(@NonNull String id, int defaultOrder) { private int getItemOrder(@NonNull String id, int defaultOrder) {
if (contextMenuItemsPreference == null) { ContextMenuItemsPreference contextMenuItemsPreference = app.getSettings().getContextMenuItemsPreference(id);
contextMenuItemsPreference = getPreference(id);
if (contextMenuItemsPreference == null) { if (contextMenuItemsPreference == null) {
return defaultOrder; return defaultOrder;
} }
}
List<String> orderIds = contextMenuItemsPreference.getOrderIds(); List<String> orderIds = contextMenuItemsPreference.getOrderIds();
if (!Algorithms.isEmpty(orderIds)) { if (!Algorithms.isEmpty(orderIds)) {
if (ordersMap == null) { int order = orderIds.indexOf(id);
ordersMap = new HashMap<>(); if (order != -1) {
for (int i = 0; i < orderIds.size(); i++) {
ordersMap.put(orderIds.get(i), i);
}
}
Integer order = ordersMap.get(id);
if (order != null) {
return order; return order;
} }
} }
@ -642,17 +626,4 @@ public class ContextMenuAdapter {
} }
return idScheme; 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;
}
} }

View file

@ -1132,9 +1132,6 @@ public class OsmandSettings {
} }
public class ContextMenuItemsPreference extends StringPreference { 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 HIDDEN = "hidden";
public static final String ORDER = "order"; public static final String ORDER = "order";
private List<String> hiddenIds; private List<String> hiddenIds;
@ -1142,9 +1139,9 @@ public class OsmandSettings {
private Object cachedPreference; private Object cachedPreference;
private String idScheme; private String idScheme;
private ContextMenuItemsPreference(String id, String defaultValue) { private ContextMenuItemsPreference(String id, String idScheme) {
super(id, defaultValue); super(id, "");
idScheme = getIdScheme(id); this.idScheme = idScheme;
} }
private void readValues() { 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<String> hidden, List<String> order, String id) { public String convertToJsonString(List<String> hidden, List<String> order, String id) {
try { try {
JSONObject json = new JSONObject(); JSONObject json = new JSONObject();
@ -3556,13 +3537,25 @@ 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(ContextMenuItemsPreference.DRAWER_ITEMS_KEY, null).makeProfile().cache(); (ContextMenuItemsPreference) new ContextMenuItemsPreference("drawer_items", DRAWER_ITEM_ID_SCHEME).makeProfile().cache();
public final ContextMenuItemsPreference CONFIGURE_MAP_ITEMS = 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 = 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<ContextMenuItemsPreference> 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"; public static final String VOICE_PROVIDER_NOT_USE = "VOICE_PROVIDER_NOT_USE";