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

Clean up RouteController user info keys #2290

Merged
merged 4 commits into from
Dec 27, 2019
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
6 changes: 3 additions & 3 deletions Example/CustomViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ class CustomViewController: UIViewController, MGLMapViewDelegate {
// do not update if we are previewing instruction steps
guard previewInstructionsView == nil else { return }

let routeProgress = notification.userInfo![RouteControllerNotificationUserInfoKey.routeProgressKey] as! RouteProgress
let location = notification.userInfo![RouteControllerNotificationUserInfoKey.locationKey] as! CLLocation
let routeProgress = notification.userInfo![RouteController.NotificationUserInfoKey.routeProgressKey] as! RouteProgress
let location = notification.userInfo![RouteController.NotificationUserInfoKey.locationKey] as! CLLocation

// Add maneuver arrow
if routeProgress.currentLegProgress.followOnStep != nil {
Expand All @@ -107,7 +107,7 @@ class CustomViewController: UIViewController, MGLMapViewDelegate {
}

@objc func updateInstructionsBanner(notification: NSNotification) {
guard let routeProgress = notification.userInfo?[RouteControllerNotificationUserInfoKey.routeProgressKey] as? RouteProgress else { return }
guard let routeProgress = notification.userInfo?[RouteController.NotificationUserInfoKey.routeProgressKey] as? RouteProgress else { return }
instructionsBannerView.update(for: routeProgress.currentLegProgress.currentStepProgress.currentVisualInstruction)
}

Expand Down
111 changes: 56 additions & 55 deletions MapboxCoreNavigation/CoreConstants.swift
Original file line number Diff line number Diff line change
Expand Up @@ -119,100 +119,101 @@ public extension Notification.Name {
/**
Posted when `RouteController` receives a user location update representing movement along the expected route.

The user info dictionary contains the keys `MBRouteControllerRouteProgressKey` and `MBRouteControllerLocationKey`.
The user info dictionary contains the keys `RouteController.NotificationUserInfoKey.routeProgressKey`, `RouteController.NotificationUserInfoKey.locationKey`, and `RouteController.NotificationUserInfoKey.rawLocationKey`.
*/
static let routeControllerProgressDidChange: Notification.Name = .init(rawValue: "RouteControllerProgressDidChange")

/**
Posted after the user diverges from the expected route, just before `RouteController` attempts to calculate a new route.

The user info dictionary contains the key `MBRouteControllerLocationKey`.
The user info dictionary contains the key `RouteController.NotificationUserInfoKey.locationKey`.
*/
static let routeControllerWillReroute: Notification.Name = .init(rawValue: "RouteControllerWillReroute")

/**
Posted when `RouteController` obtains a new route in response to the user diverging from a previous route.

The user info dictionary contains the keys `MBRouteControllerLocationKey` and `MBRouteControllerIsProactiveKey`.
The user info dictionary contains the keys `RouteController.NotificationUserInfoKey.locationKey` and `RouteController.NotificationUserInfoKey.isProactiveKey`.
*/
static let routeControllerDidReroute: Notification.Name = .init(rawValue: "RouteControllerDidReroute")

/**
Posted when `RouteController` fails to reroute the user after the user diverges from the expected route.

The user info dictionary contains the key `MBRouteControllerRoutingErrorKey`.
The user info dictionary contains the key `RouteController.NotificationUserInfoKey.routingErrorKey`.
*/
static let routeControllerDidFailToReroute: Notification.Name = .init(rawValue: "RouteControllerDidFailToReroute")

/**
Posted when `RouteController` detects that the user has passed an ideal point for saying an instruction aloud.

The user info dictionary contains the key `MBRouteControllerRouteProgressKey`.
The user info dictionary contains the keys `RouteController.NotificationUserInfoKey.routeProgressKey` and `RouteController.NotificationUserInfoKey.spokenInstructionKey`.
*/
static let routeControllerDidPassSpokenInstructionPoint: Notification.Name = .init(rawValue: "RouteControllerDidPassSpokenInstructionPoint")

/**
Posted when `RouteController` detects that the user has passed an ideal point for display an instruction visually.

The user info dictionary contains the key `MBRouteControllerRouteProgressKey`.
The user info dictionary contains the keys `RouteController.NotificationUserInfoKey.routeProgressKey` and `RouteController.NotificationUserInfoKey.visualInstructionKey`.
*/
static let routeControllerDidPassVisualInstructionPoint: Notification.Name = .init(rawValue: "MBRouteControllerDidPassVisualInstructionPoint")
static let routeControllerDidPassVisualInstructionPoint: Notification.Name = .init(rawValue: "RouteControllerDidPassVisualInstructionPoint")

/**
Posted when something changes in the shared `MBNavigationSettings` object.
Posted when something changes in the shared `NavigationSettings` object.

The user info dictionary indicates which keys and values changed.

*/
static let navigationSettingsDidChange: Notification.Name = .init(rawValue: "MBNavigationSettingsDidChange")
static let navigationSettingsDidChange: Notification.Name = .init(rawValue: "NavigationSettingsDidChange")
}

/**
Keys in the user info dictionaries of various notifications posted by instances
of `RouteController`.
*/
public struct RouteControllerNotificationUserInfoKey: Hashable, Equatable, RawRepresentable {
public typealias RawValue = String

public var rawValue: String

public init(rawValue: String) {
self.rawValue = rawValue
}

/**
A key in the user info dictionary of a `routeControllerProgressDidChange`, `routeControllerDidPassVisualInstructionPoint`, or `routeControllerDidPassSpokenInstructionPoint` notification. The corresponding value is a `RouteProgress` object representing the current route progress.
*/
public static let routeProgressKey: RouteControllerNotificationUserInfoKey = .init(rawValue: "progress")

/**
A key in the user info dictionary of a `routeControllerProgressDidChange` or `routeControllerWillReroute` notification. The corresponding value is a `CLLocation` object representing the current idealized user location.
*/
public static let locationKey: RouteControllerNotificationUserInfoKey = .init(rawValue: "location")

extension RouteController {
/**
A key in the user info dictionary of a `routeControllerProgressDidChange` or `routeControllerWillReroute` notification. The corresponding value is a `CLLocation` object representing the current raw user location.
Keys in the user info dictionaries of various notifications posted by instances
of `RouteController`.
*/
public static let rawLocationKey: RouteControllerNotificationUserInfoKey = .init(rawValue: "rawLocation")

/**
A key in the user info dictionary of a `routeControllerDidFailToReroute` notification. The corresponding value is an `NSError` object indicating why `RouteController` was unable to calculate a new route.
*/
public static let routingErrorKey: RouteControllerNotificationUserInfoKey = .init(rawValue: "error")

/**
A key in the user info dictionary of an `routeControllerDidPassVisualInstructionPoint`. The corresponding value is an `MBVisualInstruction` object representing the current visual instruction.
*/
public static let visualInstructionKey: RouteControllerNotificationUserInfoKey = .init(rawValue: "visualInstruction")

/**
A key in the user info dictionary of a `routeControllerDidPassSpokenInstructionPoint` notification. The corresponding value is an `MBSpokenInstruction` object representing the current visual instruction.
*/
public static let spokenInstructionKey: RouteControllerNotificationUserInfoKey = .init(rawValue: "spokenInstruction")

/**
A key in the user info dictionary of a `routeControllerDidReroute` notification. The corresponding value is an `NSNumber` instance containing a Boolean value indicating whether `RouteController` proactively rerouted the user onto a faster route.
*/
public static let isProactiveKey: RouteControllerNotificationUserInfoKey = .init(rawValue: "RouteControllerDidFindFasterRoute")
public struct NotificationUserInfoKey: Hashable, Equatable, RawRepresentable {
public typealias RawValue = String

public var rawValue: String

public init(rawValue: String) {
self.rawValue = rawValue
}

/**
A key in the user info dictionary of a `Notification.Name.routeControllerProgressDidChange`, `Notification.Name.routeControllerDidPassVisualInstructionPoint`, or `Notification.Name.routeControllerDidPassSpokenInstructionPoint` notification. The corresponding value is a `RouteProgress` object representing the current route progress.
*/
public static let routeProgressKey: NotificationUserInfoKey = .init(rawValue: "progress")

/**
A key in the user info dictionary of a `Notification.Name.routeControllerProgressDidChange`, `Notification.Name.routeControllerWillReroute`, or `Notification.Name.routeControllerDidReroute` notification. The corresponding value is a `CLLocation` object representing the current idealized user location.
*/
public static let locationKey: NotificationUserInfoKey = .init(rawValue: "location")

/**
A key in the user info dictionary of a `Notification.Name.routeControllerProgressDidChange` notification. The corresponding value is a `CLLocation` object representing the current raw user location.
*/
public static let rawLocationKey: NotificationUserInfoKey = .init(rawValue: "rawLocation")

/**
A key in the user info dictionary of a `Notification.Name.routeControllerDidFailToReroute` notification. The corresponding value is an `NSError` object indicating why `RouteController` was unable to calculate a new route.
*/
public static let routingErrorKey: NotificationUserInfoKey = .init(rawValue: "error")

/**
A key in the user info dictionary of an `Notification.Name.routeControllerDidPassVisualInstructionPoint`. The corresponding value is an `VisualInstruction` object representing the current visual instruction.
*/
public static let visualInstructionKey: NotificationUserInfoKey = .init(rawValue: "visualInstruction")

/**
A key in the user info dictionary of a `Notification.Name.routeControllerDidPassSpokenInstructionPoint` notification. The corresponding value is an `SpokenInstruction` object representing the current visual instruction.
*/
public static let spokenInstructionKey: NotificationUserInfoKey = .init(rawValue: "spokenInstruction")

/**
A key in the user info dictionary of a `Notification.Name.routeControllerDidReroute` notification. The corresponding value is an `NSNumber` instance containing a Boolean value indicating whether `RouteController` proactively rerouted the user onto a faster route.
*/
public static let isProactiveKey: NotificationUserInfoKey = .init(rawValue: "RouteControllerDidFindFasterRoute")
}
}

20 changes: 10 additions & 10 deletions MapboxCoreNavigation/LegacyRouteController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -291,10 +291,10 @@ open class LegacyRouteController: NSObject, Router, InternalRouter, CLLocationMa

//Fire the notification (for now)
NotificationCenter.default.post(name: .routeControllerProgressDidChange, object: self, userInfo: [
RouteControllerNotificationUserInfoKey.routeProgressKey: progress,
RouteControllerNotificationUserInfoKey.locationKey: location, //guaranteed value
RouteControllerNotificationUserInfoKey.rawLocationKey: rawLocation //raw
])
RouteController.NotificationUserInfoKey.routeProgressKey: progress,
RouteController.NotificationUserInfoKey.locationKey: location, //guaranteed value
RouteController.NotificationUserInfoKey.rawLocationKey: rawLocation, //raw
])
}
}

Expand Down Expand Up @@ -342,7 +342,7 @@ open class LegacyRouteController: NSObject, Router, InternalRouter, CLLocationMa

delegate?.router(self, willRerouteFrom: location)
NotificationCenter.default.post(name: .routeControllerWillReroute, object: self, userInfo: [
RouteControllerNotificationUserInfoKey.locationKey: location
RouteController.NotificationUserInfoKey.locationKey: location,
])

self.lastRerouteLocation = location
Expand All @@ -356,7 +356,7 @@ open class LegacyRouteController: NSObject, Router, InternalRouter, CLLocationMa
if let error = error {
strongSelf.delegate?.router(strongSelf, didFailToRerouteWith: error)
NotificationCenter.default.post(name: .routeControllerDidFailToReroute, object: self, userInfo: [
RouteControllerNotificationUserInfoKey.routingErrorKey: error
RouteController.NotificationUserInfoKey.routingErrorKey: error,
])
return
}
Expand Down Expand Up @@ -467,8 +467,8 @@ open class LegacyRouteController: NSObject, Router, InternalRouter, CLLocationMa
if userSnapToStepDistanceFromManeuver <= spokenInstruction.distanceAlongStep || firstInstructionOnFirstStep {
delegate?.router(self, didPassSpokenInstructionPoint: spokenInstruction, routeProgress: routeProgress)
NotificationCenter.default.post(name: .routeControllerDidPassSpokenInstructionPoint, object: self, userInfo: [
RouteControllerNotificationUserInfoKey.routeProgressKey: routeProgress,
RouteControllerNotificationUserInfoKey.spokenInstructionKey: spokenInstruction
RouteController.NotificationUserInfoKey.routeProgressKey: routeProgress,
RouteController.NotificationUserInfoKey.spokenInstructionKey: spokenInstruction,
])

routeProgress.currentLegProgress.currentStepProgress.spokenInstructionIndex += 1
Expand All @@ -486,8 +486,8 @@ open class LegacyRouteController: NSObject, Router, InternalRouter, CLLocationMa
if userSnapToStepDistanceFromManeuver <= visualInstruction.distanceAlongStep || isFirstLocation {
delegate?.router(self, didPassVisualInstructionPoint: visualInstruction, routeProgress: routeProgress)
NotificationCenter.default.post(name: .routeControllerDidPassVisualInstructionPoint, object: self, userInfo: [
RouteControllerNotificationUserInfoKey.routeProgressKey: routeProgress,
RouteControllerNotificationUserInfoKey.visualInstructionKey: visualInstruction,
RouteController.NotificationUserInfoKey.routeProgressKey: routeProgress,
RouteController.NotificationUserInfoKey.visualInstructionKey: visualInstruction,
])
currentStepProgress.visualInstructionIndex += 1
return
Expand Down
22 changes: 11 additions & 11 deletions MapboxCoreNavigation/RouteController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -298,19 +298,19 @@ open class RouteController: NSObject {

//Fire the notification (for now)
NotificationCenter.default.post(name: .routeControllerProgressDidChange, object: self, userInfo: [
RouteControllerNotificationUserInfoKey.routeProgressKey: progress,
RouteControllerNotificationUserInfoKey.locationKey: location, //guaranteed value
RouteControllerNotificationUserInfoKey.rawLocationKey: rawLocation //raw
NotificationUserInfoKey.routeProgressKey: progress,
NotificationUserInfoKey.locationKey: location, //guaranteed value
NotificationUserInfoKey.rawLocationKey: rawLocation, //raw
])
}
}

private func announcePassage(of spokenInstructionPoint: SpokenInstruction, routeProgress: RouteProgress) {
delegate?.router(self, didPassSpokenInstructionPoint: spokenInstructionPoint, routeProgress: routeProgress)

let info: [RouteControllerNotificationUserInfoKey: Any] = [
let info: [NotificationUserInfoKey: Any] = [
.routeProgressKey: routeProgress,
.spokenInstructionKey: spokenInstructionPoint
.spokenInstructionKey: spokenInstructionPoint,
]

NotificationCenter.default.post(name: .routeControllerDidPassSpokenInstructionPoint, object: self, userInfo: info)
Expand All @@ -319,9 +319,9 @@ open class RouteController: NSObject {
private func announcePassage(of visualInstructionPoint: VisualInstructionBanner, routeProgress: RouteProgress) {
delegate?.router(self, didPassVisualInstructionPoint: visualInstructionPoint, routeProgress: routeProgress)

let info: [RouteControllerNotificationUserInfoKey: Any] = [
let info: [NotificationUserInfoKey: Any] = [
.routeProgressKey: routeProgress,
.visualInstructionKey: visualInstructionPoint
.visualInstructionKey: visualInstructionPoint,
]

NotificationCenter.default.post(name: .routeControllerDidPassVisualInstructionPoint, object: self, userInfo: info)
Expand Down Expand Up @@ -376,8 +376,8 @@ extension RouteController: Router {

delegate?.router(self, willRerouteFrom: location)
NotificationCenter.default.post(name: .routeControllerWillReroute, object: self, userInfo: [
RouteControllerNotificationUserInfoKey.locationKey: location
])
NotificationUserInfoKey.locationKey: location,
])

self.lastRerouteLocation = location

Expand All @@ -395,8 +395,8 @@ extension RouteController: Router {
if let error = error {
strongSelf.delegate?.router(strongSelf, didFailToRerouteWith: error)
NotificationCenter.default.post(name: .routeControllerDidFailToReroute, object: self, userInfo: [
RouteControllerNotificationUserInfoKey.routingErrorKey: error
])
NotificationUserInfoKey.routingErrorKey: error,
])
return
}

Expand Down
2 changes: 1 addition & 1 deletion MapboxCoreNavigation/Router.swift
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ extension InternalRouter where Self: Router {
}

func announce(reroute newRoute: Route, at location: CLLocation?, proactive: Bool) {
var userInfo = [RouteControllerNotificationUserInfoKey: Any]()
var userInfo = [RouteController.NotificationUserInfoKey: Any]()
if let location = location {
userInfo[.locationKey] = location
}
Expand Down
2 changes: 1 addition & 1 deletion MapboxCoreNavigation/SimulatedLocationManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ open class SimulatedLocationManager: NavigationLocationManager {
}

@objc private func progressDidChange(_ notification: Notification) {
routeProgress = notification.userInfo![RouteControllerNotificationUserInfoKey.routeProgressKey] as? RouteProgress
routeProgress = notification.userInfo![RouteController.NotificationUserInfoKey.routeProgressKey] as? RouteProgress
}

@objc private func didReroute(_ notification: Notification) {
Expand Down
Loading