Skip to content

Commit

Permalink
Merge pull request #200 from mapbox/jerrad/routeobject-copying
Browse files Browse the repository at this point in the history
Adding support for `NSCopying` protocol.
  • Loading branch information
JThramer authored Nov 9, 2017
2 parents 4488a7f + 0232a6f commit 9a1c7e3
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 10 deletions.
47 changes: 46 additions & 1 deletion MapboxDirections/MBRouteOptions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ public enum MeasurementSystem: UInt, CustomStringConvertible {
Pass an instance of this class into the `Directions.calculate(_:completionHandler:)` method.
*/
@objc(MBRouteOptions)
open class RouteOptions: NSObject, NSSecureCoding {
open class RouteOptions: NSObject, NSSecureCoding, NSCopying{
// MARK: Creating a Route Options Object

/**
Expand Down Expand Up @@ -230,6 +230,11 @@ open class RouteOptions: NSObject, NSSecureCoding {
}

includesSpokenInstructions = decoder.decodeBool(forKey: "includesSpokenInstructions")

guard let distanceMeasurementSystem = MeasurementSystem(description: decoder.decodeObject(of: NSString.self, forKey: "distanceMeasurementSystem") as String? ?? "") else {
return nil
}
self.distanceMeasurementSystem = distanceMeasurementSystem
}

open static var supportsSecureCoding = true
Expand All @@ -246,6 +251,7 @@ open class RouteOptions: NSObject, NSSecureCoding {
coder.encode(includesExitRoundaboutManeuver, forKey: "includesExitRoundaboutManeuver")
coder.encode(locale, forKey: "locale")
coder.encode(includesSpokenInstructions, forKey: "includesSpokenInstructions")
coder.encode(distanceMeasurementSystem.description, forKey: "distanceMeasurementSystem")
}

// MARK: Specifying the Path of the Route
Expand Down Expand Up @@ -453,6 +459,45 @@ open class RouteOptions: NSObject, NSSecureCoding {
}
return (waypoints, routes)
}

// MARK: NSCopying
open func copy(with zone: NSZone? = nil) -> Any {
let copy = RouteOptions(waypoints: waypoints, profileIdentifier: profileIdentifier)
copy.allowsUTurnAtWaypoint = allowsUTurnAtWaypoint
copy.includesAlternativeRoutes = includesAlternativeRoutes
copy.includesSteps = includesSteps
copy.shapeFormat = shapeFormat
copy.routeShapeResolution = routeShapeResolution
copy.attributeOptions = attributeOptions
copy.includesExitRoundaboutManeuver = includesExitRoundaboutManeuver
copy.locale = locale
copy.includesSpokenInstructions = includesSpokenInstructions
copy.distanceMeasurementSystem = distanceMeasurementSystem
return copy
}

//MARK: - OBJ-C Equality
open override func isEqual(_ object: Any?) -> Bool {
guard let opts = object as? RouteOptions else { return false }
return isEqual(to: opts)
}

@objc(isEqualToRouteOptions:)
open func isEqual(to routeOptions: RouteOptions?) -> Bool {
guard let other = routeOptions else { return false }
guard waypoints == other.waypoints,
profileIdentifier == other.profileIdentifier,
allowsUTurnAtWaypoint == other.allowsUTurnAtWaypoint,
includesSteps == other.includesSteps,
shapeFormat == other.shapeFormat,
routeShapeResolution == other.routeShapeResolution,
attributeOptions == other.attributeOptions,
includesExitRoundaboutManeuver == other.includesExitRoundaboutManeuver,
locale == other.locale,
includesSpokenInstructions == other.includesSpokenInstructions,
distanceMeasurementSystem == other.distanceMeasurementSystem else { return false }
return true
}
}

// MARK: Support for Directions API v4
Expand Down
43 changes: 34 additions & 9 deletions MapboxDirectionsTests/RouteOptionsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,8 @@ import XCTest

class RouteOptionsTests: XCTestCase {
func testCoding() {
let coordinates = [
CLLocationCoordinate2D(latitude: 52.5109, longitude: 13.4301),
CLLocationCoordinate2D(latitude: 52.5080, longitude: 13.4265),
CLLocationCoordinate2D(latitude: 52.5021, longitude: 13.4316),
]

let options = RouteOptions(coordinates: coordinates, profileIdentifier: .automobileAvoidingTraffic)
options.locale = Locale(identifier: "en")


let options = RouteOptions.testInstance
let encodedData = NSMutableData()
let keyedArchiver = NSKeyedArchiver(forWritingWith: encodedData)
keyedArchiver.requiresSecureCoding = true
Expand All @@ -25,6 +18,7 @@ class RouteOptionsTests: XCTestCase {

XCTAssertNotNil(unarchivedOptions)

let coordinates = RouteOptions.testCoordinates
let unarchivedWaypoints = unarchivedOptions.waypoints
XCTAssertEqual(unarchivedWaypoints.count, coordinates.count)
XCTAssertEqual(unarchivedWaypoints[0].coordinate.latitude, coordinates[0].latitude)
Expand All @@ -39,4 +33,35 @@ class RouteOptionsTests: XCTestCase {
XCTAssertEqual(unarchivedOptions.includesSpokenInstructions, options.includesSpokenInstructions)
XCTAssertEqual(unarchivedOptions.distanceMeasurementSystem, options.distanceMeasurementSystem)
}
func testCopying() {
let testInstance = RouteOptions.testInstance
guard let copy = testInstance.copy() as? RouteOptions else { return XCTFail("RouteOptions copy method should an object of same type") }
XCTAssertNotNil(copy, "Copy should not be nil.")
XCTAssertTrue(testInstance == copy, "Test Instance and copy should be semantically equivalent.")
XCTAssertFalse(testInstance === copy, "Test Instance and copy should not be identical.")

}
}

private extension RouteOptions {
static var testCoordinates: [CLLocationCoordinate2D] {
return [
CLLocationCoordinate2D(latitude: 52.5109, longitude: 13.4301),
CLLocationCoordinate2D(latitude: 52.5080, longitude: 13.4265),
CLLocationCoordinate2D(latitude: 52.5021, longitude: 13.4316),
]
}
static var testInstance: RouteOptions {
let opts = RouteOptions(coordinates: self.testCoordinates, profileIdentifier: .automobileAvoidingTraffic)
opts.locale = Locale(identifier: "en_US")
opts.allowsUTurnAtWaypoint = true
opts.shapeFormat = .polyline
opts.routeShapeResolution = .full
opts.attributeOptions = [.congestionLevel]
opts.includesExitRoundaboutManeuver = true
opts.includesSpokenInstructions = true
opts.distanceMeasurementSystem = .metric

return opts
}
}

0 comments on commit 9a1c7e3

Please sign in to comment.