sort items on add

This commit is contained in:
veliymolfar 2020-04-13 15:39:34 +03:00
parent 52fdde1dc6
commit b6938eafb9
24 changed files with 97 additions and 81 deletions

View file

@ -69,9 +69,17 @@ public class ContextMenuAdapter {
@LayoutRes
private int DEFAULT_LAYOUT_ID = R.layout.list_menu_item_native;
List<ContextMenuItem> items = new ArrayList<>();
List<ContextMenuItem> hiddenItems = new ArrayList<>();
private boolean profileDependent = false;
private boolean nightMode;
private ConfigureMapMenu.OnClickListener changeAppModeListener = null;
private OsmandApplication app;
private HashMap<String, Integer> ordersMap;
private ContextMenuItemsPreference contextMenuItemsPreference;
public ContextMenuAdapter(OsmandApplication app) {
this.app = app;
}
public int length() {
return items.size();
@ -87,12 +95,17 @@ public class ContextMenuAdapter {
public void addItem(ContextMenuItem item) {
try {
if (!item.isHidden()){
String id = item.getId();
if (id != null) {
item.setHidden(isItemHidden(id));
item.setOrder(getItemCustomOrder(id, item.getOrder()));
}
if (item.isHidden()) {
hiddenItems.add(item);
} else {
items.add(item.getPos(), item);
}
sortItemsByOrder();
} catch (IndexOutOfBoundsException ex) {
items.add(item);
}
@ -152,43 +165,43 @@ public class ContextMenuAdapter {
});
}
public void sortItemsByCustomOrder(OsmandApplication app) {
ContextMenuItemsPreference pref = getPreference(app);
if (pref == null) {
sortItemsByOrder();
return;
private boolean isItemHidden(@NonNull String id) {
if (contextMenuItemsPreference == null) {
contextMenuItemsPreference = getPreference(id);
if (contextMenuItemsPreference == null) {
return false;
}
}
List<String> hiddenIds = contextMenuItemsPreference.getHiddenIds();
if (!Algorithms.isEmpty(hiddenIds)) {
return hiddenIds.contains(id);
}
return false;
}
List<String> hiddenIds = pref.getHiddenIds();
List<String> orderIds = pref.getOrderIds();
private int getItemCustomOrder(@NonNull String id, int defaultOrder) {
if (contextMenuItemsPreference == null) {
contextMenuItemsPreference = getPreference(id);
if (contextMenuItemsPreference == null) {
return defaultOrder;
}
}
List<String> orderIds = contextMenuItemsPreference.getOrderIds();
if (!Algorithms.isEmpty(orderIds)) {
HashMap<String, Integer> ordersMap = new HashMap<>();
if (ordersMap == null) {
ordersMap = new HashMap<>();
for (int i = 0; i < orderIds.size(); i++) {
ordersMap.put(orderIds.get(i), i);
}
for (ContextMenuItem item : items) {
Integer order = ordersMap.get(item.getId());
}
Integer order = ordersMap.get(id);
if (order != null) {
item.setOrder(order);
return order;
}
}
sortItemsByOrder();
return defaultOrder;
}
if (!Algorithms.isEmpty(hiddenIds)) {
List<ContextMenuItem> filtered = new ArrayList<>();
for (ContextMenuItem item : items) {
if (!hiddenIds.contains(item.getId())) {
filtered.add(item);
}
}
items = filtered;
}
}
public ArrayAdapter<ContextMenuItem> createListAdapter(final Activity activity, final boolean lightTheme) {
final int layoutId = DEFAULT_LAYOUT_ID;
final OsmandApplication app = ((OsmandApplication) activity.getApplication());
@ -606,6 +619,10 @@ public class ContextMenuAdapter {
return items;
}
public List<ContextMenuItem> getHiddenItems() {
return hiddenItems;
}
private String getIdScheme() {
String idScheme = "";
for (ContextMenuItem item : items) {
@ -626,10 +643,8 @@ public class ContextMenuAdapter {
return idScheme;
}
private ContextMenuItemsPreference getPreference(OsmandApplication app) {
for (ContextMenuItem item : items) {
String id = item.getId();
if (id != null) {
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)) {
@ -638,7 +653,6 @@ public class ContextMenuAdapter {
return app.getSettings().CONTEXT_MENU_ACTIONS_ITEMS;
}
}
}
return null;
}
}

View file

@ -42,7 +42,7 @@ public class HelpActivity extends OsmandActionBarActivity implements AdapterView
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_help_screen);
ContextMenuAdapter contextMenuAdapter = new ContextMenuAdapter();
ContextMenuAdapter contextMenuAdapter = new ContextMenuAdapter(getMyApplication());
contextMenuAdapter.setDefaultLayoutId(R.layout.two_line_with_images_list_item);
createBeginWithOsmandItems(contextMenuAdapter);

View file

@ -437,7 +437,7 @@ public class MapActivityActions implements DialogProvider {
}
public void contextMenuPoint(final double latitude, final double longitude, final ContextMenuAdapter iadapter, Object selectedObj) {
final ContextMenuAdapter adapter = iadapter == null ? new ContextMenuAdapter() : iadapter;
final ContextMenuAdapter adapter = iadapter == null ? new ContextMenuAdapter(getMyApplication()) : iadapter;
addActionsToAdapter(latitude, longitude, adapter, selectedObj, false);
showAdditionalActionsFragment(adapter, getContextMenuItemClickListener(latitude, longitude, adapter));
}
@ -705,7 +705,7 @@ public class MapActivityActions implements DialogProvider {
public ContextMenuAdapter createMainOptionsMenu() {
final OsmandMapTileView mapView = mapActivity.getMapView();
final OsmandApplication app = mapActivity.getMyApplication();
ContextMenuAdapter optionsMenuHelper = new ContextMenuAdapter();
ContextMenuAdapter optionsMenuHelper = new ContextMenuAdapter(app);
boolean nightMode = getMyApplication().getDaynightHelper().isNightModeForMapControls();
if (drawerMode == DRAWER_MODE_SWITCH_PROFILE) {
@ -1182,7 +1182,6 @@ public class MapActivityActions implements DialogProvider {
}
menuItemsListView.setDivider(null);
final ContextMenuAdapter contextMenuAdapter = createMainOptionsMenu();
contextMenuAdapter.sortItemsByCustomOrder(getMyApplication());
contextMenuAdapter.setDefaultLayoutId(R.layout.simple_list_menu_item);
final ArrayAdapter<ContextMenuItem> simpleListAdapter = contextMenuAdapter.createListAdapter(mapActivity,
!nightMode);

View file

@ -270,7 +270,7 @@ public class MapActivityLayers {
public void showMultichoicePoiFilterDialog(final OsmandMapTileView mapView, final DismissListener listener) {
final OsmandApplication app = getApplication();
final PoiFiltersHelper poiFilters = app.getPoiFilters();
final ContextMenuAdapter adapter = new ContextMenuAdapter();
final ContextMenuAdapter adapter = new ContextMenuAdapter(app);
final List<PoiUIFilter> list = new ArrayList<>();
for (PoiUIFilter f : poiFilters.getSortedPoiFilters(true)) {
addFilterToList(adapter, list, f, true);
@ -344,7 +344,7 @@ public class MapActivityLayers {
public void showSingleChoicePoiFilterDialog(final OsmandMapTileView mapView, final DismissListener listener) {
final OsmandApplication app = getApplication();
final PoiFiltersHelper poiFilters = app.getPoiFilters();
final ContextMenuAdapter adapter = new ContextMenuAdapter();
final ContextMenuAdapter adapter = new ContextMenuAdapter(app);
adapter.addItem(new ContextMenuItem.ItemBuilder()
.setTitleId(R.string.shared_string_search, app)
.setIcon(R.drawable.ic_action_search_dark).createItem());

View file

@ -475,7 +475,7 @@ public class SettingsNavigationActivity extends SettingsBaseActivity {
return true;
} else if (preference == autoZoom) {
final ApplicationMode am = settings.getApplicationMode();
final ContextMenuAdapter adapter = new ContextMenuAdapter();
final ContextMenuAdapter adapter = new ContextMenuAdapter(getMyApplication());
int i = 0;
int selectedIndex = -1;
adapter.addItem(ContextMenuItem.createBuilder(getString(R.string.auto_zoom_none))
@ -546,7 +546,7 @@ public class SettingsNavigationActivity extends SettingsBaseActivity {
return true;
} else if (preference == reliefFactorRouting) {
final ApplicationMode am = settings.getApplicationMode();
final ContextMenuAdapter adapter = new ContextMenuAdapter();
final ContextMenuAdapter adapter = new ContextMenuAdapter(getMyApplication());
int i = 0;
int selectedIndex = -1;
for (RoutingParameter p : reliefFactorParameters) {

View file

@ -702,7 +702,6 @@ public class DashboardOnMap implements ObservableScrollViewCallbacks, IRouteInfo
cm = mapActivity.getMapLayers().getMapWidgetRegistry().getViewConfigureMenuAdapter(mapActivity);
} else if (visibleType == DashboardType.CONFIGURE_MAP) {
cm = new ConfigureMapMenu().createListAdapter(mapActivity);
cm.sortItemsByCustomOrder(getMyApplication());
} else if (visibleType == DashboardType.LIST_MENU) {
cm = mapActivity.getMapActions().createMainOptionsMenu();
} else if (visibleType == DashboardType.ROUTE_PREFERENCES) {

View file

@ -129,7 +129,7 @@ public class ConfigureMapMenu {
OsmandApplication app = ma.getMyApplication();
boolean nightMode = app.getDaynightHelper().isNightModeForMapControls();
int themeRes = nightMode ? R.style.OsmandDarkTheme : R.style.OsmandLightTheme;
ContextMenuAdapter adapter = new ContextMenuAdapter();
ContextMenuAdapter adapter = new ContextMenuAdapter(app);
adapter.setDefaultLayoutId(R.layout.list_item_icon_and_menu);
adapter.addItem(new ContextMenuItem.ItemBuilder()
.setId(APP_PROFILES_ID)

View file

@ -24,7 +24,7 @@ public class RasterMapMenu {
public static ContextMenuAdapter createListAdapter(final MapActivity mapActivity,
final RasterMapType type) {
boolean nightMode = mapActivity.getMyApplication().getDaynightHelper().isNightModeForMapControls();
ContextMenuAdapter adapter = new ContextMenuAdapter();
ContextMenuAdapter adapter = new ContextMenuAdapter(mapActivity.getMyApplication());
adapter.setDefaultLayoutId(R.layout.list_item_icon_and_menu);
adapter.setProfileDependent(true);
adapter.setNightMode(nightMode);

View file

@ -164,7 +164,7 @@ public class LocalIndexesFragment extends OsmandExpandableListFragment implement
private void showContextMenu(final LocalIndexInfo info) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
final ContextMenuAdapter adapter = new ContextMenuAdapter();
final ContextMenuAdapter adapter = new ContextMenuAdapter(getMyApplication());
basicFileOperation(info, adapter);
OsmandPlugin.onContextMenuActivity(getActivity(), null, info, adapter);
@ -704,7 +704,7 @@ public class LocalIndexesFragment extends OsmandExpandableListFragment implement
//hide action bar from downloadindexfragment
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
int iconColorResId = getMyApplication().getSettings().isLightContent() ? R.color.active_buttons_and_links_text_light : R.color.active_buttons_and_links_text_dark;
optionsMenuAdapter = new ContextMenuAdapter();
optionsMenuAdapter = new ContextMenuAdapter(requireMyApplication());
ItemClickListener listener = new ContextMenuAdapter.ItemClickListener() {
@Override
public boolean onContextMenuClick(ArrayAdapter<ContextMenuItem> adapter,

View file

@ -234,7 +234,7 @@ public class GpxUiHelper {
}
allGpxList.add(0, new GPXInfo(activity.getString(R.string.show_current_gpx_title), 0, 0));
final ContextMenuAdapter adapter = createGpxContextMenuAdapter(allGpxList, selectedGpxList, true);
final ContextMenuAdapter adapter = createGpxContextMenuAdapter(allGpxList, selectedGpxList, true, app);
return createDialog(activity, true, true, true, callbackWithObject, allGpxList, adapter, dialogThemeRes, nightMode);
}
@ -253,7 +253,7 @@ public class GpxUiHelper {
list.add(0, new GPXInfo(activity.getString(R.string.show_current_gpx_title), 0, 0));
}
final ContextMenuAdapter adapter = createGpxContextMenuAdapter(list, null, showCurrentGpx);
final ContextMenuAdapter adapter = createGpxContextMenuAdapter(list, null, showCurrentGpx, app);
return createDialog(activity, showCurrentGpx, multipleChoice, false, callbackWithObject, list, adapter, dialogThemeRes, nightMode);
}
return null;
@ -280,7 +280,7 @@ public class GpxUiHelper {
}
}
final ContextMenuAdapter adapter = createGpxContextMenuAdapter(list, null, showCurrentGpx);
final ContextMenuAdapter adapter = createGpxContextMenuAdapter(list, null, showCurrentGpx, app);
return createSingleChoiceDialog(activity, showCurrentGpx, callbackWithObject, list, adapter);
}
return null;
@ -288,8 +288,9 @@ public class GpxUiHelper {
private static ContextMenuAdapter createGpxContextMenuAdapter(List<GPXInfo> allGpxList,
List<String> selectedGpxList,
boolean showCurrentTrack) {
final ContextMenuAdapter adapter = new ContextMenuAdapter();
boolean showCurrentTrack,
OsmandApplication app) {
final ContextMenuAdapter adapter = new ContextMenuAdapter(app);
//element position in adapter
int i = 0;
for (GPXInfo gpxInfo : allGpxList) {

View file

@ -1064,7 +1064,7 @@ public class MapContextMenu extends MenuTitleController implements StateChangedL
public ContextMenuAdapter getActionsContextMenuAdapter(boolean all) {
MapActivity mapActivity = getMapActivity();
final ContextMenuAdapter menuAdapter = new ContextMenuAdapter();
final ContextMenuAdapter menuAdapter = new ContextMenuAdapter(getMyApplication());
if (mapActivity != null) {
LatLon latLon = getLatLon();
for (OsmandMapLayer layer : mapActivity.getMapView().getLayers()) {

View file

@ -566,7 +566,6 @@ public class MapContextMenuFragment extends BaseOsmAndFragment implements Downlo
// Action buttons
// TODO refactor section
ContextMenuAdapter adapter = menu.getActionsContextMenuAdapter(false);
adapter.sortItemsByCustomOrder(requireMyApplication());
List<ContextMenuItem> items = adapter.getItems();
List<ContextMenuItem> main = new ArrayList<>();
List<ContextMenuItem> additional = new ArrayList<>();
@ -582,10 +581,10 @@ public class MapContextMenuFragment extends BaseOsmAndFragment implements Downlo
1f
);
buttons.removeAllViews();
ContextMenuAdapter mainAdapter = new ContextMenuAdapter();
ContextMenuAdapter mainAdapter = new ContextMenuAdapter(requireMyApplication());
mainAdapter.updateItems(main);
ContextMenuItemClickListener mainListener = menu.getContextMenuItemClickListener(mainAdapter);
ContextMenuAdapter additionalAdapter = new ContextMenuAdapter();
ContextMenuAdapter additionalAdapter = new ContextMenuAdapter(requireMyApplication());
additionalAdapter.updateItems(additional);
ContextMenuItemClickListener additionalListener = menu.getContextMenuItemClickListener(additionalAdapter);

View file

@ -84,7 +84,7 @@ public class RoutePreferencesMenu {
Object obj = listAdapter.getItem(item);
if (obj instanceof LocalRoutingParameterGroup) {
final LocalRoutingParameterGroup group = (LocalRoutingParameterGroup) obj;
final ContextMenuAdapter adapter = new ContextMenuAdapter();
final ContextMenuAdapter adapter = new ContextMenuAdapter(app);
int i = 0;
int selectedIndex = -1;
for (LocalRoutingParameter p : group.getRoutingParameters()) {

View file

@ -475,7 +475,7 @@ public class AvailableGPXFragment extends OsmandExpandableListFragment implement
((FavoritesActivity) getActivity()).updateListViewFooter(footerView);
// TODO Rewrite without ContextMenuAdapter
optionsMenuAdapter = new ContextMenuAdapter();
optionsMenuAdapter = new ContextMenuAdapter(app);
ItemClickListener listener = new ContextMenuAdapter.ItemClickListener() {
@Override
public boolean onContextMenuClick(ArrayAdapter<ContextMenuItem> adapter, final int itemId, int pos, boolean isChecked, int[] viewCoordinates) {
@ -760,7 +760,7 @@ public class AvailableGPXFragment extends OsmandExpandableListFragment implement
private void moveGpx(final GpxInfo info) {
final ContextMenuAdapter menuAdapter = new ContextMenuAdapter();
final ContextMenuAdapter menuAdapter = new ContextMenuAdapter(app);
ContextMenuItem.ItemBuilder itemBuilder = new ContextMenuItem.ItemBuilder();
final List<File> dirs = new ArrayList<>();

View file

@ -26,7 +26,7 @@ public class OsmNotesMenu {
private static Integer[] zoomIntValues = {8, 9, 10, 11, 12, 13, 14, 15, 16};
public static ContextMenuAdapter createListAdapter(final MapActivity mapActivity) {
ContextMenuAdapter adapter = new ContextMenuAdapter();
ContextMenuAdapter adapter = new ContextMenuAdapter(mapActivity.getMyApplication());
boolean nightMode = mapActivity.getMyApplication().getDaynightHelper().isNightModeForMapControls();
adapter.setDefaultLayoutId(R.layout.list_item_icon_and_menu);
adapter.setProfileDependent(true);

View file

@ -288,7 +288,7 @@ public class ShowHidePoiAction extends QuickAction {
private void showSingleChoicePoiFilterDialog(final OsmandApplication app, final MapActivity activity, final Adapter filtersAdapter) {
final PoiFiltersHelper poiFilters = app.getPoiFilters();
final ContextMenuAdapter adapter = new ContextMenuAdapter();
final ContextMenuAdapter adapter = new ContextMenuAdapter(app);
final List<PoiUIFilter> list = new ArrayList<>();

View file

@ -113,7 +113,7 @@ public class RoutingOptionsHelper {
}
public void selectVoiceGuidance(final MapActivity mapActivity, final CallbackWithObject<String> callback, ApplicationMode applicationMode) {
final ContextMenuAdapter adapter = new ContextMenuAdapter();
final ContextMenuAdapter adapter = new ContextMenuAdapter(app);
String[] entries;
final String[] entrieValues;
@ -310,7 +310,7 @@ public class RoutingOptionsHelper {
public void showLocalRoutingParameterGroupDialog(final LocalRoutingParameterGroup group, final MapActivity mapActivity, final OnClickListener listener) {
OsmandSettings settings = app.getSettings();
final ContextMenuAdapter adapter = new ContextMenuAdapter();
final ContextMenuAdapter adapter = new ContextMenuAdapter(app);
int i = 0;
int selectedIndex = -1;
for (LocalRoutingParameter p : group.getRoutingParameters()) {

View file

@ -261,6 +261,7 @@ public class ConfigureMenuItemsFragment extends BaseOsmAndFragment
@Override
public void onClick(View v) {
List<ContextMenuItem> defItems = contextMenuAdapter.getDefaultItems();
defItems.addAll(contextMenuAdapter.getHiddenItems());
sortByCustomOrder(defItems, menuItemsOrder);
List<String> ids = new ArrayList<>();
for (ContextMenuItem item : defItems) {
@ -435,6 +436,7 @@ public class ConfigureMenuItemsFragment extends BaseOsmAndFragment
public List<AdapterItem> getItemsForRearrangeAdapter(@Nullable List<String> hiddenItemsIds, @Nullable HashMap<String, Integer> itemsOrderIds, boolean hidden) {
List<ContextMenuItem> defItems = contextMenuAdapter.getDefaultItems();
defItems.addAll(contextMenuAdapter.getHiddenItems());
if (itemsOrderIds == null || itemsOrderIds.isEmpty()) {
initDefaultOrders(defItems);
} else {

View file

@ -264,8 +264,8 @@ public class ConfigureMenuRootFragment extends BaseOsmAndFragment {
contextMenuAdapter = menu.getActionsContextMenuAdapter(true);
break;
}
int hiddenCount = ConfigureMenuItemsFragment.getSettingForScreen(app, type).getHiddenIds().size();
int allCount = contextMenuAdapter.getDefaultItems().size();
int hiddenCount = contextMenuAdapter.getHiddenItems().size();
int allCount = contextMenuAdapter.getDefaultItems().size() + contextMenuAdapter.getHiddenItems().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);
}

View file

@ -66,6 +66,7 @@ public class ChangeGeneralProfilesPrefBottomSheet extends BasePreferenceBottomSh
.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
app.getSettings().setPreferenceForAllModes(prefId, newValue);
updateTargetSettings(false, true);
if (listener != null) {
listener.onApplied();
@ -85,6 +86,7 @@ public class ChangeGeneralProfilesPrefBottomSheet extends BasePreferenceBottomSh
.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
app.getSettings().setPreference(prefId, newValue, getAppMode());
updateTargetSettings(false, false);
if (listener != null) {
listener.onApplied();

View file

@ -38,7 +38,7 @@ public class ContourLinesMenu {
OsmandPlugin.enablePlugin(mapActivity, mapActivity.getMyApplication(), plugin, true);
}
boolean nightMode = isNightMode(mapActivity.getMyApplication());
ContextMenuAdapter adapter = new ContextMenuAdapter();
ContextMenuAdapter adapter = new ContextMenuAdapter(mapActivity.getMyApplication());
adapter.setDefaultLayoutId(R.layout.list_item_icon_and_menu);
adapter.setProfileDependent(true);
adapter.setNightMode(nightMode);

View file

@ -404,7 +404,7 @@ public class TerrainFragment extends BaseOsmAndFragment implements View.OnClickL
}
private void updateDownloadSection() {
final ContextMenuAdapter adapter = new ContextMenuAdapter();
final ContextMenuAdapter adapter = new ContextMenuAdapter(app);
adapter.setDefaultLayoutId(R.layout.list_item_icon_and_menu);
adapter.setProfileDependent(true);
adapter.setNightMode(nightMode);

View file

@ -798,7 +798,7 @@ public class MapWidgetRegistry {
}
public ContextMenuAdapter getViewConfigureMenuAdapter(final MapActivity map) {
final ContextMenuAdapter cm = new ContextMenuAdapter();
final ContextMenuAdapter cm = new ContextMenuAdapter(app);
boolean nightMode = app.getDaynightHelper().isNightModeForMapControls();
cm.setProfileDependent(true);
cm.setNightMode(nightMode);

View file

@ -54,7 +54,7 @@ public class WikipediaPoiMenu {
final int languageActionStringId = R.string.shared_string_language;
final int spaceHeight = app.getResources().getDimensionPixelSize(R.dimen.bottom_sheet_big_item_height);
final boolean enabled = app.getPoiFilters().isShowingAnyPoi(PoiTemplateList.WIKI);
ContextMenuAdapter adapter = new ContextMenuAdapter();
ContextMenuAdapter adapter = new ContextMenuAdapter(app);
adapter.setDefaultLayoutId(R.layout.dash_item_with_description_72dp);
adapter.setProfileDependent(true);
adapter.setNightMode(nightMode);