From c5467e3b2b86376806b3e55d619cae2845ef24fb Mon Sep 17 00:00:00 2001 From: udumft Date: Wed, 20 Apr 2022 11:30:35 +0300 Subject: [PATCH] vk-3689-nn-route: renamed RouteCoordinator handlers and type alieases; renamed RouteController updateNavigator argument --- .../CoreNavigationNavigator.swift | 6 ++--- .../RouteController.swift | 10 +++---- .../RoutesCoordinator.swift | 26 +++++++++---------- .../RoutesCoordinatorTests.swift | 6 ++--- 4 files changed, 24 insertions(+), 24 deletions(-) diff --git a/Sources/MapboxCoreNavigation/CoreNavigationNavigator.swift b/Sources/MapboxCoreNavigation/CoreNavigationNavigator.swift index 013c1af7921..2024f776ebf 100644 --- a/Sources/MapboxCoreNavigation/CoreNavigationNavigator.swift +++ b/Sources/MapboxCoreNavigation/CoreNavigationNavigator.swift @@ -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 @@ -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] ?? [] @@ -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) -> Void) { diff --git a/Sources/MapboxCoreNavigation/RouteController.swift b/Sources/MapboxCoreNavigation/RouteController.swift index 5e83454b395..82bea4b6878 100644 --- a/Sources/MapboxCoreNavigation/RouteController.swift +++ b/Sources/MapboxCoreNavigation/RouteController.swift @@ -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) -> Void)?) { guard case .route(let routeOptions) = indexedRouteResponse.routeResponse.options else { completion?(.failure(RouteControllerError.internalError)) @@ -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 { @@ -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() @@ -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: diff --git a/Sources/MapboxCoreNavigation/RoutesCoordinator.swift b/Sources/MapboxCoreNavigation/RoutesCoordinator.swift index fc999ea3b9e..bb960014871 100644 --- a/Sources/MapboxCoreNavigation/RoutesCoordinator.swift +++ b/Sources/MapboxCoreNavigation/RoutesCoordinator.swift @@ -11,26 +11,26 @@ final class RoutesCoordinator { case activeNavigation(UUID) } - typealias SetMainRouteHandler = (RouteInterface?, _ legIndex: UInt32, _ completion: @escaping (Result) -> Void) -> Void - typealias SetAlternativeRoutesHandler = ([RouteInterface], _ completion: @escaping (Result<[RouteAlternative], Error>) -> Void) -> Void + typealias MainRouteSetupHandler = (RouteInterface?, _ legIndex: UInt32, _ completion: @escaping (Result) -> 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 } @@ -51,7 +51,7 @@ final class RoutesCoordinator { state = .activeNavigation(uuid) lock.unlock() - setMainRoute(route, legIndex, completion) + mainRouteSetupHandler(route, legIndex, completion) } /// - Parameters: @@ -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) } } diff --git a/Tests/MapboxCoreNavigationTests/RoutesCoordinatorTests.swift b/Tests/MapboxCoreNavigationTests/RoutesCoordinatorTests.swift index 7ac3350c2ea..776b8f3fbf5 100644 --- a/Tests/MapboxCoreNavigationTests/RoutesCoordinatorTests.swift +++ b/Tests/MapboxCoreNavigationTests/RoutesCoordinatorTests.swift @@ -77,16 +77,16 @@ private extension RoutesCoordinatorTests { var expectedRouteIndex = UInt32.max var expectedResult: Result! - 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) })