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

Rewrite the library with full Objective-C support and API documentation #47

Merged
merged 23 commits into from
Jun 2, 2016

Conversation

1ec5
Copy link
Contributor

@1ec5 1ec5 commented May 31, 2016

This PR rewrites the entire library to fully bridge to Objective-C while feeling more natural in Swift, handle errors more robustly, and provide complete API documentation. This PR, which closely parallels mapbox/MapboxGeocoder.swift#41 and mapbox/MapboxStatic.swift#19, is the last major step before consolidating the three libraries into one library that can eventually connect to all of Mapbox’s server APIs.

The goal is no longer to be a (poor) drop-in replacement for MapKit’s MKDirections API but rather to be as idiomatic as possible in Swift and Objective-C simultaneously, with a preference for Swift. Symbol names generally conform to platform and language conventions, but in some cases, symbol names eschew MapKit terminology in favor of Directions API terminology, so that api-documentation makes sense in the context of this library. The MB class prefix has been removed from Swift but is kept for Objective-C.

Directions API v4 support has been reimplemented as a series of subclasses of the options and model objects. The previous approach of inserting switch statements everywhere proved less than ideal. Use RouteOptionsV4 instead of RouteOptions to connect to the v4 API.

Requests

Removed the dependency on RequestKit. Once we consolidate the three Mapbox Swift libraries, it’ll be straightforward for each of the API handlers to share the simple URL request and response code. This library chooses reasonable default networking behavior; if you need anything more sophisticated, you can access the URL request information to use with a custom NSURLSession. Directions no longer needs to be strongly held in order for the request to finish. Instead, the request is made against the shared URL session; to use a custom URL session, make the request yourself using the URL returned by the URLForCalculatingDirections(options:) method. Removed the cancel() method; instead, directly cancel the NSURLSessionDataTask returned by calculateDirections(options:completionHandler:).

Added a shared (singleton) Directions object. Use the shared object if you’ve set your Mapbox access token in the MGLMapboxAccessToken key of your application’s Info.plist file. Otherwise, create a Directions object with the access token explicitly. A single directions object can handle multiple requests concurrently (since it just passes the requests off to the shared URL session).

Eliminated the separate APIs for calculating ETAs. Instead, all the options supported by the Directions API, including flags for including steps and the overview geometry, are now exposed through a new RouteOptions class. Effectively, this change adds support for the geometries, overview, radiuses, steps, and continue_straight query parameters and allows the heading accuracy of any waypoint to be customized. However, note that steps are no longer returned by default, and the overview geometry is simplified by default. If this turns out to be a major inconvenience or surprise, we can add specialized options classes for either use case. The access_token parameter is now the last URL parameter instead of the first. Broke out MBDirectionsRequest.TransportType into three profile identifier constants. You always specify a profile identifier, not a transport type. That makes the desired mode of transportation more open-ended.

Responses

Removed the MBDirectionsResponse class in favor of passing the waypoints and routes from the response directly into the completion handler. Renamed Route.geometry to Route.coordinates. For Objective-C compatibility, there are additional methods that work with C arrays of coordinates. Also, all enums are backed by integer types instead of String, but they’re CustomStringConvertible to allow for the same expressiveness we’re used to in Swift.

Error handling is much more robust now. Various error conditions returned by the API cause the localized failure reason and recovery suggestion to be set in the NSError object that is passed into the completion handler.

Documentation and other changes

Documented every public symbol. Some of the more finicky options get lots more documentation, discussing why you would use the option, when you avoid using the option, etc. There’s little chance this voluminous documentation will head off developer error, but it along with the improved error messages will help developers find their way when things do go wrong.

Updated test fixtures and expectations for the v5 server-side API and the revamped client-side API, and updated examples.

Fixes #41, fixes #43, fixes #44.

/cc @mapbox/directions @tmcw @friedbunny

1ec5 added 9 commits May 29, 2016 10:48
MBPoint is now MBNamedLocation, a subclass of CLLocation. JSON is now JSONDictionary. Also copied over some other basic types from MapboxGeocoder.swift.
Rewrote the library to more closely parallel mapbox/MapboxGeocoder.swift#41 and mapbox/MapboxStatic.swift#19. Removed the class prefix from Swift but kept it for Objective-C. The goal is no longer to be a (poor) drop-in replacement for MapKit’s MKDirections API but rather to be as idiomatic as possible in Swift and Objective-C, with a priority on Swift. Symbols conform to platform naming conventions but in some cases eschew MapKit terminology in favor of Geocoding API terminology, so that api-documentation makes sense in the context of this library.

Removed the dependency on RequestKit. This library chooses reasonable default networking behavior; if you need anything more sophisticated, you can access the URL request information to use with a custom NSURLSession. Directions no longer needs to be strongly held in order for the request to finish. Instead, the request is made against the shared URL session; to use a custom URL session, make the request yourself using the URL returned by the URLForCalculatingDirections(options:) method. Removed the cancel() method; instead, directly cancel the NSURLSessionDataTask returned by calculateDirections(options:completionHandler:).

Added a shared (singleton) Directions object. Use the shared object if you’ve set your Mapbox access token in the MGLMapboxAccessToken key of your application’s Info.plist file. Otherwise, create a Directions object with the access token explicitly. A single directions object can handle multiple requests concurrently (since it just passes the requests off to the shared URL session).

Eliminated the separate APIs for calculating ETAs. Instead, all the options supported by the Directions API, including flags for including steps and the overview geometry, are now exposed through a new RouteOptions class. Effectively, this change adds support for the geometries, overview, radiuses, steps, and continue_straight query parameters and allows the heading accuracy of any waypoint to be customized. However, note that steps are no longer returned by default, and the overview geometry is simplified by default. The access_token parameter is now the last URL parameter instead of the first. Broke out MBDirectionsRequest.TransportType into three profile identifier constants. You always specify a profile identifier, not a transport type. That makes the desired mode of transportation more open-ended.

Removed the MBDirectionsResponse class in favor of passing the waypoints and routes from the response directly into the completion handler. Renamed Route.geometry to Route.coordinates. For Objective-C compatibility, all enums are backed by integer types instead of String, but they’re CustomStringConvertible to allow for the same expressiveness we’re used to in Swift.

Error handling is much more robust now. Various error conditions returned by the API cause the localized failure reason and recovery suggestion to be set in the NSError object that is passed into the completion handler.

Updated test fixtures and expectations for the v5 server-side API and the revamped client-side API, and updated examples.

Fixes #41, fixes #43, fixes #44.
The API documentation was incorrect regarding the implicit default value for the continue_straight query parameter (which depends on the profile identifier). It’s far easier to U-turn on a bike or on foot than it is in a car, so it must be the case that U-turns at waypoints are forbidden by default for driving directions only.
Renamed some RouteOptions properties to use declarative instead of imperative, which is reserved for method names.
Documented every public symbol. Some of the more finicky options get lots more documentation, discussing why you would use the option, when you avoid using the option, etc.
@1ec5 1ec5 self-assigned this May 31, 2016

If the value of this property is `true`, a returned route may require an immediate U-turn at an intermediate waypoint. At an intermediate waypoint, if the value of this property is `false`, each returned route may continue straight ahead or turn to either side but may not U-turn. This property has no effect if only two waypoints are specified.

Set this property to `true` if you expect the user to traverse each leg of the trip separately. For example, it would be quite easy for the user to effectively “U-turn” at a waypoint if the user first parks the car and patronizes a restaurant there before embarking on the next leg of the trip. Set this property to `false` if you expect the user to proceed to the next waypoint immediately upon arrival. For example, if the user only needs to drop off a passenger or package at the waypoint before continuing, it would be inconvenient to perform a U-turn at that location.
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Here’s the documentation corresponding to the continue_straight query parameter.

1ec5 added 2 commits May 30, 2016 20:42
Took an initializer for the Route class public, for applications that send the URL request themselves using a custom HTTP networking library.
@1ec5
Copy link
Contributor Author

1ec5 commented May 31, 2016

This PR goes in a very different direction than #30 does. After seeing how MapboxStatic.swift gets away without much in the way of networking features, I think this library can get away with something similar. One big benefit with this approach is that there’s no object to keep a strong reference to. On the other hand, #20 becomes the developer’s responsibility.

1ec5 added 3 commits May 30, 2016 22:05
Restored support for Directions API v4 via a series of v4-specific classes. Removed the code that synthesized a route leg summary because both versions of the API always return summaries now.
1ec5 added 2 commits May 31, 2016 01:57
Waypoint no longer stores a CLLocation.
1ec5 added 5 commits June 1, 2016 23:55
v4 responses don’t include a code property, so don’t treat it as a precondition for a valid response.

Fixed an issue in which the RouteLeg designated initializer recomputed the steps instead of using the steps computed by the convenience initializers.
The geometry field is completely omitted (not empty) when the route shape resolution is .None.
Fixed an issue causing coordinates to be omitted at all the wrong times when getting v4 directions.
Added proper error-handling logic. Reformatted example output to use properly formatted numbers and units and to display distances how they’re meant to be displayed (as maneuver delimiters).
@1ec5 1ec5 merged commit 7da9c91 into master Jun 2, 2016
@1ec5 1ec5 deleted the 1ec5-swifter branch June 2, 2016 07:46
@1ec5 1ec5 mentioned this pull request Oct 28, 2018
6 tasks
@1ec5 1ec5 added the backwards incompatible changes that break backwards compatibility of public API label Dec 18, 2018
@1ec5 1ec5 added this to the v0.6.0 milestone Dec 18, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backwards incompatible changes that break backwards compatibility of public API objective-c
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Read access token from Info.plist Add test cases for v5 API taskWithRouter causes errors
1 participant