forked from rodvand/GeoEvents
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathIndexViewController.m
More file actions
336 lines (262 loc) · 9.73 KB
/
IndexViewController.m
File metadata and controls
336 lines (262 loc) · 9.73 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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
//
// IndexViewController.m
// GeoEvents
//
// Created by Martin Roedvand on 12/12/2009.
// Copyright 2009 Redwater software. All rights reserved.
//
#import "IndexViewController.h"
#import "GeoEvents_finalAppDelegate.h"
#import "EditableDetailCell.h"
@implementation IndexViewController
@synthesize searchViewViewController,
searchField,
latitude,
longitude,
locationFound,
run,
settingsViewController,
activityIndicator;
- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"GeoEvents";
GeoEvents_finalAppDelegate * appDelegate = [UIApplication sharedApplication].delegate;
appDelegate.events = nil;
//Add button to navigationBar
UIBarButtonItem * settingsBtn = [[UIBarButtonItem alloc]initWithTitle:@"Settings"
style:UIBarButtonItemStyleBordered target:self action:@selector(goToSettings:)];
[self.navigationItem setRightBarButtonItem:settingsBtn animated:NO];
[settingsBtn release];
self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
run = 0;
locationController = [[MyCLController alloc] init];
locationController.delegate = self;
[locationController.locationManager startUpdatingLocation];
}
- (void)goToSettings:(id)sender {
if(settingsViewController == nil) {
SettingsViewController * settings = [[SettingsViewController alloc] initWithStyle:UITableViewStyleGrouped];
self.settingsViewController = settings;
[settings release];
}
[self.navigationController pushViewController:self.settingsViewController animated:YES];
}
- (void)dealloc {
[super dealloc];
}
- (void)locationUpdate:(CLLocation *)location {
bool simulator = YES;
GeoEvents_finalAppDelegate * appDelegate = [UIApplication sharedApplication].delegate;
latitude = [NSNumber numberWithDouble:[location coordinate].latitude];
longitude = [NSNumber numberWithDouble:[location coordinate].longitude];
appDelegate.lat = latitude;
appDelegate.lon = longitude;
if(run == 5 || simulator && !locationFound) {
locationFound = YES;
NSArray * indexPathArray = [NSArray arrayWithObject:[NSIndexPath indexPathForRow:1 inSection:0]];
[self.tableView insertRowsAtIndexPaths:indexPathArray withRowAnimation:UITableViewRowAnimationBottom];
}
/*
NSLog(@"Location: %f", [location coordinate].latitude);
NSLog(@"Location: %f", [longitude doubleValue]);
*/
[locationController.locationManager stopUpdatingLocation];
}
- (void)locationError:(NSError *)error {
}
- (void)search:(NSString *)searchText addToSearchHistory:(bool)addToSearch {
GeoEvents_finalAppDelegate * appDelegate = [UIApplication sharedApplication].delegate;
NSMutableArray * theSearchHistory = appDelegate.searchHistory;
//We now have a search history
appDelegate.searchSuggestions = NO;
if(addToSearch && searchText != nil) {
[theSearchHistory addObject:searchText];
}
appDelegate.searchString = searchText;
[self loadSearchView:NO];
}
- (void)viewWillAppear:(BOOL)animated {
[activityIndicator stopAnimating];
[self.tableView reloadData];
}
- (void)searchByGps {
[self loadSearchView:YES];
}
-(void)loadSearchView:(bool)isUsingGps {
GeoEvents_finalAppDelegate * appDelegate = [UIApplication sharedApplication].delegate;
appDelegate.isUsingGps = isUsingGps;
SearchViewController * searchView = [[SearchViewController alloc] initWithStyle:UITableViewStylePlain];
self.searchViewViewController = searchView;
[self.navigationController pushViewController:searchViewViewController animated:YES];
[searchView release];
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
#pragma mark Table view methods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return NUM_SECTIONS;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
GeoEvents_finalAppDelegate * appDelegate = [UIApplication sharedApplication].delegate;
NSMutableArray * theSearchHistory = appDelegate.searchHistory;
switch(section) {
case searchSection:
if(locationFound) {
return NUM_HEADER_SECTION_ROWS;
} else {
return 1;
}
case historySection:
if(theSearchHistory != nil) {
if([theSearchHistory count] > 3) {
return 3;
} else {
return [theSearchHistory count];
}
}
//case settingSection:
// return 1;
default:
return 1;
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
static NSString *CellSearch = @"searchHistory";
static NSString *CellGPS = @"GPS";
//static NSString *CellSetting = @"Settings";
if(indexPath.section == searchSection) {
switch(indexPath.row) {
// Our cell where we fill in text and search
case searchSectionSearchRow:
;
EditableDetailCell *sectionSearchCell = [[[EditableDetailCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
searchField = [sectionSearchCell textField];
[searchField setClearButtonMode:UITextFieldViewModeNever];
[searchField setReturnKeyType:UIReturnKeySearch];
[searchField setAutocorrectionType:UITextAutocorrectionTypeNo];
[searchField setPlaceholder:@"City, country"];
searchField.delegate = self;
return sectionSearchCell;
// Our cell where we search using the GPS information
case searchSectionSearchByGpsRow:
;
UITableViewCell *gpsSearchCell = [tableView dequeueReusableCellWithIdentifier:CellGPS];
if (gpsSearchCell == nil) {
gpsSearchCell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellGPS] autorelease];
}
if(locationFound) {
gpsSearchCell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
[gpsSearchCell.textLabel setText:@"Search using GPS"];
} else {
[gpsSearchCell.textLabel setText:@"Aquiring GPS data"];
}
return gpsSearchCell;
default:
NSAssert(NO, @"Unhandled value in searchSection cellForRowAtIndexPath");
}
}
if(indexPath.section == historySection) {
// Get appDelegate and the array where we keep our search history
GeoEvents_finalAppDelegate * appDelegate = [UIApplication sharedApplication].delegate;
NSMutableArray * theSearchHistory = appDelegate.searchHistory;
UITableViewCell * searchHistoryCell = [tableView dequeueReusableCellWithIdentifier:CellSearch];
if (searchHistoryCell == nil) {
searchHistoryCell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
searchHistoryCell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
[searchHistoryCell.textLabel setText:[[theSearchHistory objectAtIndex:[theSearchHistory count] - indexPath.row - 1]capitalizedString]];
return searchHistoryCell;
}
/*
if (indexPath.section == settingSection) {
Get the AppDelegate and get our global setting for range.
GeoEvents_finalAppDelegate * appDelegate = [UIApplication sharedApplication].delegate;
NSNumber * rangeSettings = appDelegate.range;
UITableViewCell * rangeSettingCell = [tableView dequeueReusableCellWithIdentifier:CellSetting];
if(rangeSettingCell == nil) {
rangeSettingCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellSetting];
}
rangeSettingCell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
[rangeSettingCell.textLabel setText:@"Search range"];
NSMutableString * rangeText;
if(rangeSettings == nil) {
rangeText = [NSMutableString stringWithString:@"Not defined"];
} else {
rangeText = [NSMutableString stringWithString:[rangeSettings stringValue]];
[rangeText appendString:@" km"];
}
[rangeSettingCell.detailTextLabel setText:rangeText];
return rangeSettingCell;
}
*/
return nil;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
GeoEvents_finalAppDelegate * appDelegate = [UIApplication sharedApplication].delegate;
NSMutableArray * theSearchHistory = appDelegate.searchHistory;
switch(section) {
case searchSection:
return @"Search";
case historySection:
if([theSearchHistory count] > 0) {
if(appDelegate.searchSuggestions) {
return @"Search suggestions";
} else {
return @"Search history";
}
}
default:
return nil;
}
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if(indexPath.section == searchSection) {
switch (indexPath.row) {
case searchSectionSearchRow:
;
break;
case searchSectionSearchByGpsRow:
if(locationFound) {
[self searchByGps];
}
break;
}
}
/*
If we have anything in our searchHistory section, we allow the user to
select one and send him to the search
*/
if(indexPath.section == historySection) {
[activityIndicator startAnimating];
GeoEvents_finalAppDelegate * appDelegate = [UIApplication sharedApplication].delegate;
NSMutableArray * theSearchHistory = appDelegate.searchHistory;
[self search:[theSearchHistory objectAtIndex:[theSearchHistory count] - indexPath.row - 1] addToSearchHistory:NO];
}
}
#pragma mark UITextField
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
if([textField text].length == 0) {
UIAlertView *charAlert = [[UIAlertView alloc]
initWithTitle:@"Empty search"
message:@"Your search is empty."
delegate:nil
cancelButtonTitle:@"OK, I'll fix that"
otherButtonTitles:nil];
[charAlert show];
[charAlert release];
} else {
[self search:[textField text] addToSearchHistory:YES];
}
return NO;
}
@end