From 204b6f8310271a0947a9b5f11fabef30bed4ffc5 Mon Sep 17 00:00:00 2001 From: PavelRatushny Date: Wed, 22 Nov 2017 18:13:02 +0200 Subject: [PATCH] Refactor --- .../src/net/osmand/plus/MapMarkersHelper.java | 36 +++++++----- .../plus/mapcontextmenu/MapContextMenu.java | 7 +++ .../MapContextMenuFragment.java | 22 +------ .../plus/mapcontextmenu/MenuController.java | 26 +++++++++ .../FavouritePointMenuController.java | 34 ++--------- .../controllers/MapMarkerMenuController.java | 58 +++++++++---------- .../controllers/WptPtMenuController.java | 34 ++--------- 7 files changed, 97 insertions(+), 120 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/MapMarkersHelper.java b/OsmAnd/src/net/osmand/plus/MapMarkersHelper.java index 92df7eb93e..6b27448356 100644 --- a/OsmAnd/src/net/osmand/plus/MapMarkersHelper.java +++ b/OsmAnd/src/net/osmand/plus/MapMarkersHelper.java @@ -457,19 +457,19 @@ public class MapMarkersHelper { } } - public void syncGroupAsync(MarkersSyncGroup group) { + public void syncGroupAsync(@NonNull MarkersSyncGroup group) { syncGroupAsync(group, true, null); } - public void syncGroupAsync(MarkersSyncGroup group, boolean enabled) { + public void syncGroupAsync(@NonNull MarkersSyncGroup group, boolean enabled) { syncGroupAsync(group, enabled, null); } - public void syncGroupAsync(MarkersSyncGroup group, OnGroupSyncedListener groupSyncedListener) { + public void syncGroupAsync(@NonNull MarkersSyncGroup group, @Nullable OnGroupSyncedListener groupSyncedListener) { syncGroupAsync(group, true, groupSyncedListener); } - private void syncGroupAsync(final MarkersSyncGroup group, final boolean enabled, final OnGroupSyncedListener groupSyncedListener) { + private void syncGroupAsync(@NonNull final MarkersSyncGroup group, final boolean enabled, @Nullable final OnGroupSyncedListener groupSyncedListener) { ctx.runInUIThread(new Runnable() { @Override public void run() { @@ -597,7 +597,13 @@ public class MapMarkersHelper { return null; } - private void addNewMarkerIfNeeded(MarkersSyncGroup group, List markers, LatLon latLon, String name, boolean enabled, FavouritePoint favouritePoint, WptPt wptPt) { + private void addNewMarkerIfNeeded(@NonNull MarkersSyncGroup group, + @NonNull List markers, + @NonNull LatLon latLon, + @NonNull String name, + boolean enabled, + @Nullable FavouritePoint favouritePoint, + @Nullable WptPt wptPt) { boolean exists = false; for (MapMarker marker : markers) { @@ -880,7 +886,7 @@ public class MapMarkersHelper { markersDbHelper.removeDisabledGroups(); } - public void updateGroupDisabled(MapMarkersGroup group, boolean disabled) { + public void updateGroupDisabled(@NonNull MapMarkersGroup group, boolean disabled) { String id = group.getGroupKey(); if (id != null) { markersDbHelper.updateSyncGroupDisabled(id, disabled); @@ -888,7 +894,7 @@ public class MapMarkersHelper { } } - private void updateSyncGroupDisabled(MapMarkersGroup group, boolean disabled) { + private void updateSyncGroupDisabled(@NonNull MapMarkersGroup group, boolean disabled) { List groupMarkers = group.getMarkers(); for (MapMarker marker : groupMarkers) { if (marker.history) { @@ -926,20 +932,20 @@ public class MapMarkersHelper { } } - public void addMapMarker(LatLon point, PointDescription historyName) { + public void addMapMarker(@NonNull LatLon point, @Nullable PointDescription historyName) { addMarkers(Collections.singletonList(point), Collections.singletonList(historyName), null, true); } - public void addMapMarkers(List points, List historyNames, @Nullable MarkersSyncGroup group) { + public void addMapMarkers(@NonNull List points, @NonNull List historyNames, @Nullable MarkersSyncGroup group) { addMarkers(points, historyNames, group, true); } - private void addMarkers(List points, List historyNames, @Nullable MarkersSyncGroup group, boolean enabled) { + private void addMarkers(@NonNull List points, @NonNull List historyNames, @Nullable MarkersSyncGroup group, boolean enabled) { addMarkers(points, historyNames, group, enabled, null, null); } - private void addMarkers(List points, List historyNames, @Nullable MarkersSyncGroup group, - boolean enabled, List favouritePoints, List wptPts) { + private void addMarkers(@NonNull List points, @NonNull List historyNames, @Nullable MarkersSyncGroup group, + boolean enabled, @Nullable List favouritePoints, @Nullable List wptPts) { if (points.size() > 0) { int colorIndex = -1; List addedMarkers = new ArrayList<>(); @@ -1192,7 +1198,7 @@ public class MapMarkersHelper { } } - private void addMarkersToGroups(List markers, boolean enabled) { + private void addMarkersToGroups(@NonNull List markers, boolean enabled) { List groups = new ArrayList<>(); for (int i = 0; i < markers.size(); i++) { MapMarkersGroup group = addMarkerToGroup(markers.get(i)); @@ -1227,7 +1233,7 @@ public class MapMarkersHelper { return null; } - private MapMarkersGroup createMapMarkerGroup(MapMarker marker) { + private MapMarkersGroup createMapMarkerGroup(@NonNull MapMarker marker) { MapMarkersGroup group = new MapMarkersGroup(); if (marker.groupName != null) { group.setName(marker.groupName); @@ -1246,7 +1252,7 @@ public class MapMarkersHelper { return group; } - private void createHeaderAndHistoryButtonInGroup(MapMarkersGroup group) { + private void createHeaderAndHistoryButtonInGroup(@NonNull MapMarkersGroup group) { if (group.getName() != null) { GroupHeader header = new GroupHeader(); int type = group.getType(); diff --git a/OsmAnd/src/net/osmand/plus/mapcontextmenu/MapContextMenu.java b/OsmAnd/src/net/osmand/plus/mapcontextmenu/MapContextMenu.java index b33f1f56ef..5a3c16fcc1 100644 --- a/OsmAnd/src/net/osmand/plus/mapcontextmenu/MapContextMenu.java +++ b/OsmAnd/src/net/osmand/plus/mapcontextmenu/MapContextMenu.java @@ -718,6 +718,13 @@ public class MapContextMenu extends MenuTitleController implements StateChangedL ? R.string.shared_string_add_to_map_markers : R.string.context_menu_item_destination_point; } + public boolean isButtonWaypointEnabled() { + if (menuController != null) { + return menuController.isWaypointButtonEnabled(); + } + return true; + } + protected void acquireIcons() { super.acquireIcons(); if (menuController != null) { diff --git a/OsmAnd/src/net/osmand/plus/mapcontextmenu/MapContextMenuFragment.java b/OsmAnd/src/net/osmand/plus/mapcontextmenu/MapContextMenuFragment.java index 0b520835c6..cde5b3f35b 100644 --- a/OsmAnd/src/net/osmand/plus/mapcontextmenu/MapContextMenuFragment.java +++ b/OsmAnd/src/net/osmand/plus/mapcontextmenu/MapContextMenuFragment.java @@ -462,16 +462,15 @@ public class MapContextMenuFragment extends BaseOsmAndFragment implements Downlo !nightMode ? R.color.icon_color : R.color.dashboard_subheader_text_dark)); buttonWaypoint.setContentDescription(getString(menu.getWaypointActionStringId())); AndroidUtils.setDashButtonBackground(getMapActivity(), buttonWaypoint, nightMode); - boolean shouldDeactivateMarkerButton = shouldDeactivateMarkerButton(menu.getObject()); - if (shouldDeactivateMarkerButton) { - deactivate(buttonWaypoint); - } else { + if (menu.isButtonWaypointEnabled()) { buttonWaypoint.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { menu.buttonWaypointPressed(); } }); + } else { + deactivate(buttonWaypoint); } final ImageButton buttonShare = (ImageButton) view.findViewById(R.id.context_menu_share_button); @@ -522,21 +521,6 @@ public class MapContextMenuFragment extends BaseOsmAndFragment implements Downlo return view; } - private boolean shouldDeactivateMarkerButton(Object object) { - MapMarkersHelper mapMarkersHelper = map.getApplication().getMapMarkersHelper(); - boolean shouldDeactivateMarkerButton = false; - if (object instanceof WptPt) { - if (mapMarkersHelper.getMapMarker((WptPt) object) != null) { - shouldDeactivateMarkerButton = true; - } - } else if (object instanceof FavouritePoint) { - if (mapMarkersHelper.getMapMarker((FavouritePoint) object) != null) { - shouldDeactivateMarkerButton = true; - } - } - return shouldDeactivateMarkerButton; - } - private void deactivate(View view) { view.setEnabled(false); view.setAlpha(0.5f); diff --git a/OsmAnd/src/net/osmand/plus/mapcontextmenu/MenuController.java b/OsmAnd/src/net/osmand/plus/mapcontextmenu/MenuController.java index 2b58f58a98..e7aa82332b 100644 --- a/OsmAnd/src/net/osmand/plus/mapcontextmenu/MenuController.java +++ b/OsmAnd/src/net/osmand/plus/mapcontextmenu/MenuController.java @@ -1,6 +1,7 @@ package net.osmand.plus.mapcontextmenu; import android.graphics.drawable.Drawable; +import android.graphics.drawable.LayerDrawable; import android.os.AsyncTask; import android.support.annotation.NonNull; import android.support.v4.content.ContextCompat; @@ -23,6 +24,7 @@ import net.osmand.map.OsmandRegions; import net.osmand.map.WorldRegion; import net.osmand.plus.GPXUtilities.WptPt; import net.osmand.plus.GpxSelectionHelper.GpxDisplayItem; +import net.osmand.plus.IconsCache; import net.osmand.plus.MapMarkersHelper.MapMarker; import net.osmand.plus.OsmandApplication; import net.osmand.plus.OsmandPlugin; @@ -95,6 +97,7 @@ public abstract class MenuController extends BaseMenuController { private PointDescription pointDescription; private LatLon latLon; private boolean active; + private Drawable showOnTopBarIcon; protected TitleButtonController leftTitleButtonController; protected TitleButtonController rightTitleButtonController; @@ -311,6 +314,10 @@ public abstract class MenuController extends BaseMenuController { this.currentMenuState = currentMenuState; } + public void setLeftTitleButtonController(TitleButtonController leftDownloadButtonController) { + this.leftTitleButtonController = leftDownloadButtonController; + } + public TitleButtonController getLeftTitleButtonController() { return leftTitleButtonController; } @@ -323,6 +330,10 @@ public abstract class MenuController extends BaseMenuController { return topRightTitleButtonController; } + public void setLeftSubtitleButtonController(TitleButtonController leftSubtitleButtonController) { + this.leftSubtitleButtonController = leftSubtitleButtonController; + } + public TitleButtonController getLeftSubtitleButtonController() { return leftSubtitleButtonController; } @@ -421,6 +432,21 @@ public abstract class MenuController extends BaseMenuController { ? R.string.shared_string_add_to_map_markers : R.string.context_menu_item_destination_point; } + public boolean isWaypointButtonEnabled() { + return true; + } + + protected Drawable getShowOnTopBarIcon() { + if (showOnTopBarIcon == null) { + IconsCache ic = getMapActivity().getMyApplication().getIconsCache(); + Drawable background = ic.getIcon(R.drawable.ic_action_device_top, + isLight() ? R.color.on_map_icon_color : R.color.ctx_menu_info_text_dark); + Drawable topbar = ic.getIcon(R.drawable.ic_action_device_topbar, R.color.dashboard_blue); + showOnTopBarIcon = new LayerDrawable(new Drawable[]{background, topbar}); + } + return showOnTopBarIcon; + } + public String getTypeStr() { return ""; } diff --git a/OsmAnd/src/net/osmand/plus/mapcontextmenu/controllers/FavouritePointMenuController.java b/OsmAnd/src/net/osmand/plus/mapcontextmenu/controllers/FavouritePointMenuController.java index bda2c9a510..e5f352735f 100644 --- a/OsmAnd/src/net/osmand/plus/mapcontextmenu/controllers/FavouritePointMenuController.java +++ b/OsmAnd/src/net/osmand/plus/mapcontextmenu/controllers/FavouritePointMenuController.java @@ -33,26 +33,7 @@ public class FavouritePointMenuController extends MenuController { final MapMarker mapMarker = markersHelper.getMapMarker(fav); if (mapMarker != null) { - leftTitleButtonController = new TitleButtonController() { - @Override - public void buttonPressed() { - markersHelper.moveMapMarkerToHistory(mapMarker); - getMapActivity().getContextMenu().close(); - } - }; - leftTitleButtonController.needColorizeIcon = false; - leftTitleButtonController.caption = getMapActivity().getString(R.string.mark_passed); - leftTitleButtonController.leftIconId = isLight() ? R.drawable.passed_icon_light : R.drawable.passed_icon_dark; - - leftSubtitleButtonController = new TitleButtonController() { - @Override - public void buttonPressed() { - markersHelper.moveMarkerToTop(mapMarker); - getMapActivity().getContextMenu().close(); - } - }; - leftSubtitleButtonController.caption = getMapActivity().getString(R.string.show_on_top_bar); - leftSubtitleButtonController.leftIcon = createShowOnTopbarIcon(); + MapMarkerMenuController.createMarkerButtons(this, mapActivity, mapMarker, getShowOnTopBarIcon()); } } @@ -98,6 +79,11 @@ public class FavouritePointMenuController extends MenuController { return FavoriteImageDrawable.getOrCreate(getMapActivity().getMyApplication(), fav.getColor(), false); } + @Override + public boolean isWaypointButtonEnabled() { + return getMapActivity().getMyApplication().getMapMarkersHelper().getMapMarker(fav) == null; + } + @Override public Drawable getSecondLineTypeIcon() { return getIcon(R.drawable.map_small_group); @@ -144,12 +130,4 @@ public class FavouritePointMenuController extends MenuController { addMyLocationToPlainItems(latLon); } } - - private Drawable createShowOnTopbarIcon() { - IconsCache ic = getMapActivity().getMyApplication().getIconsCache(); - Drawable background = ic.getIcon(R.drawable.ic_action_device_top, - isLight() ? R.color.on_map_icon_color : R.color.ctx_menu_info_text_dark); - Drawable topbar = ic.getIcon(R.drawable.ic_action_device_topbar, R.color.dashboard_blue); - return new LayerDrawable(new Drawable[]{background, topbar}); - } } diff --git a/OsmAnd/src/net/osmand/plus/mapcontextmenu/controllers/MapMarkerMenuController.java b/OsmAnd/src/net/osmand/plus/mapcontextmenu/controllers/MapMarkerMenuController.java index 7977446d9f..3fc86aa0eb 100644 --- a/OsmAnd/src/net/osmand/plus/mapcontextmenu/controllers/MapMarkerMenuController.java +++ b/OsmAnd/src/net/osmand/plus/mapcontextmenu/controllers/MapMarkerMenuController.java @@ -22,36 +22,7 @@ public class MapMarkerMenuController extends MenuController { super(new MenuBuilder(mapActivity), pointDescription, mapActivity); this.mapMarker = mapMarker; builder.setShowNearestWiki(true); - final MapMarkersHelper markersHelper = mapActivity.getMyApplication().getMapMarkersHelper(); - - leftTitleButtonController = new TitleButtonController() { - @Override - public void buttonPressed() { - markersHelper.moveMapMarkerToHistory(getMapMarker()); - getMapActivity().getContextMenu().close(); - } - }; - leftTitleButtonController.needColorizeIcon = false; - leftTitleButtonController.caption = getMapActivity().getString(R.string.mark_passed); - leftTitleButtonController.leftIconId = isLight() ? R.drawable.passed_icon_light : R.drawable.passed_icon_dark; - - leftSubtitleButtonController = new TitleButtonController() { - @Override - public void buttonPressed() { - markersHelper.moveMarkerToTop(getMapMarker()); - getMapActivity().getContextMenu().close(); - } - }; - leftSubtitleButtonController.caption = getMapActivity().getString(R.string.show_on_top_bar); - leftSubtitleButtonController.leftIcon = createShowOnTopbarIcon(); - } - - private Drawable createShowOnTopbarIcon() { - IconsCache ic = getMapActivity().getMyApplication().getIconsCache(); - Drawable background = ic.getIcon(R.drawable.ic_action_device_top, - isLight() ? R.color.on_map_icon_color : R.color.ctx_menu_info_text_dark); - Drawable topbar = ic.getIcon(R.drawable.ic_action_device_topbar, R.color.dashboard_blue); - return new LayerDrawable(new Drawable[]{background, topbar}); + createMarkerButtons(this, mapActivity, mapMarker, getShowOnTopBarIcon()); } @Override @@ -104,4 +75,31 @@ public class MapMarkerMenuController extends MenuController { public int getWaypointActionStringId() { return R.string.rename_marker; } + + public static void createMarkerButtons(MenuController menuController, final MapActivity mapActivity, final MapMarker mapMarker, Drawable leftIcon) { + final MapMarkersHelper markersHelper = mapActivity.getMyApplication().getMapMarkersHelper(); + + TitleButtonController leftTitleButtonController = menuController.new TitleButtonController() { + @Override + public void buttonPressed() { + markersHelper.moveMapMarkerToHistory(mapMarker); + mapActivity.getContextMenu().close(); + } + }; + leftTitleButtonController.needColorizeIcon = false; + leftTitleButtonController.caption = mapActivity.getString(R.string.mark_passed); + leftTitleButtonController.leftIconId = menuController.isLight() ? R.drawable.passed_icon_light : R.drawable.passed_icon_dark; + menuController.setLeftTitleButtonController(leftTitleButtonController); + + TitleButtonController leftSubtitleButtonController = menuController.new TitleButtonController() { + @Override + public void buttonPressed() { + markersHelper.moveMarkerToTop(mapMarker); + mapActivity.getContextMenu().close(); + } + }; + leftSubtitleButtonController.caption = mapActivity.getString(R.string.show_on_top_bar); + leftSubtitleButtonController.leftIcon = leftIcon; + menuController.setLeftSubtitleButtonController(leftSubtitleButtonController); + } } \ No newline at end of file diff --git a/OsmAnd/src/net/osmand/plus/mapcontextmenu/controllers/WptPtMenuController.java b/OsmAnd/src/net/osmand/plus/mapcontextmenu/controllers/WptPtMenuController.java index ee45d6a477..016ea7a9c5 100644 --- a/OsmAnd/src/net/osmand/plus/mapcontextmenu/controllers/WptPtMenuController.java +++ b/OsmAnd/src/net/osmand/plus/mapcontextmenu/controllers/WptPtMenuController.java @@ -28,26 +28,7 @@ public class WptPtMenuController extends MenuController { final MapMarker mapMarker = markersHelper.getMapMarker(wpt); if (mapMarker != null) { - leftTitleButtonController = new TitleButtonController() { - @Override - public void buttonPressed() { - markersHelper.moveMapMarkerToHistory(mapMarker); - getMapActivity().getContextMenu().close(); - } - }; - leftTitleButtonController.needColorizeIcon = false; - leftTitleButtonController.caption = getMapActivity().getString(R.string.mark_passed); - leftTitleButtonController.leftIconId = isLight() ? R.drawable.passed_icon_light : R.drawable.passed_icon_dark; - - leftSubtitleButtonController = new TitleButtonController() { - @Override - public void buttonPressed() { - markersHelper.moveMarkerToTop(mapMarker); - getMapActivity().getContextMenu().close(); - } - }; - leftSubtitleButtonController.caption = getMapActivity().getString(R.string.show_on_top_bar); - leftSubtitleButtonController.leftIcon = createShowOnTopbarIcon(); + MapMarkerMenuController.createMarkerButtons(this, mapActivity, mapMarker, getShowOnTopBarIcon()); } } @@ -100,6 +81,11 @@ public class WptPtMenuController extends MenuController { } } + @Override + public boolean isWaypointButtonEnabled() { + return getMapActivity().getMyApplication().getMapMarkersHelper().getMapMarker(wpt) == null; + } + @Override public String getTypeStr() { return wpt.category != null ? wpt.category : ""; @@ -109,12 +95,4 @@ public class WptPtMenuController extends MenuController { public String getCommonTypeStr() { return getMapActivity().getString(R.string.gpx_wpt); } - - private Drawable createShowOnTopbarIcon() { - IconsCache ic = getMapActivity().getMyApplication().getIconsCache(); - Drawable background = ic.getIcon(R.drawable.ic_action_device_top, - isLight() ? R.color.on_map_icon_color : R.color.ctx_menu_info_text_dark); - Drawable topbar = ic.getIcon(R.drawable.ic_action_device_topbar, R.color.dashboard_blue); - return new LayerDrawable(new Drawable[]{background, topbar}); - } }