From 7048f8c52d51c47ad432413d0cfe78e6f51a6c5e Mon Sep 17 00:00:00 2001 From: Skalii Date: Sat, 27 Mar 2021 22:51:59 +0200 Subject: [PATCH] fix dynamic updating graphs; some fixes; --- .../monitoring/TripRecordingBottomSheet.java | 55 +++--- .../plus/myplaces/GPXItemPagerAdapter.java | 173 +++++++++++------- .../plus/track/GpxBlockStatisticsBuilder.java | 5 + 3 files changed, 139 insertions(+), 94 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/monitoring/TripRecordingBottomSheet.java b/OsmAnd/src/net/osmand/plus/monitoring/TripRecordingBottomSheet.java index 21994e3cb7..aa2a5b6cb6 100644 --- a/OsmAnd/src/net/osmand/plus/monitoring/TripRecordingBottomSheet.java +++ b/OsmAnd/src/net/osmand/plus/monitoring/TripRecordingBottomSheet.java @@ -48,7 +48,6 @@ import net.osmand.plus.activities.SavingTrackHelper; import net.osmand.plus.base.MenuBottomSheetDialogFragment; import net.osmand.plus.base.bottomsheetmenu.BaseBottomSheetItem; import net.osmand.plus.helpers.AndroidUiHelper; -import net.osmand.plus.helpers.GpxUiHelper; import net.osmand.plus.mapcontextmenu.other.TrackChartPoints; import net.osmand.plus.myplaces.GPXItemPagerAdapter; import net.osmand.plus.myplaces.GPXTabItemType; @@ -94,7 +93,6 @@ public class TripRecordingBottomSheet extends MenuBottomSheetDialogFragment impl private View statusContainer; private AppCompatImageView trackAppearanceIcon; - private TrackDisplayHelper displayHelper; private TrackChartPoints trackChartPoints; private GPXItemPagerAdapter graphsAdapter; private int graphTabPosition = 0; @@ -157,7 +155,6 @@ public class TripRecordingBottomSheet extends MenuBottomSheetDialogFragment impl } }); - setupDisplayHelper(); segmentsTabs = itemView.findViewById(R.id.segments_container); createSegmentsTabs(segmentsTabs); @@ -186,8 +183,10 @@ public class TripRecordingBottomSheet extends MenuBottomSheetDialogFragment impl boolean wasTrackMonitored = !wasTrackMonitored(); if (!wasTrackMonitored) { blockStatisticsBuilder.stopUpdatingStatBlocks(); + stopUpdatingGraph(); } else { blockStatisticsBuilder.runUpdatingStatBlocksIfNeeded(); + runUpdatingGraph(); } settings.SAVE_GLOBAL_TRACK_TO_GPX.set(wasTrackMonitored); updateStatus(); @@ -256,10 +255,8 @@ public class TripRecordingBottomSheet extends MenuBottomSheetDialogFragment impl updateTrackIcon(app, trackAppearanceIcon); } if (key.equals(UPDATE_DYNAMIC_ITEMS)) { - blockStatisticsBuilder.stopUpdatingStatBlocks(); - blockStatisticsBuilder.runUpdatingStatBlocksIfNeeded(); - stopUpdatingGraph(); - runUpdatingGraph(); + blockStatisticsBuilder.restartUpdatingStatBlocks(); + restartUpdatingGraph(); } } } @@ -271,11 +268,11 @@ public class TripRecordingBottomSheet extends MenuBottomSheetDialogFragment impl } } - public void stopUpdatingGPS() { + private void stopUpdatingGPS() { handler.removeCallbacks(updatingGPS); } - public void runUpdatingGPS() { + private void runUpdatingGPS() { updatingGPS = new Runnable() { @Override public void run() { @@ -287,38 +284,30 @@ public class TripRecordingBottomSheet extends MenuBottomSheetDialogFragment impl handler.post(updatingGPS); } - public void stopUpdatingGraph() { + private void stopUpdatingGraph() { handler.removeCallbacks(updatingGraph); } - public void runUpdatingGraph() { + private void runUpdatingGraph() { updatingGraph = new Runnable() { @Override public void run() { int interval = app.getSettings().SAVE_GLOBAL_TRACK_INTERVAL.get(); - graphsAdapter.setGpxItem(GpxUiHelper.makeGpxDisplayItem(app, displayHelper.getGpx())); - graphsAdapter.fetchTabTypesIfNeeded(INIT_TAB_ITEMS); graphsAdapter.updateGraph(graphTabPosition); - AndroidUiHelper.updateVisibility(segmentsTabs, graphsAdapter.isVisible()); + AndroidUiHelper.updateVisibility(segmentsTabs, graphsAdapter.isTabsVisible()); handler.postDelayed(this, Math.max(GPS_UPDATE_INTERVAL, interval)); } }; handler.post(updatingGraph); } - private void setupDisplayHelper() { - displayHelper = new TrackDisplayHelper(app); - GPXFile gpxFile = getGPXFile(); - if (!selectedGpxFile.isShowCurrentTrack()) { - File file = new File(gpxFile.path); - displayHelper.setFile(file); - displayHelper.setGpxDataItem(app.getGpxDbHelper().getItem(file)); - } - displayHelper.setGpx(gpxFile); + private void restartUpdatingGraph() { + stopUpdatingGraph(); + runUpdatingGraph(); } private void createSegmentsTabs(ViewGroup viewGroup) { - View segmentView = SegmentGPXAdapter.createGpxTabsView(displayHelper, viewGroup, this, nightMode); + View segmentView = SegmentGPXAdapter.createGpxTabsView(null, viewGroup, this, nightMode); AndroidUiHelper.setVisibility(View.GONE, segmentView.findViewById(R.id.list_item_divider)); WrapContentHeightViewPager pager = segmentView.findViewById(R.id.pager); PagerSlidingTabStrip tabLayout = segmentView.findViewById(R.id.sliding_tabs); @@ -330,6 +319,7 @@ public class TripRecordingBottomSheet extends MenuBottomSheetDialogFragment impl public void onTabSelected(int position) { graphTabPosition = position; blockStatisticsBuilder.setTabItem(INIT_TAB_ITEMS[graphTabPosition]); + blockStatisticsBuilder.restartUpdatingStatBlocks(); } @Override @@ -339,8 +329,7 @@ public class TripRecordingBottomSheet extends MenuBottomSheetDialogFragment impl } }); - graphsAdapter = new GPXItemPagerAdapter(app, GpxUiHelper.makeGpxDisplayItem(app, displayHelper.getGpx()), - displayHelper, nightMode, this, true); + graphsAdapter = new GPXItemPagerAdapter(app, nightMode, this, true, true); graphsAdapter.setChartHMargin(getResources().getDimensionPixelSize(R.dimen.content_padding)); pager.setAdapter(graphsAdapter); @@ -618,11 +607,23 @@ public class TripRecordingBottomSheet extends MenuBottomSheetDialogFragment impl } } + private TrackDisplayHelper getDisplayHelper() { + TrackDisplayHelper displayHelper = new TrackDisplayHelper(app); + GPXFile gpxFile = getGPXFile(); + if (!selectedGpxFile.isShowCurrentTrack()) { + File file = new File(gpxFile.path); + displayHelper.setFile(file); + displayHelper.setGpxDataItem(app.getGpxDbHelper().getItem(file)); + } + displayHelper.setGpx(gpxFile); + return displayHelper; + } + @Override public void onPointSelected(TrkSegment segment, double lat, double lon) { if (trackChartPoints == null) { trackChartPoints = new TrackChartPoints(); - trackChartPoints.setGpx(displayHelper.getGpx()); + trackChartPoints.setGpx(getDisplayHelper().getGpx()); } MapActivity mapActivity = getMapActivity(); if (mapActivity != null) { diff --git a/OsmAnd/src/net/osmand/plus/myplaces/GPXItemPagerAdapter.java b/OsmAnd/src/net/osmand/plus/myplaces/GPXItemPagerAdapter.java index 984f588341..cea3454fbc 100644 --- a/OsmAnd/src/net/osmand/plus/myplaces/GPXItemPagerAdapter.java +++ b/OsmAnd/src/net/osmand/plus/myplaces/GPXItemPagerAdapter.java @@ -39,7 +39,6 @@ import net.osmand.plus.OsmandApplication; import net.osmand.plus.R; import net.osmand.plus.UiUtilities; import net.osmand.plus.UiUtilities.CustomRadioButtonType; -import net.osmand.plus.activities.SavingTrackHelper; import net.osmand.plus.helpers.AndroidUiHelper; import net.osmand.plus.helpers.GpxUiHelper; import net.osmand.plus.helpers.GpxUiHelper.GPXDataSetAxisType; @@ -52,8 +51,7 @@ import net.osmand.plus.views.controls.WrapContentHeightViewPager.ViewAtPositionI import net.osmand.util.Algorithms; import net.osmand.util.MapUtils; -import org.apache.commons.lang3.ArrayUtils; - +import java.io.File; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.ArrayList; @@ -82,6 +80,7 @@ public class GPXItemPagerAdapter extends PagerAdapter implements CustomTabProvid private WptPt selectedWpt; private TrkSegment segment; private GpxDisplayItem gpxItem; + private GPXTrackAnalysis analysis; private GPXTabItemType[] tabTypes; private SparseArray views = new SparseArray<>(); @@ -90,14 +89,45 @@ public class GPXItemPagerAdapter extends PagerAdapter implements CustomTabProvid private boolean chartClicked; private boolean nightMode; private boolean onlyGraphs; + private boolean recordingTrack = false; private int chartHMargin = 0; public void setChartHMargin(int chartHMargin) { this.chartHMargin = chartHMargin; } - public void setGpxItem(GpxDisplayItem gpxItem) { - this.gpxItem = gpxItem; + private SelectedGpxFile getCurrentTrack() { + return app.getSavingTrackHelper().getCurrentTrack(); + } + + private GPXFile getGpxFile() { + return recordingTrack ? getCurrentTrack().getGpxFile() : displayHelper.getGpx(); + } + + public GPXItemPagerAdapter(@NonNull OsmandApplication app, + boolean nightMode, + @NonNull SegmentActionsListener actionsListener, + boolean onlyGraphs, + boolean recordingTrack) { + super(); + this.app = app; + this.nightMode = nightMode; + this.actionsListener = actionsListener; + this.onlyGraphs = onlyGraphs; + this.recordingTrack = recordingTrack; + iconsCache = app.getUIUtilities(); + + displayHelper = new TrackDisplayHelper(app); + GPXFile gpxFile = getGpxFile(); + if (!getCurrentTrack().isShowCurrentTrack()) { + File file = new File(gpxFile.path); + displayHelper.setFile(file); + displayHelper.setGpxDataItem(app.getGpxDbHelper().getItem(file)); + } + displayHelper.setGpx(gpxFile); + + updateAnalysis(); + fetchTabTypes(); } public GPXItemPagerAdapter(@NonNull OsmandApplication app, @@ -114,17 +144,39 @@ public class GPXItemPagerAdapter extends PagerAdapter implements CustomTabProvid this.actionsListener = actionsListener; this.onlyGraphs = onlyGraphs; iconsCache = app.getUIUtilities(); + updateAnalysis(); fetchTabTypes(); } + private void updateAnalysis() { + analysis = null; + if (recordingTrack) { + GPXFile currentGpx = getCurrentTrack().getGpxFile(); + if (!currentGpx.isEmpty()) { + analysis = currentGpx.getAnalysis(0); + } + gpxItem = GpxUiHelper.makeGpxDisplayItem(app, currentGpx); + } else { + if (gpxItem != null) { + analysis = gpxItem.analysis; + } + } + } + private void fetchTabTypes() { List tabTypeList = new ArrayList<>(); - tabTypeList.add(GPXTabItemType.GPX_TAB_ITEM_GENERAL); - if (gpxItem != null && gpxItem.analysis != null) { - if (gpxItem.analysis.hasElevationData) { + if (recordingTrack) { + if (analysis != null && (analysis.hasElevationData || analysis.hasSpeedData)) { + tabTypeList.add(GPXTabItemType.GPX_TAB_ITEM_GENERAL); + } + } else { + tabTypeList.add(GPXTabItemType.GPX_TAB_ITEM_GENERAL); + } + if (analysis != null) { + if (analysis.hasElevationData) { tabTypeList.add(GPX_TAB_ITEM_ALTITUDE); } - if (gpxItem.analysis.isSpeedSpecified()) { + if (analysis.isSpeedSpecified()) { tabTypeList.add(GPX_TAB_ITEM_SPEED); } } @@ -134,17 +186,12 @@ public class GPXItemPagerAdapter extends PagerAdapter implements CustomTabProvid private List getDataSets(LineChart chart, GPXTabItemType tabType, LineGraphType firstType, LineGraphType secondType) { List dataSets = dataSetsMap.get(tabType); - GPXTrackAnalysis analysis = null; boolean withoutGaps = true; - SavingTrackHelper helper = app.getSavingTrackHelper(); - if (displayHelper.getGpx().equals(helper.getCurrentGpx())) { - SelectedGpxFile selectedGpxFile = helper.getCurrentTrack(); - GPXFile currentGpx = selectedGpxFile.getGpxFile(); - analysis = currentGpx.getAnalysis(0); - withoutGaps = !selectedGpxFile.isJoinSegments() + if (recordingTrack) { + GPXFile currentGpx = getGpxFile(); + withoutGaps = !getCurrentTrack().isJoinSegments() && (Algorithms.isEmpty(currentGpx.tracks) || currentGpx.tracks.get(0).generalTrack); } else if (gpxItem != null) { - analysis = gpxItem.analysis; GpxDataItem gpxDataItem = displayHelper.getGpxDataItem(); withoutGaps = gpxItem.isGeneralTrack() && gpxDataItem != null && !gpxDataItem.isJoinSegments(); } @@ -231,20 +278,17 @@ public class GPXItemPagerAdapter extends PagerAdapter implements CustomTabProvid LineChart chart = view.findViewById(R.id.chart); ViewGroup.MarginLayoutParams lp = (ViewGroup.MarginLayoutParams) chart.getLayoutParams(); AndroidUtils.setMargins(lp, chartHMargin, lp.topMargin, chartHMargin, lp.bottomMargin); - GPXFile gpxFile = displayHelper.getGpx(); - if (gpxFile != null && gpxItem != null) { - GPXTrackAnalysis analysis = gpxItem.analysis; + if (analysis != null) { setupChart(view, chart); - switch (tabType) { case GPX_TAB_ITEM_GENERAL: - setupGeneralTab(view, chart, analysis, gpxFile, position); + setupGeneralTab(view, chart, position); break; case GPX_TAB_ITEM_ALTITUDE: - setupAltitudeTab(view, chart, analysis, gpxFile, position); + setupAltitudeTab(view, chart, position); break; case GPX_TAB_ITEM_SPEED: - setupSpeedTab(view, chart, analysis, gpxFile, position); + setupSpeedTab(view, chart, position); break; } } @@ -277,7 +321,7 @@ public class GPXItemPagerAdapter extends PagerAdapter implements CustomTabProvid return view; } - private void setupSpeedTab(View view, LineChart chart, GPXTrackAnalysis analysis, GPXFile gpxFile, int position) { + private void setupSpeedTab(View view, LineChart chart, int position) { if (analysis != null && analysis.isSpeedSpecified()) { if (analysis.hasSpeedData) { GpxUiHelper.setupGPXChart(app, chart, CHART_LABEL_COUNT); @@ -349,7 +393,7 @@ public class GPXItemPagerAdapter extends PagerAdapter implements CustomTabProvid }); } - private void setupAltitudeTab(View view, LineChart chart, GPXTrackAnalysis analysis, GPXFile gpxFile, int position) { + private void setupAltitudeTab(View view, LineChart chart, int position) { if (analysis != null) { if (analysis.hasElevationData) { GpxUiHelper.setupGPXChart(app, chart, CHART_LABEL_COUNT); @@ -416,7 +460,7 @@ public class GPXItemPagerAdapter extends PagerAdapter implements CustomTabProvid } } - private void setupGeneralTab(View view, LineChart chart, GPXTrackAnalysis analysis, GPXFile gpxFile, int position) { + private void setupGeneralTab(View view, LineChart chart, int position) { if (analysis != null) { if (analysis.hasElevationData || analysis.hasSpeedData) { GpxUiHelper.setupGPXChart(app, chart, CHART_LABEL_COUNT); @@ -657,10 +701,12 @@ public class GPXItemPagerAdapter extends PagerAdapter implements CustomTabProvid @Override public void tabStylesUpdated(View tabsContainer, int currentPosition) { - ViewGroup.MarginLayoutParams params = (MarginLayoutParams) tabsContainer.getLayoutParams(); - params.height = app.getResources().getDimensionPixelSize(!onlyGraphs ? R.dimen.dialog_button_height : R.dimen.context_menu_buttons_bottom_height); - tabsContainer.setLayoutParams(params); - UiUtilities.updateCustomRadioButtons(app, tabsContainer, nightMode, getCustomRadioButtonType(currentPosition)); + if (getCount() > 0) { + ViewGroup.MarginLayoutParams params = (MarginLayoutParams) tabsContainer.getLayoutParams(); + params.height = app.getResources().getDimensionPixelSize(!onlyGraphs ? R.dimen.dialog_button_height : R.dimen.context_menu_buttons_bottom_height); + tabsContainer.setLayoutParams(params); + UiUtilities.updateCustomRadioButtons(app, tabsContainer, nightMode, getCustomRadioButtonType(currentPosition)); + } } private CustomRadioButtonType getCustomRadioButtonType(int index) { @@ -692,7 +738,6 @@ public class GPXItemPagerAdapter extends PagerAdapter implements CustomTabProvid void updateJoinGapsInfo(View view, int position) { if (view != null) { - GPXTrackAnalysis analysis = gpxItem.analysis; GPXTabItemType tabType = tabTypes[position]; boolean visible = gpxItem.isGeneralTrack() && analysis != null && tabType.equals(GPXTabItemType.GPX_TAB_ITEM_GENERAL); AndroidUiHelper.updateVisibility(view.findViewById(R.id.gpx_join_gaps_container), visible); @@ -731,50 +776,44 @@ public class GPXItemPagerAdapter extends PagerAdapter implements CustomTabProvid } } - public boolean isVisible() { - for (int i = 0; i < views.size(); i++) { - if (views.get(i).findViewById(R.id.chart).getVisibility() == View.VISIBLE) { - if (tabTypes[i] == GPX_TAB_ITEM_GENERAL && (gpxItem == null || gpxItem.analysis == null)) { - return false; + public boolean isTabsVisible() { + if (getCount() > 0 && views.size() > 0) { + for (int i = 0; i < getCount(); i++) { + LineChart lc = getViewAtPosition(i).findViewById(R.id.chart); + if (!lc.isEmpty() && !getGpxFile().isEmpty()) { + return true; } - return true; } } return false; } public void updateGraph(int position) { - LineGraphType firstType = tabTypes[position] == GPX_TAB_ITEM_SPEED ? SPEED : ALTITUDE; - LineGraphType secondType = null; - if (tabTypes[position] == GPX_TAB_ITEM_ALTITUDE) { - secondType = SLOPE; - } else if (tabTypes[position] == GPX_TAB_ITEM_GENERAL) { - secondType = SPEED; - } - - LineChart chart = getViewAtPosition(position).findViewById(R.id.chart); - List dataSets = getDataSets(chart, tabTypes[position], firstType, secondType); - boolean isEmptyDataSets = Algorithms.isEmpty(dataSets); - AndroidUiHelper.updateVisibility(chart, !isEmptyDataSets); - chart.clear(); - if (!isEmptyDataSets) { - chart.setData(new LineData(dataSets)); - } - if (chart.getAxisRight().getLabelCount() != CHART_LABEL_COUNT - || chart.getAxisLeft().getLabelCount() != CHART_LABEL_COUNT) { - GpxUiHelper.setupGPXChart(app, chart, CHART_LABEL_COUNT); - } - updateChart(chart); - notifyDataSetChanged(); - } - - public void fetchTabTypesIfNeeded(GPXTabItemType[] tabTypes) { - if (!ArrayUtils.isEquals(this.tabTypes, tabTypes)) { - fetchTabTypes(); - for (int i = 0; i < this.tabTypes.length; i++) { - updateGraph(i); + updateAnalysis(); + fetchTabTypes(); + if (getCount() > 0 && views.size() > 0) { + LineGraphType firstType = tabTypes[position] == GPX_TAB_ITEM_SPEED ? SPEED : ALTITUDE; + LineGraphType secondType = null; + if (tabTypes[position] == GPX_TAB_ITEM_ALTITUDE) { + secondType = SLOPE; + } else if (tabTypes[position] == GPX_TAB_ITEM_GENERAL) { + secondType = SPEED; } + LineChart chart = getViewAtPosition(position).findViewById(R.id.chart); + List dataSets = getDataSets(chart, tabTypes[position], firstType, secondType); + boolean isEmptyDataSets = Algorithms.isEmpty(dataSets); + AndroidUiHelper.updateVisibility(chart, !isEmptyDataSets); + chart.clear(); + if (!isEmptyDataSets) { + chart.setData(new LineData(dataSets)); + } + if (chart.getAxisRight().getLabelCount() != CHART_LABEL_COUNT + || chart.getAxisLeft().getLabelCount() != CHART_LABEL_COUNT) { + GpxUiHelper.setupGPXChart(app, chart, CHART_LABEL_COUNT); + } + updateChart(chart); } + notifyDataSetChanged(); } private TrkSegment getTrkSegment() { diff --git a/OsmAnd/src/net/osmand/plus/track/GpxBlockStatisticsBuilder.java b/OsmAnd/src/net/osmand/plus/track/GpxBlockStatisticsBuilder.java index 499706c83e..f62ce6a82a 100644 --- a/OsmAnd/src/net/osmand/plus/track/GpxBlockStatisticsBuilder.java +++ b/OsmAnd/src/net/osmand/plus/track/GpxBlockStatisticsBuilder.java @@ -131,6 +131,11 @@ public class GpxBlockStatisticsBuilder { } } + public void restartUpdatingStatBlocks() { + stopUpdatingStatBlocks(); + runUpdatingStatBlocksIfNeeded(); + } + public void initItems() { GPXFile gpxFile = getGPXFile(); if (app == null || gpxFile == null) {