ContextMenuItemsPreference refactor
This commit is contained in:
parent
b229c7405b
commit
2c7fd3606f
3 changed files with 85 additions and 64 deletions
|
@ -36,7 +36,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.ConfigureMenuRootFragment.ScreenType;
|
|
||||||
import net.osmand.plus.settings.RearrangeMenuItemsAdapter.AdapterItem;
|
import net.osmand.plus.settings.RearrangeMenuItemsAdapter.AdapterItem;
|
||||||
|
|
||||||
import org.apache.commons.logging.Log;
|
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.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_DIVIDER_ID;
|
|
||||||
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;
|
import static net.osmand.plus.settings.RearrangeMenuItemsAdapter.AdapterItemType.MENU_ITEM;
|
||||||
|
@ -87,8 +85,12 @@ public class ContextMenuAdapter {
|
||||||
|
|
||||||
public void addItem(ContextMenuItem item) {
|
public void addItem(ContextMenuItem item) {
|
||||||
try {
|
try {
|
||||||
// TODO
|
|
||||||
items.add(item.getPos(), item);
|
|
||||||
|
if (!item.isHidden()){
|
||||||
|
items.add(item.getPos(), item);
|
||||||
|
}
|
||||||
|
|
||||||
} catch (IndexOutOfBoundsException ex) {
|
} catch (IndexOutOfBoundsException ex) {
|
||||||
items.add(item);
|
items.add(item);
|
||||||
}
|
}
|
||||||
|
@ -641,7 +643,7 @@ public class ContextMenuAdapter {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void initItemsCustomOrder(@NonNull OsmandApplication app) {
|
public void initItemsCustomOrder(@NonNull OsmandApplication app) {
|
||||||
OsmandSettings.MenuItemConfigPreference preference = null;
|
OsmandSettings.ContextMenuItemsPreference preference = null;
|
||||||
for (ContextMenuItem item : items) {
|
for (ContextMenuItem item : items) {
|
||||||
String id = item.getId();
|
String id = item.getId();
|
||||||
if (id != null) {
|
if (id != null) {
|
||||||
|
|
|
@ -1127,72 +1127,56 @@ public class OsmandSettings {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public class MenuItemConfigPreference extends StringPreference {
|
public class ContextMenuItemsPreference extends StringPreference {
|
||||||
private static final String HIDDEN = "hidden";
|
public static final String HIDDEN = "hidden";
|
||||||
private static final String ORDER = "order";
|
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);
|
super(id, defaultValue);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void addIdsToJsonArray(@NonNull JSONArray jsonArray, List<String> ids) {
|
private void readValues() {
|
||||||
if (ids != null && !ids.isEmpty()) {
|
cachedPreference = getPreferences();
|
||||||
for (String id : ids) {
|
String jsonString = get();
|
||||||
jsonArray.put(id);
|
if (Algorithms.isEmpty(jsonString)) {
|
||||||
}
|
return;
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private List<String> getIdValues(String itemName) {
|
|
||||||
List<String> ids = new ArrayList<>();
|
|
||||||
String itemsString = get();
|
|
||||||
if (itemsString == null) {
|
|
||||||
return ids;
|
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
JSONObject json = new JSONObject(itemsString);
|
JSONObject json = new JSONObject(jsonString);
|
||||||
JSONObject items = json.optJSONObject(getId());
|
JSONObject items = json.optJSONObject(getId());
|
||||||
JSONArray idsArray = items.optJSONArray(itemName);
|
hiddenIds = new ArrayList<>();
|
||||||
if (idsArray != null) {
|
orderIds = new ArrayList<>();
|
||||||
for (int i = 0; i < idsArray.length(); i++) {
|
populateIdsList(items.optJSONArray(HIDDEN), hiddenIds);
|
||||||
String id = idsArray.optString(i);
|
populateIdsList(items.optJSONArray(ORDER), orderIds);
|
||||||
ids.add(id);
|
} 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);
|
||||||
}
|
}
|
||||||
} 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);
|
|
||||||
}
|
|
||||||
return "";
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean setIdValues(List<String> hidden, List<String> order) {
|
|
||||||
return set(convertToJsonString(hidden, order));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<String> getHiddenIds() {
|
public List<String> getHiddenIds() {
|
||||||
return getIdValues(HIDDEN);
|
if (cachedPreference != getPreferences() || hiddenIds == null) {
|
||||||
|
readValues();
|
||||||
|
}
|
||||||
|
return hiddenIds;
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<String> getOrderIds() {
|
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)
|
public final ListStringPreference INACTIVE_POI_FILTERS = (ListStringPreference)
|
||||||
new ListStringPreference("inactive_poi_filters", null, ",,").makeProfile().cache();
|
new ListStringPreference("inactive_poi_filters", null, ",,").makeProfile().cache();
|
||||||
|
|
||||||
public final MenuItemConfigPreference DRAWER_ITEMS =
|
public final ContextMenuItemsPreference DRAWER_ITEMS =
|
||||||
(MenuItemConfigPreference) new MenuItemConfigPreference("drawer_items", null).makeProfile().cache();
|
(ContextMenuItemsPreference) new ContextMenuItemsPreference("drawer_items", null).makeProfile().cache();
|
||||||
|
|
||||||
public final MenuItemConfigPreference CONFIGURE_MAP_ITEMS =
|
public final ContextMenuItemsPreference CONFIGURE_MAP_ITEMS =
|
||||||
(MenuItemConfigPreference) new MenuItemConfigPreference("configure_map_items", null).makeProfile().cache();
|
(ContextMenuItemsPreference) new ContextMenuItemsPreference("configure_map_items", null).makeProfile().cache();
|
||||||
|
|
||||||
public final MenuItemConfigPreference CONTEXT_MENU_ACTIONS_ITEMS =
|
public final ContextMenuItemsPreference CONTEXT_MENU_ACTIONS_ITEMS =
|
||||||
(MenuItemConfigPreference) new MenuItemConfigPreference("context_menu_actions_items", null).makeProfile().cache();
|
(ContextMenuItemsPreference) new ContextMenuItemsPreference("context_menu_actions_items", null).makeProfile().cache();
|
||||||
|
|
||||||
public static final String VOICE_PROVIDER_NOT_USE = "VOICE_PROVIDER_NOT_USE";
|
public static final String VOICE_PROVIDER_NOT_USE = "VOICE_PROVIDER_NOT_USE";
|
||||||
|
|
||||||
|
|
|
@ -22,6 +22,7 @@ import androidx.recyclerview.widget.LinearLayoutManager;
|
||||||
import androidx.recyclerview.widget.RecyclerView;
|
import androidx.recyclerview.widget.RecyclerView;
|
||||||
|
|
||||||
import net.osmand.AndroidUtils;
|
import net.osmand.AndroidUtils;
|
||||||
|
import net.osmand.PlatformUtil;
|
||||||
import net.osmand.plus.ApplicationMode;
|
import net.osmand.plus.ApplicationMode;
|
||||||
import net.osmand.plus.ContextMenuAdapter;
|
import net.osmand.plus.ContextMenuAdapter;
|
||||||
import net.osmand.plus.ContextMenuItem;
|
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.AdapterItem;
|
||||||
import net.osmand.plus.settings.RearrangeMenuItemsAdapter.MenuItemsAdapterListener;
|
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.ArrayList;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import static net.osmand.aidlapi.OsmAndCustomizationConstants.MAP_CONTEXT_MENU_MORE_ID;
|
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.BUTTON;
|
||||||
import static net.osmand.plus.settings.RearrangeMenuItemsAdapter.AdapterItemType.DESCRIPTION;
|
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.DIVIDER;
|
||||||
|
@ -55,6 +63,7 @@ public class ConfigureMenuItemsFragment extends BaseOsmAndFragment
|
||||||
implements SelectCopyAppModeBottomSheet.CopyAppModePrefsListener {
|
implements SelectCopyAppModeBottomSheet.CopyAppModePrefsListener {
|
||||||
|
|
||||||
public static final String TAG = ConfigureMenuItemsFragment.class.getName();
|
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 ITEM_TYPE_KEY = "item_type_key";
|
||||||
private static final String ITEMS_ORDER_KEY = "items_order_key";
|
private static final String ITEMS_ORDER_KEY = "items_order_key";
|
||||||
private static final String HIDDEN_ITEMS_KEY = "hidden_items_key";
|
private static final String HIDDEN_ITEMS_KEY = "hidden_items_key";
|
||||||
|
@ -238,7 +247,7 @@ public class ConfigureMenuItemsFragment extends BaseOsmAndFragment
|
||||||
ids.add(item.getId());
|
ids.add(item.getId());
|
||||||
}
|
}
|
||||||
FragmentManager fm = getFragmentManager();
|
FragmentManager fm = getFragmentManager();
|
||||||
String stringToSave = getSettingForScreen(app, screenType).convertToJsonString(hiddenMenuItems, ids);
|
String stringToSave = convertToJsonString(hiddenMenuItems, ids, getSettingForScreen(app, screenType).getId());
|
||||||
if (fm != null) {
|
if (fm != null) {
|
||||||
ChangeGeneralProfilesPrefBottomSheet.showInstance(fm,
|
ChangeGeneralProfilesPrefBottomSheet.showInstance(fm,
|
||||||
getSettingForScreen(app, screenType).getId(),
|
getSettingForScreen(app, screenType).getId(),
|
||||||
|
@ -266,6 +275,32 @@ public class ConfigureMenuItemsFragment extends BaseOsmAndFragment
|
||||||
return root;
|
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() {
|
private List<AdapterItem> getAdapterItems() {
|
||||||
List<AdapterItem> items = new ArrayList<>();
|
List<AdapterItem> items = new ArrayList<>();
|
||||||
items.add(new AdapterItem(DESCRIPTION, screenType));
|
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) {
|
switch (screenType) {
|
||||||
case DRAWER:
|
case DRAWER:
|
||||||
return app.getSettings().DRAWER_ITEMS;
|
return app.getSettings().DRAWER_ITEMS;
|
||||||
|
|
Loading…
Reference in a new issue