Skip to content

Commit

Permalink
vk-3689-nn-route: renamed RouteCoordinator handlers and type alieases…
Browse files Browse the repository at this point in the history
…; renamed RouteController updateNavigator argument
  • Loading branch information
Udumft committed Apr 20, 2022
1 parent d77ac35 commit c5467e3
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 24 deletions.
6 changes: 3 additions & 3 deletions Sources/MapboxCoreNavigation/CoreNavigationNavigator.swift
Expand Up @@ -33,7 +33,7 @@ class Navigator {
}

private lazy var routeCoordinator: RoutesCoordinator = {
.init(setMainRouteHandler: { [weak self] route, legIndex, completion in
.init(mainRouteSetupHandler: { [weak self] route, legIndex, completion in
self?.navigator.setPrimaryRouteForRoute(route, legIndex: legIndex) { [weak self] result in
if result.isValue() {
let routeInfo = result.value as! RouteInfo
Expand All @@ -55,7 +55,7 @@ class Navigator {
completion(.failure(NavigatorError.failedToUpdateRoutes(reason: "Unexpected internal response")))
}
}
}, setAlternativeRoutesHandler: { [weak self] routes, completion in
}, alternativeRoutesSetupHandler: { [weak self] routes, completion in
self?.navigator.setAlternativeRoutesForRoutes(routes) { [weak self] result in
if result.isValue() {
let alternativeRoutes = result.value as? [RouteAlternative] ?? []
Expand Down Expand Up @@ -242,7 +242,7 @@ class Navigator {
}

func setAlternativeRoutes(_ routes: [RouteInterface], completion: @escaping (Result<[RouteAlternative], Error>) -> Void) {
routeCoordinator.setAlternativeRoutes(routes, completion)
routeCoordinator.alternativeRoutesSetupHandler(routes, completion)
}

func unsetRoutes(uuid: UUID, completion: @escaping (Result<RouteInfo, Error>) -> Void) {
Expand Down
10 changes: 5 additions & 5 deletions Sources/MapboxCoreNavigation/RouteController.swift
Expand Up @@ -234,7 +234,7 @@ open class RouteController: NSObject {
whether the change was successful.
*/
private func updateNavigator(with indexedRouteResponse: IndexedRouteResponse,
starting legIndex: Int,
fromLegIndex legIndex: Int,
completion: ((Result<RouteInfo, Error>) -> Void)?) {
guard case .route(let routeOptions) = indexedRouteResponse.routeResponse.options else {
completion?(.failure(RouteControllerError.internalError))
Expand Down Expand Up @@ -372,11 +372,11 @@ open class RouteController: NSObject {
}

@objc func fallbackToOffline(_ notification: Notification) {
updateNavigator(with: indexedRouteResponse, starting: self.routeProgress.legIndex, completion: nil)
updateNavigator(with: indexedRouteResponse, fromLegIndex: self.routeProgress.legIndex, completion: nil)
}

@objc func restoreToOnline(_ notification: Notification) {
updateNavigator(with: indexedRouteResponse, starting: self.routeProgress.legIndex, completion: nil)
updateNavigator(with: indexedRouteResponse, fromLegIndex: self.routeProgress.legIndex, completion: nil)
}

func isValidNavigationStatus(_ status: NavigationStatus) -> Bool {
Expand Down Expand Up @@ -567,7 +567,7 @@ open class RouteController: NSObject {
BillingHandler.shared.beginBillingSession(for: .activeGuidance, uuid: sessionUUID)

subscribeNotifications()
updateNavigator(with: self.indexedRouteResponse, starting: 0) { [weak self] _ in
updateNavigator(with: self.indexedRouteResponse, fromLegIndex: 0) { [weak self] _ in
self?.isInitialized = true
}
Self.instanceLock.lock()
Expand Down Expand Up @@ -735,7 +735,7 @@ extension RouteController: Router {
let routeOptions = routeOptions ?? routeProgress.routeOptions
let routeProgress = RouteProgress(route: route, options: routeOptions)
updateNavigator(with: indexedRouteResponse,
starting: routeProgress.legIndex) { [weak self] result in
fromLegIndex: routeProgress.legIndex) { [weak self] result in
guard let self = self else { return }
switch result {
case .success:
Expand Down
26 changes: 13 additions & 13 deletions Sources/MapboxCoreNavigation/RoutesCoordinator.swift
Expand Up @@ -11,26 +11,26 @@ final class RoutesCoordinator {
case activeNavigation(UUID)
}

typealias SetMainRouteHandler = (RouteInterface?, _ legIndex: UInt32, _ completion: @escaping (Result<RouteInfo, Error>) -> Void) -> Void
typealias SetAlternativeRoutesHandler = ([RouteInterface], _ completion: @escaping (Result<[RouteAlternative], Error>) -> Void) -> Void
typealias MainRouteSetupHandler = (RouteInterface?, _ legIndex: UInt32, _ completion: @escaping (Result<RouteInfo, Error>) -> Void) -> Void
typealias AlternativeRoutesSetupHandler = ([RouteInterface], _ completion: @escaping (Result<[RouteAlternative], Error>) -> Void) -> Void

private struct ActiveNavigationSession {
let uuid: UUID
}

private let setMainRoute: SetMainRouteHandler
let setAlternativeRoutes: SetAlternativeRoutesHandler
private let mainRouteSetupHandler: MainRouteSetupHandler
let alternativeRoutesSetupHandler: AlternativeRoutesSetupHandler
/// The lock that protects mutable state in `RoutesCoordinator`.
private let lock: NSLock
private var state: State

/// Create a new coordinator that will coordinate "setMainRoute" requests.
/// - Parameter setAlternativeRoutesHandler: The handler that passes `RouteInterface` object to underlying Navigator as main route.
/// - Parameter setAlternativeRoutesHandler: The handler that passes `RouteInterface` array to underlying Navigator as alternative routes.
init(setMainRouteHandler: @escaping SetMainRouteHandler,
setAlternativeRoutesHandler: @escaping SetAlternativeRoutesHandler) {
self.setMainRoute = setMainRouteHandler
self.setAlternativeRoutes = setAlternativeRoutesHandler
/// Create a new coordinator that will coordinate requests to set main and alternative routes.
/// - Parameter mainRouteSetupHandler: The handler that passes `RouteInterface` object to underlying Navigator as main route.
/// - Parameter alternativeRoutesSetupHandler: The handler that passes `RouteInterface` array to underlying Navigator as alternative routes.
init(mainRouteSetupHandler: @escaping MainRouteSetupHandler,
alternativeRoutesSetupHandler: @escaping AlternativeRoutesSetupHandler) {
self.mainRouteSetupHandler = mainRouteSetupHandler
self.alternativeRoutesSetupHandler = alternativeRoutesSetupHandler
lock = .init()
state = .passiveNavigation
}
Expand All @@ -51,7 +51,7 @@ final class RoutesCoordinator {
state = .activeNavigation(uuid)
lock.unlock()

setMainRoute(route, legIndex, completion)
mainRouteSetupHandler(route, legIndex, completion)
}

/// - Parameters:
Expand All @@ -66,7 +66,7 @@ final class RoutesCoordinator {
state = .passiveNavigation
lock.unlock()
// TODO: Is it safe to set the leg index to 0 when unsetting a route?
setMainRoute(nil, 0, completion)
mainRouteSetupHandler(nil, 0, completion)
}
}

Expand Down
6 changes: 3 additions & 3 deletions Tests/MapboxCoreNavigationTests/RoutesCoordinatorTests.swift
Expand Up @@ -77,16 +77,16 @@ private extension RoutesCoordinatorTests {
var expectedRouteIndex = UInt32.max
var expectedResult: Result<RouteInfo, RoutesCoordinatorError>!

let handler: RoutesCoordinator.SetMainRouteHandler = { routes, routeIndex, completion in
let handler: RoutesCoordinator.MainRouteSetupHandler = { routes, routeIndex, completion in
XCTAssertEqual(routes?.getRouteId(), expectedRoutes?.getRouteId())
XCTAssertEqual(routeIndex, expectedRouteIndex)
completion(expectedResult.mapError { $0 as Error })
}

let coordinator = RoutesCoordinator(setMainRouteHandler: { route, routeIndex, completion in
let coordinator = RoutesCoordinator(mainRouteSetupHandler: { route, routeIndex, completion in
handler(route, routeIndex, completion)
},
setAlternativeRoutesHandler: { routes, completion in
alternativeRoutesSetupHandler: { routes, completion in
XCTAssertTrue(routes.isEmpty)
})

Expand Down

0 comments on commit c5467e3

Please sign in to comment.