Skip to content

Commit

Permalink
Merge pull request #3010 from osmandapp/Transport-stop-index-fix
Browse files Browse the repository at this point in the history
Transport stop index fix
  • Loading branch information
alex-osm committed Jul 25, 2023
2 parents 4fb23a8 + 071939a commit 18af004
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ - (void) addRoutes:(NSMutableArray<OATransportStopRoute *> *)routes dataInterfac
}

r.distance = dist;
[r initStopIndex];
[routes addObject:r];
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ - (void)addLastItems:(NSMutableArray *)arr end:(OARTargetPoint *)end routeRes:(c
- (void)buildCollapsibleCells:(NSMutableArray *)arr color:(UIColor *)color segment:(const std::shared_ptr<TransportRouteResultSegment> &)segment stopType:(OATransportStopType *)stopType stops:(const std::vector<std::shared_ptr<TransportStop>, std::allocator<std::shared_ptr<TransportStop> > > &)stops section:(NSInteger)section {
OATransportStopRoute *r = [[OATransportStopRoute alloc] init];
r.type = stopType;
[r initStopIndex];
NSMutableDictionary *collapsableCell = [NSMutableDictionary new];
NSMutableArray *subItems = [NSMutableArray new];
NSMutableArray<NSIndexPath *> *indexPaths = [NSMutableArray new];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,19 +71,20 @@ - (void) leftControlButtonPressed
{
int previousStop = [self getPreviousStop];
if (previousStop != -1)
[self showTransportStop:_transportRoute.route->forwardStops[previousStop]];
[self showTransportStop:_transportRoute.route->forwardStops[previousStop] stopIndex:previousStop];
}

- (void) rightControlButtonPressed
{
int nextStop = [self getNextStop];
if (nextStop != -1)
[self showTransportStop:_transportRoute.route->forwardStops[nextStop]];
[self showTransportStop:_transportRoute.route->forwardStops[nextStop] stopIndex:nextStop];
}

- (void) showTransportStop:(std::shared_ptr<OsmAnd::TransportStop>)stop
- (void) showTransportStop:(std::shared_ptr<OsmAnd::TransportStop>)stop stopIndex:(int)stopIndex
{
_transportRoute.stop = stop;
[_transportRoute setStopIndex:stopIndex];
_transportRoute.refStop = stop;

[self refreshContextMenu];
Expand Down Expand Up @@ -189,22 +190,10 @@ - (ETopToolbarType) topToolbarType
return ETopToolbarTypeFixed;
}

- (int) getCurrentStop
{
const auto& stops = _transportRoute.route->forwardStops;
for (int i = 0; i < stops.size(); i++)
{
auto stop = stops[i];
if (stop->getName(_lang, _transliterate) == _transportRoute.stop->getName(_lang, _transliterate))
return i;
}
return -1;
}

- (int) getNextStop
{
const auto& stops = _transportRoute.route->forwardStops;
int currentPos = [self getCurrentStop];
int currentPos = [_transportRoute getStopIndex];
if (currentPos != -1 && currentPos + 1 < stops.size())
return currentPos + 1;

Expand All @@ -213,7 +202,7 @@ - (int) getNextStop

- (int) getPreviousStop
{
int currentPos = [self getCurrentStop];
int currentPos = [_transportRoute getStopIndex];
if (currentPos > 0)
return currentPos - 1;

Expand All @@ -223,7 +212,7 @@ - (int) getPreviousStop
- (void) buildRows:(NSMutableArray<OARowInfo *> *)rows
{
const auto& stops = _transportRoute.route->forwardStops;
int currentStop = [self getCurrentStop];
int currentStop = [_transportRoute getStopIndex];
UIImage *defaultIcon = [OATargetInfoViewController getIcon:[NSString stringWithFormat:@"%@.png", !_transportRoute.type ? @"mx_route_bus_ref" : _transportRoute.type.resId]];
int startPosition = 0;
if (!_transportRoute.showWholeRoute && currentStop > 1)
Expand Down Expand Up @@ -324,7 +313,7 @@ - (void)onRowClick:(OATargetMenuViewController *)sender rowInfo:(OARowInfo *)row
const auto& stops = _transportRoute.route->forwardStops;
int index = [[rowInfo.key substringFromIndex:5] intValue];
if (index < stops.size())
[self showTransportStop:stops[index]];
[self showTransportStop:stops[index] stopIndex:-1];
}
}

Expand Down
5 changes: 5 additions & 0 deletions Sources/Transport/OATransportStopRoute.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ UIKIT_EXTERN NSString *const OATransportStopRouteArrow;
@property (nonatomic) NSString *desc;
@property (nonatomic, assign) std::shared_ptr<const OsmAnd::TransportRoute> route;
@property (nonatomic, assign) std::shared_ptr<const OsmAnd::TransportStop> stop;
@property (nonatomic) int stopIndex;
@property (nonatomic) int distance;
@property (nonatomic) BOOL showWholeRoute;

Expand All @@ -32,6 +33,10 @@ UIKIT_EXTERN NSString *const OATransportStopRouteArrow;
- (UIColor *) getColor:(BOOL)nightMode;
- (NSString *) getTypeStr;

- (void) initStopIndex;
- (int) getStopIndex;
- (void) setStopIndex:(int)stopIndex;

- (OATransportStopRoute *) clone;

@end
52 changes: 52 additions & 0 deletions Sources/Transport/OATransportStopRoute.mm
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#import "OAColors.h"
#import "OAUtilities.h"
#import "OAOsmAndFormatter.h"
#import "OAAppSettings.h"

#include <OsmAndCore.h>
#include <OsmAndCore/Utilities.h>
Expand All @@ -32,6 +33,16 @@ @interface OATransportStopRoute ()

@implementation OATransportStopRoute

- (instancetype) init
{
self = [super init];
if (self)
{
_stopIndex = -1;
}
return self;
}

- (NSString *) getDescription:(BOOL)useDistance
{
OsmAndAppInstance app = [OsmAndApp instance];
Expand Down Expand Up @@ -169,9 +180,50 @@ - (OATransportStopRoute *) clone
res.stop = self.stop;
res.type = self.type;
res.desc = self.desc;
res.stopIndex = self.stopIndex;
res.distance = self.distance;
res.showWholeRoute = self. showWholeRoute;
return res;
}

- (void) initStopIndex
{
if (_route == nullptr || _stop == nullptr)
return;

NSString *_prefLang = [OAAppSettings sharedManager].settingPrefMapLanguage.get;
BOOL _transliterate = [OAAppSettings sharedManager].settingMapLanguageTranslit.get;
QString _lang = QString::fromNSString(_prefLang);

auto& stops = _route->forwardStops;
for (int i = 0; i < stops.size(); i++)
{
auto stop = stops[i];
if (_stop->getName(_lang, _transliterate) == stop->getName(_lang, _transliterate))
{
_stopIndex = i;
break;
}
}
if (_stopIndex == -1)
_stopIndex = 0;
}

- (int) getStopIndex
{
if (_stopIndex == -1)
[self initStopIndex];
return _stopIndex;
}

- (void) setStopIndex:(int)stopIndex
{
if (_route == nullptr || _stop == nullptr)
return;
if (_stopIndex == -1)
[self initStopIndex];
else if (_stopIndex < _route->forwardStops.size())
_stopIndex = stopIndex;
}

@end

0 comments on commit 18af004

Please sign in to comment.