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

Adding support for NSCopying protocol. #200

Merged
merged 3 commits into from
Nov 9, 2017
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
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 @@ -228,6 +228,11 @@ open class RouteOptions: NSObject, NSSecureCoding {
locale = decoder.decodeObject(of: NSLocale.self, forKey: "locale") as Locale?

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 @@ -244,6 +249,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 @@ -458,6 +464,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 {
Copy link
Contributor

Choose a reason for hiding this comment

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

Cool, this gets us a little bit closer to implementing #151. 👍

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
}
}