From 4038f430c596fb0966bdf92d5e3e657adbc65501 Mon Sep 17 00:00:00 2001 From: Skalii Date: Wed, 24 Mar 2021 15:09:13 +0200 Subject: [PATCH 1/6] add dynamic update for charts --- .../monitoring/TripRecordingBottomSheet.java | 76 +++++++----- .../TripRecordingClearDataBottomSheet.java | 4 + .../TripRecordingOptionsBottomSheet.java | 12 ++ .../plus/myplaces/GPXItemPagerAdapter.java | 84 ++++++++++++-- .../plus/track/GpxBlockStatisticsBuilder.java | 108 +++++++++--------- 5 files changed, 190 insertions(+), 94 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/monitoring/TripRecordingBottomSheet.java b/OsmAnd/src/net/osmand/plus/monitoring/TripRecordingBottomSheet.java index 8db1508454..2de4293238 100644 --- a/OsmAnd/src/net/osmand/plus/monitoring/TripRecordingBottomSheet.java +++ b/OsmAnd/src/net/osmand/plus/monitoring/TripRecordingBottomSheet.java @@ -49,6 +49,7 @@ 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; import net.osmand.plus.myplaces.SegmentActionsListener; import net.osmand.plus.myplaces.SegmentGPXAdapter; import net.osmand.plus.settings.backend.OsmandSettings; @@ -69,18 +70,19 @@ import java.util.List; import static net.osmand.AndroidUtils.getSecondaryTextColorId; import static net.osmand.AndroidUtils.setPadding; import static net.osmand.plus.UiUtilities.CompoundButtonType.GLOBAL; -import static net.osmand.plus.track.GpxBlockStatisticsBuilder.INIT_BLOCKS_ALTITUDE; -import static net.osmand.plus.track.GpxBlockStatisticsBuilder.INIT_BLOCKS_GENERAL; -import static net.osmand.plus.track.GpxBlockStatisticsBuilder.INIT_BLOCKS_SPEED; +import static net.osmand.plus.myplaces.GPXTabItemType.GPX_TAB_ITEM_ALTITUDE; +import static net.osmand.plus.myplaces.GPXTabItemType.GPX_TAB_ITEM_GENERAL; +import static net.osmand.plus.myplaces.GPXTabItemType.GPX_TAB_ITEM_SPEED; public class TripRecordingBottomSheet extends MenuBottomSheetDialogFragment implements SegmentActionsListener { public static final String TAG = TripRecordingBottomSheet.class.getSimpleName(); private static final Log LOG = PlatformUtil.getLog(TripRecordingBottomSheet.class); public static final String UPDATE_TRACK_ICON = "update_track_icon"; + public static final String UPDATE_DYNAMIC_ITEMS = "update_dynamic_items"; private static final int GPS_UPDATE_INTERVAL = 1000; - private static final String[] INIT_BLOCKS_KEYS = - new String[]{INIT_BLOCKS_GENERAL, INIT_BLOCKS_ALTITUDE, INIT_BLOCKS_SPEED}; + public static final GPXTabItemType[] INIT_TAB_ITEMS = + new GPXTabItemType[]{GPX_TAB_ITEM_GENERAL, GPX_TAB_ITEM_ALTITUDE, GPX_TAB_ITEM_SPEED}; private OsmandApplication app; private OsmandSettings settings; @@ -89,16 +91,18 @@ public class TripRecordingBottomSheet extends MenuBottomSheetDialogFragment impl private View statusContainer; private AppCompatImageView trackAppearanceIcon; - private LinearLayout segmentsContainer; private TrackDisplayHelper displayHelper; private TrackChartPoints trackChartPoints; private GPXItemPagerAdapter graphsAdapter; + private int graphTabPosition = 0; + private ViewGroup segmentsTabs; private GpxBlockStatisticsBuilder blockStatisticsBuilder; private SelectedGpxFile selectedGpxFile; private final Handler handler = new Handler(); private Runnable updatingGPS; + private Runnable updatingGraph; private GPXFile getGPXFile() { return selectedGpxFile.getGpxFile(); @@ -151,14 +155,15 @@ public class TripRecordingBottomSheet extends MenuBottomSheetDialogFragment impl } }); - segmentsContainer = itemView.findViewById(R.id.segments_container); - createSegmentsTabs(segmentsContainer); + setupDisplayHelper(); + segmentsTabs = itemView.findViewById(R.id.segments_container); + createSegmentsTabs(segmentsTabs); RecyclerView statBlocks = itemView.findViewById(R.id.block_statistics); blockStatisticsBuilder = new GpxBlockStatisticsBuilder(app, selectedGpxFile, nightMode); blockStatisticsBuilder.setBlocksView(statBlocks); blockStatisticsBuilder.setBlocksClickable(false); - blockStatisticsBuilder.setInitBlocksKey(INIT_BLOCKS_GENERAL); + blockStatisticsBuilder.setTabItem(GPX_TAB_ITEM_GENERAL); blockStatisticsBuilder.initStatBlocks(null, ContextCompat.getColor(app, getActiveTextColorId(nightMode))); @@ -220,6 +225,7 @@ public class TripRecordingBottomSheet extends MenuBottomSheetDialogFragment impl super.onResume(); blockStatisticsBuilder.runUpdatingStatBlocksIfNeeded(); runUpdatingGPS(); + runUpdatingGraph(); MapActivity mapActivity = getMapActivity(); if (mapActivity != null) { mapActivity.getMapLayers().getGpxLayer().setTrackChartPoints(trackChartPoints); @@ -231,25 +237,28 @@ public class TripRecordingBottomSheet extends MenuBottomSheetDialogFragment impl super.onPause(); blockStatisticsBuilder.stopUpdatingStatBlocks(); stopUpdatingGPS(); + stopUpdatingGraph(); MapActivity mapActivity = getMapActivity(); if (mapActivity != null) { mapActivity.getMapLayers().getGpxLayer().setTrackChartPoints(null); } } - public void show() { + public void show(String... keys) { Dialog dialog = getDialog(); if (dialog != null) { dialog.show(); } - } - - public void show(String... keys) { - show(); for (String key : keys) { if (key.equals(UPDATE_TRACK_ICON)) { updateTrackIcon(app, trackAppearanceIcon); } + if (key.equals(UPDATE_DYNAMIC_ITEMS)) { + blockStatisticsBuilder.stopUpdatingStatBlocks(); + blockStatisticsBuilder.runUpdatingStatBlocksIfNeeded(); + stopUpdatingGraph(); + runUpdatingGraph(); + } } } @@ -276,26 +285,37 @@ public class TripRecordingBottomSheet extends MenuBottomSheetDialogFragment impl handler.post(updatingGPS); } - private void recreateStatBlocks(String initBlocksKey) { - blockStatisticsBuilder.stopUpdatingStatBlocks(); - blockStatisticsBuilder.setInitBlocksKey(initBlocksKey); - blockStatisticsBuilder.runUpdatingStatBlocksIfNeeded(); + public void stopUpdatingGraph() { + handler.removeCallbacks(updatingGraph); + } + + public 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()); + 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(getGPXFile().path); + File file = new File(gpxFile.path); displayHelper.setFile(file); displayHelper.setGpxDataItem(app.getGpxDbHelper().getItem(file)); } - displayHelper.setGpx(getGPXFile()); + displayHelper.setGpx(gpxFile); } private void createSegmentsTabs(ViewGroup viewGroup) { - viewGroup.removeAllViews(); - setupDisplayHelper(); - View segmentView = SegmentGPXAdapter.createGpxTabsView(displayHelper, viewGroup, this, nightMode); AndroidUiHelper.setVisibility(View.GONE, segmentView.findViewById(R.id.list_item_divider)); WrapContentHeightViewPager pager = segmentView.findViewById(R.id.pager); @@ -303,17 +323,19 @@ public class TripRecordingBottomSheet extends MenuBottomSheetDialogFragment impl tabLayout.setOnTabReselectedListener(new PagerSlidingTabStrip.OnTabReselectedListener() { @Override public void onTabSelected(int position) { - recreateStatBlocks(INIT_BLOCKS_KEYS[position]); + graphTabPosition = position; + blockStatisticsBuilder.setTabItem(INIT_TAB_ITEMS[graphTabPosition]); } @Override public void onTabReselected(int position) { - recreateStatBlocks(INIT_BLOCKS_KEYS[position]); + graphTabPosition = position; + blockStatisticsBuilder.setTabItem(INIT_TAB_ITEMS[graphTabPosition]); } }); - graphsAdapter = new GPXItemPagerAdapter(app, GpxUiHelper.makeGpxDisplayItem(app, - displayHelper.getGpx()), displayHelper, nightMode, this, true); + graphsAdapter = new GPXItemPagerAdapter(app, GpxUiHelper.makeGpxDisplayItem(app, displayHelper.getGpx()), + displayHelper, nightMode, this, true); pager.setAdapter(graphsAdapter); tabLayout.setViewPager(pager); diff --git a/OsmAnd/src/net/osmand/plus/monitoring/TripRecordingClearDataBottomSheet.java b/OsmAnd/src/net/osmand/plus/monitoring/TripRecordingClearDataBottomSheet.java index 88814e4819..e6b820fae2 100644 --- a/OsmAnd/src/net/osmand/plus/monitoring/TripRecordingClearDataBottomSheet.java +++ b/OsmAnd/src/net/osmand/plus/monitoring/TripRecordingClearDataBottomSheet.java @@ -18,6 +18,7 @@ import androidx.fragment.app.Fragment; import androidx.fragment.app.FragmentManager; import static net.osmand.AndroidUtils.getPrimaryTextColorId; +import static net.osmand.plus.monitoring.TripRecordingOptionsBottomSheet.ACTION_CLEAR_DATA; public class TripRecordingClearDataBottomSheet extends MenuBottomSheetDialogFragment implements TripRecordingBottomSheet.DismissTargetFragment { @@ -110,6 +111,9 @@ public class TripRecordingClearDataBottomSheet extends MenuBottomSheetDialogFrag @Override public void dismissTarget() { Fragment target = getTargetFragment(); + Bundle args = new Bundle(); + args.putBoolean(ACTION_CLEAR_DATA, true); + target.setArguments(args); if (target instanceof TripRecordingOptionsBottomSheet) { ((TripRecordingOptionsBottomSheet) target).dismiss(); } diff --git a/OsmAnd/src/net/osmand/plus/monitoring/TripRecordingOptionsBottomSheet.java b/OsmAnd/src/net/osmand/plus/monitoring/TripRecordingOptionsBottomSheet.java index ac42775bd2..f787b5d3f1 100644 --- a/OsmAnd/src/net/osmand/plus/monitoring/TripRecordingOptionsBottomSheet.java +++ b/OsmAnd/src/net/osmand/plus/monitoring/TripRecordingOptionsBottomSheet.java @@ -34,11 +34,13 @@ import net.osmand.plus.track.SaveGpxAsyncTask.SaveGpxListener; import net.osmand.util.Algorithms; import static net.osmand.AndroidUtils.getPrimaryTextColorId; +import static net.osmand.plus.monitoring.TripRecordingBottomSheet.UPDATE_DYNAMIC_ITEMS; public class TripRecordingOptionsBottomSheet extends MenuBottomSheetDialogFragment implements TripRecordingBottomSheet.DismissTargetFragment { public static final String TAG = TripRecordingOptionsBottomSheet.class.getSimpleName(); public static final String ACTION_STOP_AND_DISMISS = "action_stop_and_discard"; + public static final String ACTION_CLEAR_DATA = "action_clear_data"; private static final int SAVE_UPDATE_INTERVAL = 1000; private OsmandApplication app; @@ -283,12 +285,22 @@ public class TripRecordingOptionsBottomSheet extends MenuBottomSheetDialogFragme return false; } + private boolean isCleared() { + Bundle args = getArguments(); + if (args != null) { + return args.getBoolean(ACTION_CLEAR_DATA); + } + return false; + } + @Override public void dismissTarget() { Fragment target = getTargetFragment(); if (target instanceof TripRecordingBottomSheet) { if (isDiscard()) { ((TripRecordingBottomSheet) target).dismiss(); + } else if (isCleared()) { + ((TripRecordingBottomSheet) target).show(UPDATE_DYNAMIC_ITEMS); } else { ((TripRecordingBottomSheet) target).show(); } diff --git a/OsmAnd/src/net/osmand/plus/myplaces/GPXItemPagerAdapter.java b/OsmAnd/src/net/osmand/plus/myplaces/GPXItemPagerAdapter.java index b8899dd9c7..3f3a05e289 100644 --- a/OsmAnd/src/net/osmand/plus/myplaces/GPXItemPagerAdapter.java +++ b/OsmAnd/src/net/osmand/plus/myplaces/GPXItemPagerAdapter.java @@ -49,6 +49,8 @@ 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.text.DateFormat; import java.text.SimpleDateFormat; import java.util.ArrayList; @@ -62,10 +64,13 @@ import static net.osmand.plus.helpers.GpxUiHelper.LineGraphType.ALTITUDE; import static net.osmand.plus.helpers.GpxUiHelper.LineGraphType.SLOPE; import static net.osmand.plus.helpers.GpxUiHelper.LineGraphType.SPEED; import static net.osmand.plus.myplaces.GPXTabItemType.GPX_TAB_ITEM_ALTITUDE; +import static net.osmand.plus.myplaces.GPXTabItemType.GPX_TAB_ITEM_GENERAL; import static net.osmand.plus.myplaces.GPXTabItemType.GPX_TAB_ITEM_SPEED; public class GPXItemPagerAdapter extends PagerAdapter implements CustomTabProvider, ViewAtPositionInterface { + private static final int CHART_LABEL_COUNT = 4; + private OsmandApplication app; private UiUtilities iconsCache; private TrackDisplayHelper displayHelper; @@ -83,6 +88,10 @@ public class GPXItemPagerAdapter extends PagerAdapter implements CustomTabProvid private boolean nightMode; private boolean onlyGraphs; + public void setGpxItem(GpxDisplayItem gpxItem) { + this.gpxItem = gpxItem; + } + public GPXItemPagerAdapter(@NonNull OsmandApplication app, @NonNull GpxDisplayItem gpxItem, @NonNull TrackDisplayHelper displayHelper, @@ -117,12 +126,17 @@ public class GPXItemPagerAdapter extends PagerAdapter implements CustomTabProvid private List getDataSets(LineChart chart, GPXTabItemType tabType, LineGraphType firstType, LineGraphType secondType) { List dataSets = dataSetsMap.get(tabType); - if (dataSets == null && chart != null) { + if (gpxItem != null) { GPXTrackAnalysis analysis = gpxItem.analysis; GpxDataItem gpxDataItem = displayHelper.getGpxDataItem(); boolean calcWithoutGaps = gpxItem.isGeneralTrack() && gpxDataItem != null && !gpxDataItem.isJoinSegments(); - dataSets = GpxUiHelper.getDataSets(chart, app, analysis, firstType, secondType, calcWithoutGaps); - dataSetsMap.put(tabType, dataSets); + if (chart != null) { + dataSets = GpxUiHelper.getDataSets(chart, app, analysis, firstType, secondType, calcWithoutGaps); + if (dataSets != null) { + dataSetsMap.remove(tabType); + } + dataSetsMap.put(tabType, dataSets); + } } return dataSets; } @@ -247,7 +261,7 @@ public class GPXItemPagerAdapter extends PagerAdapter implements CustomTabProvid private void setupSpeedTab(View view, LineChart chart, GPXTrackAnalysis analysis, GPXFile gpxFile, int position) { if (analysis != null && analysis.isSpeedSpecified()) { if (analysis.hasSpeedData) { - GpxUiHelper.setupGPXChart(app, chart, 4); + GpxUiHelper.setupGPXChart(app, chart, CHART_LABEL_COUNT); chart.setData(new LineData(getDataSets(chart, GPX_TAB_ITEM_SPEED, SPEED, null))); updateChart(chart); chart.setVisibility(View.VISIBLE); @@ -319,7 +333,7 @@ public class GPXItemPagerAdapter extends PagerAdapter implements CustomTabProvid private void setupAltitudeTab(View view, LineChart chart, GPXTrackAnalysis analysis, GPXFile gpxFile, int position) { if (analysis != null) { if (analysis.hasElevationData) { - GpxUiHelper.setupGPXChart(app, chart, 4); + GpxUiHelper.setupGPXChart(app, chart, CHART_LABEL_COUNT); chart.setData(new LineData(getDataSets(chart, GPX_TAB_ITEM_ALTITUDE, ALTITUDE, SLOPE))); updateChart(chart); chart.setVisibility(View.VISIBLE); @@ -386,7 +400,7 @@ public class GPXItemPagerAdapter extends PagerAdapter implements CustomTabProvid private void setupGeneralTab(View view, LineChart chart, GPXTrackAnalysis analysis, GPXFile gpxFile, int position) { if (analysis != null) { if (analysis.hasElevationData || analysis.hasSpeedData) { - GpxUiHelper.setupGPXChart(app, chart, 4); + GpxUiHelper.setupGPXChart(app, chart, CHART_LABEL_COUNT); chart.setData(new LineData(getDataSets(chart, GPXTabItemType.GPX_TAB_ITEM_GENERAL, ALTITUDE, SPEED))); updateChart(chart); chart.setVisibility(View.VISIBLE); @@ -685,17 +699,65 @@ public class GPXItemPagerAdapter extends PagerAdapter implements CustomTabProvid void updateChart(LineChart chart) { if (chart != null && !chart.isEmpty()) { - if (gpxItem.chartMatrix != null) { - chart.getViewPortHandler().refresh(new Matrix(gpxItem.chartMatrix), chart, true); - } - if (gpxItem.chartHighlightPos != -1) { - chart.highlightValue(gpxItem.chartHighlightPos, 0); + if (gpxItem != null) { + if (gpxItem.chartMatrix != null) { + chart.getViewPortHandler().refresh(new Matrix(gpxItem.chartMatrix), chart, true); + } + if (gpxItem.chartHighlightPos != -1) { + chart.highlightValue(gpxItem.chartHighlightPos, 0); + } } else { chart.highlightValue(null); } } } + 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; + } + 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); + } + } + } + private TrkSegment getTrkSegment() { for (Track track : gpxItem.group.getGpx().tracks) { if (!track.generalTrack && !gpxItem.isGeneralTrack() || track.generalTrack && gpxItem.isGeneralTrack()) { diff --git a/OsmAnd/src/net/osmand/plus/track/GpxBlockStatisticsBuilder.java b/OsmAnd/src/net/osmand/plus/track/GpxBlockStatisticsBuilder.java index 96478490bb..4ffd0897f2 100644 --- a/OsmAnd/src/net/osmand/plus/track/GpxBlockStatisticsBuilder.java +++ b/OsmAnd/src/net/osmand/plus/track/GpxBlockStatisticsBuilder.java @@ -30,6 +30,7 @@ import net.osmand.plus.R; import net.osmand.plus.helpers.AndroidUiHelper; import net.osmand.plus.helpers.GpxUiHelper; import net.osmand.plus.helpers.GpxUiHelper.GPXDataSetType; +import net.osmand.plus.myplaces.GPXTabItemType; import net.osmand.plus.myplaces.SegmentActionsListener; import net.osmand.plus.widgets.TextViewEx; import net.osmand.util.Algorithms; @@ -49,10 +50,6 @@ public class GpxBlockStatisticsBuilder { private static final Log LOG = PlatformUtil.getLog(GpxBlockStatisticsBuilder.class); private static final int BLOCKS_UPDATE_INTERVAL = 1000; - public static final String INIT_BLOCKS_BASE = "init_blocks_base"; - public static final String INIT_BLOCKS_GENERAL = "init_blocks_general"; - public static final String INIT_BLOCKS_ALTITUDE = "init_blocks_altitude"; - public static final String INIT_BLOCKS_SPEED = "init_blocks_speed"; private final OsmandApplication app; private final boolean nightMode; @@ -64,7 +61,7 @@ public class GpxBlockStatisticsBuilder { private BlockStatisticsAdapter adapter; private final List items = new ArrayList<>(); private boolean blocksClickable = true; - private String initBlocksKey = INIT_BLOCKS_BASE; + private GPXTabItemType tabItem = null; private final Handler handler = new Handler(); private Runnable updatingItems; @@ -88,8 +85,8 @@ public class GpxBlockStatisticsBuilder { this.blocksView = blocksView; } - public void setInitBlocksKey(String initBlocksKey) { - this.initBlocksKey = initBlocksKey; + public void setTabItem(GPXTabItemType tabItem) { + this.tabItem = tabItem; } @Nullable @@ -154,55 +151,54 @@ public class GpxBlockStatisticsBuilder { } items.clear(); if (analysis != null) { - switch (initBlocksKey) { - case INIT_BLOCKS_GENERAL: { - float totalDistance = withoutGaps ? analysis.totalDistanceWithoutGaps : analysis.totalDistance; - float timeSpan = withoutGaps ? analysis.timeSpanWithoutGaps : analysis.timeSpan; - Date start = new Date(analysis.startTime); - Date end = new Date(analysis.endTime); - prepareDataDistance(totalDistance); - prepareDataTimeSpan(timeSpan); - prepareDataStartTime(start); - prepareDataEndTime(end); - break; - } - case INIT_BLOCKS_ALTITUDE: { - String min = OsmAndFormatter.getFormattedAlt(analysis.minElevation, app); - String max = OsmAndFormatter.getFormattedAlt(analysis.maxElevation, app); - String asc = OsmAndFormatter.getFormattedAlt(analysis.diffElevationUp, app); - String desc = OsmAndFormatter.getFormattedAlt(analysis.diffElevationDown, app); - prepareDataAverageAltitude(); - prepareDataAltitudeRange(min, max); - prepareDataAscent(asc); - prepareDataDescent(desc); - break; - } - case INIT_BLOCKS_SPEED: { - String avg = OsmAndFormatter.getFormattedSpeed(analysis.avgSpeed, app); - String max = OsmAndFormatter.getFormattedSpeed(analysis.maxSpeed, app); - long timeMoving = withoutGaps ? analysis.timeMovingWithoutGaps : analysis.timeMoving; - float totalDistanceMoving = withoutGaps ? analysis.totalDistanceMovingWithoutGaps : analysis.totalDistanceMoving; - prepareDataAverageSpeed(avg); - prepareDataMaximumSpeed(max); - prepareDataTimeMoving(timeMoving); - prepareDataDistanceCorrected(totalDistanceMoving); - break; - } - default: - case INIT_BLOCKS_BASE: { - float totalDistance = withoutGaps ? analysis.totalDistanceWithoutGaps : analysis.totalDistance; - String asc = OsmAndFormatter.getFormattedAlt(analysis.diffElevationUp, app); - String desc = OsmAndFormatter.getFormattedAlt(analysis.diffElevationDown, app); - String avg = OsmAndFormatter.getFormattedSpeed(analysis.avgSpeed, app); - String max = OsmAndFormatter.getFormattedSpeed(analysis.maxSpeed, app); - float timeSpan = withoutGaps ? analysis.timeSpanWithoutGaps : analysis.timeSpan; - prepareDataDistance(totalDistance); - prepareDataAscent(asc); - prepareDataDescent(desc); - prepareDataAverageSpeed(avg); - prepareDataMaximumSpeed(max); - prepareDataTimeSpan(timeSpan); - break; + if (tabItem == null) { + float totalDistance = withoutGaps ? analysis.totalDistanceWithoutGaps : analysis.totalDistance; + String asc = OsmAndFormatter.getFormattedAlt(analysis.diffElevationUp, app); + String desc = OsmAndFormatter.getFormattedAlt(analysis.diffElevationDown, app); + String avg = OsmAndFormatter.getFormattedSpeed(analysis.avgSpeed, app); + String max = OsmAndFormatter.getFormattedSpeed(analysis.maxSpeed, app); + float timeSpan = withoutGaps ? analysis.timeSpanWithoutGaps : analysis.timeSpan; + prepareDataDistance(totalDistance); + prepareDataAscent(asc); + prepareDataDescent(desc); + prepareDataAverageSpeed(avg); + prepareDataMaximumSpeed(max); + prepareDataTimeSpan(timeSpan); + } else { + switch (tabItem) { + case GPX_TAB_ITEM_GENERAL: { + float totalDistance = withoutGaps ? analysis.totalDistanceWithoutGaps : analysis.totalDistance; + float timeSpan = withoutGaps ? analysis.timeSpanWithoutGaps : analysis.timeSpan; + Date start = new Date(analysis.startTime); + Date end = new Date(analysis.endTime); + prepareDataDistance(totalDistance); + prepareDataTimeSpan(timeSpan); + prepareDataStartTime(start); + prepareDataEndTime(end); + break; + } + case GPX_TAB_ITEM_ALTITUDE: { + String min = OsmAndFormatter.getFormattedAlt(analysis.minElevation, app); + String max = OsmAndFormatter.getFormattedAlt(analysis.maxElevation, app); + String asc = OsmAndFormatter.getFormattedAlt(analysis.diffElevationUp, app); + String desc = OsmAndFormatter.getFormattedAlt(analysis.diffElevationDown, app); + prepareDataAverageAltitude(); + prepareDataAltitudeRange(min, max); + prepareDataAscent(asc); + prepareDataDescent(desc); + break; + } + case GPX_TAB_ITEM_SPEED: { + String avg = OsmAndFormatter.getFormattedSpeed(analysis.avgSpeed, app); + String max = OsmAndFormatter.getFormattedSpeed(analysis.maxSpeed, app); + long timeMoving = withoutGaps ? analysis.timeMovingWithoutGaps : analysis.timeMoving; + float totalDistanceMoving = withoutGaps ? analysis.totalDistanceMovingWithoutGaps : analysis.totalDistanceMoving; + prepareDataAverageSpeed(avg); + prepareDataMaximumSpeed(max); + prepareDataTimeMoving(timeMoving); + prepareDataDistanceCorrected(totalDistanceMoving); + break; + } } } } From a4e1d93f9e1446a1a83205c6d15a8d1b41a356d2 Mon Sep 17 00:00:00 2001 From: Skalii Date: Fri, 26 Mar 2021 11:38:24 +0200 Subject: [PATCH 2/6] small fixes --- .../monitoring/TripRecordingClearDataBottomSheet.java | 3 ++- .../monitoring/TripRecordingDiscardBottomSheet.java | 3 ++- .../monitoring/TripRecordingOptionsBottomSheet.java | 10 ++++++---- 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/monitoring/TripRecordingClearDataBottomSheet.java b/OsmAnd/src/net/osmand/plus/monitoring/TripRecordingClearDataBottomSheet.java index e6b820fae2..cf09a2e7f0 100644 --- a/OsmAnd/src/net/osmand/plus/monitoring/TripRecordingClearDataBottomSheet.java +++ b/OsmAnd/src/net/osmand/plus/monitoring/TripRecordingClearDataBottomSheet.java @@ -11,6 +11,7 @@ import net.osmand.plus.base.MenuBottomSheetDialogFragment; import net.osmand.plus.base.bottomsheetmenu.BaseBottomSheetItem; import net.osmand.plus.base.bottomsheetmenu.BottomSheetItemWithDescription; import net.osmand.plus.base.bottomsheetmenu.simpleitems.DividerSpaceItem; +import net.osmand.plus.monitoring.TripRecordingBottomSheet.DismissTargetFragment; import net.osmand.plus.monitoring.TripRecordingBottomSheet.ItemType; import androidx.annotation.NonNull; @@ -20,7 +21,7 @@ import androidx.fragment.app.FragmentManager; import static net.osmand.AndroidUtils.getPrimaryTextColorId; import static net.osmand.plus.monitoring.TripRecordingOptionsBottomSheet.ACTION_CLEAR_DATA; -public class TripRecordingClearDataBottomSheet extends MenuBottomSheetDialogFragment implements TripRecordingBottomSheet.DismissTargetFragment { +public class TripRecordingClearDataBottomSheet extends MenuBottomSheetDialogFragment implements DismissTargetFragment { public static final String TAG = TripRecordingClearDataBottomSheet.class.getSimpleName(); diff --git a/OsmAnd/src/net/osmand/plus/monitoring/TripRecordingDiscardBottomSheet.java b/OsmAnd/src/net/osmand/plus/monitoring/TripRecordingDiscardBottomSheet.java index cff99d9daa..1136980b51 100644 --- a/OsmAnd/src/net/osmand/plus/monitoring/TripRecordingDiscardBottomSheet.java +++ b/OsmAnd/src/net/osmand/plus/monitoring/TripRecordingDiscardBottomSheet.java @@ -12,6 +12,7 @@ import net.osmand.plus.base.MenuBottomSheetDialogFragment; import net.osmand.plus.base.bottomsheetmenu.BaseBottomSheetItem; import net.osmand.plus.base.bottomsheetmenu.BottomSheetItemWithDescription; import net.osmand.plus.base.bottomsheetmenu.simpleitems.DividerSpaceItem; +import net.osmand.plus.monitoring.TripRecordingBottomSheet.DismissTargetFragment; import net.osmand.plus.monitoring.TripRecordingBottomSheet.ItemType; import androidx.annotation.NonNull; @@ -21,7 +22,7 @@ import androidx.fragment.app.FragmentManager; import static net.osmand.AndroidUtils.getPrimaryTextColorId; import static net.osmand.plus.monitoring.TripRecordingOptionsBottomSheet.ACTION_STOP_AND_DISMISS; -public class TripRecordingDiscardBottomSheet extends MenuBottomSheetDialogFragment implements TripRecordingBottomSheet.DismissTargetFragment { +public class TripRecordingDiscardBottomSheet extends MenuBottomSheetDialogFragment implements DismissTargetFragment { public static final String TAG = TripRecordingDiscardBottomSheet.class.getSimpleName(); diff --git a/OsmAnd/src/net/osmand/plus/monitoring/TripRecordingOptionsBottomSheet.java b/OsmAnd/src/net/osmand/plus/monitoring/TripRecordingOptionsBottomSheet.java index 5aa2aabd3a..f72de8a640 100644 --- a/OsmAnd/src/net/osmand/plus/monitoring/TripRecordingOptionsBottomSheet.java +++ b/OsmAnd/src/net/osmand/plus/monitoring/TripRecordingOptionsBottomSheet.java @@ -26,7 +26,7 @@ import net.osmand.plus.base.MenuBottomSheetDialogFragment; import net.osmand.plus.base.bottomsheetmenu.BaseBottomSheetItem; import net.osmand.plus.base.bottomsheetmenu.SimpleBottomSheetItem; import net.osmand.plus.base.bottomsheetmenu.simpleitems.DividerSpaceItem; -import net.osmand.plus.helpers.AndroidUiHelper; +import net.osmand.plus.monitoring.TripRecordingBottomSheet.DismissTargetFragment; import net.osmand.plus.monitoring.TripRecordingBottomSheet.ItemType; import net.osmand.plus.myplaces.SaveCurrentTrackTask; import net.osmand.plus.settings.backend.OsmandSettings; @@ -36,7 +36,7 @@ import net.osmand.util.Algorithms; import static net.osmand.AndroidUtils.getPrimaryTextColorId; import static net.osmand.plus.monitoring.TripRecordingBottomSheet.UPDATE_DYNAMIC_ITEMS; -public class TripRecordingOptionsBottomSheet extends MenuBottomSheetDialogFragment implements TripRecordingBottomSheet.DismissTargetFragment { +public class TripRecordingOptionsBottomSheet extends MenuBottomSheetDialogFragment implements DismissTargetFragment { public static final String TAG = TripRecordingOptionsBottomSheet.class.getSimpleName(); public static final String ACTION_STOP_AND_DISMISS = "action_stop_and_discard"; @@ -212,8 +212,10 @@ public class TripRecordingOptionsBottomSheet extends MenuBottomSheetDialogFragme @Override public void run() { String time = getTimeTrackSaved(); - TripRecordingBottomSheet.createItem(app, nightMode, buttonSave, ItemType.SAVE, hasDataToSave(), !Algorithms.isEmpty(time) ? time : null); - TripRecordingBottomSheet.createItem(app, nightMode, buttonClear, ItemType.CLEAR_DATA, hasDataToSave(), null); + TripRecordingBottomSheet.createItem(app, nightMode, buttonSave, ItemType.SAVE, + hasDataToSave(), !Algorithms.isEmpty(time) ? time : null); + TripRecordingBottomSheet.createItem(app, nightMode, buttonClear, ItemType.CLEAR_DATA, + hasDataToSave(), null); handler.postDelayed(this, SAVE_UPDATE_INTERVAL); } }; From 0addf8d56bae6c78adfed861c55ef3cb47029486 Mon Sep 17 00:00:00 2001 From: Skalii Date: Fri, 26 Mar 2021 12:54:53 +0200 Subject: [PATCH 3/6] fix updating graphs after starting a new segment --- .../plus/myplaces/GPXItemPagerAdapter.java | 35 ++++++++++++------- 1 file changed, 23 insertions(+), 12 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/myplaces/GPXItemPagerAdapter.java b/OsmAnd/src/net/osmand/plus/myplaces/GPXItemPagerAdapter.java index 42609ad9e1..984f588341 100644 --- a/OsmAnd/src/net/osmand/plus/myplaces/GPXItemPagerAdapter.java +++ b/OsmAnd/src/net/osmand/plus/myplaces/GPXItemPagerAdapter.java @@ -33,11 +33,13 @@ import net.osmand.GPXUtilities.TrkSegment; import net.osmand.GPXUtilities.WptPt; import net.osmand.plus.GPXDatabase.GpxDataItem; import net.osmand.plus.GpxSelectionHelper.GpxDisplayItem; +import net.osmand.plus.GpxSelectionHelper.SelectedGpxFile; import net.osmand.plus.OsmAndFormatter; 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; @@ -132,17 +134,26 @@ public class GPXItemPagerAdapter extends PagerAdapter implements CustomTabProvid private List getDataSets(LineChart chart, GPXTabItemType tabType, LineGraphType firstType, LineGraphType secondType) { List dataSets = dataSetsMap.get(tabType); - if (gpxItem != null) { - GPXTrackAnalysis analysis = gpxItem.analysis; + 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() + && (Algorithms.isEmpty(currentGpx.tracks) || currentGpx.tracks.get(0).generalTrack); + } else if (gpxItem != null) { + analysis = gpxItem.analysis; GpxDataItem gpxDataItem = displayHelper.getGpxDataItem(); - boolean calcWithoutGaps = gpxItem.isGeneralTrack() && gpxDataItem != null && !gpxDataItem.isJoinSegments(); - if (chart != null) { - dataSets = GpxUiHelper.getDataSets(chart, app, analysis, firstType, secondType, calcWithoutGaps); - if (dataSets != null) { - dataSetsMap.remove(tabType); - } - dataSetsMap.put(tabType, dataSets); + withoutGaps = gpxItem.isGeneralTrack() && gpxDataItem != null && !gpxDataItem.isJoinSegments(); + } + if (chart != null && analysis != null) { + dataSets = GpxUiHelper.getDataSets(chart, app, analysis, firstType, secondType, withoutGaps); + if (dataSets != null) { + dataSetsMap.remove(tabType); } + dataSetsMap.put(tabType, dataSets); } return dataSets; } @@ -217,12 +228,12 @@ public class GPXItemPagerAdapter extends PagerAdapter implements CustomTabProvid public Object instantiateItem(@NonNull ViewGroup container, int position) { GPXTabItemType tabType = tabTypes[position]; View view = getViewForTab(container, tabType); + 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; - LineChart chart = view.findViewById(R.id.chart); - ViewGroup.MarginLayoutParams lp = (ViewGroup.MarginLayoutParams) chart.getLayoutParams(); - AndroidUtils.setMargins(lp, chartHMargin, lp.topMargin, chartHMargin, lp.bottomMargin); setupChart(view, chart); switch (tabType) { From 7048f8c52d51c47ad432413d0cfe78e6f51a6c5e Mon Sep 17 00:00:00 2001 From: Skalii Date: Sat, 27 Mar 2021 22:51:59 +0200 Subject: [PATCH 4/6] 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) { From 07280935c06428b3301c61f6474b868cb4b30715 Mon Sep 17 00:00:00 2001 From: Skalii Date: Mon, 29 Mar 2021 19:18:56 +0300 Subject: [PATCH 5/6] some fixes --- .../monitoring/TripRecordingBottomSheet.java | 16 +-- .../TripRecordingClearDataBottomSheet.java | 11 +- .../TripRecordingDiscardBottomSheet.java | 13 ++- .../TripRecordingOptionsBottomSheet.java | 11 +- .../plus/myplaces/GPXItemPagerAdapter.java | 100 ++++++++---------- 5 files changed, 70 insertions(+), 81 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/monitoring/TripRecordingBottomSheet.java b/OsmAnd/src/net/osmand/plus/monitoring/TripRecordingBottomSheet.java index aa2a5b6cb6..d4b3115f39 100644 --- a/OsmAnd/src/net/osmand/plus/monitoring/TripRecordingBottomSheet.java +++ b/OsmAnd/src/net/osmand/plus/monitoring/TripRecordingBottomSheet.java @@ -329,7 +329,7 @@ public class TripRecordingBottomSheet extends MenuBottomSheetDialogFragment impl } }); - graphsAdapter = new GPXItemPagerAdapter(app, nightMode, this, true, true); + graphsAdapter = new GPXItemPagerAdapter(app, null, null, nightMode, this, true); graphsAdapter.setChartHMargin(getResources().getDimensionPixelSize(R.dimen.content_padding)); pager.setAdapter(graphsAdapter); @@ -607,23 +607,11 @@ 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(getDisplayHelper().getGpx()); + trackChartPoints.setGpx(getGPXFile()); } MapActivity mapActivity = getMapActivity(); if (mapActivity != null) { diff --git a/OsmAnd/src/net/osmand/plus/monitoring/TripRecordingClearDataBottomSheet.java b/OsmAnd/src/net/osmand/plus/monitoring/TripRecordingClearDataBottomSheet.java index cf09a2e7f0..aeb33abf93 100644 --- a/OsmAnd/src/net/osmand/plus/monitoring/TripRecordingClearDataBottomSheet.java +++ b/OsmAnd/src/net/osmand/plus/monitoring/TripRecordingClearDataBottomSheet.java @@ -112,10 +112,15 @@ public class TripRecordingClearDataBottomSheet extends MenuBottomSheetDialogFrag @Override public void dismissTarget() { Fragment target = getTargetFragment(); - Bundle args = new Bundle(); - args.putBoolean(ACTION_CLEAR_DATA, true); - target.setArguments(args); if (target instanceof TripRecordingOptionsBottomSheet) { + Bundle args = target.getArguments(); + if (args != null) { + args.putBoolean(ACTION_CLEAR_DATA, true); + } else { + args = new Bundle(); + args.putBoolean(ACTION_CLEAR_DATA, true); + target.setArguments(args); + } ((TripRecordingOptionsBottomSheet) target).dismiss(); } } diff --git a/OsmAnd/src/net/osmand/plus/monitoring/TripRecordingDiscardBottomSheet.java b/OsmAnd/src/net/osmand/plus/monitoring/TripRecordingDiscardBottomSheet.java index 1136980b51..6739187152 100644 --- a/OsmAnd/src/net/osmand/plus/monitoring/TripRecordingDiscardBottomSheet.java +++ b/OsmAnd/src/net/osmand/plus/monitoring/TripRecordingDiscardBottomSheet.java @@ -68,10 +68,15 @@ public class TripRecordingDiscardBottomSheet extends MenuBottomSheetDialogFragme dismiss(); Fragment target = getTargetFragment(); - if (target != null) { - Bundle args = new Bundle(); - args.putBoolean(ACTION_STOP_AND_DISMISS, true); - target.setArguments(args); + if (target instanceof TripRecordingOptionsBottomSheet) { + Bundle args = target.getArguments(); + if (args != null) { + args.putBoolean(ACTION_STOP_AND_DISMISS, true); + } else { + args = new Bundle(); + args.putBoolean(ACTION_STOP_AND_DISMISS, true); + target.setArguments(args); + } } dismissTarget(); } diff --git a/OsmAnd/src/net/osmand/plus/monitoring/TripRecordingOptionsBottomSheet.java b/OsmAnd/src/net/osmand/plus/monitoring/TripRecordingOptionsBottomSheet.java index f72de8a640..6004adb74a 100644 --- a/OsmAnd/src/net/osmand/plus/monitoring/TripRecordingOptionsBottomSheet.java +++ b/OsmAnd/src/net/osmand/plus/monitoring/TripRecordingOptionsBottomSheet.java @@ -259,9 +259,14 @@ public class TripRecordingOptionsBottomSheet extends MenuBottomSheetDialogFragme if (mapActivity != null && plugin != null) { stopUpdatingTimeTrackSaved(); plugin.saveCurrentTrack(null, mapActivity, false, true); - Bundle args = new Bundle(); - args.putBoolean(ACTION_STOP_AND_DISMISS, true); - setArguments(args); + Bundle args = getArguments(); + if (args != null) { + args.putBoolean(ACTION_STOP_AND_DISMISS, true); + } else { + args = new Bundle(); + args.putBoolean(ACTION_STOP_AND_DISMISS, true); + setArguments(args); + } dismiss(); dismissTarget(); } diff --git a/OsmAnd/src/net/osmand/plus/myplaces/GPXItemPagerAdapter.java b/OsmAnd/src/net/osmand/plus/myplaces/GPXItemPagerAdapter.java index cea3454fbc..2df66263e4 100644 --- a/OsmAnd/src/net/osmand/plus/myplaces/GPXItemPagerAdapter.java +++ b/OsmAnd/src/net/osmand/plus/myplaces/GPXItemPagerAdapter.java @@ -12,6 +12,7 @@ import android.widget.ImageView; import android.widget.TextView; import androidx.annotation.NonNull; +import androidx.annotation.Nullable; import androidx.appcompat.widget.SwitchCompat; import androidx.viewpager.widget.PagerAdapter; @@ -89,50 +90,19 @@ 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; } - private SelectedGpxFile getCurrentTrack() { - return app.getSavingTrackHelper().getCurrentTrack(); - } - - private GPXFile getGpxFile() { - return recordingTrack ? getCurrentTrack().getGpxFile() : displayHelper.getGpx(); + private boolean isShowCurrentTrack() { + return displayHelper.getGpx() != null && displayHelper.getGpx().showCurrentTrack; } 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, - @NonNull GpxDisplayItem gpxItem, - @NonNull TrackDisplayHelper displayHelper, + @Nullable GpxDisplayItem gpxItem, + @Nullable TrackDisplayHelper displayHelper, boolean nightMode, @NonNull SegmentActionsListener actionsListener, boolean onlyGraphs) { @@ -140,22 +110,36 @@ public class GPXItemPagerAdapter extends PagerAdapter implements CustomTabProvid this.app = app; this.gpxItem = gpxItem; this.nightMode = nightMode; - this.displayHelper = displayHelper; this.actionsListener = actionsListener; this.onlyGraphs = onlyGraphs; iconsCache = app.getUIUtilities(); + + if (displayHelper == null) { + this.displayHelper = new TrackDisplayHelper(app); + SelectedGpxFile currentTrack = app.getSavingTrackHelper().getCurrentTrack(); + GPXFile gpxFile = currentTrack.getGpxFile(); + if (currentTrack.isShowCurrentTrack()) { + File file = new File(gpxFile.path); + this.displayHelper.setFile(file); + this.displayHelper.setGpxDataItem(app.getGpxDbHelper().getItem(file)); + } + this.displayHelper.setGpx(gpxFile); + } else { + this.displayHelper = displayHelper; + } + updateAnalysis(); fetchTabTypes(); } private void updateAnalysis() { analysis = null; - if (recordingTrack) { - GPXFile currentGpx = getCurrentTrack().getGpxFile(); - if (!currentGpx.isEmpty()) { - analysis = currentGpx.getAnalysis(0); + if (isShowCurrentTrack()) { + GPXFile gpxFile = displayHelper.getGpx(); + if (gpxFile != null && !gpxFile.isEmpty()) { + analysis = gpxFile.getAnalysis(0); + gpxItem = GpxUiHelper.makeGpxDisplayItem(app, gpxFile); } - gpxItem = GpxUiHelper.makeGpxDisplayItem(app, currentGpx); } else { if (gpxItem != null) { analysis = gpxItem.analysis; @@ -165,7 +149,7 @@ public class GPXItemPagerAdapter extends PagerAdapter implements CustomTabProvid private void fetchTabTypes() { List tabTypeList = new ArrayList<>(); - if (recordingTrack) { + if (isShowCurrentTrack()) { if (analysis != null && (analysis.hasElevationData || analysis.hasSpeedData)) { tabTypeList.add(GPXTabItemType.GPX_TAB_ITEM_GENERAL); } @@ -187,17 +171,17 @@ public class GPXItemPagerAdapter extends PagerAdapter implements CustomTabProvid LineGraphType firstType, LineGraphType secondType) { List dataSets = dataSetsMap.get(tabType); boolean withoutGaps = true; - if (recordingTrack) { - GPXFile currentGpx = getGpxFile(); - withoutGaps = !getCurrentTrack().isJoinSegments() - && (Algorithms.isEmpty(currentGpx.tracks) || currentGpx.tracks.get(0).generalTrack); + if (isShowCurrentTrack()) { + GPXFile gpxFile = displayHelper.getGpx(); + withoutGaps = !app.getSavingTrackHelper().getCurrentTrack().isJoinSegments() && gpxFile != null + && (Algorithms.isEmpty(gpxFile.tracks) || gpxFile.tracks.get(0).generalTrack); } else if (gpxItem != null) { GpxDataItem gpxDataItem = displayHelper.getGpxDataItem(); withoutGaps = gpxItem.isGeneralTrack() && gpxDataItem != null && !gpxDataItem.isJoinSegments(); } if (chart != null && analysis != null) { dataSets = GpxUiHelper.getDataSets(chart, app, analysis, firstType, secondType, withoutGaps); - if (dataSets != null) { + if (!Algorithms.isEmpty(dataSets)) { dataSetsMap.remove(tabType); } dataSetsMap.put(tabType, dataSets); @@ -212,7 +196,7 @@ public class GPXItemPagerAdapter extends PagerAdapter implements CustomTabProvid if (ds != null && ds.size() > 0) { for (GPXUtilities.Track t : gpxItem.group.getGpx().tracks) { for (TrkSegment s : t.segments) { - if (s.points.size() > 0 && s.points.get(0).equals(gpxItem.analysis.locationStart)) { + if (s.points.size() > 0 && s.points.get(0).equals(analysis.locationStart)) { segment = s; break; } @@ -236,7 +220,7 @@ public class GPXItemPagerAdapter extends PagerAdapter implements CustomTabProvid if (gpxItem.chartAxisType == GPXDataSetAxisType.TIME) { float time = pos * 1000; for (WptPt p : segment.points) { - if (p.time - gpxItem.analysis.startTime >= time) { + if (p.time - analysis.startTime >= time) { wpt = p; break; } @@ -739,20 +723,21 @@ public class GPXItemPagerAdapter extends PagerAdapter implements CustomTabProvid void updateJoinGapsInfo(View view, int position) { if (view != null) { 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); + boolean generalTrack = gpxItem.isGeneralTrack(); boolean joinSegments = displayHelper.isJoinSegments(); + boolean visible = generalTrack && analysis != null && tabType.equals(GPXTabItemType.GPX_TAB_ITEM_GENERAL); + AndroidUiHelper.updateVisibility(view.findViewById(R.id.gpx_join_gaps_container), visible); ((SwitchCompat) view.findViewById(R.id.gpx_join_gaps_switch)).setChecked(joinSegments); if (analysis != null) { if (tabType.equals(GPXTabItemType.GPX_TAB_ITEM_GENERAL)) { - float totalDistance = !joinSegments && gpxItem.isGeneralTrack() ? analysis.totalDistanceWithoutGaps : analysis.totalDistance; - float timeSpan = !joinSegments && gpxItem.isGeneralTrack() ? analysis.timeSpanWithoutGaps : analysis.timeSpan; + float totalDistance = !joinSegments && generalTrack ? analysis.totalDistanceWithoutGaps : analysis.totalDistance; + float timeSpan = !joinSegments && generalTrack ? analysis.timeSpanWithoutGaps : analysis.timeSpan; ((TextView) view.findViewById(R.id.distance_text)).setText(OsmAndFormatter.getFormattedDistance(totalDistance, app)); ((TextView) view.findViewById(R.id.duration_text)).setText(Algorithms.formatDuration((int) (timeSpan / 1000), app.accessibilityEnabled())); } else if (tabType.equals(GPX_TAB_ITEM_SPEED)) { - long timeMoving = !joinSegments && gpxItem.isGeneralTrack() ? analysis.timeMovingWithoutGaps : analysis.timeMoving; - float totalDistanceMoving = !joinSegments && gpxItem.isGeneralTrack() ? analysis.totalDistanceMovingWithoutGaps : analysis.totalDistanceMoving; + long timeMoving = !joinSegments && generalTrack ? analysis.timeMovingWithoutGaps : analysis.timeMoving; + float totalDistanceMoving = !joinSegments && generalTrack ? analysis.totalDistanceMovingWithoutGaps : analysis.totalDistanceMoving; ((TextView) view.findViewById(R.id.time_moving_text)).setText(Algorithms.formatDuration((int) (timeMoving / 1000), app.accessibilityEnabled())); ((TextView) view.findViewById(R.id.distance_text)).setText(OsmAndFormatter.getFormattedDistance(totalDistanceMoving, app)); @@ -777,10 +762,11 @@ public class GPXItemPagerAdapter extends PagerAdapter implements CustomTabProvid } public boolean isTabsVisible() { - if (getCount() > 0 && views.size() > 0) { + GPXFile gpxFile = displayHelper.getGpx(); + if (gpxFile != null && 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()) { + if (!lc.isEmpty() && !gpxFile.isEmpty()) { return true; } } From 82fa75db1d843d2e24296052e6194a1b5f4688c5 Mon Sep 17 00:00:00 2001 From: Skalii Date: Mon, 29 Mar 2021 22:11:37 +0300 Subject: [PATCH 6/6] small fixes --- .../monitoring/TripRecordingBottomSheet.java | 9 ++++++++- .../plus/myplaces/GPXItemPagerAdapter.java | 17 ++--------------- 2 files changed, 10 insertions(+), 16 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/monitoring/TripRecordingBottomSheet.java b/OsmAnd/src/net/osmand/plus/monitoring/TripRecordingBottomSheet.java index d4b3115f39..89734b7415 100644 --- a/OsmAnd/src/net/osmand/plus/monitoring/TripRecordingBottomSheet.java +++ b/OsmAnd/src/net/osmand/plus/monitoring/TripRecordingBottomSheet.java @@ -329,7 +329,14 @@ public class TripRecordingBottomSheet extends MenuBottomSheetDialogFragment impl } }); - graphsAdapter = new GPXItemPagerAdapter(app, null, null, nightMode, this, true); + TrackDisplayHelper displayHelper = new TrackDisplayHelper(app); + GPXFile gpxFile = getGPXFile(); + File file = new File(gpxFile.path); + displayHelper.setFile(file); + displayHelper.setGpxDataItem(app.getGpxDbHelper().getItem(file)); + displayHelper.setGpx(gpxFile); + + graphsAdapter = new GPXItemPagerAdapter(app, null, displayHelper, nightMode, this, true); graphsAdapter.setChartHMargin(getResources().getDimensionPixelSize(R.dimen.content_padding)); pager.setAdapter(graphsAdapter); diff --git a/OsmAnd/src/net/osmand/plus/myplaces/GPXItemPagerAdapter.java b/OsmAnd/src/net/osmand/plus/myplaces/GPXItemPagerAdapter.java index 2df66263e4..8f0c5a1dc5 100644 --- a/OsmAnd/src/net/osmand/plus/myplaces/GPXItemPagerAdapter.java +++ b/OsmAnd/src/net/osmand/plus/myplaces/GPXItemPagerAdapter.java @@ -102,32 +102,19 @@ public class GPXItemPagerAdapter extends PagerAdapter implements CustomTabProvid public GPXItemPagerAdapter(@NonNull OsmandApplication app, @Nullable GpxDisplayItem gpxItem, - @Nullable TrackDisplayHelper displayHelper, + @NonNull TrackDisplayHelper displayHelper, boolean nightMode, @NonNull SegmentActionsListener actionsListener, boolean onlyGraphs) { super(); this.app = app; this.gpxItem = gpxItem; + this.displayHelper = displayHelper; this.nightMode = nightMode; this.actionsListener = actionsListener; this.onlyGraphs = onlyGraphs; iconsCache = app.getUIUtilities(); - if (displayHelper == null) { - this.displayHelper = new TrackDisplayHelper(app); - SelectedGpxFile currentTrack = app.getSavingTrackHelper().getCurrentTrack(); - GPXFile gpxFile = currentTrack.getGpxFile(); - if (currentTrack.isShowCurrentTrack()) { - File file = new File(gpxFile.path); - this.displayHelper.setFile(file); - this.displayHelper.setGpxDataItem(app.getGpxDbHelper().getItem(file)); - } - this.displayHelper.setGpx(gpxFile); - } else { - this.displayHelper = displayHelper; - } - updateAnalysis(); fetchTabTypes(); }