-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkViewController.swift
More file actions
199 lines (166 loc) · 5.75 KB
/
LinkViewController.swift
File metadata and controls
199 lines (166 loc) · 5.75 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
//
// LinkViewController.swift
// OnTheMap
//
// Created by Darren Leith on 16/02/2016.
// Copyright © 2016 Darren Leith. All rights reserved.
//
import UIKit
import MapKit
class LinkViewController: UIViewController {
//MARK: - outlets
@IBOutlet weak var mapView: MKMapView!
@IBOutlet weak var linkTextField: UITextField!
@IBOutlet weak var activityIndicator: UIActivityIndicatorView!
//MARK: - properties
var currentStudent: StudentInformation?
var ParseSharedInstance = ParseClient.sharedInstance
//MARK: - lifecycle methods
override func viewDidLoad() {
super.viewDidLoad()
linkTextField.delegate = self
currentStudent = ParseClient.sharedInstance.currentStudent
activityIndicator.hidesWhenStopped = true
setUpActivityIndicator()
addAnnotationsToMap()
}
//MARK: - add annotations to the map
func addAnnotationsToMap() {
performUIUpdatesOnMain { () -> Void in
if let student = self.currentStudent, lon = student.longitude, lat = student.latitude {
let lat = CLLocationDegrees(Double((lat)))
let long = CLLocationDegrees(Double((lon)))
let coordinate = CLLocationCoordinate2D(latitude: lat, longitude: long)
let annotation = MKPointAnnotation()
annotation.coordinate = coordinate
self.mapView.addAnnotation(annotation)
let camera = MKMapCamera(lookingAtCenterCoordinate: coordinate, fromEyeCoordinate: coordinate, eyeAltitude: 10000.0)
self.mapView.setCamera(camera, animated: true)
} else {
//cant get a reference to the student
self.showAlert("Oops.!", message: "Unable to parse student data")
}
}
}
//MARK: - submit location and URL
@IBAction func submitLocation(sender: AnyObject) {
self.activityIndicator.startAnimating()
if let urlString = linkTextField.text {
if confirmURL(urlString) {
ParseSharedInstance.currentStudent?.mediaURL = "\(urlString)"
if let overwrite = ParseSharedInstance.onTheMap { //has the student already posted
if overwrite {
self.overwriteLocation()
} else if overwrite == false {
self.submitNewLocation()
}
}
} else {
self.showAlert("Oops..!", message:"Thats not a valid https link")
}
} else {
self.showAlert("Whoops!", message:"Please enter a link")
}
}
//MARK: - submit a new location
func submitNewLocation() {
activityIndicator.startAnimating()
ParseSharedInstance.postStudentLocation(ParseSharedInstance.currentStudent) { (completed, errorString) in
if completed == true {
self.activityIndicator.stopAnimating()
self.presentingViewController?.dismissViewControllerAnimated(true, completion: nil)
} else {
if let errorString = errorString {
self.showAlert("Error", message: errorString)
}else {
self.showAlert("Error", message:"Unable to sumbit the location")
}
}
}
}
//MARK: - overwrite location
func overwriteLocation() {
ParseSharedInstance.overwriteStudent(ParseSharedInstance.currentStudent) { (completed, errorString) in
if completed == true {
self.activityIndicator.stopAnimating()
self.presentingViewController?.dismissViewControllerAnimated(true, completion: nil)
} else {
if let errorString = errorString {
self.showAlert("Error", message: errorString)
}else {
self.showAlert("Error", message:"Unable to sumbit student data")
}
}
}
}
//MARK: - cancel
@IBAction func cancelButtonPressed(sender: AnyObject) {
dismissViewControllerAnimated(true, completion: nil)
}
//MARK: - helper methods
//verify that the URL is of the correct syntax
func confirmURL(urlString: String?) ->Bool {
if let urlString = urlString {
let pattern = "^(https?:\\/\\/)([a-zA-Z0-9_\\-~]+\\.)+[a-zA-Z0-9_\\-~\\/\\.]+$"
if let _ = urlString.rangeOfString(pattern, options: .RegularExpressionSearch){
if let url = NSURL(string: urlString) {
if UIApplication.sharedApplication().canOpenURL(url) {
return true
}
}
}
}
return false
}
//show an alert controller
func showAlert(title: String? , message: String?) {
performUIUpdatesOnMain { () -> Void in
self.activityIndicator.stopAnimating()
if title != nil && message != nil {
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)
}
}
}
//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
}
}
//MARK: - Textfield delegate
extension LinkViewController: UITextFieldDelegate {
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
if linkTextField.isFirstResponder() {
linkTextField.resignFirstResponder()
}
}
func textFieldShouldReturn(textField: UITextField) -> Bool {
if linkTextField.isFirstResponder() && linkTextField.text!.isEmpty == false {
linkTextField.resignFirstResponder()
}
return false
}
}
// MARK: - MKMapViewDelegate
extension LinkViewController: MKMapViewDelegate {
func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
let reuseId = "pin"
var pinView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId) as? MKPinAnnotationView
if pinView == nil {
pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
pinView!.pinTintColor = UIColor.redColor()
}
else { pinView!.annotation = annotation }
return pinView
}
}