ContextMenuItemsPreference refactor

This commit is contained in:
veliymolfar 2020-04-10 17:18:02 +03:00
parent b229c7405b
commit 2c7fd3606f
3 changed files with 85 additions and 64 deletions

View file

@ -36,7 +36,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.ConfigureMenuRootFragment.ScreenType;
import net.osmand.plus.settings.RearrangeMenuItemsAdapter.AdapterItem;
import org.apache.commons.logging.Log;
@ -53,7 +52,6 @@ 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_DIVIDER_ID;
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;
@ -87,8 +85,12 @@ public class ContextMenuAdapter {
public void addItem(ContextMenuItem item) {
try {
// TODO
if (!item.isHidden()){
items.add(item.getPos(), item);
}
} catch (IndexOutOfBoundsException ex) {
items.add(item);
}
@ -641,7 +643,7 @@ public class ContextMenuAdapter {
}
public void initItemsCustomOrder(@NonNull OsmandApplication app) {
OsmandSettings.MenuItemConfigPreference preference = null;
OsmandSettings.ContextMenuItemsPreference preference = null;
for (ContextMenuItem item : items) {
String id = item.getId();
if (id != null) {

View file

@ -1127,72 +1127,56 @@ public class OsmandSettings {
}
}
public class MenuItemConfigPreference extends StringPreference {
private static final String HIDDEN = "hidden";
private static final String ORDER = "order";
public class ContextMenuItemsPreference extends StringPreference {
public static final String HIDDEN = "hidden";
public static final String ORDER = "order";
private List<String> hiddenIds;
private List<String> orderIds;
private Object cachedPreference;
private MenuItemConfigPreference(String id, String defaultValue) {
private ContextMenuItemsPreference(String id, String defaultValue) {
super(id, defaultValue);
}
private void addIdsToJsonArray(@NonNull JSONArray jsonArray, List<String> ids) {
if (ids != null && !ids.isEmpty()) {
for (String id : ids) {
jsonArray.put(id);
}
}
}
private List<String> getIdValues(String itemName) {
List<String> ids = new ArrayList<>();
String itemsString = get();
if (itemsString == null) {
return ids;
private void readValues() {
cachedPreference = getPreferences();
String jsonString = get();
if (Algorithms.isEmpty(jsonString)) {
return;
}
try {
JSONObject json = new JSONObject(itemsString);
JSONObject json = new JSONObject(jsonString);
JSONObject items = json.optJSONObject(getId());
JSONArray idsArray = items.optJSONArray(itemName);
if (idsArray != null) {
for (int i = 0; i < idsArray.length(); i++) {
String id = idsArray.optString(i);
ids.add(id);
}
}
hiddenIds = new ArrayList<>();
orderIds = new ArrayList<>();
populateIdsList(items.optJSONArray(HIDDEN), hiddenIds);
populateIdsList(items.optJSONArray(ORDER), orderIds);
} catch (JSONException e) {
LOG.error("Error converting to json string: " + e);
}
return ids;
}
public String convertToJsonString(List<String> hidden, List<String> order) {
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(getId(), items);
return json.toString();
} catch (JSONException e) {
LOG.error("Error converting to json string: " + e);
private void populateIdsList(JSONArray jsonArray, @NonNull List<String> list) {
if (jsonArray != null) {
for (int i = 0; i < jsonArray.length(); i++) {
String id = jsonArray.optString(i);
list.add(id);
}
return "";
}
public boolean setIdValues(List<String> hidden, List<String> order) {
return set(convertToJsonString(hidden, order));
}
public List<String> getHiddenIds() {
return getIdValues(HIDDEN);
if (cachedPreference != getPreferences() || hiddenIds == null) {
readValues();
}
return hiddenIds;
}
public List<String> getOrderIds() {
return getIdValues(ORDER);
if (cachedPreference != getPreferences() || orderIds == null) {
readValues();
}
return orderIds;
}
}
@ -3520,14 +3504,14 @@ public class OsmandSettings {
public final ListStringPreference INACTIVE_POI_FILTERS = (ListStringPreference)
new ListStringPreference("inactive_poi_filters", null, ",,").makeProfile().cache();
public final MenuItemConfigPreference DRAWER_ITEMS =
(MenuItemConfigPreference) new MenuItemConfigPreference("drawer_items", null).makeProfile().cache();
public final ContextMenuItemsPreference DRAWER_ITEMS =
(ContextMenuItemsPreference) new ContextMenuItemsPreference("drawer_items", null).makeProfile().cache();
public final MenuItemConfigPreference CONFIGURE_MAP_ITEMS =
(MenuItemConfigPreference) new MenuItemConfigPreference("configure_map_items", null).makeProfile().cache();
public final ContextMenuItemsPreference CONFIGURE_MAP_ITEMS =
(ContextMenuItemsPreference) new ContextMenuItemsPreference("configure_map_items", null).makeProfile().cache();
public final MenuItemConfigPreference CONTEXT_MENU_ACTIONS_ITEMS =
(MenuItemConfigPreference) new MenuItemConfigPreference("context_menu_actions_items", null).makeProfile().cache();
public final ContextMenuItemsPreference CONTEXT_MENU_ACTIONS_ITEMS =
(ContextMenuItemsPreference) new ContextMenuItemsPreference("context_menu_actions_items", null).makeProfile().cache();
public static final String VOICE_PROVIDER_NOT_USE = "VOICE_PROVIDER_NOT_USE";

View file

@ -22,6 +22,7 @@ import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import net.osmand.AndroidUtils;
import net.osmand.PlatformUtil;
import net.osmand.plus.ApplicationMode;
import net.osmand.plus.ContextMenuAdapter;
import net.osmand.plus.ContextMenuItem;
@ -41,11 +42,18 @@ import net.osmand.plus.views.controls.ReorderItemTouchHelperCallback;
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.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;
@ -55,6 +63,7 @@ public class ConfigureMenuItemsFragment extends BaseOsmAndFragment
implements SelectCopyAppModeBottomSheet.CopyAppModePrefsListener {
public static final String TAG = ConfigureMenuItemsFragment.class.getName();
private static final Log LOG = PlatformUtil.getLog(ConfigureMenuItemsFragment.class.getName());
private static final String ITEM_TYPE_KEY = "item_type_key";
private static final String ITEMS_ORDER_KEY = "items_order_key";
private static final String HIDDEN_ITEMS_KEY = "hidden_items_key";
@ -238,7 +247,7 @@ public class ConfigureMenuItemsFragment extends BaseOsmAndFragment
ids.add(item.getId());
}
FragmentManager fm = getFragmentManager();
String stringToSave = getSettingForScreen(app, screenType).convertToJsonString(hiddenMenuItems, ids);
String stringToSave = convertToJsonString(hiddenMenuItems, ids, getSettingForScreen(app, screenType).getId());
if (fm != null) {
ChangeGeneralProfilesPrefBottomSheet.showInstance(fm,
getSettingForScreen(app, screenType).getId(),
@ -266,6 +275,32 @@ public class ConfigureMenuItemsFragment extends BaseOsmAndFragment
return root;
}
private String convertToJsonString(List<String> hidden, List<String> order, String id) {
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);
return json.toString();
} catch (JSONException e) {
LOG.error("Error converting to json string: " + e);
}
return "";
}
private void addIdsToJsonArray(@NonNull JSONArray jsonArray, List<String> ids) {
if (ids != null && !ids.isEmpty()) {
for (String id : ids) {
jsonArray.put(id);
}
}
}
private List<AdapterItem> getAdapterItems() {
List<AdapterItem> items = new ArrayList<>();
items.add(new AdapterItem(DESCRIPTION, screenType));
@ -384,7 +419,7 @@ public class ConfigureMenuItemsFragment extends BaseOsmAndFragment
}
}
public static OsmandSettings.MenuItemConfigPreference getSettingForScreen(OsmandApplication app, ScreenType screenType) {
public static OsmandSettings.ContextMenuItemsPreference getSettingForScreen(OsmandApplication app, ScreenType screenType) {
switch (screenType) {
case DRAWER:
return app.getSettings().DRAWER_ITEMS;