forked from intuit/LocationManager
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathINTULocationRequest.m
245 lines (219 loc) · 6.76 KB
/
INTULocationRequest.m
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
//
// INTULocationRequest.m
//
// Copyright (c) 2014-2015 Intuit Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// 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 "INTULocationRequest.h"
@interface INTULocationRequest ()
// Redeclare this property as readwrite for internal use.
@property (nonatomic, assign, readwrite) BOOL hasTimedOut;
/** The NSDate representing the time when the request started. Set when the |timeout| property is set. */
@property (nonatomic, strong) NSDate *requestStartTime;
/** The timer that will fire to notify this request that it has timed out. Started when the |timeout| property is set. */
@property (nonatomic, strong) NSTimer *timeoutTimer;
@end
@implementation INTULocationRequest
static INTULocationRequestID _nextRequestID = 0;
/**
Returns a unique request ID (within the lifetime of the application).
*/
+ (INTULocationRequestID)getUniqueRequestID
{
_nextRequestID++;
return _nextRequestID;
}
/**
Calls the designated initializer with an autogenerated unique requestID.
*/
- (id)init
{
return [self initWithRequestID:[INTULocationRequest getUniqueRequestID]];
}
/**
Designated initializer.
Use regular init method to autogenerate a unique requestID.
*/
- (id)initWithRequestID:(INTULocationRequestID)requestID
{
self = [super init];
if (self) {
_requestID = requestID;
_hasTimedOut = NO;
}
return self;
}
/**
Returns the associated recency threshold (in seconds) for the location request's desired accuracy level.
*/
- (NSTimeInterval)updateTimeStaleThreshold
{
switch (self.desiredAccuracy) {
case INTULocationAccuracyRoom:
return kINTUUpdateTimeStaleThresholdRoom;
break;
case INTULocationAccuracyHouse:
return kINTUUpdateTimeStaleThresholdHouse;
break;
case INTULocationAccuracyBlock:
return kINTUUpdateTimeStaleThresholdBlock;
break;
case INTULocationAccuracyNeighborhood:
return kINTUUpdateTimeStaleThresholdNeighborhood;
break;
case INTULocationAccuracyCity:
return kINTUUpdateTimeStaleThresholdCity;
break;
default:
NSAssert(NO, @"Unknown desired accuracy.");
return 0.0;
break;
}
}
/**
Returns the associated horizontal accuracy threshold (in meters) for the location request's desired accuracy level.
*/
- (CLLocationAccuracy)horizontalAccuracyThreshold
{
switch (self.desiredAccuracy) {
case INTULocationAccuracyRoom:
return kINTUHorizontalAccuracyThresholdRoom;
break;
case INTULocationAccuracyHouse:
return kINTUHorizontalAccuracyThresholdHouse;
break;
case INTULocationAccuracyBlock:
return kINTUHorizontalAccuracyThresholdBlock;
break;
case INTULocationAccuracyNeighborhood:
return kINTUHorizontalAccuracyThresholdNeighborhood;
break;
case INTULocationAccuracyCity:
return kINTUHorizontalAccuracyThresholdCity;
break;
default:
NSAssert(NO, @"Unknown desired accuracy.");
return 0.0;
break;
}
}
/**
Completes the location request.
*/
- (void)complete
{
[self.timeoutTimer invalidate];
self.timeoutTimer = nil;
self.requestStartTime = nil;
}
/**
Forces the location request to consider itself timed out.
*/
- (void)forceTimeout
{
if (self.desiredAccuracy > INTULocationAccuracyNone) {
// Only one-off location requests (not subscription requests) should ever be considered timed out
self.hasTimedOut = YES;
}
}
/**
Cancels the location request.
*/
- (void)cancel
{
[self.timeoutTimer invalidate];
self.timeoutTimer = nil;
self.requestStartTime = nil;
}
/**
Starts the location request's timeout timer if a nonzero timeout value is set, and the timer has not already been started.
*/
- (void)startTimeoutTimerIfNeeded
{
if (self.timeout > 0 && !self.timeoutTimer) {
self.requestStartTime = [NSDate date];
self.timeoutTimer = [NSTimer scheduledTimerWithTimeInterval:self.timeout target:self selector:@selector(timeoutTimerFired:) userInfo:nil repeats:NO];
}
}
/**
Dynamic property that returns whether this is a subscription request (desired accuracy is INTULocationAccuracyNone).
*/
- (BOOL)isSubscription
{
return self.desiredAccuracy == INTULocationAccuracyNone;
}
/**
Dynamic property that returns how long the request has been alive (since the timeout value was set).
*/
- (NSTimeInterval)timeAlive
{
if (self.requestStartTime == nil) {
return 0.0;
}
return fabs([self.requestStartTime timeIntervalSinceNow]);
}
/**
Returns whether the location request has timed out or not.
Once this becomes YES, it will not automatically reset to NO even if a new timeout value is set.
*/
- (BOOL)hasTimedOut
{
if (self.timeout > 0.0 && self.timeAlive > self.timeout) {
_hasTimedOut = YES;
}
return _hasTimedOut;
}
/**
Callback when the timeout timer fires. Notifies the delegate that this event has occurred.
*/
- (void)timeoutTimerFired:(NSTimer *)timer
{
self.hasTimedOut = YES;
[self.delegate locationRequestDidTimeout:self];
}
/**
Two location requests are considered equal if their request IDs match.
*/
- (BOOL)isEqual:(id)object
{
if (object == self) {
return YES;
}
if (!object || ![object isKindOfClass:[self class]]) {
return NO;
}
if (((INTULocationRequest *)object).requestID == self.requestID) {
return YES;
}
return NO;
}
/**
Return a hash based on the string representation of the request ID.
*/
- (NSUInteger)hash
{
return [[NSString stringWithFormat:@"%ld", (long) self.requestID] hash];
}
- (void)dealloc
{
[_timeoutTimer invalidate];
}
@end