Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rerouting customiztion rollback #3668

Merged
merged 1 commit into from
Jan 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@

### Location tracking

* Added the `RouterDelegate.router(_:initialManeuverBufferWhenReroutingFrom:)` method and related `NavigationServiceDelegate` and `NavigationViewControllerDelegate` methods to configure rerouting buffering. ([#3454](https://github.com/mapbox/mapbox-navigation-ios/pull/3454))
* Added `RouterDelegate.router(_:requestBehaviorForReroutingWith:)` and related `NavigationServiceDelegate` and `NavigationViewControllerDelegate` methods to provide customization for rerouting mechanism by providing a user-defined route as a reroute. ([#3472](https://github.com/mapbox/mapbox-navigation-ios/pull/3472))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😞 Looking forward to a revival of this useful feature!

* Fixed an issue where customized `.puck2D` and `.puck3D` of `NavigationMapView.userLocationStyle` is not shown during simulated active navigation. ([#3674](https://github.com/mapbox/mapbox-navigation-ios/pull/3674))
* Added the `NavigationLocationProvider.didUpdateLocations(locations:)` to send locations update to `MapView` and notify its `LocationConsumer`. ([#3674](https://github.com/mapbox/mapbox-navigation-ios/pull/3674))

Expand Down
10 changes: 0 additions & 10 deletions Example/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -662,14 +662,4 @@ extension ViewController: NavigationViewControllerDelegate {
dismissActiveNavigationViewController()
clearNavigationMapView()
}

func navigationViewController(_ navigationViewController: NavigationViewController, initialManeuverBufferWhenReroutingFrom location: CLLocation) -> LocationDistance? {

guard let currentSpeed = navigationViewController.navigationService.router.location?.speed else {
return nil
}

// allowing at least 8 seconds for user to react to new route
return currentSpeed * 8
}
}
8 changes: 0 additions & 8 deletions Sources/MapboxCoreNavigation/NavigationService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -551,14 +551,6 @@ extension MapboxNavigationService: RouterDelegate {
delegate?.navigationService(self, willRerouteFrom: location)
}

public func router(_ router: Router, initialManeuverBufferWhenReroutingFrom location: CLLocation) -> LocationDistance? {
return delegate?.navigationService(self, initialManeuverBufferWhenReroutingFrom: location) ?? Default.reroutingManeuverRadius
}

public func router(_ router: Router, requestBehaviorForReroutingWith options: RouteOptions) -> ReroutingRequestBehavior {
return delegate?.navigationService(self, requestBehaviorForReroutingWith: options) ?? .default
}

public func router(_ router: Router, didRerouteAlong route: Route, at location: CLLocation?, proactive: Bool) {
//notify the events manager that the route has changed
eventsManager.reportReroute(progress: router.routeProgress, proactive: proactive)
Expand Down
51 changes: 3 additions & 48 deletions Sources/MapboxCoreNavigation/NavigationServiceDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import Foundation
import CoreLocation
import MapboxDirections
import os.log
import Turf

/**
A navigation service delegate interacts with one or more `NavigationService` instances (such as `MapboxNavigationService` objects) during turn-by-turn navigation. This protocol is the main way that your application can synchronize its state with the SDK’s location-related functionality. Each of the protocol’s methods is optional.
Expand Down Expand Up @@ -34,41 +33,13 @@ public protocol NavigationServiceDelegate: AnyObject, UnimplementedLogging {
/**
Called immediately before the navigation service calculates a new route.

This method is called after `navigationService(_:shouldRerouteFrom:)` is called, simultaneously with the `Notification.Name.routeControllerWillReroute` notification being posted, and before `navigationService(_:initialManeuverBufferWhenReroutingFrom:)` is called.
This method is called after `navigationService(_:shouldRerouteFrom:)` is called, simultaneously with the `Notification.Name.routeControllerWillReroute` notification being posted, and before `navigationService(_:didRerouteAlong:)` is called.

- parameter service: The navigation service that will calculate a new route.
- parameter location: The user’s current location.
*/
func navigationService(_ service: NavigationService, willRerouteFrom location: CLLocation)

/**
Configures distance (in meters) before the first maneuver in requested reroute.

If implemented, this method allows to override set `RouteOptions.initialManeuverAvoidanceRadius` value which is useful when adjusting reroute according to current user velocity in order to avoid dangerous maneuvers in the beginning of the route.

This method is called after `navigationService(_:willRerouteFrom:)` is called, and before `navigationService(_:requestBehaviorForReroutingWith:)` is called.

- parameter service: The navigation service that will calculate a new route.
- parameter location: The user’s current location.
- returns: `LocationDistance` value which overrides (by passing a non-nil value) or leaves maneuvers offset as it was originally set (by passing `nil`).
*/
func navigationService(_ service: NavigationService, initialManeuverBufferWhenReroutingFrom location: CLLocation) -> LocationDistance?

/**
Called when the navigation service calculates a new route.

This method allows customizing the rerouting by providing custom `RouteResponse`. SDK will then treat it as if it was fetched as usual and apply as a reroute.

- note: Multiple method calls will not interrupt the first ongoing request.

This method is called after `navigationService(_:initialManeuverBufferWhenReroutingFrom:)` is called, and before `navigationService(_:didRerouteAlong:)` is called.

- parameter service: The navigation service that will calculate a new route.
- parameter options: `RouteOptions` that are supposed to be used for route calculation.
- returns: `.default` to let the SDK handle retrieving new `Route`, or `.custom` to provide your own reroute.
*/
func navigationService(_ service: NavigationService, requestBehaviorForReroutingWith options: RouteOptions) -> ReroutingRequestBehavior

/**
Called when a location has been identified as unqualified to navigate on.

Expand All @@ -83,7 +54,7 @@ public protocol NavigationServiceDelegate: AnyObject, UnimplementedLogging {
/**
Called immediately after the navigation service receives a new route.

This method is called after `navigationService(_:requestBehaviorForReroutingWith:)` and simultaneously with the `Notification.Name.routeControllerDidReroute` notification being posted.
This method is called after `navigationService(_:willRerouteFrom:)` and simultaneously with the `Notification.Name.routeControllerDidReroute` notification being posted.

- parameter service: The navigation service that has calculated a new route.
- parameter route: The new route.
Expand All @@ -93,7 +64,7 @@ public protocol NavigationServiceDelegate: AnyObject, UnimplementedLogging {
/**
Called when the navigation service fails to receive a new route.

This method is called after `navigationService(_:requestBehaviorForReroutingWith:)` and simultaneously with the `Notification.Name.routeControllerDidFailToReroute` notification being posted.
This method is called after `navigationService(_:willRerouteFrom:)` and simultaneously with the `Notification.Name.routeControllerDidFailToReroute` notification being posted.

- parameter service: The navigation service that has calculated a new route.
- parameter error: An error raised during the process of obtaining a new route.
Expand Down Expand Up @@ -258,22 +229,6 @@ public extension NavigationServiceDelegate {
logUnimplemented(protocolType: NavigationServiceDelegate.self, level: .debug)
}

/**
`UnimplementedLogging` prints a warning to standard output the first time this method is called.
*/
func navigationService(_ service: NavigationService, initialManeuverBufferWhenReroutingFrom location: CLLocation) -> LocationDistance? {
logUnimplemented(protocolType: NavigationServiceDelegate.self, level: .debug)
return MapboxNavigationService.Default.reroutingManeuverRadius
}

/**
`UnimplementedLogging` prints a warning to standard output the first time this method is called.
*/
func navigationService(_ service: NavigationService, requestBehaviorForReroutingWith options: RouteOptions) -> ReroutingRequestBehavior {
logUnimplemented(protocolType: NavigationServiceDelegate.self, level: .debug)
return .default
}

/**
`UnimplementedLogging` prints a warning to standard output the first time this method is called.
*/
Expand Down
1 change: 0 additions & 1 deletion Sources/MapboxCoreNavigation/RouteController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import os.log
open class RouteController: NSObject {
public enum DefaultBehavior {
public static let shouldRerouteFromLocation: Bool = true
public static let reroutingManeuverRadius: LocationDistance? = nil
public static let shouldDiscardLocation: Bool = false
public static let didArriveAtWaypoint: Bool = true
public static let shouldPreventReroutesWhenArrivingAtWaypoint: Bool = true
Expand Down
31 changes: 2 additions & 29 deletions Sources/MapboxCoreNavigation/Router.swift
Original file line number Diff line number Diff line change
Expand Up @@ -336,23 +336,10 @@ extension InternalRouter where Self: Router {
routeTask?.cancel()
let options = progress.reroutingOptions(from: origin)

if isRerouting {
switch (delegate?.router(self, initialManeuverBufferWhenReroutingFrom: origin) ?? RouteController.DefaultBehavior.reroutingManeuverRadius) {
case .some(let distance):
if distance == 0 {
options.initialManeuverAvoidanceRadius = nil
} else {
options.initialManeuverAvoidanceRadius = distance
}
case .none:
// do nothing
break
}
}

lastRerouteLocation = origin

let parseResult: Directions.RouteCompletionHandler = {(session, result) in
routeTask = routingProvider.calculateRoutes(options: options) {(session, result) in
defer { self.routeTask = nil }
switch result {
case .failure(let error):
return completion(session, .failure(error))
Expand All @@ -364,20 +351,6 @@ extension InternalRouter where Self: Router {
return completion(session, .success(.init(routeResponse: response, routeIndex: mostSimilarIndex)))
}
}

switch delegate?.router(self, requestBehaviorForReroutingWith: options) {
case .default, .none:
routeTask = routingProvider.calculateRoutes(options: options) { [weak self] (session, result) in
parseResult(session, result)
self?.routeTask = nil
}
case .custom(let reroutingResult):
let (options, result) = reroutingResult()
let session = Directions.Session(options: options,
credentials: Credentials())

parseResult(session, result)
}
}

func announceImpendingReroute(at location: CLLocation) {
Expand Down
64 changes: 3 additions & 61 deletions Sources/MapboxCoreNavigation/RouterDelegate.swift
Original file line number Diff line number Diff line change
@@ -1,26 +1,6 @@
import Foundation
import CoreLocation
import MapboxDirections
import Turf

/**
Configuration for a source to get new `Route` when rerouting occurs.
*/
public enum ReroutingRequestBehavior {
public typealias ReroutingRequestData = (options: RouteOptions, result: Result<RouteResponse, DirectionsError>)

/**
Navigation SDK is responsible for rerouting request
*/
case `default`

/**
Navigation SDK will use provided `ReroutingRequestData` for rerouting.

- note: Associated callback will be executed on a background thread.
*/
case custom(() -> ReroutingRequestData)
}

/**
A router delegate interacts with one or more `Router` instances, such as `RouteController` objects, during turn-by-turn navigation. This protocol is similar to `NavigationServiceDelegate`, which is the main way that your application can synchronize its state with the SDK’s location-related functionality. Normally, you should not need to make a class conform to the `RouterDelegate` protocol or call any of its methods directly, but you would need to call this protocol’s methods if you implement a custom `Router` class.
Expand Down Expand Up @@ -48,41 +28,13 @@ public protocol RouterDelegate: AnyObject, UnimplementedLogging {
/**
Called immediately before the router calculates a new route.

This method is called after `router(_:shouldRerouteFrom:)` is called, and before `router(_:initialManeuverBufferWhenReroutingFrom:)` is called.
This method is called after `router(_:shouldRerouteFrom:)` is called, and before `router(_:didRerouteAlong:)` is called.

- parameter router: The router that will calculate a new route.
- parameter location: The user’s current location.
*/
func router(_ router: Router, willRerouteFrom location: CLLocation)

/**
Configures distance (in meters) before the first maneuver in requested reroute.

If implemented, this method allows to override set `RouteOptions.initialManeuverAvoidanceRadius` value which is useful when adjusting reroute according to current user velocity in order to avoid dangerous maneuvers in the beginning of the route.

This method is called after `router(_:willRerouteFrom:)` is called, and before `router(_:requestBehaviorForReroutingWith:)` is called.

- parameter router: The router that has detected the need to calculate a new route.
- parameter location: The user’s current location.
- returns: `LocationDistance` value which overrides (by passing a non-nil value) or leaves maneuvers offset as it was originally set (by passing `nil`).
*/
func router(_ router: Router, initialManeuverBufferWhenReroutingFrom location: CLLocation) -> LocationDistance?

/**
Called when the router calculates a new route.

This method allows customizing the rerouting by providing custom `RouteResponse`. SDK will then treat it as if it was fetched as usual and apply as a reroute.

- note: Multiple method calls will not interrupt the first ongoing request.

This method is called after `router(_:initialManeuverBufferWhenReroutingFrom:)` is called, and before `router(_:didRerouteAlong:)` is called.

- parameter router: The router that will calculate a new route.
- parameter options: `RouteOptions` that are supposed to be used for route calculation.
- returns: `.default` to let the SDK handle retrieving new `Route`, or `.custom` to provide your own reroute.
*/
func router(_ router: Router, requestBehaviorForReroutingWith options: RouteOptions) -> ReroutingRequestBehavior

/**
Called when a location has been identified as unqualified to navigate on.

Expand All @@ -97,7 +49,7 @@ public protocol RouterDelegate: AnyObject, UnimplementedLogging {
/**
Called immediately after the router receives a new route.

This method is called after `router(_:requestBehaviorForReroutingWith:)` method is called.
This method is called after `router(_:willRerouteFrom:)` method is called.

- parameter router: The router that has calculated a new route.
- parameter route: The new route.
Expand All @@ -107,7 +59,7 @@ public protocol RouterDelegate: AnyObject, UnimplementedLogging {
/**
Called when the router fails to receive a new route.

This method is called after `router(_:requestBehaviorForReroutingWith:)`.
This method is called after `router(_:willRerouteFrom:)`.

- parameter router: The router that has calculated a new route.
- parameter error: An error raised during the process of obtaining a new route.
Expand Down Expand Up @@ -208,16 +160,6 @@ public extension RouterDelegate {
logUnimplemented(protocolType: RouterDelegate.self, level: .debug)
}

func router(_ router: Router, initialManeuverBufferWhenReroutingFrom location: CLLocation) -> LocationDistance? {
logUnimplemented(protocolType: RouterDelegate.self, level: .debug)
return RouteController.DefaultBehavior.reroutingManeuverRadius
}

func router(_ router: Router, requestBehaviorForReroutingWith options: RouteOptions) -> ReroutingRequestBehavior {
logUnimplemented(protocolType: RouterDelegate.self, level: .debug)
return .default
}

func router(_ router: Router, shouldDiscard location: CLLocation) -> Bool {
logUnimplemented(protocolType: RouterDelegate.self, level: .debug)
return RouteController.DefaultBehavior.shouldDiscardLocation
Expand Down
10 changes: 1 addition & 9 deletions Sources/MapboxNavigation/NavigationViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -718,20 +718,12 @@ extension NavigationViewController: NavigationServiceDelegate {

public func navigationService(_ service: NavigationService, willRerouteFrom location: CLLocation) {
for component in navigationComponents {
_ = component.navigationService(service, willRerouteFrom: location)
component.navigationService(service, willRerouteFrom: location)
}

delegate?.navigationViewController(self, willRerouteFrom: location)
}

public func navigationService(_ service: NavigationService, initialManeuverBufferWhenReroutingFrom location: CLLocation) -> LocationDistance? {
return delegate?.navigationViewController(self, initialManeuverBufferWhenReroutingFrom: location) ?? RouteController.DefaultBehavior.reroutingManeuverRadius
}

public func navigationService(_ service: NavigationService, requestBehaviorForReroutingWith options: RouteOptions) -> ReroutingRequestBehavior {
return delegate?.navigationViewController(self, requestBehaviorForReroutingWith:options) ?? .default
}

public func navigationService(_ service: NavigationService, didRerouteAlong route: Route, at location: CLLocation?, proactive: Bool) {
for component in navigationComponents {
component.navigationService(service, didRerouteAlong: route, at: location, proactive: proactive)
Expand Down
Loading