diff --git a/Sources/Helpers/OAGPXUIHelper.h b/Sources/Helpers/OAGPXUIHelper.h index dc1a244efe..8e17654f64 100644 --- a/Sources/Helpers/OAGPXUIHelper.h +++ b/Sources/Helpers/OAGPXUIHelper.h @@ -36,6 +36,7 @@ NS_ASSUME_NONNULL_BEGIN + (NSString *) getDescription:(OASGpxDataItem *)gpx; + (long) getSegmentTime:(OASTrkSegment *)segment; ++ (long)liveTimeSpanForGpx:(OASGpxFile *)gpx withoutGaps:(BOOL)withoutGaps; + (double) getSegmentDistance:(OASTrkSegment *)segment; + (NSArray *) getSortedGPXFilesInfo:(nullable NSString *)dir diff --git a/Sources/Helpers/OAGPXUIHelper.mm b/Sources/Helpers/OAGPXUIHelper.mm index b0ae74b020..0c5fc86ca0 100644 --- a/Sources/Helpers/OAGPXUIHelper.mm +++ b/Sources/Helpers/OAGPXUIHelper.mm @@ -217,6 +217,34 @@ + (long)getSegmentTime:(OASTrkSegment *)segment return endTime - startTime; } ++ (long)liveTimeSpanForGpx:(OASGpxFile *)gpx withoutGaps:(BOOL)withoutGaps +{ + long totalActiveDuration = 0; + long earliestSegmentStart = LONG_MAX; + long latestSegmentEnd = 0; + for (OASTrack *track in gpx.tracks) + { + for (OASTrkSegment *segment in track.segments) + { + NSArray *points = segment.points; + if (points.count < 2) + continue; + + long segmentDuration = [self getSegmentTime:segment]; + totalActiveDuration += segmentDuration; + long segmentStartTime = ((OASWptPt *)points.firstObject).time; + long segmentEndTime = ((OASWptPt *)points.lastObject).time; + earliestSegmentStart = MIN(earliestSegmentStart, segmentStartTime); + latestSegmentEnd = MAX(latestSegmentEnd, segmentEndTime); + } + } + + if (earliestSegmentStart == LONG_MAX) + return 0; + + return withoutGaps ? totalActiveDuration : (latestSegmentEnd - earliestSegmentStart); +} + + (double) getSegmentDistance:(OASTrkSegment *)segment { double distance = 0; diff --git a/Sources/Plugins/Monitoring/OATripRecordingTimeWidget.mm b/Sources/Plugins/Monitoring/OATripRecordingTimeWidget.mm index 0ba3e96377..a8a97484a5 100644 --- a/Sources/Plugins/Monitoring/OATripRecordingTimeWidget.mm +++ b/Sources/Plugins/Monitoring/OATripRecordingTimeWidget.mm @@ -36,11 +36,8 @@ - (instancetype)initWithСustomId:(NSString *)customId [weakSelf setIcon:@"widget_track_recording_duration"]; OASGpxFile *currentTrack = [OASavingTrackHelper sharedInstance].currentTrack; - BOOL withoutGaps = ![[OAAppSettings sharedManager].currentTrackIsJoinSegments get] && - ((!currentTrack.tracks || currentTrack.tracks.count == 0) || currentTrack.tracks[0].generalTrack); - - OASGpxTrackAnalysis *analysis = [currentTrack getAnalysisFileTimestamp:0]; - long timeSpan = withoutGaps ? analysis.timeSpanWithoutGaps : analysis.timeSpan; + BOOL withoutGaps = ![[OAAppSettings sharedManager].currentTrackIsJoinSegments get]; + long timeSpan = [OAGPXUIHelper liveTimeSpanForGpx:currentTrack withoutGaps:withoutGaps]; if (cachedTimeSpan != timeSpan) {