Skip to content

Commit

Permalink
Remove on CoreLocation to avoid dynamic linking issues
Browse files Browse the repository at this point in the history
  • Loading branch information
JP Wright committed Sep 8, 2017
1 parent 7219f90 commit 21dd5f9
Show file tree
Hide file tree
Showing 4 changed files with 172 additions and 95 deletions.
Expand Up @@ -77,16 +77,15 @@ client.fetchEntries(with: query).next {
}

//: If you have location-enabled content, you can use it for searching as well. Sort results by distance:
import CoreLocation
query = Query(onContentTypeFor: "1t9IbcfdCk6m04uISSsaIK").where("fields.center", .isNear(CLLocationCoordinate2D(latitude: 38, longitude: -122)))
query = Query(onContentTypeFor: "1t9IbcfdCk6m04uISSsaIK").where("fields.center", .isNear(Location(latitude: 38, longitude: -122)))
client.fetchEntries(with: query).next {
let names = $0.items.flatMap { $0.fields.string(at: "name") }
print(names)
}

//: Or retrieve all resources in a bounding rectangle:
let bottomLeft = CLLocationCoordinate2D(latitude: 40, longitude: -124)
let topRight = CLLocationCoordinate2D(latitude: 36, longitude: -121)
let bottomLeft = Location(latitude: 40, longitude: -124)
let topRight = Location(latitude: 36, longitude: -121)
let boundingBox = Bounds.box(bottomLeft: bottomLeft, topRight: topRight)
query = Query(onContentTypeFor: "1t9IbcfdCk6m04uISSsaIK").where("fields.center", .isWithin(boundingBox))
client.fetchEntries(with: query).next {
Expand Down
22 changes: 17 additions & 5 deletions Sources/Contentful/Query.swift
Expand Up @@ -7,8 +7,6 @@
//

import Foundation
import Interstellar
import CoreLocation

/// The available URL parameter names for queries; used internally by the various `Contentful.Query` types.
/// Use these static variables to avoid making typos when constructing queries. It is recommended to take
Expand Down Expand Up @@ -74,11 +72,25 @@ extension Date: QueryableRange {
}
}

/**
Small struct to store location coordinates. This is used in preferences over CoreLocation types to avoid
extra linking requirements for the SDK.
*/
public struct Location {
let latitude: Double
let longitude: Double

public init(latitude: Double, longitude: Double) {
self.latitude = latitude
self.longitude = longitude
}
}

/// Use bounding boxes or bounding circles to perform queries on location-enabled content.
/// See: <https://www.contentful.com/developers/docs/references/content-delivery-api/#/reference/search-parameters/locations-in-a-bounding-object>
public enum Bounds {
case box(bottomLeft: CLLocationCoordinate2D, topRight: CLLocationCoordinate2D)
case circle(center: CLLocationCoordinate2D, radius: Double)
case box(bottomLeft: Location, topRight: Location)
case circle(center: Location, radius: Double)
}

/// All the possible MIME types that are supported by Contentful. \
Expand Down Expand Up @@ -135,7 +147,7 @@ public enum QueryOperation {
case isGreaterThanOrEqualTo(QueryableRange)

/// Proximity searches.
case isNear(CLLocationCoordinate2D)
case isNear(Location)
case isWithin(Bounds)

fileprivate var string: String {
Expand Down
5 changes: 2 additions & 3 deletions Sources/Contentful/Resource.swift
Expand Up @@ -8,7 +8,6 @@

import Foundation
import ObjectMapper
import CoreLocation

/// Protocol for resources inside Contentful
public class Resource: ImmutableMappable {
Expand Down Expand Up @@ -236,11 +235,11 @@ public extension Dictionary where Key: ExpressibleByStringLiteral {
- Parameter key: The name of the field to extract the `Bool` value from.
- Returns: The `Bool` value, or `nil` if data contained is not convertible to a `Bool`.
*/
public func location(at key: Key) -> CLLocationCoordinate2D? {
public func location(at key: Key) -> Location? {
let coordinateJSON = self[key] as? [String: Any]
guard let longitude = coordinateJSON?["lon"] as? Double else { return nil }
guard let latitude = coordinateJSON?["lat"] as? Double else { return nil }
let location = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
let location = Location(latitude: latitude, longitude: longitude)
return location
}

Expand Down

0 comments on commit 21dd5f9

Please sign in to comment.