Skip to content

feat: add ParseGeoPoint convenience methods for CoreLocation #287

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

Merged
merged 9 commits into from
Nov 23, 2021
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
6 changes: 3 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
env:
DEVELOPER_DIR: ${{ env.CI_XCODE_13 }}
- name: Build-Test
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
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
env:
DEVELOPER_DIR: ${{ env.CI_XCODE_13 }}
- name: Prepare codecov
Expand Down Expand Up @@ -49,7 +49,7 @@ jobs:
security unlock-keychain -p "" temporary
security set-keychain-settings -lut 7200 temporary
- name: Build-Test
run: set -o pipefail && env NSUnbufferedIO=YES xcodebuild -workspace Parse.xcworkspace -scheme ParseSwift\ \(macOS\) -destination platform\=macOS -derivedDataPath DerivedData test | xcpretty
run: set -o pipefail && env NSUnbufferedIO=YES xcodebuild -workspace Parse.xcworkspace -scheme ParseSwift\ \(macOS\) -destination platform\=macOS -derivedDataPath DerivedData clean test | xcpretty
env:
DEVELOPER_DIR: ${{ env.CI_XCODE_13 }}
- name: Prepare codecov
Expand All @@ -74,7 +74,7 @@ jobs:
steps:
- uses: actions/checkout@v2
- name: Build
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
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
env:
DEVELOPER_DIR: ${{ env.CI_XCODE_13 }}
- name: Prepare codecov
Expand Down
2 changes: 1 addition & 1 deletion ParseSwift.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -2014,7 +2014,7 @@
);
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/sh;
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";
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";
};
918CED61268A23A700CFDC83 /* SwiftLint */ = {
isa = PBXShellScriptBuildPhase;
Expand Down
62 changes: 47 additions & 15 deletions Sources/ParseSwift/Types/ParseGeoPoint.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public struct ParseGeoPoint: Codable, Hashable {
Create a new `ParseGeoPoint` instance with the specified latitude and longitude.
- parameter latitude: Latitude of point in degrees.
- parameter longitude: Longitude of point in degrees.
- throws: `ParseError`.
- throws: An error of `ParseError` type.
*/
public init(latitude: Double, longitude: Double) throws {
self.latitude = latitude
Expand All @@ -64,19 +64,6 @@ public struct ParseGeoPoint: Codable, Hashable {
}
}

#if canImport(CoreLocation)
/**
Creates a new `ParseGeoPoint` instance for the given `CLLocation`, set to the location's coordinates.
- parameter location: Instance of `CLLocation`, with set latitude and longitude.
- throws: `ParseError`.
*/
public init(location: CLLocation) throws {
self.longitude = location.coordinate.longitude
self.latitude = location.coordinate.latitude
try validate()
}
#endif

/**
Get distance in radians from this point to specified point.

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

/**
Get distance in miles from this point to specified point.

- parameter point: `ParseGeoPoint` that represents the location of other point.
- returns: Distance in miles between the receiver and `point`.
*/
Expand Down Expand Up @@ -154,3 +140,49 @@ extension ParseGeoPoint: CustomStringConvertible {
debugDescription
}
}

#if canImport(CoreLocation)
// MARK: CLLocation
public extension ParseGeoPoint {

/**
Creates a new `ParseGeoPoint` instance for the given `CLLocation`, set to the location's coordinates.
- parameter location: Instance of `CLLocation`, with set latitude and longitude.
- throws: An error of `ParseError` type.
*/
init(location: CLLocation) throws {
self.longitude = location.coordinate.longitude
self.latitude = location.coordinate.latitude
try validate()
}

/**
Creates a new `ParseGeoPoint` instance for the given `CLLocationCoordinate2D`, set to the location's coordinates.
- parameter location: Instance of `CLLocationCoordinate2D`, with set latitude and longitude.
- throws: An error of `ParseError` type.
*/
init(coordinate: CLLocationCoordinate2D) throws {
self.longitude = coordinate.longitude
self.latitude = coordinate.latitude
try validate()
}

/**
Creates a new `CLLocation` instance for the given `ParseGeoPoint`, set to the location's coordinates.
- parameter geopoint: Instance of `ParseGeoPoint`, with set latitude and longitude.
- returns: Returns a `CLLocation`.
*/
func toCLLocation() -> CLLocation {
CLLocation(latitude: latitude, longitude: longitude)
}

/**
Creates a new `CLLocationCoordinate2D` instance for the given `ParseGeoPoint`, set to the location's coordinates.
- parameter geopoint: Instance of `ParseGeoPoint`, with set latitude and longitude.
- returns: Returns a `CLLocationCoordinate2D`.
*/
func toCLLocationCoordinate2D() -> CLLocationCoordinate2D {
CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
}
}
#endif
22 changes: 22 additions & 0 deletions Tests/ParseSwiftTests/ParseGeoPointTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,27 @@ class ParseGeoPointTests: XCTestCase {
XCTAssertEqual(geoPoint.latitude, location.coordinate.latitude)
XCTAssertEqual(geoPoint.longitude, location.coordinate.longitude)
}

func testGeoPointFromLocationCoordinate2D() throws {
let location = CLLocationCoordinate2D(latitude: 10.0, longitude: 20.0)
let geoPoint = try ParseGeoPoint(coordinate: location)
XCTAssertEqual(geoPoint.latitude, location.latitude)
XCTAssertEqual(geoPoint.longitude, location.longitude)
}

func testToCLLocation() throws {
let point = try ParseGeoPoint(latitude: 10, longitude: 20)
let location = point.toCLLocation()
XCTAssertEqual(point.latitude, location.coordinate.latitude)
XCTAssertEqual(point.longitude, location.coordinate.longitude)
}

func testToCLLocationCoordinate2D() throws {
let point = try ParseGeoPoint(latitude: 10, longitude: 20)
let location = point.toCLLocationCoordinate2D()
XCTAssertEqual(point.latitude, location.latitude)
XCTAssertEqual(point.longitude, location.longitude)
}
#endif

func testGeoPointEncoding() throws {
Expand Down Expand Up @@ -215,4 +236,5 @@ class ParseGeoPointTests: XCTestCase {
XCTAssertTrue(point.debugDescription.contains("10"))
XCTAssertTrue(point.debugDescription.contains("20"))
}

}