-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLocation.m
242 lines (201 loc) · 8 KB
/
Location.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
//
// Location.m
// AvocadoTest1
//
// Created by Christophe Biocca on 12-01-27.
// Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//
#import "Location.h"
#import "OpeningHours.h"
@implementation Location
@synthesize primaryKey;
@synthesize address;
static NSDateComponents* oneDay;
static NSDateComponents* minusOneDay;
+(void)initialize{
if(!oneDay){
oneDay = [[NSDateComponents alloc] init];
[oneDay setDay:1];
}
if(!minusOneDay){
minusOneDay = [[NSDateComponents alloc] init];
[minusOneDay setDay:-1];
}
}
-(id)initFromData:(NSDictionary*)inputData{
if(self = [super init]){
primaryKey = [inputData objectForKey:@"pk"];
NSDictionary* hoursDict = [inputData objectForKey:@"hours"];
latitude = [inputData objectForKey:@"latitude"];
longitude = [inputData objectForKey:@"longitude"];
address = [inputData objectForKey:@"address"];
CLLog(LOG_LEVEL_INFO, [NSString stringWithFormat:@"LOCATION: %@", inputData]);
NSMutableArray* hours = [NSMutableArray arrayWithCapacity:7];
[hoursDict enumerateKeysAndObjectsUsingBlock:^(NSString* key, NSDictionary* obj, BOOL *stop) {
[hours addObject:[[OpeningHours alloc] initWithData:obj day:key]];
}];
[hours sortUsingDescriptors:[NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"day" ascending:YES]]];
storeHours = hours;
lastCall = 900;
}
return self;
}
- (Location *)initWithCoder:(NSCoder *)decoder
{
if (self = [super initWithCoder:decoder])
{
lastCall = [[decoder decodeObjectForKey:@"lastCall"] intValue];
}
return self;
}
- (void)encodeWithCoder:(NSCoder *)encoder
{
[super encodeWithCoder:encoder];
[encoder encodeObject:[NSNumber numberWithInt:lastCall] forKey:@"lastCall"];
}
+(NSDate*)mergeComponents:(NSDateComponents*)components withDate:(NSDate*)date{
NSDateComponents* dateComponents = [[NSCalendar currentCalendar]
components:
NSEraCalendarUnit |
NSYearCalendarUnit |
NSMonthCalendarUnit |
NSDayCalendarUnit
fromDate:date];
[dateComponents setHour:[components hour]];
[dateComponents setMinute:[components minute]];
[dateComponents setSecond:[components second]];
return [[NSCalendar currentCalendar] dateFromComponents:dateComponents];
}
-(OpeningHours*)hoursForDay:(NSDate*)day{
NSDateComponents* components = [[NSCalendar currentCalendar] components:NSWeekdayCalendarUnit fromDate:day];
return [storeHours objectAtIndex:[components weekday] - [[NSCalendar currentCalendar] firstWeekday]];
}
-(NSDate*)nextOpenAfterThisTime:(NSDate*)date
{
NSDate *nextOpen = [Location mergeComponents:[[self hoursForDay:date] opens] withDate:date];
NSDate *dayIterator = date;
while ([date compare:nextOpen] != NSOrderedAscending)
{
dayIterator = [[NSCalendar currentCalendar] dateByAddingComponents:oneDay
toDate:dayIterator
options:0];
nextOpen = [Location mergeComponents:[[self hoursForDay:date] opens] withDate:dayIterator];
}
return nextOpen;
}
-(NSDate*)nextClosedAfterThisTime:(NSDate*)date
{
NSDate *nextClosed = [Location mergeComponents:[[self hoursForDay:date] closes] withDate:date];
NSDate *dayIterator = date;
while ([date compare:nextClosed] != NSOrderedAscending)
{
dayIterator = [[NSCalendar currentCalendar] dateByAddingComponents:oneDay
toDate:dayIterator
options:0];
nextClosed = [Location mergeComponents:[[self hoursForDay:date] closes] withDate:dayIterator];
}
return nextClosed;
}
-(NSDate*)opensToday{
NSDate* now = [NSDate date];
NSDate* yesterday = [[NSCalendar currentCalendar] dateByAddingComponents:minusOneDay
toDate:now
options:0];
NSDate* yesterdayClose = [self nextClosedAfterThisTime:yesterday];
if([yesterdayClose compare:now] == NSOrderedAscending){
return [self nextOpenAfterThisTime:now];
} else {
return [self nextOpenAfterThisTime:yesterday];
}
}
-(NSDate*)closesToday{
NSDate* now = [NSDate date];
NSDate* yesterday = [[NSCalendar currentCalendar] dateByAddingComponents:minusOneDay
toDate:now
options:0];
NSDate* yesterdayClose = [self nextClosedAfterThisTime:yesterday];
if([yesterdayClose compare:now] == NSOrderedAscending){
return [self nextClosedAfterThisTime:now];
} else {
return yesterdayClose;
}
}
-(StoreState)storeState{
NSDate* now = [NSDate date];
NSDate* close = [self nextClosedAfterThisTime:now];
NSDate* open = [self nextOpenAfterThisTime:now];
if([close timeIntervalSinceDate:now] < lastCall){
return Closing;
}
if([close compare:open] == NSOrderedAscending)
{
return Open;
}
return Closed;
}
-(NSDate*)nextOpen{
NSDate* now = [NSDate date];
NSDate* todayOpen = [self opensToday];
if([now compare:todayOpen] != NSOrderedDescending){
return todayOpen;
} else {
return [self nextOpenAfterThisTime:[[NSCalendar currentCalendar] dateByAddingComponents:oneDay
toDate:now
options:0]];
}
}
-(NSDate*)nextClose{
NSDate* now = [NSDate date];
NSDate* todayOpen = [self opensToday];
if([now compare:todayOpen] != NSOrderedDescending){
return [self closesToday];
} else {
return [self nextClosedAfterThisTime:[[NSCalendar currentCalendar] dateByAddingComponents:oneDay
toDate:now
options:0]];
}
}
-(NSString *)storeHourBlurb
{
NSDateFormatter* formatter = [NSDateFormatter new];
[formatter setDateStyle:NSDateFormatterNoStyle];
[formatter setTimeStyle:NSDateFormatterShortStyle];
if([self storeState] != Closed)
{
NSString *openTime = [formatter stringFromDate:[self opensToday]];
NSString *closeTime = [formatter stringFromDate:[self closesToday]];
return [NSString stringWithFormat:@"Open from %@ to %@",openTime,closeTime];
}
else
{
NSString *openTime = [formatter stringFromDate:[self nextOpen]];
//NSString *closeTime = [formatter stringFromDate:[[self currentLocation] nextClose]];
[formatter setDateFormat:@"EEEE"];
NSString *dayOfWeek = [formatter stringFromDate:[self nextOpen]];
return [NSString stringWithFormat:@"Pita Factory opens on %@ at %@.",dayOfWeek,openTime];
}
}
-(BOOL)wouldBeOpenAt:(NSDate *)thisTime
{
NSDate *helperOpen = [self nextOpenAfterThisTime:thisTime];
NSDate *helperClosed = [self nextClosedAfterThisTime:thisTime];
if ([helperClosed compare:helperOpen] == NSOrderedAscending) {
return YES;
}
return NO;
}
- (NSString *)subtitle{
return [NSString stringWithFormat:@"%@\n%@", address, [self storeHourBlurb]];
}
- (NSString *)title{
return @"Pita Factory";
}
-(id)copyWithZone:(NSZone *)zone
{
return self;
}
- (CLLocationCoordinate2D)coordinate
{
return CLLocationCoordinate2DMake([latitude doubleValue], [longitude doubleValue]);
}
@end