|
1 |
| -import MapKit |
2 |
| -#if !PMKCocoaPods |
3 |
| -import PromiseKit |
4 |
| -#endif |
5 |
| - |
6 |
| -/** |
7 |
| - To import the `MKDirections` category: |
8 |
| - |
9 |
| - use_frameworks! |
10 |
| - pod "PromiseKit/MapKit" |
11 |
| - |
12 |
| - And then in your sources: |
13 |
| - |
14 |
| - import PromiseKit |
15 |
| -*/ |
16 |
| -extension MKDirections { |
17 |
| - /// Begins calculating the requested route information asynchronously. |
18 |
| - public func calculate() -> Promise<MKDirectionsResponse> { |
19 |
| - return Promise { calculate(completionHandler: $0.resolve) } |
20 |
| - } |
21 |
| - |
22 |
| - /// Begins calculating the requested travel-time information asynchronously. |
23 |
| - public func calculateETA() -> Promise<MKETAResponse> { |
24 |
| - return Promise { calculateETA(completionHandler: $0.resolve) } |
25 |
| - } |
26 |
| -} |
| 1 | +import MapKit |
| 2 | +#if !PMKCocoaPods |
| 3 | +import PromiseKit |
| 4 | +#endif |
| 5 | + |
| 6 | +/** |
| 7 | + To import the `MKDirections` category: |
| 8 | + |
| 9 | + use_frameworks! |
| 10 | + pod "PromiseKit/MapKit" |
| 11 | + |
| 12 | + And then in your sources: |
| 13 | + |
| 14 | + import PromiseKit |
| 15 | +*/ |
| 16 | +extension MKDirections { |
| 17 | +#if swift(>=4.2) |
| 18 | + /// Begins calculating the requested route information asynchronously. |
| 19 | + public func calculate() -> Promise<Response> { |
| 20 | + return Promise<Response>(cancellableTask: MKDirectionsTask(self)) { calculate(completionHandler: $0.resolve) } |
| 21 | + } |
| 22 | + |
| 23 | + /// Begins calculating the requested travel-time information asynchronously. |
| 24 | + public func calculateETA() -> Promise<ETAResponse> { |
| 25 | + return Promise<ETAResponse>(cancellableTask: MKDirectionsTask(self)) { calculateETA(completionHandler: $0.resolve) } |
| 26 | + } |
| 27 | +#else |
| 28 | + /// Begins calculating the requested route information asynchronously. |
| 29 | + public func calculate() -> Promise<MKDirectionsResponse> { |
| 30 | + return Promise<MKDirectionsResponse>(cancellableTask: MKDirectionsTask(self)) { calculate(completionHandler: $0.resolve) } |
| 31 | + } |
| 32 | + |
| 33 | + /// Begins calculating the requested travel-time information asynchronously. |
| 34 | + public func calculateETA() -> Promise<MKETAResponse> { |
| 35 | + return Promise<MKETAResponse>(cancellableTask: MKDirectionsTask(self)) { calculateETA(completionHandler: $0.resolve) } |
| 36 | + } |
| 37 | +#endif |
| 38 | +} |
| 39 | + |
| 40 | +private class MKDirectionsTask: CancellableTask { |
| 41 | + let directions: MKDirections |
| 42 | + var cancelAttempted = false |
| 43 | + |
| 44 | + init(_ directions: MKDirections) { |
| 45 | + self.directions = directions |
| 46 | + } |
| 47 | + |
| 48 | + func cancel() { |
| 49 | + directions.cancel() |
| 50 | + cancelAttempted = true |
| 51 | + } |
| 52 | + |
| 53 | + var isCancelled: Bool { |
| 54 | + return cancelAttempted && !directions.isCalculating |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +//////////////////////////////////////////////////////////// Cancellable wrappers |
| 59 | + |
| 60 | +extension MKDirections { |
| 61 | +#if swift(>=4.2) |
| 62 | + /// Begins calculating the requested route information asynchronously. |
| 63 | + public func cancellableCalculate() -> CancellablePromise<Response> { |
| 64 | + return cancellable(calculate()) |
| 65 | + } |
| 66 | + |
| 67 | + /// Begins calculating the requested travel-time information asynchronously. |
| 68 | + public func cancellableCalculateETA() -> CancellablePromise<ETAResponse> { |
| 69 | + return cancellable(calculateETA()) |
| 70 | + } |
| 71 | +#else |
| 72 | + /// Begins calculating the requested route information asynchronously. |
| 73 | + public func cancellableCalculate() -> CancellablePromise<MKDirectionsResponse> { |
| 74 | + return cancellable(calculate()) |
| 75 | + } |
| 76 | + |
| 77 | + /// Begins calculating the requested travel-time information asynchronously. |
| 78 | + public func cancellableCalculateETA() -> CancellablePromise<MKETAResponse> { |
| 79 | + return cancellable(calculateETA()) |
| 80 | + } |
| 81 | +#endif |
| 82 | +} |
0 commit comments