Skip to content

Commit 27d8842

Browse files
committed
vk-1024-hybrid-router: added NavigationRouter to provide hybrid router functionality for requesting and refreshing routes; removed Directions extension and Navigator.routerInterface; added CacheHandlerFactory to cache latest CacheHandle created.
1 parent 6b827ee commit 27d8842

21 files changed

+577
-244
lines changed

Example/ViewController.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -479,7 +479,7 @@ class ViewController: UIViewController {
479479
}
480480

481481
func requestRoute(with options: RouteOptions, success: @escaping RouteRequestSuccess, failure: RouteRequestFailure?) {
482-
Directions.shared.calculateWithCache(options: options) { (session, result) in
482+
NavigationRouter().requestRoutes(options: options) { (session, result) in
483483
switch result {
484484
case let .success(response):
485485
success(response)

MapboxNavigation.xcodeproj/project.pbxproj

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535
16EF6C22211BA4B300AA580B /* CarPlayMapViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 16EF6C21211BA4B300AA580B /* CarPlayMapViewController.swift */; };
3636
1FFDFD92249C1AA80091746A /* JunctionView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1FFDFD91249C1AA70091746A /* JunctionView.swift */; };
3737
2B07444124B4832400615E87 /* TokenTestViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2B07444024B4832400615E87 /* TokenTestViewController.swift */; };
38+
2B2C8DBA26E75884002CCF08 /* NavigationRouter.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2B2C8DB926E75884002CCF08 /* NavigationRouter.swift */; };
39+
2B2C8DBC26E7589F002CCF08 /* CacheHandlerFactory.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2B2C8DBB26E7589F002CCF08 /* CacheHandlerFactory.swift */; };
3840
2B3ED38C2609FA7900861A84 /* ArrivalController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2B3ED38B2609FA7900861A84 /* ArrivalController.swift */; };
3941
2B3ED3962609FB2300861A84 /* CameraController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2B3ED3952609FB2300861A84 /* CameraController.swift */; };
4042
2B3ED3B4260A162900861A84 /* NavigationViewData.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2B3ED3B3260A162900861A84 /* NavigationViewData.swift */; };
@@ -50,7 +52,6 @@
5052
2B81EC28241A237E00145086 /* SpeechSynthesizersControllerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2B81EC27241A237E00145086 /* SpeechSynthesizersControllerTests.swift */; };
5153
2B871272263966F0001082A9 /* TileStoreConfiguration.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2B8712682639631C001082A9 /* TileStoreConfiguration.swift */; };
5254
2B91C9B12416357700E532A5 /* MapboxSpeechSynthesizer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2B91C9B02416357700E532A5 /* MapboxSpeechSynthesizer.swift */; };
53-
2B955B1C25EFDDCB00BBFEC6 /* Directions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2B955B1B25EFDDCB00BBFEC6 /* Directions.swift */; };
5455
2BBED92F265E2C7D00F90032 /* NativeHandlersFactory.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2BBED92E265E2C7D00F90032 /* NativeHandlersFactory.swift */; };
5556
2BBED93B267A3AB900F90032 /* BillingHandler.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2BBED93A267A3AB900F90032 /* BillingHandler.swift */; };
5657
2BBEEDA52508DB1700C8DA4A /* RouteLegProgress.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2BBEEDA42508DB1700C8DA4A /* RouteLegProgress.swift */; };
@@ -522,6 +523,8 @@
522523
16EF6C21211BA4B300AA580B /* CarPlayMapViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CarPlayMapViewController.swift; sourceTree = "<group>"; };
523524
1FFDFD91249C1AA70091746A /* JunctionView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = JunctionView.swift; sourceTree = "<group>"; };
524525
2B07444024B4832400615E87 /* TokenTestViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TokenTestViewController.swift; sourceTree = "<group>"; };
526+
2B2C8DB926E75884002CCF08 /* NavigationRouter.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NavigationRouter.swift; sourceTree = "<group>"; };
527+
2B2C8DBB26E7589F002CCF08 /* CacheHandlerFactory.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CacheHandlerFactory.swift; sourceTree = "<group>"; };
525528
2B3ED38B2609FA7900861A84 /* ArrivalController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ArrivalController.swift; sourceTree = "<group>"; };
526529
2B3ED3952609FB2300861A84 /* CameraController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CameraController.swift; sourceTree = "<group>"; };
527530
2B3ED3B3260A162900861A84 /* NavigationViewData.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NavigationViewData.swift; sourceTree = "<group>"; };
@@ -537,7 +540,6 @@
537540
2B81EC27241A237E00145086 /* SpeechSynthesizersControllerTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SpeechSynthesizersControllerTests.swift; sourceTree = "<group>"; };
538541
2B8712682639631C001082A9 /* TileStoreConfiguration.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TileStoreConfiguration.swift; sourceTree = "<group>"; };
539542
2B91C9B02416357700E532A5 /* MapboxSpeechSynthesizer.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MapboxSpeechSynthesizer.swift; sourceTree = "<group>"; };
540-
2B955B1B25EFDDCB00BBFEC6 /* Directions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Directions.swift; sourceTree = "<group>"; };
541543
2BBED92E265E2C7D00F90032 /* NativeHandlersFactory.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NativeHandlersFactory.swift; sourceTree = "<group>"; };
542544
2BBED93A267A3AB900F90032 /* BillingHandler.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BillingHandler.swift; sourceTree = "<group>"; };
543545
2BBEEDA42508DB1700C8DA4A /* RouteLegProgress.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RouteLegProgress.swift; sourceTree = "<group>"; };
@@ -1708,7 +1710,6 @@
17081710
C582FD5E203626E900A9086E /* CLLocationDirection.swift */,
17091711
2E82B9DB26E61F4600B7837F /* CongestionLevel.swift */,
17101712
C5D9800E1EFBCDAD006DBF2E /* Date.swift */,
1711-
2B955B1B25EFDDCB00BBFEC6 /* Directions.swift */,
17121713
3A163AE2249901D000D66A0D /* FixLocation.swift */,
17131714
DAF27247264E028B00C0AC37 /* Geometry.swift */,
17141715
C51DF8651F38C31C006C6A15 /* Locale.swift */,
@@ -1767,13 +1768,15 @@
17671768
DABA591425E58D5600D0C1DB /* Accounts.swift */,
17681769
2BBED93A267A3AB900F90032 /* BillingHandler.swift */,
17691770
C58D6BAC1DDCF2AE00387F53 /* CoreConstants.swift */,
1771+
2B2C8DBB26E7589F002CCF08 /* CacheHandlerFactory.swift */,
17701772
351BEC0B1E5BCC72006FE110 /* DistanceFormatter.swift */,
17711773
B417913A2624F9EA001E0348 /* MBXInfo.plist */,
17721774
C5ADFBCD1DDCC7840011824B /* Info.plist */,
17731775
C5ADFBCC1DDCC7840011824B /* MapboxCoreNavigation.h */,
17741776
2BBED92E265E2C7D00F90032 /* NativeHandlersFactory.swift */,
17751777
353E68FB1EF0B7F8007B2AE5 /* NavigationLocationManager.swift */,
17761778
C5E7A31B1F4F6828001CB015 /* NavigationRouteOptions.swift */,
1779+
2B2C8DB926E75884002CCF08 /* NavigationRouter.swift */,
17771780
35375EC01F31FA86004CE727 /* NavigationSettings.swift */,
17781781
351174F31EF1C0530065E248 /* ReplayLocationManager.swift */,
17791782
C5ADFBF91DDCC9580011824B /* LegacyRouteController.swift */,
@@ -2614,6 +2617,7 @@
26142617
417D127726E24D0800E0AB16 /* FreeDriveEventDetails.swift in Sources */,
26152618
353E68FC1EF0B7F8007B2AE5 /* NavigationLocationManager.swift in Sources */,
26162619
2BE7013D25359C7B00F46E4E /* RouteAlert.swift in Sources */,
2620+
2B2C8DBC26E7589F002CCF08 /* CacheHandlerFactory.swift in Sources */,
26172621
353E68FE1EF0B985007B2AE5 /* BundleAdditions.swift in Sources */,
26182622
11D1F8A22696EBD40053A93F /* Dictionary+Equality.swift in Sources */,
26192623
2EFAE005264C1F9200B618C4 /* RoadObjectLocation.swift in Sources */,
@@ -2668,7 +2672,6 @@
26682672
353E69041EF0C4E5007B2AE5 /* SimulatedLocationManager.swift in Sources */,
26692673
DA5F450025F07DE200F573EC /* ElectronicHorizonOptions.swift in Sources */,
26702674
DAFA92071F01735000A7FB09 /* DistanceFormatter.swift in Sources */,
2671-
2B955B1C25EFDDCB00BBFEC6 /* Directions.swift in Sources */,
26722675
DAF27252264E02D800C0AC37 /* RoadObject.swift in Sources */,
26732676
5A39B9282498F9890026DFD1 /* PassiveLocationManager.swift in Sources */,
26742677
118D883526F8CA0700B2ED7B /* EndOfRouteFeedback.swift in Sources */,
@@ -2682,6 +2685,7 @@
26822685
C578DA081EFD0FFF0052079F /* ProcessInfo.swift in Sources */,
26832686
2B42586E2657BF9100B487C3 /* TileStore.swift in Sources */,
26842687
2BE7016925371E3400F46E4E /* Incident.swift in Sources */,
2688+
2B2C8DBA26E75884002CCF08 /* NavigationRouter.swift in Sources */,
26852689
E2805A5826CB994500165DB9 /* NSLock+MapboxInternal.swift in Sources */,
26862690
C58D6BAD1DDCF2AE00387F53 /* CoreConstants.swift in Sources */,
26872691
2B7ACA9B25E3F84700B0ACFD /* PredictiveCacheManager.swift in Sources */,
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import MapboxDirections
2+
import MapboxNavigationNative
3+
4+
protocol CacheHandlerData {
5+
var tileStorePath: String { get }
6+
var credentials: DirectionsCredentials { get }
7+
var tilesVersion: String? { get }
8+
var historyDirectoryURL: URL? { get }
9+
var targetVersion: String? { get }
10+
var configFactoryType: ConfigFactory.Type { get }
11+
}
12+
13+
extension NativeHandlersFactory: CacheHandlerData { }
14+
15+
enum CacheHandlerFactory {
16+
17+
private struct CacheKey: CacheHandlerData {
18+
let tileStorePath: String
19+
let credentials: DirectionsCredentials
20+
let tilesVersion: String?
21+
let historyDirectoryURL: URL?
22+
let targetVersion: String?
23+
let configFactoryType: ConfigFactory.Type
24+
25+
init(data: CacheHandlerData) {
26+
self.tileStorePath = data.tileStorePath
27+
self.credentials = data.credentials
28+
self.tilesVersion = data.tilesVersion
29+
self.historyDirectoryURL = data.historyDirectoryURL
30+
self.targetVersion = data.targetVersion
31+
self.configFactoryType = data.configFactoryType
32+
}
33+
34+
static func != (lhs: CacheKey, rhs: CacheHandlerData) -> Bool {
35+
return lhs.tileStorePath != rhs.tileStorePath ||
36+
lhs.credentials != rhs.credentials ||
37+
lhs.tilesVersion != rhs.tilesVersion ||
38+
lhs.historyDirectoryURL != rhs.historyDirectoryURL ||
39+
lhs.targetVersion != rhs.targetVersion ||
40+
lhs.configFactoryType != rhs.configFactoryType
41+
}
42+
}
43+
44+
private static var key: CacheKey? = nil
45+
private static var cachedHandle: CacheHandle!
46+
private static let lock = NSLock()
47+
48+
static func getHandler(for tilesConfig: TilesConfig,
49+
config: ConfigHandle,
50+
historyRecorder: HistoryRecorderHandle?,
51+
cacheData: CacheHandlerData) -> CacheHandle {
52+
lock.lock(); defer {
53+
lock.unlock()
54+
}
55+
56+
if key == nil || key! != cacheData {
57+
cachedHandle = CacheFactory.build(for: tilesConfig,
58+
config: config,
59+
historyRecorder: historyRecorder)
60+
key = .init(data: cacheData)
61+
}
62+
return cachedHandle
63+
}
64+
}

Sources/MapboxCoreNavigation/CoreNavigationNavigator.swift

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,6 @@ class Navigator {
1717

1818
private(set) var cacheHandle: CacheHandle
1919

20-
lazy var routerInterface: MapboxNavigationNative.RouterInterface = {
21-
return MapboxNavigationNative.RouterFactory.build(for: .hybrid,
22-
cache: cacheHandle,
23-
historyRecorder: historyRecorder)
24-
}()
25-
2620
var mostRecentNavigationStatus: NavigationStatus? = nil
2721

2822
private(set) var tileStore: TileStore

Sources/MapboxCoreNavigation/Directions.swift

Lines changed: 0 additions & 73 deletions
This file was deleted.

Sources/MapboxCoreNavigation/LegacyRouteController.swift

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ open class LegacyRouteController: NSObject, Router, InternalRouter, CLLocationMa
1818
public unowned var dataSource: RouterDataSource
1919

2020
/**
21-
The Directions object used to create the route.
21+
Routing source type used to create the route.
2222
*/
23-
public var directions: Directions
23+
public var routingSource: NavigationRouter.RouterSource
2424

2525
public var route: Route {
2626
routeProgress.route
@@ -102,7 +102,7 @@ open class LegacyRouteController: NSObject, Router, InternalRouter, CLLocationMa
102102

103103
var didFindFasterRoute = false
104104

105-
var routeTask: URLSessionDataTask?
105+
var routeTask: NavigationRouter.RoutingRequest?
106106

107107

108108
// MARK: Navigating
@@ -122,12 +122,13 @@ open class LegacyRouteController: NSObject, Router, InternalRouter, CLLocationMa
122122
var userSnapToStepDistanceFromManeuver: CLLocationDistance?
123123

124124
public func updateRoute(with indexedRouteResponse: IndexedRouteResponse, routeOptions: RouteOptions?) {
125-
guard let routes = indexedRouteResponse.routeResponse.routes, routes.count > indexedRouteResponse.routeIndex else {
125+
guard let route = indexedRouteResponse.selectedRoute else {
126126
preconditionFailure("`indexedRouteResponse` does not contain route for index `\(indexedRouteResponse.routeIndex)` when updating route.")
127127
}
128128
let routeOptions = routeOptions ?? routeProgress.routeOptions
129-
routeProgress = RouteProgress(route: routes[indexedRouteResponse.routeIndex], options: routeOptions)
129+
routeProgress = RouteProgress(route: route, options: routeOptions)
130130
self.indexedRouteResponse = indexedRouteResponse
131+
announce(reroute: route, at: location, proactive: false)
131132
}
132133

133134
public func advanceLegIndex(completionHandler: AdvanceLegCompletionHandler? = nil) {
@@ -181,8 +182,8 @@ open class LegacyRouteController: NSObject, Router, InternalRouter, CLLocationMa
181182
return false
182183
}
183184

184-
required public init(alongRouteAtIndex routeIndex: Int, in routeResponse: RouteResponse, options: RouteOptions, directions: Directions = NavigationSettings.shared.directions, dataSource source: RouterDataSource) {
185-
self.directions = directions
185+
required public init(alongRouteAtIndex routeIndex: Int, in routeResponse: RouteResponse, options: RouteOptions, routingSource: NavigationRouter.RouterSource = .hybrid, dataSource source: RouterDataSource) {
186+
self.routingSource = routingSource
186187
self.indexedRouteResponse = .init(routeResponse: routeResponse, routeIndex: routeIndex)
187188
self.routeProgress = RouteProgress(route: routeResponse.routes![routeIndex], options: options)
188189
self.dataSource = source

Sources/MapboxCoreNavigation/NativeHandlersFactory.swift

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,10 @@ class NativeHandlersFactory {
4848
}()
4949

5050
lazy var cacheHandle: CacheHandle = {
51-
CacheFactory.build(for: tilesConfig,
52-
config: configHandle,
53-
historyRecorder: historyRecorder)
51+
CacheHandlerFactory.getHandler(for: tilesConfig,
52+
config: configHandle,
53+
historyRecorder: historyRecorder,
54+
cacheData: self)
5455
}()
5556

5657
lazy var roadGraph: RoadGraph = {

0 commit comments

Comments
 (0)