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

feat: add ParseGeoPoint convenience methods for CoreLocation #287

Merged
merged 9 commits into from Nov 23, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Expand Up @@ -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
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
32 changes: 32 additions & 0 deletions Sources/ParseSwift/Types/ParseGeoPoint.swift
Expand Up @@ -75,6 +75,17 @@ public struct ParseGeoPoint: Codable, Hashable {
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: `ParseError`.
*/
jaysonng marked this conversation as resolved.
Show resolved Hide resolved
public init(locationCoordinate: CLLocationCoordinate2D) throws {
self.longitude = locationCoordinate.longitude
self.latitude = locationCoordinate.latitude
try validate()
}
#endif

/**
Expand Down Expand Up @@ -154,3 +165,24 @@ extension ParseGeoPoint: CustomStringConvertible {
debugDescription
}
}

// MARK: Convenience
extension ParseGeoPoint {
/**
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`
*/
public func toCLLocation() -> CLLocation {
return CLLocation(latitude: latitude, longitude: longitude)
jaysonng marked this conversation as resolved.
Show resolved Hide resolved
}

/**
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`
*/
public func toCLLocationCoordinate2D() -> CLLocationCoordinate2D {
return CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
jaysonng marked this conversation as resolved.
Show resolved Hide resolved
}
}
21 changes: 21 additions & 0 deletions Tests/ParseSwiftTests/ParseGeoPointTests.swift
Expand Up @@ -53,6 +53,13 @@ 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(locationCoordinate: location)
XCTAssertEqual(geoPoint.latitude, location.latitude)
XCTAssertEqual(geoPoint.longitude, location.longitude)
}
#endif

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

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