From 1c22f9f814af7b10a617b48cf824719878c7fcf3 Mon Sep 17 00:00:00 2001 From: crimean Date: Sat, 20 Apr 2019 18:03:33 +0300 Subject: [PATCH] Fix map zoom when chart zooms at route details --- .../osmand/plus/base/ContextMenuFragment.java | 4 +- .../other/TrackDetailsMenu.java | 31 ++++++--- .../MapRouteInfoMenuFragment.java | 4 +- .../RouteDetailsFragment.java | 66 +++++++++++++++---- .../cards/CardChartListener.java | 8 +++ .../cards/RouteStatisticCard.java | 12 +++- 6 files changed, 99 insertions(+), 26 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/base/ContextMenuFragment.java b/OsmAnd/src/net/osmand/plus/base/ContextMenuFragment.java index 183134f0e4..3de5833f19 100644 --- a/OsmAnd/src/net/osmand/plus/base/ContextMenuFragment.java +++ b/OsmAnd/src/net/osmand/plus/base/ContextMenuFragment.java @@ -878,7 +878,7 @@ public abstract class ContextMenuFragment extends BaseOsmAndFragment { if (getActivity() == null) { return; } - calculateLayout(view); + calculateLayout(view, initLayout); if (!moving) { doLayoutMenu(); @@ -895,7 +895,7 @@ public abstract class ContextMenuFragment extends BaseOsmAndFragment { } } - protected void calculateLayout(View view) { + protected void calculateLayout(View view, boolean initLayout) { menuFullHeight = mainView.getHeight(); menuBottomViewHeight = menuFullHeight; menuFullHeightMax = view.findViewById(R.id.route_menu_cards_container).getHeight() + diff --git a/OsmAnd/src/net/osmand/plus/mapcontextmenu/other/TrackDetailsMenu.java b/OsmAnd/src/net/osmand/plus/mapcontextmenu/other/TrackDetailsMenu.java index 4d280d6960..932b78a48a 100644 --- a/OsmAnd/src/net/osmand/plus/mapcontextmenu/other/TrackDetailsMenu.java +++ b/OsmAnd/src/net/osmand/plus/mapcontextmenu/other/TrackDetailsMenu.java @@ -338,14 +338,13 @@ public class TrackDetailsMenu { int tileBoxWidthPx = 0; int tileBoxHeightPx = 0; - TrackDetailsMenuFragment fragment = getMenuFragment(); - if (fragment != null) { - boolean portrait = AndroidUiHelper.isOrientationPortrait(mapActivity); - if (!portrait) { - tileBoxWidthPx = tb.getPixWidth() - fragment.getWidth(); - } else { - tileBoxHeightPx = tb.getPixHeight() - fragment.getHeight(); - } + boolean portrait = AndroidUiHelper.isOrientationPortrait(mapActivity); + if (!portrait) { + int width = getFragmentWidth(); + tileBoxWidthPx = width != -1 ? tb.getPixWidth() - width : 0; + } else { + int height = getFragmentHeight(); + tileBoxHeightPx = height != -1 ? tb.getPixHeight() - height : 0; } if (tileBoxHeightPx > 0 || tileBoxWidthPx > 0) { if (forceFit) { @@ -363,6 +362,22 @@ public class TrackDetailsMenu { } } + protected int getFragmentWidth() { + TrackDetailsMenuFragment fragment = getMenuFragment(); + if (fragment != null) { + return fragment.getWidth(); + } + return -1; + } + + protected int getFragmentHeight() { + TrackDetailsMenuFragment fragment = getMenuFragment(); + if (fragment != null) { + return fragment.getHeight(); + } + return -1; + } + public void refreshChart(LineChart chart, boolean forceFit) { MapActivity mapActivity = getMapActivity(); GpxDisplayItem gpxItem = getGpxItem(); diff --git a/OsmAnd/src/net/osmand/plus/routepreparationmenu/MapRouteInfoMenuFragment.java b/OsmAnd/src/net/osmand/plus/routepreparationmenu/MapRouteInfoMenuFragment.java index 12e5f2ea51..d0b11b84b5 100644 --- a/OsmAnd/src/net/osmand/plus/routepreparationmenu/MapRouteInfoMenuFragment.java +++ b/OsmAnd/src/net/osmand/plus/routepreparationmenu/MapRouteInfoMenuFragment.java @@ -246,11 +246,11 @@ public class MapRouteInfoMenuFragment extends ContextMenuFragment { } @Override - protected void calculateLayout(View view) { + protected void calculateLayout(View view, boolean initLayout) { menuTitleHeight = view.findViewById(R.id.route_menu_top_shadow_all).getHeight() + view.findViewById(R.id.control_buttons).getHeight() - view.findViewById(R.id.buttons_shadow).getHeight(); - super.calculateLayout(view); + super.calculateLayout(view, initLayout); } private void adjustMapPosition(int y) { diff --git a/OsmAnd/src/net/osmand/plus/routepreparationmenu/RouteDetailsFragment.java b/OsmAnd/src/net/osmand/plus/routepreparationmenu/RouteDetailsFragment.java index 15ed87f13b..fd7a83fddc 100644 --- a/OsmAnd/src/net/osmand/plus/routepreparationmenu/RouteDetailsFragment.java +++ b/OsmAnd/src/net/osmand/plus/routepreparationmenu/RouteDetailsFragment.java @@ -35,6 +35,8 @@ import com.github.mikephil.charting.charts.LineChart; import com.github.mikephil.charting.data.Entry; import com.github.mikephil.charting.highlight.Highlight; import com.github.mikephil.charting.interfaces.dataprovider.BarDataProvider; +import com.github.mikephil.charting.listener.ChartTouchListener; +import com.github.mikephil.charting.listener.ChartTouchListener.ChartGesture; import com.github.mikephil.charting.renderer.HorizontalBarChartRenderer; import com.github.mikephil.charting.utils.ViewPortHandler; @@ -120,12 +122,25 @@ public class RouteDetailsFragment extends ContextMenuFragment implements PublicT private RouteDetailsFragmentListener routeDetailsListener; private RouteStatisticCard statisticCard; private List routeInfoCards = new ArrayList<>(); - private TrackDetailsMenu trackDetailsMenu; + private RouteDetailsMenu routeDetailsMenu; public interface RouteDetailsFragmentListener { void onNavigationRequested(); } + private class RouteDetailsMenu extends TrackDetailsMenu { + + @Override + protected int getFragmentWidth() { + return getWidth(); + } + + @Override + protected int getFragmentHeight() { + return getMenuFullHeight(); + } + } + @Override public int getMainLayoutId() { return R.layout.route_info_layout; @@ -186,8 +201,8 @@ public class RouteDetailsFragment extends ContextMenuFragment implements PublicT MapActivity mapActivity = getMapActivity(); if (mapActivity != null && isPortrait()) { mapActivity.findViewById(R.id.bottom_controls_container).setVisibility(View.GONE); - if (trackDetailsMenu != null) { - trackDetailsMenu.setMapActivity(mapActivity); + if (routeDetailsMenu != null) { + routeDetailsMenu.setMapActivity(mapActivity); } } } @@ -197,8 +212,8 @@ public class RouteDetailsFragment extends ContextMenuFragment implements PublicT MapActivity mapActivity = getMapActivity(); if (mapActivity != null && isPortrait()) { mapActivity.findViewById(R.id.bottom_controls_container).setVisibility(View.VISIBLE); - if (trackDetailsMenu != null) { - trackDetailsMenu.setMapActivity(null); + if (routeDetailsMenu != null) { + routeDetailsMenu.setMapActivity(null); } } super.onPause(); @@ -376,17 +391,24 @@ public class RouteDetailsFragment extends ContextMenuFragment implements PublicT addRouteCard(cardsContainer, routeSmoothnessCard); } } - trackDetailsMenu = new TrackDetailsMenu(); + routeDetailsMenu = new RouteDetailsMenu(); GpxDisplayItem gpxItem = statisticCard.getGpxItem(); if (gpxItem != null) { - trackDetailsMenu.setGpxItem(gpxItem); + routeDetailsMenu.setGpxItem(gpxItem); } - trackDetailsMenu.setMapActivity(mapActivity); + routeDetailsMenu.setMapActivity(mapActivity); LineChart chart = statisticCard.getChart(); if (chart != null) { chart.setExtraRightOffset(16); chart.setExtraLeftOffset(16); - trackDetailsMenu.refreshChart(chart, true); + } + } + + @Override + protected void calculateLayout(View view, boolean initLayout) { + super.calculateLayout(view, initLayout); + if (!initLayout && getCurrentMenuState() != MenuState.FULL_SCREEN) { + refreshChart(false); } } @@ -1644,12 +1666,13 @@ public class RouteDetailsFragment extends ContextMenuFragment implements PublicT } } - private void refreshChart() { + private void refreshChart(boolean forceFit) { MapActivity mapActivity = getMapActivity(); - if (mapActivity != null && trackDetailsMenu != null && statisticCard != null) { + if (mapActivity != null && routeDetailsMenu != null && statisticCard != null && + !mapActivity.getMyApplication().getRoutingHelper().isFollowingMode()) { LineChart chart = statisticCard.getChart(); if (chart != null) { - trackDetailsMenu.refreshChart(chart, false); + routeDetailsMenu.refreshChart(chart, forceFit); mapActivity.refreshMap(); } } @@ -1670,7 +1693,7 @@ public class RouteDetailsFragment extends ContextMenuFragment implements PublicT @Override public void onValueSelected(BaseCard card, Entry e, Highlight h) { - refreshChart(); + refreshChart(false); highlightRouteInfoCharts(h); } @@ -1679,6 +1702,23 @@ public class RouteDetailsFragment extends ContextMenuFragment implements PublicT highlightRouteInfoCharts(null); } + @Override + public void onChartGestureStart(BaseCard card, MotionEvent me, ChartGesture lastPerformedGesture) { + } + + @Override + public void onChartGestureEnd(BaseCard card, MotionEvent me, ChartGesture lastPerformedGesture, boolean hasTranslated) { + if ((lastPerformedGesture == ChartGesture.DRAG && hasTranslated) || + lastPerformedGesture == ChartGesture.X_ZOOM || + lastPerformedGesture == ChartGesture.Y_ZOOM || + lastPerformedGesture == ChartGesture.PINCH_ZOOM || + lastPerformedGesture == ChartGesture.DOUBLE_TAP || + lastPerformedGesture == ChartGesture.ROTATE) { + + refreshChart(true); + } + } + public static class CumulativeInfo { public int distance; public int time; diff --git a/OsmAnd/src/net/osmand/plus/routepreparationmenu/cards/CardChartListener.java b/OsmAnd/src/net/osmand/plus/routepreparationmenu/cards/CardChartListener.java index aa6193b034..6b73428631 100644 --- a/OsmAnd/src/net/osmand/plus/routepreparationmenu/cards/CardChartListener.java +++ b/OsmAnd/src/net/osmand/plus/routepreparationmenu/cards/CardChartListener.java @@ -1,7 +1,11 @@ package net.osmand.plus.routepreparationmenu.cards; +import android.view.MotionEvent; + import com.github.mikephil.charting.data.Entry; import com.github.mikephil.charting.highlight.Highlight; +import com.github.mikephil.charting.listener.ChartTouchListener; +import com.github.mikephil.charting.listener.ChartTouchListener.ChartGesture; public interface CardChartListener { /** @@ -17,4 +21,8 @@ public interface CardChartListener { * Called when nothing has been selected or an "un-select" has been made. */ void onNothingSelected(BaseCard card); + + void onChartGestureStart(BaseCard card, MotionEvent me, ChartGesture lastPerformedGesture); + + void onChartGestureEnd(BaseCard card, MotionEvent me, ChartGesture lastPerformedGesture, boolean hasTranslated); } diff --git a/OsmAnd/src/net/osmand/plus/routepreparationmenu/cards/RouteStatisticCard.java b/OsmAnd/src/net/osmand/plus/routepreparationmenu/cards/RouteStatisticCard.java index 41c17a15ac..ce7a0a5cc6 100644 --- a/OsmAnd/src/net/osmand/plus/routepreparationmenu/cards/RouteStatisticCard.java +++ b/OsmAnd/src/net/osmand/plus/routepreparationmenu/cards/RouteStatisticCard.java @@ -278,16 +278,21 @@ public class RouteStatisticCard extends BaseCard { }); mChart.setOnChartGestureListener(new OnChartGestureListener() { - + boolean hasTranslated = false; float highlightDrawX = -1; @Override public void onChartGestureStart(MotionEvent me, ChartGesture lastPerformedGesture) { + hasTranslated = false; if (mChart.getHighlighted() != null && mChart.getHighlighted().length > 0) { highlightDrawX = mChart.getHighlighted()[0].getDrawX(); } else { highlightDrawX = -1; } + CardChartListener chartListener = getChartListener(); + if (chartListener != null) { + chartListener.onChartGestureStart(RouteStatisticCard.this, me, lastPerformedGesture); + } } @Override @@ -299,6 +304,10 @@ public class RouteStatisticCard extends BaseCard { } else { gpxItem.chartHighlightPos = -1; } + CardChartListener chartListener = getChartListener(); + if (chartListener != null) { + chartListener.onChartGestureEnd(RouteStatisticCard.this, me, lastPerformedGesture, hasTranslated); + } } @Override @@ -323,6 +332,7 @@ public class RouteStatisticCard extends BaseCard { @Override public void onChartTranslate(MotionEvent me, float dX, float dY) { + hasTranslated = true; if (highlightDrawX != -1) { Highlight h = mChart.getHighlightByTouchPoint(highlightDrawX, 0f); if (h != null) {