-
Notifications
You must be signed in to change notification settings - Fork 0
/
LocationViewController.swift
202 lines (163 loc) · 6.29 KB
/
LocationViewController.swift
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
//
// LocationViewController.swift
// OnTheMap
//
// Created by Darren Leith on 16/02/2016.
// Copyright © 2016 Darren Leith. All rights reserved.
//
import UIKit
import MapKit
class LocationViewController: UIViewController {
//MARK: notifications enum
enum Notifications {
static let keyboardWillShow = "UIKeyboardWillShowNotification"
static let keyboardWillHide = "UIKeyboardWillHideNotification"
}
//MARK: - outlets
@IBOutlet weak var activityIndicator: UIActivityIndicatorView!
@IBOutlet weak var locationTextField: UITextField!
var ParseSharedInstance = ParseClient.sharedInstance
var keyboardUp: Bool = false
//MARK: - lifecycle methods
override func viewDidLoad() {
super.viewDidLoad()
locationTextField.delegate = self
setUpActivityIndicator()
activityIndicator.hidesWhenStopped = true
}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
//subscribe to keyboard notifications
subscribeToKeyboardWillShowNotification()
subscribeToKeyboardWillHideNotification()
}
override func viewWillDisappear(animated: Bool) {
super.viewWillDisappear(animated)
//unsubscribe to keyboard notifications
unsubscribeFromKeyboardWillShowNotification()
unsubscribeFromKeyboardWillHideNotification()
}
//MARK: - cancelled
@IBAction func didCancel(sender: AnyObject) {
self.dismissViewControllerAnimated(true, completion: nil)
}
//MARK: - submitted a location
@IBAction func didSubmitLocation(sender: AnyObject) {
//error validation: textfield is empty
if locationTextField.text!.isEmpty {
showAlert("Whoops!", message: "Please enter a location")
return
}
self.activityIndicator.startAnimating()
let geocoder = CLGeocoder()
geocoder.geocodeAddressString(locationTextField.text!) {
placemark, error in
if let _ = error {
self.showAlert("Whoops!", message: "Unable to find that address")
return
}
if let placemark = placemark{
if placemark.count > 0 {
let placemark = placemark.first!
if let country = placemark.country, state = placemark.administrativeArea {
if let city = placemark.locality {
self.ParseSharedInstance.currentStudent?.mapString = "\(city), \(state), \(country)"
self.ParseSharedInstance.currentStudent?.latitude = Float(placemark.location!.coordinate.latitude)
self.ParseSharedInstance.currentStudent?.longitude = Float(placemark.location!.coordinate.longitude)
self.presentViewWith(self.ParseSharedInstance.currentStudent?.mapString,lat: self.ParseSharedInstance.currentStudent?.latitude,lon: self.ParseSharedInstance.currentStudent?.longitude)
self.stopActivityIndicator()
} else {
self.ParseSharedInstance.currentStudent?.mapString = "\(state), \(country)"
self.ParseSharedInstance.currentStudent?.latitude = Float(placemark.location!.coordinate.latitude)
self.ParseSharedInstance.currentStudent?.longitude = Float(placemark.location!.coordinate.longitude)
self.presentViewWith(self.ParseSharedInstance.currentStudent?.mapString,lat: nil,lon: nil)
self.stopActivityIndicator()
}
} else {
self.showAlert("Whoops!", message:"Please choose a more specific location")
}
} else {
self.showAlert("Whoops!", message:"Unable to find that location")
}
} else {
self.showAlert("Whoops!", message: "Unable to find that location")
}
}
}
//MARK: - Helper Methods
func stopActivityIndicator() {
performUIUpdatesOnMain { () -> Void in
self.activityIndicator.stopAnimating()
}
}
//Shows an alert
func showAlert(title: String? , message: String?) {
performUIUpdatesOnMain { () -> Void in
self.activityIndicator.stopAnimating()
let errorAlert =
UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.Alert)
errorAlert.addAction(UIAlertAction(title: "Try Again", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(errorAlert, animated: true, completion: nil)
}
}
//present LinkViewController
func presentViewWith(mapString: String?, lat: Float?, lon: Float? ) {
performUIUpdatesOnMain { () -> Void in
if mapString != nil {
self.performSegueWithIdentifier("presentLinkViewController", sender: self)
}
}
}
//MARK: - keyboard movements
func keyboardWillAppear(notification: NSNotification){
if !keyboardUp {
view.frame.origin.y -= getKeyboardHeight(notification)
keyboardUp = true
}
}
func keyboardWillHide(notification: NSNotification){
//Move view back into position
view.frame.origin.y += getKeyboardHeight(notification)
keyboardUp = false
}
//get half the keyboard height
func getKeyboardHeight(notification: NSNotification) -> CGFloat {
let userInfo = notification.userInfo
let keyboardSize = userInfo![UIKeyboardFrameEndUserInfoKey] as! NSValue //of CGRect
return (keyboardSize.CGRectValue().height / 2)
}
//run on main thread
func performUIUpdatesOnMain(updates: () -> Void) {
dispatch_async(dispatch_get_main_queue()) {
updates()
}
}
//initialize the activity indicator
func setUpActivityIndicator() {
activityIndicator.backgroundColor = UIColor(white: 0.3, alpha: 0.8)
activityIndicator.hidesWhenStopped = true
activityIndicator.activityIndicatorViewStyle = .WhiteLarge
}
//subscribe/unsubscribe to keyboard notifications
func subscribeToKeyboardWillShowNotification() {
NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillAppear:", name: Notifications.keyboardWillShow, object: nil)
}
func subscribeToKeyboardWillHideNotification() {
NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillHide:", name: Notifications.keyboardWillHide, object: nil)
}
func unsubscribeFromKeyboardWillShowNotification() {
NSNotificationCenter.defaultCenter().removeObserver(self, name: UIKeyboardWillShowNotification, object: nil)
}
func unsubscribeFromKeyboardWillHideNotification() {
NSNotificationCenter.defaultCenter().removeObserver(self, name: UIKeyboardWillHideNotification, object: nil)
}
}
//MARK: - text field delegate
extension LocationViewController: UITextFieldDelegate {
func textFieldShouldReturn(textField: UITextField) -> Bool {
if locationTextField.isFirstResponder() && locationTextField.text!.isEmpty == false {
locationTextField.resignFirstResponder()
}
return false
}
}