Skip to content

Commit

Permalink
temp save
Browse files Browse the repository at this point in the history
  • Loading branch information
frederoni committed Dec 12, 2017
1 parent db32185 commit 1c7c808
Show file tree
Hide file tree
Showing 19 changed files with 587 additions and 667 deletions.
20 changes: 20 additions & 0 deletions MapboxDirections.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@
objects = {

/* Begin PBXBuildFile section */
3533E73D1FDAF4DC00B62812 /* MBDirectionsResponse.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3533E73C1FDAF4DC00B62812 /* MBDirectionsResponse.swift */; };
3533E73E1FDAF4DC00B62812 /* MBDirectionsResponse.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3533E73C1FDAF4DC00B62812 /* MBDirectionsResponse.swift */; };
3533E73F1FDAF4DC00B62812 /* MBDirectionsResponse.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3533E73C1FDAF4DC00B62812 /* MBDirectionsResponse.swift */; };
3533E7401FDAF4DC00B62812 /* MBDirectionsResponse.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3533E73C1FDAF4DC00B62812 /* MBDirectionsResponse.swift */; };
357EA9A71FDFCDA000320FB5 /* Codable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 357EA9A61FDFCD9F00320FB5 /* Codable.swift */; };
357EA9A81FDFCDA000320FB5 /* Codable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 357EA9A61FDFCD9F00320FB5 /* Codable.swift */; };
357EA9A91FDFCDA000320FB5 /* Codable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 357EA9A61FDFCD9F00320FB5 /* Codable.swift */; };
357EA9AA1FDFCDA000320FB5 /* Codable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 357EA9A61FDFCD9F00320FB5 /* Codable.swift */; };
358D48811E2EAB2500F32A65 /* MBRouteOptions.h in Headers */ = {isa = PBXBuildFile; fileRef = 358D48801E2EAB2500F32A65 /* MBRouteOptions.h */; settings = {ATTRIBUTES = (Public, ); }; };
358D48821E2EAB2500F32A65 /* MBRouteOptions.h in Headers */ = {isa = PBXBuildFile; fileRef = 358D48801E2EAB2500F32A65 /* MBRouteOptions.h */; settings = {ATTRIBUTES = (Public, ); }; };
358D48831E2EAB2500F32A65 /* MBRouteOptions.h in Headers */ = {isa = PBXBuildFile; fileRef = 358D48801E2EAB2500F32A65 /* MBRouteOptions.h */; settings = {ATTRIBUTES = (Public, ); }; };
Expand Down Expand Up @@ -194,6 +202,8 @@
/* End PBXContainerItemProxy section */

/* Begin PBXFileReference section */
3533E73C1FDAF4DC00B62812 /* MBDirectionsResponse.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MBDirectionsResponse.swift; sourceTree = "<group>"; };
357EA9A61FDFCD9F00320FB5 /* Codable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Codable.swift; sourceTree = "<group>"; };
358D48801E2EAB2500F32A65 /* MBRouteOptions.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MBRouteOptions.h; sourceTree = "<group>"; };
358D48851E2EAB4500F32A65 /* MBRouteOptions.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MBRouteOptions.m; sourceTree = "<group>"; };
35A328531FD9984C00D65492 /* CoreLocation.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CoreLocation.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -394,6 +404,8 @@
C52552B81FA15D5900B1545C /* MBVisualInstruction.swift */,
C52552BF1FA1628A00B1545C /* MBVisualInstructionComponent.swift */,
35A328531FD9984C00D65492 /* CoreLocation.swift */,
3533E73C1FDAF4DC00B62812 /* MBDirectionsResponse.swift */,
357EA9A61FDFCD9F00320FB5 /* Codable.swift */,
);
path = MapboxDirections;
sourceTree = "<group>";
Expand Down Expand Up @@ -967,6 +979,7 @@
DA1A10C91D00F969009F82FA /* MBRouteLeg.swift in Sources */,
C52552BA1FA15D5E00B1545C /* MBVisualInstruction.swift in Sources */,
C584E3F71F201C7B00BBC9BB /* MBRoadClasses.swift in Sources */,
3533E73E1FDAF4DC00B62812 /* MBDirectionsResponse.swift in Sources */,
358D48871E2EAB4500F32A65 /* MBRouteOptions.m in Sources */,
DA1A10CC1D00F969009F82FA /* MBWaypoint.swift in Sources */,
DA1A10CA1D00F969009F82FA /* MBRouteOptions.swift in Sources */,
Expand All @@ -977,6 +990,7 @@
C52CE38F1F6AF63C0069963D /* MBSpokenInstruction.swift in Sources */,
35A328551FD9CF0300D65492 /* CoreLocation.swift in Sources */,
C58EA7AB1E9D7F73008F98CE /* MBCongestion.swift in Sources */,
357EA9A81FDFCDA000320FB5 /* Codable.swift in Sources */,
C547EC691DB59F8F009817F3 /* MBLane.swift in Sources */,
DA1A10C71D00F969009F82FA /* MBDirections.swift in Sources */,
);
Expand Down Expand Up @@ -1007,6 +1021,7 @@
DA1A10EF1D010247009F82FA /* MBRouteLeg.swift in Sources */,
C52552BB1FA15D5F00B1545C /* MBVisualInstruction.swift in Sources */,
C584E3F81F201C7C00BBC9BB /* MBRoadClasses.swift in Sources */,
3533E73F1FDAF4DC00B62812 /* MBDirectionsResponse.swift in Sources */,
358D48881E2EAB4500F32A65 /* MBRouteOptions.m in Sources */,
DA1A10F21D010247009F82FA /* MBWaypoint.swift in Sources */,
DA1A10F01D010247009F82FA /* MBRouteOptions.swift in Sources */,
Expand All @@ -1017,6 +1032,7 @@
C52CE3901F6AF63D0069963D /* MBSpokenInstruction.swift in Sources */,
35A328561FD9CF0300D65492 /* CoreLocation.swift in Sources */,
C58EA7AC1E9D7F74008F98CE /* MBCongestion.swift in Sources */,
357EA9A91FDFCDA000320FB5 /* Codable.swift in Sources */,
C547EC6A1DB59F90009817F3 /* MBLane.swift in Sources */,
DA1A10ED1D010247009F82FA /* MBDirections.swift in Sources */,
);
Expand Down Expand Up @@ -1047,6 +1063,7 @@
DA1A11061D0103A3009F82FA /* MBRouteLeg.swift in Sources */,
C52552BC1FA15D6000B1545C /* MBVisualInstruction.swift in Sources */,
C584E3F91F201C7D00BBC9BB /* MBRoadClasses.swift in Sources */,
3533E7401FDAF4DC00B62812 /* MBDirectionsResponse.swift in Sources */,
358D48891E2EAB4500F32A65 /* MBRouteOptions.m in Sources */,
DA1A11091D0103A3009F82FA /* MBWaypoint.swift in Sources */,
DA1A11071D0103A3009F82FA /* MBRouteOptions.swift in Sources */,
Expand All @@ -1057,6 +1074,7 @@
C52CE3911F6AF63E0069963D /* MBSpokenInstruction.swift in Sources */,
35A328571FD9CF0400D65492 /* CoreLocation.swift in Sources */,
C58EA7AD1E9D7F75008F98CE /* MBCongestion.swift in Sources */,
357EA9AA1FDFCDA000320FB5 /* Codable.swift in Sources */,
C547EC6B1DB59F91009817F3 /* MBLane.swift in Sources */,
DA1A11041D0103A3009F82FA /* MBDirections.swift in Sources */,
);
Expand All @@ -1071,6 +1089,7 @@
C52552B91FA15D5900B1545C /* MBVisualInstruction.swift in Sources */,
C52552C01FA1628A00B1545C /* MBVisualInstructionComponent.swift in Sources */,
C59426071F1EA6C400C8E59C /* MBRoadClasses.swift in Sources */,
3533E73D1FDAF4DC00B62812 /* MBDirectionsResponse.swift in Sources */,
358D48861E2EAB4500F32A65 /* MBRouteOptions.m in Sources */,
DAC05F161CFBFAC400FA0071 /* MBWaypoint.swift in Sources */,
C57D55081DB58C0200B94B74 /* MBLane.swift in Sources */,
Expand All @@ -1081,6 +1100,7 @@
C55FB44B1F6AEBF6006BD1E9 /* MBSpokenInstruction.swift in Sources */,
35A328541FD9984C00D65492 /* CoreLocation.swift in Sources */,
C58EA7AA1E9D7EAD008F98CE /* MBCongestion.swift in Sources */,
357EA9A71FDFCDA000320FB5 /* Codable.swift in Sources */,
C57D55011DB5669600B94B74 /* MBIntersection.swift in Sources */,
DA2E03E91CB0E0B000D1269A /* MBRouteStep.swift in Sources */,
);
Expand Down
28 changes: 28 additions & 0 deletions MapboxDirections/Codable.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import Foundation

extension Decodable {
static internal func from<T: Decodable>(json: String, using encoding: String.Encoding = .utf8) -> T? {
guard let data = json.data(using: encoding) else { return nil }
return from(data: data) as T?
}

static internal func from<T: Decodable>(data: Data) -> T? {
let decoder = JSONDecoder()
return try! decoder.decode(T.self, from: data) as T?
}
}

struct GenericDecodable<T: Decodable, U: Decodable>: Decodable {
var t: T?
var u: U?

var value: Decodable? {
return t ?? u
}

init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()
t = try? container.decode(T.self)
u = try? container.decode(U.self)
}
}
2 changes: 1 addition & 1 deletion MapboxDirections/MBCongestion.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import Foundation
A `CongestionLevel` indicates the level of traffic congestion along a road segment relative to the normal flow of traffic along that segment. You can color-code a route line according to the congestion level along each segment of the route.
*/
@objc(MBCongestionLevel)
public enum CongestionLevel: Int, CustomStringConvertible {
public enum CongestionLevel: Int, CustomStringConvertible, Codable {
/**
There is not enough data to determine the level of congestion along the road segment.
*/
Expand Down
54 changes: 28 additions & 26 deletions MapboxDirections/MBDirections.swift
Original file line number Diff line number Diff line change
Expand Up @@ -166,20 +166,25 @@ open class Directions: NSObject {
@objc(calculateDirectionsWithOptions:completionHandler:)
@discardableResult open func calculate(_ options: RouteOptions, completionHandler: @escaping CompletionHandler) -> URLSessionDataTask {
let url = self.url(forCalculating: options)
let task = dataTask(with: url, completionHandler: { (json) in
let response = options.response(from: json)
if let routes = response.1 {
for route in routes {
route.accessToken = self.accessToken
route.apiEndpoint = self.apiEndpoint
route.routeIdentifier = json["uuid"] as? String
let task = dataTask(with: url, handler: { (data) in
do {
let decoder = DirectionsDecoder(userInfo: [CodingUserInfoKey.routeOptions!: options])
let result = try decoder.decode(DirectionsResponse.self, from: data)
result.routes?.forEach {
$0.accessToken = self.accessToken
$0.apiEndpoint = self.apiEndpoint
$0.routeIdentifier = result.uuid
}
completionHandler(result.waypoints, result.routes, nil)
} catch {
completionHandler(nil, nil, error as NSError)
}
completionHandler(response.0, response.1, nil)
}) { (error) in
completionHandler(nil, nil, error)
}

task.resume()

return task
}

Expand All @@ -192,32 +197,18 @@ open class Directions: NSObject {
- returns: The data task for the URL.
- postcondition: The caller must resume the returned task.
*/
fileprivate func dataTask(with url: URL, completionHandler: @escaping (_ json: JSONDictionary) -> Void, errorHandler: @escaping (_ error: NSError) -> Void) -> URLSessionDataTask {
fileprivate func dataTask(with url: URL, handler: @escaping (_ data: Data) -> Void, errorHandler: @escaping (_ error: NSError) -> Void) -> URLSessionDataTask {

var request = URLRequest(url: url)
request.setValue(userAgent, forHTTPHeaderField: "User-Agent")
return URLSession.shared.dataTask(with: request as URLRequest) { (data, response, error) in
var json: JSONDictionary = [:]
if let data = data, response?.mimeType == "application/json" {
do {
json = try JSONSerialization.jsonObject(with: data, options: []) as! JSONDictionary
} catch {
assert(false, "Invalid data")
}
}

let apiStatusCode = json["code"] as? String
let apiMessage = json["message"] as? String
guard data != nil && error == nil && ((apiStatusCode == nil && apiMessage == nil) || apiStatusCode == "Ok") else {
let apiError = Directions.informativeError(describing: json, response: response, underlyingError: error as NSError?)
DispatchQueue.main.async {
errorHandler(apiError)
}
guard let data = data, response?.mimeType == "application/json" else {
errorHandler(Directions.informativeError(describing: [:], response: response, underlyingError: error as NSError?))
return
}

DispatchQueue.main.async {
completionHandler(json)
handler(data)
}
}
}
Expand Down Expand Up @@ -310,3 +301,14 @@ extension HTTPURLResponse {
}

}

extension CodingUserInfoKey {
static let routeOptions = CodingUserInfoKey(rawValue: "options")
}

class DirectionsDecoder: JSONDecoder {
init(userInfo: [CodingUserInfoKey: Any]) {
super.init()
self.userInfo = userInfo
}
}
18 changes: 18 additions & 0 deletions MapboxDirections/MBDirectionsResponse.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import Foundation

struct DirectionsResponse: Codable {

enum CodingKeys: String, CodingKey {
case code
case message
case uuid
case routes
case waypoints
}

let code: String?
let message: String?
let uuid: String?
let routes: [Route]?
let waypoints: [Waypoint]?
}
Loading

0 comments on commit 1c7c808

Please sign in to comment.