Skip to content

Commit 7c53ef3

Browse files
jaysonngJayson Ngcbaker6
authored
feat: add ParseGeoPoint convenience methods for CoreLocation (#287)
* SwiftLint Fix for M1 Macs Added Path variable fix for M1 Mac's see: https://www.anotheriosdevblog.com/installing-swiftlint-on-a-m1/ * Update ci.yml * ParseGeoPoint Convenience Methods for CoreLocation adding toCLLocation and toCLLocationCoordinate2D convenience methods and Tests. * Moved tests to IF CoreLocation block. * Renaming locationCoordate to coordinate and removing return for one liners Renaming locationCoordate to coordinate and removing return for one liners * Add fixes * doc nirs * Add clean to iOS and macOS builds Co-authored-by: Jayson Ng <[email protected]> Co-authored-by: Corey <[email protected]>
1 parent b4bdfb1 commit 7c53ef3

File tree

4 files changed

+73
-19
lines changed

4 files changed

+73
-19
lines changed

.github/workflows/ci.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ jobs:
1818
env:
1919
DEVELOPER_DIR: ${{ env.CI_XCODE_13 }}
2020
- name: Build-Test
21-
run: set -o pipefail && env NSUnbufferedIO=YES xcodebuild -workspace Parse.xcworkspace -scheme ParseSwift\ \(iOS\) -destination platform\=iOS\ Simulator,name\=iPhone\ 12\ Pro\ Max -derivedDataPath DerivedData test | xcpretty
21+
run: set -o pipefail && env NSUnbufferedIO=YES xcodebuild -workspace Parse.xcworkspace -scheme ParseSwift\ \(iOS\) -destination platform\=iOS\ Simulator,name\=iPhone\ 12\ Pro\ Max -derivedDataPath DerivedData clean test | xcpretty
2222
env:
2323
DEVELOPER_DIR: ${{ env.CI_XCODE_13 }}
2424
- name: Prepare codecov
@@ -49,7 +49,7 @@ jobs:
4949
security unlock-keychain -p "" temporary
5050
security set-keychain-settings -lut 7200 temporary
5151
- name: Build-Test
52-
run: set -o pipefail && env NSUnbufferedIO=YES xcodebuild -workspace Parse.xcworkspace -scheme ParseSwift\ \(macOS\) -destination platform\=macOS -derivedDataPath DerivedData test | xcpretty
52+
run: set -o pipefail && env NSUnbufferedIO=YES xcodebuild -workspace Parse.xcworkspace -scheme ParseSwift\ \(macOS\) -destination platform\=macOS -derivedDataPath DerivedData clean test | xcpretty
5353
env:
5454
DEVELOPER_DIR: ${{ env.CI_XCODE_13 }}
5555
- name: Prepare codecov
@@ -74,7 +74,7 @@ jobs:
7474
steps:
7575
- uses: actions/checkout@v2
7676
- name: Build
77-
run: set -o pipefail && env NSUnbufferedIO=YES xcodebuild -workspace Parse.xcworkspace -scheme ParseSwift\ \(tvOS\) -destination platform\=tvOS\ Simulator,name\=Apple\ TV -derivedDataPath DerivedData test | xcpretty
77+
run: set -o pipefail && env NSUnbufferedIO=YES xcodebuild -workspace Parse.xcworkspace -scheme ParseSwift\ \(tvOS\) -destination platform\=tvOS\ Simulator,name\=Apple\ TV -derivedDataPath DerivedData clean test | xcpretty
7878
env:
7979
DEVELOPER_DIR: ${{ env.CI_XCODE_13 }}
8080
- name: Prepare codecov

ParseSwift.xcodeproj/project.pbxproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2014,7 +2014,7 @@
20142014
);
20152015
runOnlyForDeploymentPostprocessing = 0;
20162016
shellPath = /bin/sh;
2017-
shellScript = "if which swiftlint >/dev/null; then\n swiftlint --fix && swiftlint --strict\nelse\n echo \"warning: SwiftLint not installed, download from https://github.com/realm/SwiftLint\"\nfi\n";
2017+
shellScript = "if test -d \"/opt/homebrew/bin/\"; then\n PATH=\"/opt/homebrew/bin/:${PATH}\"\nfi\n\nexport PATH\n\nif which swiftlint >/dev/null; then\n swiftlint --fix && swiftlint --strict\nelse\n echo \"warning: SwiftLint not installed, download from https://github.com/realm/SwiftLint\"\nfi\n";
20182018
};
20192019
918CED61268A23A700CFDC83 /* SwiftLint */ = {
20202020
isa = PBXShellScriptBuildPhase;

Sources/ParseSwift/Types/ParseGeoPoint.swift

Lines changed: 47 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public struct ParseGeoPoint: Codable, Hashable {
4040
Create a new `ParseGeoPoint` instance with the specified latitude and longitude.
4141
- parameter latitude: Latitude of point in degrees.
4242
- parameter longitude: Longitude of point in degrees.
43-
- throws: `ParseError`.
43+
- throws: An error of `ParseError` type.
4444
*/
4545
public init(latitude: Double, longitude: Double) throws {
4646
self.latitude = latitude
@@ -64,19 +64,6 @@ public struct ParseGeoPoint: Codable, Hashable {
6464
}
6565
}
6666

67-
#if canImport(CoreLocation)
68-
/**
69-
Creates a new `ParseGeoPoint` instance for the given `CLLocation`, set to the location's coordinates.
70-
- parameter location: Instance of `CLLocation`, with set latitude and longitude.
71-
- throws: `ParseError`.
72-
*/
73-
public init(location: CLLocation) throws {
74-
self.longitude = location.coordinate.longitude
75-
self.latitude = location.coordinate.latitude
76-
try validate()
77-
}
78-
#endif
79-
8067
/**
8168
Get distance in radians from this point to specified point.
8269

@@ -102,7 +89,6 @@ public struct ParseGeoPoint: Codable, Hashable {
10289

10390
/**
10491
Get distance in miles from this point to specified point.
105-
10692
- parameter point: `ParseGeoPoint` that represents the location of other point.
10793
- returns: Distance in miles between the receiver and `point`.
10894
*/
@@ -154,3 +140,49 @@ extension ParseGeoPoint: CustomStringConvertible {
154140
debugDescription
155141
}
156142
}
143+
144+
#if canImport(CoreLocation)
145+
// MARK: CLLocation
146+
public extension ParseGeoPoint {
147+
148+
/**
149+
Creates a new `ParseGeoPoint` instance for the given `CLLocation`, set to the location's coordinates.
150+
- parameter location: Instance of `CLLocation`, with set latitude and longitude.
151+
- throws: An error of `ParseError` type.
152+
*/
153+
init(location: CLLocation) throws {
154+
self.longitude = location.coordinate.longitude
155+
self.latitude = location.coordinate.latitude
156+
try validate()
157+
}
158+
159+
/**
160+
Creates a new `ParseGeoPoint` instance for the given `CLLocationCoordinate2D`, set to the location's coordinates.
161+
- parameter location: Instance of `CLLocationCoordinate2D`, with set latitude and longitude.
162+
- throws: An error of `ParseError` type.
163+
*/
164+
init(coordinate: CLLocationCoordinate2D) throws {
165+
self.longitude = coordinate.longitude
166+
self.latitude = coordinate.latitude
167+
try validate()
168+
}
169+
170+
/**
171+
Creates a new `CLLocation` instance for the given `ParseGeoPoint`, set to the location's coordinates.
172+
- parameter geopoint: Instance of `ParseGeoPoint`, with set latitude and longitude.
173+
- returns: Returns a `CLLocation`.
174+
*/
175+
func toCLLocation() -> CLLocation {
176+
CLLocation(latitude: latitude, longitude: longitude)
177+
}
178+
179+
/**
180+
Creates a new `CLLocationCoordinate2D` instance for the given `ParseGeoPoint`, set to the location's coordinates.
181+
- parameter geopoint: Instance of `ParseGeoPoint`, with set latitude and longitude.
182+
- returns: Returns a `CLLocationCoordinate2D`.
183+
*/
184+
func toCLLocationCoordinate2D() -> CLLocationCoordinate2D {
185+
CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
186+
}
187+
}
188+
#endif

Tests/ParseSwiftTests/ParseGeoPointTests.swift

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,27 @@ class ParseGeoPointTests: XCTestCase {
5353
XCTAssertEqual(geoPoint.latitude, location.coordinate.latitude)
5454
XCTAssertEqual(geoPoint.longitude, location.coordinate.longitude)
5555
}
56+
57+
func testGeoPointFromLocationCoordinate2D() throws {
58+
let location = CLLocationCoordinate2D(latitude: 10.0, longitude: 20.0)
59+
let geoPoint = try ParseGeoPoint(coordinate: location)
60+
XCTAssertEqual(geoPoint.latitude, location.latitude)
61+
XCTAssertEqual(geoPoint.longitude, location.longitude)
62+
}
63+
64+
func testToCLLocation() throws {
65+
let point = try ParseGeoPoint(latitude: 10, longitude: 20)
66+
let location = point.toCLLocation()
67+
XCTAssertEqual(point.latitude, location.coordinate.latitude)
68+
XCTAssertEqual(point.longitude, location.coordinate.longitude)
69+
}
70+
71+
func testToCLLocationCoordinate2D() throws {
72+
let point = try ParseGeoPoint(latitude: 10, longitude: 20)
73+
let location = point.toCLLocationCoordinate2D()
74+
XCTAssertEqual(point.latitude, location.latitude)
75+
XCTAssertEqual(point.longitude, location.longitude)
76+
}
5677
#endif
5778

5879
func testGeoPointEncoding() throws {
@@ -215,4 +236,5 @@ class ParseGeoPointTests: XCTestCase {
215236
XCTAssertTrue(point.debugDescription.contains("10"))
216237
XCTAssertTrue(point.debugDescription.contains("20"))
217238
}
239+
218240
}

0 commit comments

Comments
 (0)