This repository was archived by the owner on Feb 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathDANReactiveLocationManager.m
More file actions
175 lines (141 loc) · 5.39 KB
/
Copy pathDANReactiveLocationManager.m
File metadata and controls
175 lines (141 loc) · 5.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
//
// DANReactiveLocationManager.m
//
// Created by Daniel Tomlinson on 08/04/2014.
//
// Copyright (c) 2014 WeAreBase.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
#import "DANReactiveLocationManager.h"
#import <ReactiveCocoa/ReactiveCocoa.h>
#import <CoreLocation/CoreLocation.h>
#import "CLLocation+RLMAdditions.h"
@interface DANReactiveLocationManager () <CLLocationManagerDelegate>
@property (nonatomic, retain) CLLocationManager *locationManager;
@property (nonatomic, retain) RACReplaySubject *recentLocationSubject;
@property (nonatomic, retain) NSTimer *updateTimer;
typedef NS_ENUM(NSInteger, DANReactiveLocationManagerMonitoringState)
{
DANReactiveLocationManagerMonitoringStateConstant = 0,
DANReactiveLocationManagerMonitoringStateSignificantChange,
DANReactiveLocationManagerMonitoringStateStoppedByError,
DANReactiveLocationManagerMonitoringStateNotMonitoring
};
@property (nonatomic, assign) DANReactiveLocationManagerMonitoringState monitoringState;
@end
@implementation DANReactiveLocationManager
#pragma mark - Init Methods
- (instancetype)init
{
self = [super init];
if (self)
{
CLLocationManager *locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters;
_locationManager = locationManager;
_monitoringState = DANReactiveLocationManagerMonitoringStateNotMonitoring;
_maximumRangeInMeters = 100;
_maximumLocationAgeInSeconds = 10;
_shouldUseDetailedLocationUpdates = NO;
_recentLocationSubject = [RACReplaySubject subject];
__weak typeof(self) weakSelf = self;
RACSignal *signal = [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber)
{
__strong typeof (weakSelf) strongSelf = weakSelf;
[strongSelf.recentLocationSubject subscribe:subscriber];
return [RACDisposable disposableWithBlock:^{
}];
}];
_locationDidUpdateSignal = signal;
}
return self;
}
- (void)setShouldUseDetailedLocationUpdates:(BOOL)detailedLocationUpdates
{
_shouldUseDetailedLocationUpdates = detailedLocationUpdates;
if (self.monitoringState == DANReactiveLocationManagerMonitoringStateConstant ||
self.monitoringState == DANReactiveLocationManagerMonitoringStateSignificantChange)
{
[self beginMonitoringLocation];
}
}
#pragma mark - Dealloc
- (void)dealloc
{
_locationManager = nil;
_recentLocationSubject = nil;
_locationDidUpdateSignal = nil;
}
#pragma mark - Public Methods
- (void)beginMonitoringLocation
{
if (self.shouldUseDetailedLocationUpdates)
{
[self beginDetailedUpdates];
}
else
{
[self beginSignificantUpdates];
}
}
- (void)stopMonitoringLocation
{
[self stopAllUpdates];
}
#pragma mark - CLLocationManagerDelegate Methods
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
if ([newLocation isOutsideRange:self.maximumRangeInMeters] || [newLocation isOlderThanAge:self.maximumLocationAgeInSeconds])
{
return;
}
[self.recentLocationSubject sendNext:newLocation];
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
self.monitoringState = DANReactiveLocationManagerMonitoringStateStoppedByError;
[self.recentLocationSubject sendError:error];
}
- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
{
if (status == kCLAuthorizationStatusAuthorized)
{
[self beginMonitoringLocation];
}
else
{
[self.recentLocationSubject sendError:[NSError errorWithDomain:kRLMErrorDomain code:0 userInfo:nil]];
[self stopAllUpdates];
self.monitoringState = DANReactiveLocationManagerMonitoringStateStoppedByError;
}
}
#pragma mark - Private methods
- (void)beginDetailedUpdates
{
[self.locationManager stopMonitoringSignificantLocationChanges];
[self.locationManager startUpdatingLocation];
self.monitoringState = DANReactiveLocationManagerMonitoringStateConstant;
}
- (void)beginSignificantUpdates
{
[self.locationManager stopUpdatingLocation];
[self.locationManager startMonitoringSignificantLocationChanges];
self.monitoringState = DANReactiveLocationManagerMonitoringStateSignificantChange;
}
- (void)stopAllUpdates
{
[self.locationManager stopUpdatingLocation];
[self.locationManager stopMonitoringSignificantLocationChanges];
self.monitoringState = DANReactiveLocationManagerMonitoringStateNotMonitoring;
}
@end