From b85b562cb6ab93efbfddf45e1e46481239bb77db Mon Sep 17 00:00:00 2001 From: veliymolfar Date: Wed, 15 Apr 2020 14:05:32 +0300 Subject: [PATCH] refactor --- .../net/osmand/plus/ContextMenuAdapter.java | 41 ++----- .../src/net/osmand/plus/OsmandSettings.java | 105 ++++++++++++------ .../settings/ConfigureMenuItemsFragment.java | 25 ++--- .../settings/ConfigureMenuRootFragment.java | 4 +- 4 files changed, 92 insertions(+), 83 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/ContextMenuAdapter.java b/OsmAnd/src/net/osmand/plus/ContextMenuAdapter.java index 2c65976608..ed2bf456f6 100644 --- a/OsmAnd/src/net/osmand/plus/ContextMenuAdapter.java +++ b/OsmAnd/src/net/osmand/plus/ContextMenuAdapter.java @@ -44,15 +44,11 @@ import java.io.File; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; -import java.util.Iterator; import java.util.LinkedHashSet; import java.util.List; import java.util.Set; 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; public class ContextMenuAdapter { private static final Log LOG = PlatformUtil.getLog(ContextMenuAdapter.class); @@ -65,7 +61,6 @@ public class ContextMenuAdapter { @LayoutRes private int DEFAULT_LAYOUT_ID = R.layout.list_menu_item_native; List items = new ArrayList<>(); - List hiddenItems = new ArrayList<>(); private boolean profileDependent = false; private boolean nightMode; private ConfigureMapMenu.OnClickListener changeAppModeListener = null; @@ -94,11 +89,7 @@ public class ContextMenuAdapter { item.setHidden(isItemHidden(id)); item.setOrder(getItemOrder(id, item.getOrder())); } - if (item.isHidden()) { - hiddenItems.add(item); - } else { - items.add(item.getPos(), item); - } + items.add(item.getPos(), item); sortItemsByOrder(); } catch (IndexOutOfBoundsException ex) { items.add(item); @@ -164,7 +155,7 @@ public class ContextMenuAdapter { if (contextMenuItemsPreference == null) { return false; } - List hiddenIds = contextMenuItemsPreference.getHiddenIds(); + List hiddenIds = contextMenuItemsPreference.get().getHiddenIds(); if (!Algorithms.isEmpty(hiddenIds)) { return hiddenIds.contains(id); } @@ -176,7 +167,7 @@ public class ContextMenuAdapter { if (contextMenuItemsPreference == null) { return defaultOrder; } - List orderIds = contextMenuItemsPreference.getOrderIds(); + List orderIds = contextMenuItemsPreference.get().getOrderIds(); if (!Algorithms.isEmpty(orderIds)) { int order = orderIds.indexOf(id); if (order != -1) { @@ -190,12 +181,14 @@ public class ContextMenuAdapter { final int layoutId = DEFAULT_LAYOUT_ID; final OsmandApplication app = ((OsmandApplication) activity.getApplication()); final OsmAndAppCustomization customization = app.getAppCustomization(); - for (Iterator iterator = items.iterator(); iterator.hasNext(); ) { - String id = iterator.next().getId(); - if (!TextUtils.isEmpty(id) && !customization.isFeatureEnabled(id)) { - iterator.remove(); + List itemsToRemove = new ArrayList<>(); + for (ContextMenuItem item : items) { + String id = item.getId(); + if (item.isHidden() || !TextUtils.isEmpty(id) && !customization.isFeatureEnabled(id)) { + itemsToRemove.add(item); } } + items.removeAll(itemsToRemove); return new ContextMenuArrayAdapter(activity, layoutId, R.id.title, items.toArray(new ContextMenuItem[items.size()]), app, lightTheme, changeAppModeListener); } @@ -603,24 +596,14 @@ public class ContextMenuAdapter { return items; } - public List getHiddenItems() { - return hiddenItems; - } - private String getIdScheme() { String idScheme = ""; for (ContextMenuItem item : items) { String id = item.getId(); if (id != null) { - if (id.startsWith(DRAWER_ITEM_ID_SCHEME)) { - idScheme = DRAWER_ITEM_ID_SCHEME; - break; - } else if (id.startsWith(CONFIGURE_MAP_ITEM_ID_SCHEME)) { - idScheme = CONFIGURE_MAP_ITEM_ID_SCHEME; - break; - } else if (id.startsWith(MAP_CONTEXT_MENU_ACTIONS)) { - idScheme = MAP_CONTEXT_MENU_ACTIONS; - break; + ContextMenuItemsPreference pref = app.getSettings().getContextMenuItemsPreference(id); + if (pref != null) { + return pref.getIdScheme(); } } } diff --git a/OsmAnd/src/net/osmand/plus/OsmandSettings.java b/OsmAnd/src/net/osmand/plus/OsmandSettings.java index cf08deae47..2d67f94d29 100644 --- a/OsmAnd/src/net/osmand/plus/OsmandSettings.java +++ b/OsmAnd/src/net/osmand/plus/OsmandSettings.java @@ -64,6 +64,7 @@ import org.json.JSONObject; import java.io.File; import java.io.IOException; +import java.io.Serializable; import java.lang.ref.WeakReference; import java.lang.reflect.Type; import java.util.ArrayList; @@ -451,6 +452,10 @@ public class OsmandSettings { } return false; } + } else if (preference instanceof ContextMenuItemsPreference) { + if (value instanceof ContextMenuItemsSettings) { + ((ContextMenuItemsPreference) preference).setModeValue(mode, (ContextMenuItemsSettings) value); + } } } return false; @@ -1131,57 +1136,87 @@ public class OsmandSettings { } } - public class ContextMenuItemsPreference extends StringPreference { - public static final String HIDDEN = "hidden"; - public static final String ORDER = "order"; - private List hiddenIds; - private List orderIds; - private Object cachedPreference; + public class ContextMenuItemsPreference extends CommonPreference { + @NonNull private String idScheme; - private ContextMenuItemsPreference(String id, String idScheme) { - super(id, ""); + private ContextMenuItemsPreference(String id, @NonNull String idScheme) { + super(id, new ContextMenuItemsSettings()); this.idScheme = idScheme; } - private void readValues() { - hiddenIds = new ArrayList<>(); - orderIds = new ArrayList<>(); - cachedPreference = getPreferences(); - String jsonString = get(); + @Override + protected ContextMenuItemsSettings getValue(Object prefs, ContextMenuItemsSettings defaultValue) { + String s = settingsAPI.getString(prefs, getId(), ""); + return readValue(s); + } + + @Override + protected boolean setValue(Object prefs, ContextMenuItemsSettings val) { + return settingsAPI.edit(prefs).putString(getId(), val.writeToJsonString(idScheme)).commit(); + } + + @Override + public ContextMenuItemsSettings parseString(String s) { + return readValue(s); + } + + private ContextMenuItemsSettings readValue(String s) { + ContextMenuItemsSettings value = new ContextMenuItemsSettings(); + value.readFromJsonString(s, idScheme); + return value; + } + + @NonNull + public String getIdScheme() { + return idScheme; + } + } + + public static class ContextMenuItemsSettings implements Serializable { + public static final String HIDDEN = "hidden"; + public static final String ORDER = "order"; + private List hiddenIds = new ArrayList<>(); + private List orderIds = new ArrayList<>(); + + public ContextMenuItemsSettings() { + + } + + public ContextMenuItemsSettings(@NonNull List hiddenIds, @NonNull List orderIds) { + this.hiddenIds = hiddenIds; + this.orderIds = orderIds; + } + + public void readFromJsonString(String jsonString, @NonNull String idScheme) { if (Algorithms.isEmpty(jsonString)) { return; } try { JSONObject json = new JSONObject(jsonString); - JSONObject items = json.optJSONObject(getId()); - populateIdsList(items.optJSONArray(HIDDEN), hiddenIds); - populateIdsList(items.optJSONArray(ORDER), orderIds); + hiddenIds = readIdsList(json.optJSONArray(HIDDEN), idScheme); + orderIds = readIdsList(json.optJSONArray(ORDER), idScheme); } catch (JSONException e) { LOG.error("Error converting to json string: " + e); } } - private void populateIdsList(JSONArray jsonArray, @NonNull List list) { + private List readIdsList(JSONArray jsonArray, @NonNull String idScheme) { + List list = new ArrayList<>(); if (jsonArray != null) { for (int i = 0; i < jsonArray.length(); i++) { String id = jsonArray.optString(i); list.add(idScheme + id); } } + return list; } - public String convertToJsonString(List hidden, List order, String id) { + public String writeToJsonString(@NonNull String idScheme) { try { JSONObject json = new JSONObject(); - JSONObject items = new JSONObject(); - JSONArray hiddenItems = new JSONArray(); - JSONArray orderItems = new JSONArray(); - addIdsToJsonArray(hiddenItems, hidden); - addIdsToJsonArray(orderItems, order); - items.put(HIDDEN, hiddenItems); - items.put(ORDER, orderItems); - json.put(id, items); + json.put(HIDDEN, getJsonArray(hiddenIds, idScheme)); + json.put(ORDER, getJsonArray(orderIds, idScheme)); return json.toString(); } catch (JSONException e) { LOG.error("Error converting to json string: " + e); @@ -1189,26 +1224,22 @@ public class OsmandSettings { return ""; } - private void addIdsToJsonArray(@NonNull JSONArray jsonArray, List ids) { + private JSONArray getJsonArray(List ids, @NonNull String idScheme) { + JSONArray jsonArray = new JSONArray(); if (ids != null && !ids.isEmpty()) { for (String id : ids) { jsonArray.put(id.replace(idScheme, "")); } } + return jsonArray; } public List getHiddenIds() { - if (cachedPreference != getPreferences() || hiddenIds == null) { - readValues(); - } - return hiddenIds; + return Collections.unmodifiableList(hiddenIds); } public List getOrderIds() { - if (cachedPreference != getPreferences() || orderIds == null) { - readValues(); - } - return orderIds; + return Collections.unmodifiableList(orderIds); } } @@ -3545,11 +3576,11 @@ public class OsmandSettings { public final ContextMenuItemsPreference CONTEXT_MENU_ACTIONS_ITEMS = (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); + public final List CONTEXT_MENU_ITEMS_PREFERENCES = Arrays.asList(DRAWER_ITEMS, CONFIGURE_MAP_ITEMS, CONTEXT_MENU_ACTIONS_ITEMS); @Nullable public ContextMenuItemsPreference getContextMenuItemsPreference(@NonNull String id) { - for (ContextMenuItemsPreference preference : contextMenuItemsPreferences) { + for (ContextMenuItemsPreference preference : CONTEXT_MENU_ITEMS_PREFERENCES) { if (id.startsWith(preference.idScheme)) { return preference; } diff --git a/OsmAnd/src/net/osmand/plus/settings/ConfigureMenuItemsFragment.java b/OsmAnd/src/net/osmand/plus/settings/ConfigureMenuItemsFragment.java index 45fc3f25d2..b0c0b55b60 100644 --- a/OsmAnd/src/net/osmand/plus/settings/ConfigureMenuItemsFragment.java +++ b/OsmAnd/src/net/osmand/plus/settings/ConfigureMenuItemsFragment.java @@ -43,9 +43,6 @@ import net.osmand.plus.settings.RearrangeMenuItemsAdapter.AdapterItem; import net.osmand.plus.settings.RearrangeMenuItemsAdapter.MenuItemsAdapterListener; import org.apache.commons.logging.Log; -import org.json.JSONArray; -import org.json.JSONException; -import org.json.JSONObject; import java.util.ArrayList; import java.util.Collections; @@ -54,8 +51,6 @@ import java.util.HashMap; import java.util.List; import static net.osmand.aidlapi.OsmAndCustomizationConstants.MAP_CONTEXT_MENU_MORE_ID; -import static net.osmand.plus.OsmandSettings.ContextMenuItemsPreference.HIDDEN; -import static net.osmand.plus.OsmandSettings.ContextMenuItemsPreference.ORDER; 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; @@ -140,9 +135,9 @@ public class ConfigureMenuItemsFragment extends BaseOsmAndFragment hiddenMenuItems = savedInstanceState.getStringArrayList(HIDDEN_ITEMS_KEY); menuItemsOrder = (HashMap) savedInstanceState.getSerializable(ITEMS_ORDER_KEY); } else { - hiddenMenuItems = getSettingForScreen(app, screenType).getHiddenIds(); + hiddenMenuItems = new ArrayList<>(getSettingForScreen(app, screenType).getModeValue(appMode).getHiddenIds()); menuItemsOrder = new HashMap<>(); - List orderIds = getSettingForScreen(app, screenType).getOrderIds(); + List orderIds = getSettingForScreen(app, screenType).getModeValue(appMode).getOrderIds(); for (int i = 0; i < orderIds.size(); i++) { menuItemsOrder.put(orderIds.get(i), i); } @@ -261,19 +256,19 @@ public class ConfigureMenuItemsFragment extends BaseOsmAndFragment @Override public void onClick(View v) { List defItems = contextMenuAdapter.getDefaultItems(); - defItems.addAll(contextMenuAdapter.getHiddenItems()); - sortByCustomOrder(defItems, menuItemsOrder); List ids = new ArrayList<>(); - for (ContextMenuItem item : defItems) { - ids.add(item.getId()); + if (!menuItemsOrder.isEmpty()) { + sortByCustomOrder(defItems, menuItemsOrder); + for (ContextMenuItem item : defItems) { + ids.add(item.getId()); + } } FragmentManager fm = getFragmentManager(); - OsmandSettings.ContextMenuItemsPreference preference = getSettingForScreen(app, screenType); - String stringToSave = preference.convertToJsonString(hiddenMenuItems, ids, preference.getId()); + OsmandSettings.ContextMenuItemsSettings prefToSave = new OsmandSettings.ContextMenuItemsSettings(hiddenMenuItems, ids); if (fm != null) { ChangeGeneralProfilesPrefBottomSheet.showInstance(fm, getSettingForScreen(app, screenType).getId(), - stringToSave, + prefToSave, getTargetFragment(), false, appMode, @@ -347,6 +342,7 @@ public class ConfigureMenuItemsFragment extends BaseOsmAndFragment menuItemsOrder.clear(); wasReset = true; isChanged = true; + getSettingForScreen(app, screenType).resetModeToDefault(appMode); instantiateContextMenuAdapter(); rearrangeAdapter.updateItems(getAdapterItems()); } @@ -436,7 +432,6 @@ public class ConfigureMenuItemsFragment extends BaseOsmAndFragment public List getItemsForRearrangeAdapter(@Nullable List hiddenItemsIds, @Nullable HashMap itemsOrderIds, boolean hidden) { List defItems = contextMenuAdapter.getDefaultItems(); - defItems.addAll(contextMenuAdapter.getHiddenItems()); if (itemsOrderIds == null || itemsOrderIds.isEmpty()) { initDefaultOrders(defItems); } else { diff --git a/OsmAnd/src/net/osmand/plus/settings/ConfigureMenuRootFragment.java b/OsmAnd/src/net/osmand/plus/settings/ConfigureMenuRootFragment.java index aeb799ae2e..fe432fa37c 100644 --- a/OsmAnd/src/net/osmand/plus/settings/ConfigureMenuRootFragment.java +++ b/OsmAnd/src/net/osmand/plus/settings/ConfigureMenuRootFragment.java @@ -264,8 +264,8 @@ public class ConfigureMenuRootFragment extends BaseOsmAndFragment { contextMenuAdapter = menu.getActionsContextMenuAdapter(true); break; } - int hiddenCount = contextMenuAdapter.getHiddenItems().size(); - int allCount = contextMenuAdapter.getDefaultItems().size() + contextMenuAdapter.getHiddenItems().size(); + int hiddenCount = ConfigureMenuItemsFragment.getSettingForScreen(app, type).getModeValue(appMode).getHiddenIds().size(); + int allCount = contextMenuAdapter.getDefaultItems().size(); 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); }