Skip to content

Commit 595898e

Browse files
Nikolay Yekimovbobgodwinx
authored andcommitted
Use #keypath and unwrap for location manager events
1 parent dd9742c commit 595898e

File tree

3 files changed

+67
-23
lines changed

3 files changed

+67
-23
lines changed

RxCoreLocation.xcodeproj/project.pbxproj

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@
1717
35B742301F82A5E000829B21 /* Quick.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = 35B7422E1F82A5E000829B21 /* Quick.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
1818
35B742311F82A5EC00829B21 /* Nimble.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 35B7422D1F82A5DF00829B21 /* Nimble.framework */; };
1919
35B742321F82A5EC00829B21 /* Quick.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 35B7422E1F82A5E000829B21 /* Quick.framework */; };
20+
A85013AF204F093C00094A90 /* RxKeyPaths.swift in Sources */ = {isa = PBXBuildFile; fileRef = A85013AE204F093C00094A90 /* RxKeyPaths.swift */; };
21+
A85013B0204F174100094A90 /* RxKeyPaths.swift in Sources */ = {isa = PBXBuildFile; fileRef = A85013AE204F093C00094A90 /* RxKeyPaths.swift */; };
22+
A85013B1204F174100094A90 /* RxKeyPaths.swift in Sources */ = {isa = PBXBuildFile; fileRef = A85013AE204F093C00094A90 /* RxKeyPaths.swift */; };
23+
A85013B2204F174200094A90 /* RxKeyPaths.swift in Sources */ = {isa = PBXBuildFile; fileRef = A85013AE204F093C00094A90 /* RxKeyPaths.swift */; };
2024
C44FC2861FB59E150051CCA2 /* ForwardsEventsBehavior.swift in Sources */ = {isa = PBXBuildFile; fileRef = C44FC2851FB59E150051CCA2 /* ForwardsEventsBehavior.swift */; };
2125
C44FC28A1FB59FC00051CCA2 /* HasEventsBehavior.swift in Sources */ = {isa = PBXBuildFile; fileRef = C44FC2891FB59FC00051CCA2 /* HasEventsBehavior.swift */; };
2226
C48D554A1FA4FBD400FF1BF0 /* RxCocoa.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = C48D55481FA4FBD300FF1BF0 /* RxCocoa.framework */; };
@@ -100,6 +104,7 @@
100104
35B7422E1F82A5E000829B21 /* Quick.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Quick.framework; path = Carthage/Build/iOS/Quick.framework; sourceTree = "<group>"; };
101105
35B742341F82A61D00829B21 /* Nimble.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Nimble.framework; path = Carthage/Build/Mac/Nimble.framework; sourceTree = "<group>"; };
102106
35B742351F82A61E00829B21 /* Quick.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Quick.framework; path = Carthage/Build/Mac/Quick.framework; sourceTree = "<group>"; };
107+
A85013AE204F093C00094A90 /* RxKeyPaths.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RxKeyPaths.swift; sourceTree = "<group>"; };
103108
C44FC2851FB59E150051CCA2 /* ForwardsEventsBehavior.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ForwardsEventsBehavior.swift; sourceTree = "<group>"; };
104109
C44FC2891FB59FC00051CCA2 /* HasEventsBehavior.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HasEventsBehavior.swift; sourceTree = "<group>"; };
105110
C48D55481FA4FBD300FF1BF0 /* RxCocoa.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = RxCocoa.framework; path = Carthage/Build/iOS/RxCocoa.framework; sourceTree = "<group>"; };
@@ -240,6 +245,7 @@
240245
3549BB5C1DA38AA500C63030 /* Core */ = {
241246
isa = PBXGroup;
242247
children = (
248+
A85013AE204F093C00094A90 /* RxKeyPaths.swift */,
243249
C4B8B1FB1FA50C1E005E0A69 /* RxSelectors.swift */,
244250
C4B8B2001FA5C9D0005E0A69 /* RxEventTypealias.swift */,
245251
C4B8B2051FA62049005E0A69 /* RxCLLocationManagerDelegateProxy.swift */,
@@ -510,6 +516,7 @@
510516
C4B8B20B1FA646B7005E0A69 /* CLLocationManagerDelegateEvents+Rx.swift in Sources */,
511517
C4B8B2061FA62049005E0A69 /* RxCLLocationManagerDelegateProxy.swift in Sources */,
512518
C4BEE2641FB319670098BB4A /* CLLocationManagerEvents+Rx.swift in Sources */,
519+
A85013AF204F093C00094A90 /* RxKeyPaths.swift in Sources */,
513520
);
514521
runOnlyForDeploymentPostprocessing = 0;
515522
};
@@ -523,6 +530,7 @@
523530
C4B8B20E1FA646B7005E0A69 /* CLLocationManagerDelegateEvents+Rx.swift in Sources */,
524531
C4B8B2091FA62049005E0A69 /* RxCLLocationManagerDelegateProxy.swift in Sources */,
525532
C4BEE2671FB319670098BB4A /* CLLocationManagerEvents+Rx.swift in Sources */,
533+
A85013B2204F174200094A90 /* RxKeyPaths.swift in Sources */,
526534
);
527535
runOnlyForDeploymentPostprocessing = 0;
528536
};
@@ -536,6 +544,7 @@
536544
C4B8B20C1FA646B7005E0A69 /* CLLocationManagerDelegateEvents+Rx.swift in Sources */,
537545
C4B8B2071FA62049005E0A69 /* RxCLLocationManagerDelegateProxy.swift in Sources */,
538546
C4BEE2651FB319670098BB4A /* CLLocationManagerEvents+Rx.swift in Sources */,
547+
A85013B0204F174100094A90 /* RxKeyPaths.swift in Sources */,
539548
);
540549
runOnlyForDeploymentPostprocessing = 0;
541550
};
@@ -549,6 +558,7 @@
549558
C4B8B20D1FA646B7005E0A69 /* CLLocationManagerDelegateEvents+Rx.swift in Sources */,
550559
C4B8B2081FA62049005E0A69 /* RxCLLocationManagerDelegateProxy.swift in Sources */,
551560
C4BEE2661FB319670098BB4A /* CLLocationManagerEvents+Rx.swift in Sources */,
561+
A85013B1204F174100094A90 /* RxKeyPaths.swift in Sources */,
552562
);
553563
runOnlyForDeploymentPostprocessing = 0;
554564
};

Sources/CLLocationManagerEvents+Rx.swift

Lines changed: 32 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -15,37 +15,42 @@ import CoreLocation
1515
extension Reactive where Base: CLLocationManager {
1616
/// Reactive Observable for `activityType`
1717
public var activityType: Observable<CLActivityType?> {
18-
return self.observe(CLActivityType.self, "activityType")
18+
return self.observe(CLActivityType.self, .activityType)
1919
}
2020
/// Reactive Observable for `distanceFilter`
2121
public var distanceFilter: Observable<CLLocationDistance> {
22-
return self.observe(CLLocationDistance.self, "distanceFilter")
23-
.map { $0 ?? 0.0 }
22+
return self.observe(CLLocationDistance.self, .distanceFilter)
23+
.map { $0 }
24+
.unwrap()
2425
}
2526
/// Reactive Observable for `desiredAccuracy`
2627
public var desiredAccuracy: Observable<CLLocationAccuracy> {
27-
return self.observe(CLLocationAccuracy.self, "desiredAccuracy")
28-
.map { $0 ?? 0.0 }
28+
return self.observe(CLLocationAccuracy.self, .desiredAccuracy)
29+
.map { $0 }
30+
.unwrap()
2931
}
3032
/// Reactive Observable for `pausesLocationUpdatesAutomatically`
3133
public var pausesLocationUpdatesAutomatically: Observable<Bool> {
32-
return self.observe(Bool.self, "pausesLocationUpdatesAutomatically")
33-
.map { $0 ?? false }
34+
return self.observe(Bool.self, .pausesLocationUpdatesAutomatically)
35+
.map { $0 }
36+
.unwrap()
3437
}
3538
/// Reactive Observable for `allowsBackgroundLocationUpdates`
3639
public var allowsBackgroundLocationUpdates: Observable<Bool> {
37-
return self.observe(Bool.self, "allowsBackgroundLocationUpdates")
38-
.map { $0 ?? false }
40+
return self.observe(Bool.self, .allowsBackgroundLocationUpdates)
41+
.map { $0 }
42+
.unwrap()
3943
}
4044
/// Reactive Observable for `showsBackgroundLocationIndicator`
4145
public var showsBackgroundLocationIndicator: Observable<Bool> {
42-
return self.observe(Bool.self, "showsBackgroundLocationIndicator")
43-
.map { $0 ?? false }
46+
return self.observe(Bool.self, .showsBackgroundLocationIndicator)
47+
.map { $0 }
48+
.unwrap()
4449
}
4550
/// Reactive Observable for `location`
4651
public var location: Observable<CLLocation?> {
4752
let updatedLocation = self.didUpdateLocations.map { $1.last }
48-
let location = self.observe(CLLocation.self, "location")
53+
let location = self.observe(CLLocation.self, .location)
4954
return Observable.of(location, updatedLocation).merge()
5055
}
5156
/// Reactive Observable for CLPlacemark
@@ -67,33 +72,37 @@ extension Reactive where Base: CLLocationManager {
6772
}
6873
/// Reactive Observable for `headingFilter`
6974
public var headingFilter: Observable<CLLocationDegrees> {
70-
return self.observe(CLLocationDegrees.self, "headingFilter")
71-
.map { $0 ?? 0.0 }
75+
return self.observe(CLLocationDegrees.self, .headingFilter)
76+
.map { $0 }
77+
.unwrap()
7278
}
7379
/// Reactive Observable for `headingOrientation`
7480
public var headingOrientation: Observable<CLDeviceOrientation?> {
75-
return self.observe(CLDeviceOrientation.self, "headingOrientation")
81+
return self.observe(CLDeviceOrientation.self, .headingOrientation)
7682
}
7783
#if os(iOS) || os(macOS)
7884
/// Reactive Observable for `heading`
7985
public var heading: Observable<CLHeading?> {
80-
return self.observe(CLHeading.self, "heading")
86+
return self.observe(CLHeading.self, .heading)
8187
}
8288
#endif
8389
/// Reactive Observable for `maximumRegionMonitoringDistance`
8490
public var maximumRegionMonitoringDistance: Observable<CLLocationDistance> {
85-
return self.observe(CLLocationDistance.self, "maximumRegionMonitoringDistance")
86-
.map { $0 ?? 0.0 }
91+
return self.observe(CLLocationDistance.self, .maximumRegionMonitoringDistance)
92+
.map { $0 }
93+
.unwrap()
8794
}
8895
/// Reactive Observable for `monitoredRegions`
8996
public var monitoredRegions: Observable<Set<CLRegion>> {
90-
return self.observe(Set<CLRegion>.self, "monitoredRegions")
91-
.map { $0 ?? [] }
97+
return self.observe(Set<CLRegion>.self, .monitoredRegions)
98+
.map { $0 }
99+
.unwrap()
92100
}
93101
/// Reactive Observable for `rangedRegions`
94102
public var rangedRegions: Observable<Set<CLRegion>> {
95-
return self.observe(Set<CLRegion>.self, "rangedRegions")
96-
.map { $0 ?? [] }
103+
return self.observe(Set<CLRegion>.self, .rangedRegions)
104+
.map { $0 }
105+
.unwrap()
97106
}
98107

99108
/// Reactive Observable for `locationServicesEnabled`
@@ -190,4 +199,4 @@ extension CLLocationManager {
190199
}
191200
}
192201
#endif
193-
}
202+
}

Sources/RxKeyPaths.swift

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//
2+
// RxKeyPaths.swift
3+
// RxCoreLocation
4+
//
5+
// Created by Nik Yekimov on 3/6/18.
6+
// Copyright © 2018 RxCoreLocation. All rights reserved.
7+
//
8+
9+
import CoreLocation.CLLocationManager
10+
11+
extension String {
12+
static let activityType = #keyPath(CLLocationManager.activityType)
13+
static let distanceFilter = #keyPath(CLLocationManager.distanceFilter)
14+
static let desiredAccuracy = #keyPath(CLLocationManager.desiredAccuracy)
15+
static let pausesLocationUpdatesAutomatically = #keyPath(CLLocationManager.pausesLocationUpdatesAutomatically)
16+
static let allowsBackgroundLocationUpdates = #keyPath(CLLocationManager.allowsBackgroundLocationUpdates)
17+
static let showsBackgroundLocationIndicator = #keyPath(CLLocationManager.showsBackgroundLocationIndicator)
18+
static let location = #keyPath(CLLocationManager.location)
19+
static let headingFilter = #keyPath(CLLocationManager.headingFilter)
20+
static let headingOrientation = #keyPath(CLLocationManager.headingOrientation)
21+
static let heading = #keyPath(CLLocationManager.heading)
22+
static let maximumRegionMonitoringDistance = #keyPath(CLLocationManager.maximumRegionMonitoringDistance)
23+
static let monitoredRegions = #keyPath(CLLocationManager.monitoredRegions)
24+
static let rangedRegions = #keyPath(CLLocationManager.rangedRegions)
25+
}

0 commit comments

Comments
 (0)