From beddaf82090a381b8ac244a18df90aee2399e94b Mon Sep 17 00:00:00 2001 From: nazar-kutz Date: Thu, 5 Nov 2020 01:02:16 +0200 Subject: [PATCH] Fix chart on map --- .../other/TrackDetailsMenu.java | 11 ++++-- .../plus/measurementtool/GraphsCard.java | 21 +++++++++- .../MeasurementToolFragment.java | 13 ++++++- .../plus/measurementtool/PointsCard.java | 4 +- .../graph/GraphAdapterHelper.java | 38 +++++++++++-------- .../RouteDetailsFragment.java | 11 ++---- .../osmand/plus/views/layers/RouteLayer.java | 16 +++++++- 7 files changed, 82 insertions(+), 32 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/mapcontextmenu/other/TrackDetailsMenu.java b/OsmAnd/src/net/osmand/plus/mapcontextmenu/other/TrackDetailsMenu.java index 1e441071a4..a45bfb27e8 100644 --- a/OsmAnd/src/net/osmand/plus/mapcontextmenu/other/TrackDetailsMenu.java +++ b/OsmAnd/src/net/osmand/plus/mapcontextmenu/other/TrackDetailsMenu.java @@ -125,8 +125,7 @@ public class TrackDetailsMenu { hidding = true; fragment.dismiss(backPressed); } else { - segment = null; - trackChartPoints = null; + reset(); } } @@ -274,8 +273,7 @@ public class TrackDetailsMenu { if (hidding) { hidding = false; visible = false; - segment = null; - trackChartPoints = null; + reset(); } } @@ -532,6 +530,11 @@ public class TrackDetailsMenu { fitTrackOnMap(chart, location, forceFit); } + public void reset() { + segment = null; + trackChartPoints = null; + } + private List getXAxisPoints(LineChart chart) { float[] entries = chart.getXAxis().mEntries; LineData lineData = chart.getLineData(); diff --git a/OsmAnd/src/net/osmand/plus/measurementtool/GraphsCard.java b/OsmAnd/src/net/osmand/plus/measurementtool/GraphsCard.java index 69277f19a6..3b2e979f55 100644 --- a/OsmAnd/src/net/osmand/plus/measurementtool/GraphsCard.java +++ b/OsmAnd/src/net/osmand/plus/measurementtool/GraphsCard.java @@ -34,6 +34,7 @@ import net.osmand.plus.measurementtool.graph.CustomGraphAdapter; import net.osmand.plus.measurementtool.graph.CustomGraphAdapter.LegendViewType; import net.osmand.plus.measurementtool.graph.BaseGraphAdapter; import net.osmand.plus.measurementtool.graph.GraphAdapterHelper; +import net.osmand.plus.measurementtool.graph.GraphAdapterHelper.RefreshMapCallback; import net.osmand.plus.routepreparationmenu.RouteDetailsFragment; import net.osmand.plus.routepreparationmenu.cards.BaseCard; import net.osmand.router.RouteSegmentResult; @@ -59,6 +60,7 @@ public class GraphsCard extends BaseCard implements OnUpdateAdditionalInfoListen private MeasurementEditingContext editingCtx; private MeasurementToolFragment fragment; private TrackDetailsMenu trackDetailsMenu; + private RefreshMapCallback refreshMapCallback; private GPXFile gpxFile; private GPXTrackAnalysis analysis; private GpxDisplayItem gpxItem; @@ -105,7 +107,7 @@ public class GraphsCard extends BaseCard implements OnUpdateAdditionalInfoListen }); GraphAdapterHelper.bindGraphAdapters(commonGraphAdapter, Arrays.asList((BaseGraphAdapter) customGraphAdapter), (ViewGroup) view); - GraphAdapterHelper.bindToMap(commonGraphAdapter, mapActivity, trackDetailsMenu); + refreshMapCallback = GraphAdapterHelper.bindToMap(commonGraphAdapter, mapActivity, trackDetailsMenu); fullUpdate(); } @@ -116,7 +118,9 @@ public class GraphsCard extends BaseCard implements OnUpdateAdditionalInfoListen @Override public void onUpdateAdditionalInfo() { - fullUpdate(); + if (editingCtx != null) { + fullUpdate(); + } } private void fullUpdate() { @@ -126,6 +130,7 @@ public class GraphsCard extends BaseCard implements OnUpdateAdditionalInfoListen updateMenu(); } updateView(); + updateChartOnMap(); } private void updateMenu() { @@ -302,6 +307,13 @@ public class GraphsCard extends BaseCard implements OnUpdateAdditionalInfoListen } } + private void updateChartOnMap() { + if (hasVisibleGraph()) { + trackDetailsMenu.reset(); + refreshMapCallback.refreshMap(false); + } + } + private void addCommonType(int titleId, boolean canBeCalculated, boolean hasData, @@ -346,6 +358,11 @@ public class GraphsCard extends BaseCard implements OnUpdateAdditionalInfoListen return fragment.isProgressBarVisible(); } + public boolean hasVisibleGraph() { + return (commonGraphContainer != null && commonGraphContainer.getVisibility() == View.VISIBLE) + || (customGraphContainer != null && customGraphContainer.getVisibility() == View.VISIBLE); + } + private abstract class GraphType { private String title; private boolean canBeCalculated; diff --git a/OsmAnd/src/net/osmand/plus/measurementtool/MeasurementToolFragment.java b/OsmAnd/src/net/osmand/plus/measurementtool/MeasurementToolFragment.java index c92d028f02..16c8e2309c 100644 --- a/OsmAnd/src/net/osmand/plus/measurementtool/MeasurementToolFragment.java +++ b/OsmAnd/src/net/osmand/plus/measurementtool/MeasurementToolFragment.java @@ -553,8 +553,13 @@ public class MeasurementToolFragment extends BaseOsmAndFragment implements Route } private void updateAdditionalInfoView() { - if (visibleCard instanceof OnUpdateAdditionalInfoListener) { - ((OnUpdateAdditionalInfoListener) visibleCard).onUpdateAdditionalInfo(); + updateAdditionalInfoView(pointsCard); + updateAdditionalInfoView(graphsCard); + } + + private void updateAdditionalInfoView(OnUpdateAdditionalInfoListener listener) { + if (listener != null) { + listener.onUpdateAdditionalInfo(); } } @@ -1514,6 +1519,10 @@ public class MeasurementToolFragment extends BaseOsmAndFragment implements Route return type.equals(currentAdditionalInfoType); } + public boolean hasVisibleGraph() { + return graphsCard != null && graphsCard.hasVisibleGraph(); + } + private String getSuggestedFileName() { GpxData gpxData = editingCtx.getGpxData(); String displayedName = null; diff --git a/OsmAnd/src/net/osmand/plus/measurementtool/PointsCard.java b/OsmAnd/src/net/osmand/plus/measurementtool/PointsCard.java index 9d14598856..f3b55473bc 100644 --- a/OsmAnd/src/net/osmand/plus/measurementtool/PointsCard.java +++ b/OsmAnd/src/net/osmand/plus/measurementtool/PointsCard.java @@ -24,7 +24,9 @@ public class PointsCard extends BaseCard implements OnUpdateAdditionalInfoListen @Override public void onUpdateAdditionalInfo() { - adapter.notifyDataSetChanged(); + if (adapter != null) { + adapter.notifyDataSetChanged(); + } } @Override diff --git a/OsmAnd/src/net/osmand/plus/measurementtool/graph/GraphAdapterHelper.java b/OsmAnd/src/net/osmand/plus/measurementtool/graph/GraphAdapterHelper.java index efacad9d50..69eb02eec7 100644 --- a/OsmAnd/src/net/osmand/plus/measurementtool/graph/GraphAdapterHelper.java +++ b/OsmAnd/src/net/osmand/plus/measurementtool/graph/GraphAdapterHelper.java @@ -5,6 +5,8 @@ import android.view.MotionEvent; import android.view.View; import android.view.ViewGroup; +import androidx.annotation.NonNull; + import com.github.mikephil.charting.charts.BarChart; import com.github.mikephil.charting.charts.LineChart; import com.github.mikephil.charting.data.Entry; @@ -96,15 +98,27 @@ public class GraphAdapterHelper { } } - public static void bindToMap(final CommonGraphAdapter graphAdapter, - final MapActivity mapActivity, - final TrackDetailsMenu detailsMenu) { + public static RefreshMapCallback bindToMap(@NonNull final CommonGraphAdapter graphAdapter, + @NonNull final MapActivity mapActivity, + @NonNull final TrackDetailsMenu detailsMenu) { + final RefreshMapCallback refreshMapCallback = new RefreshMapCallback() { + @Override + public void refreshMap(boolean forceFit) { + LineChart chart = graphAdapter.getChart(); + OsmandApplication app = mapActivity.getMyApplication(); + if (!app.getRoutingHelper().isFollowingMode()) { + detailsMenu.refreshChart(chart, forceFit); + mapActivity.refreshMap(); + } + } + }; + graphAdapter.addValueSelectedListener(BIND_TO_MAP_KEY, new CommonGraphAdapter.ExternalValueSelectedListener() { @Override public void onValueSelected(Entry e, Highlight h) { - refreshChart(mapActivity, graphAdapter.getChart(), detailsMenu, false); + refreshMapCallback.refreshMap(false); } @Override @@ -125,21 +139,15 @@ public class GraphAdapterHelper { lastPerformedGesture == ChartTouchListener.ChartGesture.PINCH_ZOOM || lastPerformedGesture == ChartTouchListener.ChartGesture.DOUBLE_TAP || lastPerformedGesture == ChartTouchListener.ChartGesture.ROTATE) { - refreshChart(mapActivity, graphAdapter.getChart(), detailsMenu, true); + refreshMapCallback.refreshMap(true); } } }); + + return refreshMapCallback; } - public static void refreshChart(MapActivity mapActivity, - LineChart chart, - TrackDetailsMenu menu, - boolean forceFit) { - if (mapActivity == null || chart == null || menu == null) return; - OsmandApplication app = mapActivity.getMyApplication(); - if (!app.getRoutingHelper().isFollowingMode()) { - menu.refreshChart(chart, forceFit); - mapActivity.refreshMap(); - } + public interface RefreshMapCallback { + void refreshMap(boolean forceFit); } } diff --git a/OsmAnd/src/net/osmand/plus/routepreparationmenu/RouteDetailsFragment.java b/OsmAnd/src/net/osmand/plus/routepreparationmenu/RouteDetailsFragment.java index c6dcac20a6..780186b068 100644 --- a/OsmAnd/src/net/osmand/plus/routepreparationmenu/RouteDetailsFragment.java +++ b/OsmAnd/src/net/osmand/plus/routepreparationmenu/RouteDetailsFragment.java @@ -60,6 +60,7 @@ import net.osmand.plus.mapcontextmenu.other.TrackDetailsMenu; import net.osmand.plus.measurementtool.graph.BaseGraphAdapter; import net.osmand.plus.measurementtool.graph.CommonGraphAdapter; import net.osmand.plus.measurementtool.graph.GraphAdapterHelper; +import net.osmand.plus.measurementtool.graph.GraphAdapterHelper.RefreshMapCallback; import net.osmand.plus.render.MapRenderRepositories; import net.osmand.plus.routepreparationmenu.cards.BaseCard; import net.osmand.plus.routepreparationmenu.cards.BaseCard.CardListener; @@ -113,6 +114,7 @@ public class RouteDetailsFragment extends ContextMenuFragment private RouteStatisticCard statisticCard; private List routeInfoCards = new ArrayList<>(); private RouteDetailsMenu routeDetailsMenu; + private RefreshMapCallback refreshMapCallback; public interface RouteDetailsFragmentListener { void onNavigationRequested(); @@ -336,7 +338,7 @@ public class RouteDetailsFragment extends ContextMenuFragment CommonGraphAdapter mainGraphAdapter = statisticCard.getGraphAdapter(); if (mainGraphAdapter != null) { GraphAdapterHelper.bindGraphAdapters(mainGraphAdapter, getRouteInfoCardsGraphAdapters(), getMainView()); - GraphAdapterHelper.bindToMap(mainGraphAdapter, mapActivity, routeDetailsMenu); + refreshMapCallback = GraphAdapterHelper.bindToMap(mainGraphAdapter, mapActivity, routeDetailsMenu); } } @@ -369,12 +371,7 @@ public class RouteDetailsFragment extends ContextMenuFragment protected void calculateLayout(View view, boolean initLayout) { super.calculateLayout(view, initLayout); if (!initLayout && getCurrentMenuState() != MenuState.FULL_SCREEN) { - MapActivity mapActivity = getMapActivity(); - CommonGraphAdapter mainGraphAdapter = statisticCard.getGraphAdapter(); - if (mainGraphAdapter != null) { - LineChart chart = mainGraphAdapter.getChart(); - GraphAdapterHelper.refreshChart(mapActivity, chart, routeDetailsMenu, false); - } + refreshMapCallback.refreshMap(false); } } diff --git a/OsmAnd/src/net/osmand/plus/views/layers/RouteLayer.java b/OsmAnd/src/net/osmand/plus/views/layers/RouteLayer.java index 8491f24a67..f966b7d897 100644 --- a/OsmAnd/src/net/osmand/plus/views/layers/RouteLayer.java +++ b/OsmAnd/src/net/osmand/plus/views/layers/RouteLayer.java @@ -26,7 +26,9 @@ import net.osmand.data.QuadRect; import net.osmand.data.RotatedTileBox; import net.osmand.data.TransportStop; import net.osmand.plus.R; +import net.osmand.plus.activities.MapActivity; import net.osmand.plus.mapcontextmenu.other.TrackChartPoints; +import net.osmand.plus.measurementtool.MeasurementToolFragment; import net.osmand.plus.profiles.LocationIcon; import net.osmand.plus.routing.RouteCalculationResult; import net.osmand.plus.routing.RouteDirectionInfo; @@ -158,7 +160,8 @@ public class RouteLayer extends OsmandMapLayer implements ContextMenuLayer.ICont @Override public void onPrepareBufferImage(Canvas canvas, RotatedTileBox tileBox, DrawSettings settings) { if ((helper.isPublicTransportMode() && transportHelper.getRoutes() != null) || - (helper.getFinalLocation() != null && helper.getRoute().isCalculated())) { + (helper.getFinalLocation() != null && helper.getRoute().isCalculated()) || + isPlanRouteGraphsAvailable()) { updateAttrs(settings, tileBox); @@ -202,6 +205,17 @@ public class RouteLayer extends OsmandMapLayer implements ContextMenuLayer.ICont } + private boolean isPlanRouteGraphsAvailable() { + if (view.getContext() instanceof MapActivity) { + MapActivity mapActivity = (MapActivity) view.getContext(); + MeasurementToolFragment fragment = mapActivity.getMeasurementToolFragment(); + if (fragment != null) { + return fragment.hasVisibleGraph(); + } + } + return false; + } + private void updateAttrs(DrawSettings settings, RotatedTileBox tileBox) { boolean updatePaints = attrs.updatePaints(view.getApplication(), settings, tileBox); attrs.isPaint3 = false;