-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainController.m
More file actions
259 lines (187 loc) · 6.18 KB
/
MainController.m
File metadata and controls
259 lines (187 loc) · 6.18 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
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
//
// MainController.m
// LocationWatcher
//
// Created by EMM on 24/07/16.
// Copyright © 2016 EMM. All rights reserved.
//
#import "MainController.h"
#import "AudioPlayer.h"
#import "LocationManager.h"
#import "Location.h"
#import "Storage.h"
@interface MainController()
@property AudioPlayer *audioPlayer;
@property LocationManager *locationManager;
@property NSMutableArray *userDefinedLocations;
@property CLLocation *currentUserLocation;
@property (nonatomic) Location *currentCellLocation;
@property NSInteger currentIndex;
@property BOOL editFlag;
@property Storage *storage;
@property BOOL playFlag;
@end
@implementation MainController
+ (MainController *)sharedInstance {
static MainController *instance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^ {
instance = [[self alloc]init];
});
return instance;
}
- (instancetype)init
{
self = [super init];
if (self) {
_currentCellLocation = [Location alloc];
_audioPlayer = [AudioPlayer sharedInstance];
_userDefinedLocations = [[NSMutableArray alloc]initWithCapacity:100];
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(didIReachAnyLocation:) name:@"userLocationUpdater" object:@"my object"];
_storage = [[Storage alloc]init];
_locationManager = [LocationManager sharedInstance];
_playFlag = false;
[self loadData];
}
return self;
}
-(NSInteger) numberOfCheckPoints{
return [_userDefinedLocations count];
}
-(BOOL) checkLocation:(CLLocation *) definedLocation :withCurrentLocation : (CLLocation *) currentLocation : ofRadius : (double )radius{
double distance = [currentLocation distanceFromLocation:definedLocation]/1000;
if(distance > radius)
return true;
else
return false;
}
-(Location *) getACheckPointAtIndex :(NSInteger)index{
return [_userDefinedLocations objectAtIndex:index];
}
-(BOOL) didIReachLocation:(Location *) location{
CLLocation *locationOfThePoint = [location getLocation];
if(_currentUserLocation == nil || locationOfThePoint == nil)
return false;
double distanceBetweenTwoPoints = [locationOfThePoint distanceFromLocation:_currentUserLocation]/1000;
NSLog(@" lop %f",locationOfThePoint.coordinate.latitude);
NSLog(@" lop %f",_currentUserLocation.coordinate.latitude);
double radius = [location getRadius];
if(distanceBetweenTwoPoints <= radius){
NSLog(@"reached Location");
return true;
}
return false;
}
-(void) didIReachAnyLocation:(NSNotification *) notification{
NSUInteger count = 0;
_playFlag = false;
if([_userDefinedLocations count] <= 0){
[self stopAudio];
return;
}
_currentUserLocation = [notification userInfo][@"location"];
for(Location *location in _userDefinedLocations){
if([self didIReachLocation:location]){
if([location isEnabled] ){
if(![location isPlaying]) {
[location setIsPlaying:true];
[self reachedLocation:count];
}
_playFlag = true;
}
}
count++;
}
if(_playFlag)
[self playAudio];
else
[self stopAudio];
}
-(void) didIReachAnyLocation{
NSUInteger count = 0;
_playFlag = false;
if([_userDefinedLocations count] <= 0){
[self stopAudio];
return;
}
for(Location *location in _userDefinedLocations){
if([self didIReachLocation:location]){
if([location isEnabled] ){
if(![location isPlaying]){
[self reachedLocation:count];
[location setIsPlaying:true];
}
_playFlag = true;
}
}
count++;
}
if(_playFlag)
[self playAudio];
else
[self stopAudio];
}
-(void) reachedLocation:(NSUInteger )index{
//[self playAudio];
NSLog(@"%lu",(unsigned long)index);
NSLog(@"%@",[[self getACheckPointAtIndex:index] getMessage]);
[[NSNotificationCenter defaultCenter]postNotificationName:@"Eezy" object:@"my object" userInfo:@{@"message": [[self getACheckPointAtIndex:index] getMessage], @"title": @"Reached a CheckPoint"}];
}
-(BOOL ) isPlaying{
return [_audioPlayer isPlaying];
}
-(void) stopAudio{
[_audioPlayer stopAudio];
}
-(void) playAudio{
[_audioPlayer playAudio];
}
-(void) loadData{
if( [_storage getData] != nil)
_userDefinedLocations = [_storage getData];
NSLog(@"%@",[_storage getData]);
}
-(void) saveData{
[_storage saveData:_userDefinedLocations];
}
-(Location *) getCurrentCellLocation{
return _currentCellLocation;
}
-(void) setCurrentCellLocation :(Location *) currentCellLocation :(NSInteger )currentIndex{
_currentCellLocation = currentCellLocation;
_currentIndex = currentIndex;
}
-(BOOL) addLocation:(Location *)newLocation{
if(_userDefinedLocations.count >= 100)
return false;
[_userDefinedLocations addObject:newLocation];
return true;
}
-(void) removeLocation:(NSUInteger )index{
if([[_userDefinedLocations objectAtIndex:index] isPlaying])
[[_userDefinedLocations objectAtIndex:index] setIsPlaying:false];
[_userDefinedLocations removeObjectAtIndex:index];
NSLog(@"%lu",(unsigned long)[_userDefinedLocations count]);
[self saveData];
[self didIReachAnyLocation];
}
-(void) updateLocation:(NSUInteger )oldIndex :(Location *)newLocation{
if([[_userDefinedLocations objectAtIndex:oldIndex] isPlaying])
[newLocation setIsPlaying:true];
else
[newLocation setIsPlaying:false];
[_userDefinedLocations removeObjectAtIndex:oldIndex];
[_userDefinedLocations insertObject:newLocation atIndex:oldIndex];
//[newLocation setisEnabled:true];
[self saveData];
}
-(NSInteger )getCurrentIndex{
return _currentIndex;
}
-(void) setEditflag:(BOOL) flag{
_editFlag = flag;
}
-(BOOL) getEditFlag {
return _editFlag;
}
@end