Skip to content

Commit 71b99d6

Browse files
author
Clément Le Provost
committed
Refactor Query.aroundRadius into an enum
This allows cleaner handling of the “all” value.
1 parent f3e171c commit 71b99d6

File tree

3 files changed

+67
-24
lines changed

3 files changed

+67
-24
lines changed

Source/Query.swift

Lines changed: 60 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -770,34 +770,56 @@ public class Query : NSObject, NSCopying {
770770
}
771771
// NOTE: Objective-C bridge moved away to `_objc_Query`
772772

773-
/// Control the radius associated with a `aroundLatLng` or `aroundLatLngViaIP` query. Defined in meters.
774-
/// If not set, the radius is computed automatically using the density of the area, you can retrieve the computed
773+
/// Applicable values for the `aroundRadius` parameter.
774+
public enum AroundRadius: Equatable {
775+
/// Specify an explicit value (in meters).
776+
case explicit(UInt)
777+
778+
/// Compute the geo distance without filtering in a geo area.
779+
/// This option will be faster than specifying a big integer.
780+
case all
781+
782+
// NOTE: Associated values disable automatic conformance to `Equatable`, so we have to implement it ourselves.
783+
static public func ==(lhs: AroundRadius, rhs: AroundRadius) -> Bool {
784+
switch (lhs, rhs) {
785+
case (let .explicit(lhsValue), let .explicit(rhsValue)): return lhsValue == rhsValue
786+
case (.all, .all): return true
787+
default: return false
788+
}
789+
}
790+
}
791+
792+
/// Control the radius associated with a `aroundLatLng` or `aroundLatLngViaIP` query.
793+
/// If not set, the radius is computed automatically using the density of the area. You can retrieve the computed
775794
/// radius in the `automaticRadius` attribute of the answer. You can also specify a minimum value for the automatic
776-
/// radius by using the `minimumAroundRadius` query parameter. You can specify `aroundRadiusAll` if you want to
777-
/// compute the geo distance without filtering in a geo area, this option will be faster than specifying a big
778-
/// integer.
795+
/// radius by using the `minimumAroundRadius` query parameter. You can specify `.all` if you want to
796+
/// compute the geo distance without filtering in a geo area (this option will be faster than specifying a big
797+
/// integer).
779798
///
780-
public var aroundRadius: UInt? {
799+
public var aroundRadius: AroundRadius? {
781800
get {
782801
if let stringValue = self["aroundRadius"] {
783802
if stringValue == "all" {
784-
return Query.aroundRadiusAll
785-
} else {
786-
return Query.parseUInt(stringValue)
803+
return .all
804+
} else if let value = Query.parseUInt(stringValue) {
805+
return .explicit(value)
787806
}
788-
} else {
789-
return nil
790807
}
808+
return nil
791809
}
792810
set {
793-
self["aroundRadius"] = newValue == Query.aroundRadiusAll ? "all" : Query.buildUInt(newValue)
811+
if let newValue = newValue {
812+
switch newValue {
813+
case let .explicit(value): self["aroundRadius"] = Query.buildUInt(value)
814+
case .all: self["aroundRadius"] = "all"
815+
}
816+
} else {
817+
self["aroundRadius"] = nil
818+
}
794819
}
795820
}
796821
// NOTE: Objective-C bridge moved away to `_objc_Query`
797822

798-
/// Special value for `aroundRadius` to compute the geo distance without filtering.
799-
public static let aroundRadiusAll: UInt = UInt.max
800-
801823
/// Control the precision of a `aroundLatLng` query. In meter. For example if you set `aroundPrecision=100`, two
802824
/// objects that are in the range 0-99m will be considered as identical in the ranking for the .variable geo
803825
/// ranking parameter (same for 100-199, 200-299, … ranges).
@@ -1235,10 +1257,31 @@ public class _objc_Query: Query {
12351257
set { self.aroundLatLngViaIP = newValue?.boolValue }
12361258
}
12371259

1260+
/// Special value for `aroundRadius` to compute the geo distance without filtering.
1261+
@objc public static let aroundRadiusAll: NSNumber = NSNumber(value: UInt.max)
1262+
12381263
@objc(aroundRadius)
12391264
public var _aroundRadius: NSNumber? {
1240-
get { return Query.toNumber(self.aroundRadius) }
1241-
set { self.aroundRadius = newValue?.uintValue }
1265+
get {
1266+
if let aroundRadius = aroundRadius {
1267+
switch aroundRadius {
1268+
case let .explicit(value): return NSNumber(value: value)
1269+
case .all: return _objc_Query.aroundRadiusAll
1270+
}
1271+
}
1272+
return nil
1273+
}
1274+
set {
1275+
if let newValue = newValue {
1276+
if newValue == _objc_Query.aroundRadiusAll {
1277+
self.aroundRadius = .all
1278+
} else {
1279+
self.aroundRadius = .explicit(newValue.uintValue)
1280+
}
1281+
} else {
1282+
self.aroundRadius = nil
1283+
}
1284+
}
12421285
}
12431286

12441287
@objc(aroundPrecision)

Tests/ObjectiveCBridging.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ - (void)testQuery {
112112
query.aroundLatLng = [[LatLng alloc] initWithLat:123.45 lng:67.89];
113113
query.aroundLatLngViaIP = [NSNumber numberWithBool:YES];
114114
query.aroundRadius = [NSNumber numberWithInt:666];
115-
query.aroundRadius = [NSNumber numberWithUnsignedInteger:[Query aroundRadiusAll]];
115+
query.aroundRadius = [Query aroundRadiusAll];
116116
query.aroundPrecision = [NSNumber numberWithInt:66];
117117
query.minimumAroundRadius = [NSNumber numberWithInt:666];
118118
query.insideBoundingBox = @[ [[GeoRect alloc] initWithP1:[[LatLng alloc] initWithLat:123.45 lng:67.89] p2:[[LatLng alloc] initWithLat:129.99 lng:69.99]] ];

Tests/QueryTests.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -452,17 +452,17 @@ class QueryTests: XCTestCase {
452452
func test_aroundRadius() {
453453
let query1 = Query()
454454
XCTAssertNil(query1.aroundRadius)
455-
query1.aroundRadius = 987
456-
XCTAssertEqual(query1.aroundRadius, 987)
455+
query1.aroundRadius = .explicit(987)
456+
XCTAssertEqual(query1.aroundRadius, .explicit(987))
457457
XCTAssertEqual(query1["aroundRadius"], "987")
458458
var query2 = Query.parse(query1.build())
459-
XCTAssertEqual(query2.aroundRadius, 987)
459+
XCTAssertEqual(query2.aroundRadius, .explicit(987))
460460

461-
query1.aroundRadius = Query.aroundRadiusAll
462-
XCTAssertEqual(query1.aroundRadius, Query.aroundRadiusAll)
461+
query1.aroundRadius = .all
462+
XCTAssertEqual(query1.aroundRadius, .all)
463463
XCTAssertEqual(query1["aroundRadius"], "all")
464464
query2 = Query.parse(query1.build())
465-
XCTAssertEqual(query2.aroundRadius, Query.aroundRadiusAll)
465+
XCTAssertEqual(query2.aroundRadius, .all)
466466
}
467467

468468
func test_aroundLatLngViaIP() {

0 commit comments

Comments
 (0)