Skip to content

Commit

Permalink
Fix slow track menu opening
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-osm committed Mar 10, 2024
1 parent 958c7ca commit 915483d
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
#import "OASavingTrackHelper.h"
#import "OAGPXTrackAnalysis.h"
#import "GeneratedAssetSymbols.h"
#import "OASelectedGPXHelper.h"
#import "OAGPXUIHelper.h"

@implementation OAGPXBaseTableData

Expand Down Expand Up @@ -231,11 +233,31 @@ - (void)updateGpxData:(BOOL)replaceGPX updateDocument:(BOOL)updateDocument
else
{
NSString *gpxFullPath = [[OsmAndApp instance].gpxPath stringByAppendingPathComponent:_gpx.gpxFilePath];
_doc = [[OAGPXMutableDocument alloc] initWithGpxFile:gpxFullPath];
const auto& activeGpx = [OASelectedGPXHelper instance].activeGpx;
auto it = activeGpx.find(QString::fromNSString(gpxFullPath));
if (it != activeGpx.end())
_doc = [[OAGPXMutableDocument alloc] initWithGpxDocument:std::const_pointer_cast<OsmAnd::GpxDocument>(it.value())];
else
_doc = [[OAGPXMutableDocument alloc] initWithGpxFile:gpxFullPath];
}
}
[self updateAnalysis];

if (_gpx && !_gpx.nearestCity && _analysis && _analysis.locationStart)
{
OAPOI *nearestCity = [OAGPXUIHelper searchNearestCity:_analysis.locationStart.position];
_gpx.nearestCity = nearestCity ? nearestCity.nameLocalized : @"";

OAGPXDatabase *db = [OAGPXDatabase sharedDb];
OAGPX *gpx = [db getGPXItem:_gpx.gpxFilePath];
if (gpx)
{
gpx.nearestCity = _gpx.nearestCity;
[db replaceGpxItem:gpx];
[db save];
}
}

if (replaceGPX)
{
if (_isCurrentTrack)
Expand All @@ -246,6 +268,7 @@ - (void)updateGpxData:(BOOL)replaceGPX updateDocument:(BOOL)updateDocument
{
OAGPXDatabase *db = [OAGPXDatabase sharedDb];
OAGPX *gpx = [db buildGpxItem:_gpx.gpxFilePath title:_doc.metadata.name desc:_doc.metadata.desc bounds:_doc.bounds document:_doc];
gpx.nearestCity = _gpx.nearestCity;
[db replaceGpxItem:gpx];
[db save];
_gpx = gpx;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@
shownTrack:(BOOL)shownTrack
isNetworkRoute:(BOOL)isNetworkRoute
routeIcon:(UIImage *)icon
title:(NSString *)title;
title:(NSString *)title
nearestCity:(NSString *)nearestCity;

+ (NSMutableArray<OAGPXTableCellData *> *)generateGpxBlockStatistics:(OAGPXTrackAnalysis *)analysis withoutGaps:(BOOL)withoutGaps;
- (void)generateGpxBlockStatistics:(OAGPXTrackAnalysis *)analysis
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ - (void)updateHeader:(BOOL)currentTrack
isNetworkRoute:(BOOL)isNetworkRoute
routeIcon:(UIImage *)icon
title:(NSString *)title
nearestCity:(NSString *)nearestCity
{
self.backgroundColor = _selectedTab != EOATrackMenuHudActionsTab
? [UIColor colorNamed:ACColorNameGroupBg] : [UIColor colorNamed:ACColorNameViewBg];
Expand All @@ -138,14 +139,12 @@ - (void)updateHeader:(BOOL)currentTrack
if (_selectedTab == EOATrackMenuHudOverviewTab)
{
CLLocationCoordinate2D gpxLocation = kCLLocationCoordinate2DInvalid;
OAPOI *nearestCity;
if (self.trackMenuDelegate)
{
if ([self.trackMenuDelegate openedFromMap])
gpxLocation = [self.trackMenuDelegate getPinLocation];
if (!CLLocationCoordinate2DIsValid(gpxLocation))
gpxLocation = [self.trackMenuDelegate getCenterGpxLocation];
nearestCity = [OAGPXUIHelper checkAndSearchNearestCity:[self.trackMenuDelegate getGeneralAnalysis]];
}

CLLocation *lastKnownLocation = _app.locationServices.lastKnownLocation;
Expand All @@ -162,11 +161,11 @@ - (void)updateHeader:(BOOL)currentTrack
self.directionTextView.textColor = [UIColor colorNamed:ACColorNameTextColorActive];
}

if (nearestCity)
if (nearestCity.length > 0)
{
self.regionIconView.image = [UIImage templateImageNamed:@"ic_small_map_point"];
self.regionIconView.tintColor = [UIColor colorNamed:ACColorNameIconColorSecondary];
[self.regionTextView setText:nearestCity.nameLocalized];
[self.regionTextView setText:nearestCity];
self.regionTextView.textColor = [UIColor colorNamed:ACColorNameTextColorSecondary];
}
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,8 @@ - (void)setupHeaderView
shownTrack:self.isShown
isNetworkRoute:_isNewRoute
routeIcon:isRoute ? _reopeningState.trackIcon : [UIImage templateImageNamed:@"ic_custom_trip"]
title:[self.gpx getNiceTitle]];
title:[self.gpx getNiceTitle]
nearestCity:self.gpx.nearestCity];

[self.scrollableView addSubview:_headerView];

Expand Down Expand Up @@ -1496,7 +1497,8 @@ - (void)saveNetworkRoute
shownTrack:self.isShown
isNetworkRoute:_isNewRoute
routeIcon:_reopeningState.trackIcon
title:[self.gpx getNiceTitle]];
title:[self.gpx getNiceTitle]
nearestCity:gpx.nearestCity];
[self setupUIBuilder];
[_uiBuilder setupTabBar:self.tabBarView
parentWidth:self.scrollableView.frame.size.width];
Expand Down
1 change: 1 addition & 0 deletions Sources/GPX/OAGPXDatabase.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ typedef NS_ENUM(NSInteger, EOAGpxSplitType) {
@property (nonatomic) double metricEnd;
@property (nonatomic) OAWptPt *locationStart;
@property (nonatomic) OAWptPt *locationEnd;
@property (nonatomic) NSString *nearestCity;

- (NSString *)getNiceTitle;

Expand Down
20 changes: 18 additions & 2 deletions Sources/GPX/OAGPXDatabase.mm
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
#import "OAUtilities.h"
#import "OASavingTrackHelper.h"
#import "OAGPXAppearanceCollection.h"
#import "OAGPXUIHelper.h"
#import "OAPOI.h"

#define kDbName @"gpx.db"

Expand Down Expand Up @@ -177,7 +179,14 @@ - (OAGPX *) buildGpxItem:(NSString *)fileName title:(NSString *)title desc:(NSSt
- (OAGPX *) buildGpxItem:(NSString *)fileName path:(NSString *)filepath title:(NSString *)title desc:(NSString *)desc bounds:(OAGpxBounds)bounds document:(OAGPXDocument *)document
{
OAGPXTrackAnalysis *analysis = [document getAnalysis:0];


NSString *nearestCity;
if (analysis.locationStart)
{
OAPOI *nearestCityPOI = [OAGPXUIHelper searchNearestCity:analysis.locationStart.position];
nearestCity = nearestCityPOI ? nearestCityPOI.nameLocalized : @"";
}

OAGPX *gpx = [[OAGPX alloc] init];
NSString *pathToRemove = [OsmAndApp.instance.gpxPath stringByAppendingString:@"/"];
gpx.bounds = bounds;
Expand Down Expand Up @@ -219,7 +228,8 @@ - (OAGPX *) buildGpxItem:(NSString *)fileName path:(NSString *)filepath title:(N
gpx.metricEnd = analysis.metricEnd;
gpx.locationStart = analysis.locationStart;
gpx.locationEnd = analysis.locationEnd;

gpx.nearestCity = nearestCity;

gpx.splitType = [self.class splitTypeByName:document.getSplitType];
gpx.splitInterval = [document getSplitInterval];
gpx.color = [document getColor:0];
Expand Down Expand Up @@ -552,6 +562,8 @@ - (NSDictionary *)generateGpxData:(OAGPX *)gpx
[wpt setObject:@(gpx.locationEnd.speed) forKey:@"speed"];
[d setObject:wpt forKey:@"locationEnd"];
}
if (gpx.nearestCity)
[d setObject:gpx.nearestCity forKey:@"nearestCity"];

return d;
}
Expand Down Expand Up @@ -671,6 +683,10 @@ - (OAGPX *)generateGpxItem:(NSDictionary *)gpxData
{
gpx.hiddenGroups = [NSSet setWithArray:value];
}
else if ([key isEqualToString:@"nearestCity"])
{
gpx.nearestCity = value;
}
}
if (!gpx.hiddenGroups)
gpx.hiddenGroups = [NSSet set];
Expand Down
2 changes: 1 addition & 1 deletion Sources/Helpers/OAGPXUIHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
preciseLocation:(BOOL)preciseLocation
joinSegments:(BOOL)joinSegments;

+ (OAPOI *)checkAndSearchNearestCity:(OAGPXTrackAnalysis *)analysis;
+ (OAPOI *)searchNearestCity:(CLLocationCoordinate2D)latLon;

- (void) openExportForTrack:(OAGPX *)gpx gpxDoc:(id)gpxDoc isCurrentTrack:(BOOL)isCurrentTrack inViewController:(UIViewController *)hostViewController hostViewControllerDelegate:(id)hostViewControllerDelegate;
- (void) copyGPXToNewFolder:(NSString *)newFolderName
Expand Down
6 changes: 0 additions & 6 deletions Sources/Helpers/OAGPXUIHelper.mm
Original file line number Diff line number Diff line change
Expand Up @@ -450,12 +450,6 @@ + (CLLocationCoordinate2D)getIntermediatePointByDistance:(double)passedDistance
return CLLocationCoordinate2DMake(prevPoint.position.latitude + dLat, prevPoint.position.longitude + dLon);
}

+ (OAPOI *)checkAndSearchNearestCity:(OAGPXTrackAnalysis *)analysis
{
OAWptPt *startPoint = analysis ? analysis.locationStart : nil;
return startPoint ? [self searchNearestCity:startPoint.position] : nil;
}

+ (OAPOI *)searchNearestCity:(CLLocationCoordinate2D)latLon
{
OsmAnd::PointI pointI = OsmAnd::Utilities::convertLatLonTo31(OsmAnd::LatLon(latLon.latitude, latLon.longitude));
Expand Down

0 comments on commit 915483d

Please sign in to comment.