-
Notifications
You must be signed in to change notification settings - Fork 375
/
SOLAddLocationViewController.m
195 lines (155 loc) · 7.23 KB
/
SOLAddLocationViewController.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
//
// SOLAddLocationViewController.m
// Sol
//
// Created by Comyar Zaheri on 9/20/13.
// Copyright (c) 2013 Comyar Zaheri. All rights reserved.
//
#import "SOLAddLocationViewController.h"
#pragma mark - SOLAddLocationViewController Class Extension
@interface SOLAddLocationViewController ()
// Used to geocode search location
@property (strong, nonatomic) CLGeocoder *geocoder;
// Results of a search
@property (strong, nonatomic) NSMutableArray *searchResults;
// Location search results display controller
@property (strong, nonatomic) UISearchDisplayController *searchController;
// -----
// @name Subviews
// -----
// Location search bar
@property (strong, nonatomic) UISearchBar *searchBar;
// Navigation bar at the top of the view
@property (strong, nonatomic) UINavigationBar *navigationBar;
// Done button inside navigation bar
@property (strong, nonatomic) UIBarButtonItem *doneButton;
@end
#pragma mark - SOLAddLocationViewController Implementation
@implementation SOLAddLocationViewController
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
self.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
self.view.backgroundColor = [UIColor clearColor];
self.view.opaque = NO;
self.geocoder = [[CLGeocoder alloc]init];
self.searchResults = [[NSMutableArray alloc]initWithCapacity:5];
self.navigationBar =[[UINavigationBar alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 64)];
[self.view addSubview:self.navigationBar];
self.doneButton = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemDone
target:self
action:@selector(doneButtonPressed)];
// Inititalize and configure search bar
self.searchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(0, 20, self.view.bounds.size.width, 44)];
self.searchBar.autocorrectionType = UITextAutocorrectionTypeNo;
self.searchBar.placeholder = @"Name of City";
self.searchBar.delegate = self;
// Initialize and configure search controller
self.searchController = [[UISearchDisplayController alloc]initWithSearchBar:self.searchBar contentsController:self];
self.searchController.delegate = self;
self.searchController.searchResultsDelegate = self;
self.searchController.searchResultsDataSource = self;
self.searchController.searchResultsTitle = @"Add Location";
self.searchController.displaysSearchBarInNavigationBar = YES;
self.searchController.navigationItem.rightBarButtonItems = @[self.doneButton];
self.navigationBar.items = @[self.searchController.navigationItem];
}
return self;
}
#pragma mark UIViewController Methods
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[[UIApplication sharedApplication]setStatusBarStyle:UIStatusBarStyleDefault animated:NO];
[self.searchController setActive:YES animated:NO];
[self.searchController.searchBar becomeFirstResponder];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
[[UIApplication sharedApplication]setStatusBarStyle:UIStatusBarStyleLightContent animated:NO];
[self.searchController setActive:NO animated:NO];
[self.searchController.searchBar resignFirstResponder];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[self.delegate dismissAddLocationViewController];
}
#pragma mark DoneButton Methods
- (void)doneButtonPressed
{
[self.delegate dismissAddLocationViewController];
}
#pragma mark UISearchDisplayControllerDelegate Methods
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
[self.geocoder geocodeAddressString:searchString completionHandler: ^ (NSArray *placemarks, NSError *error) {
self.searchResults = [[NSMutableArray alloc]initWithCapacity:1];
for(CLPlacemark *placemark in placemarks) {
if(placemark.locality) {
[self.searchResults addObject:placemark];
}
}
[controller.searchResultsTableView reloadData];
}];
return NO;
}
- (void)searchDisplayController:(UISearchDisplayController *)controller willShowSearchResultsTableView:(UITableView *)tableView
{
[tableView setFrame:CGRectMake(0, CGRectGetHeight(self.navigationBar.bounds), CGRectGetWidth(self.view.bounds),
CGRectGetHeight(self.view.bounds) - CGRectGetHeight(self.navigationBar.bounds))];
tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
tableView.backgroundColor = [UIColor clearColor];
tableView.delegate = self;
[self.view bringSubviewToFront:self.navigationBar];
}
#pragma mark UITableViewDelegate Methods
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @"CellIdentifier";
// Dequeue cell
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if(!cell) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
// Configure cell for the search results table view
if(tableView == self.searchController.searchResultsTableView) {
cell.backgroundColor = [UIColor clearColor];
cell.textLabel.textColor = [UIColor whiteColor];
CLPlacemark *placemark = [self.searchResults objectAtIndex:indexPath.row];
NSString *city = placemark.locality;
NSString *country = placemark.country;
NSString *cellText = [NSString stringWithFormat:@"%@, %@", city, country];
if([[country lowercaseString] isEqualToString:@"united states"]) {
NSString *state = placemark.administrativeArea;
cellText = [NSString stringWithFormat:@"%@, %@", city, state];
}
cell.textLabel.text = cellText;
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if(tableView == self.searchController.searchResultsTableView) {
[tableView cellForRowAtIndexPath:indexPath].selected = NO;
CLPlacemark *placemark = [self.searchResults objectAtIndex:indexPath.row];
[self.delegate didAddLocationWithPlacemark:placemark];
[self.delegate dismissAddLocationViewController];
}
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.searchResults count];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
#pragma mark UIScrollViewDelegate Methods
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
self.navigationBar.frame = CGRectMake(self.view.bounds.origin.x, self.view.bounds.origin.y,
CGRectGetWidth(self.navigationBar.frame),
CGRectGetHeight(self.navigationBar.frame));
}
@end