-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCreateVenueViewController.swift
More file actions
295 lines (240 loc) · 11.2 KB
/
CreateVenueViewController.swift
File metadata and controls
295 lines (240 loc) · 11.2 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
//
// CreateVenueViewController.swift
// Bourbon-iOS
//
// Created by Alyssa Torres on 5/3/17.
// Copyright © 2017 Ourglass. All rights reserved.
//
import UIKit
import CoreLocation
import PKHUD
import SwiftyJSON
class CreateVenueViewController: UITableViewController {
let curLocStr = "Current location"
let locationManager = CLLocationManager()
var currentLocation: CLLocation?
@IBOutlet weak var yelpSearchTerm: UITextField!
@IBOutlet weak var yelpSearchLocation: UITextField!
@IBOutlet weak var useCurrentLocationButton: UIButton! {
didSet {
useCurrentLocationButton.isEnabled = false
}
}
@IBOutlet weak var findButton: UIButton!
@IBOutlet weak var createVenueButton: UIButton!
@IBOutlet weak var createVenueActivityIndicator: UIActivityIndicatorView!
@IBOutlet weak var venueName: UITextField!
@IBOutlet weak var address1: UITextField!
@IBOutlet weak var address2: UITextField!
@IBOutlet weak var city: UITextField!
@IBOutlet weak var state: UITextField!
@IBOutlet weak var zip: UITextField!
@IBOutlet weak var errorBlock: UIView!
@IBOutlet weak var errorBlockLabel: UILabel!
// keep a reference to the text field delegates or they will get deallocated
var textFieldDelegates = [CustomTextFieldDelegate]()
var selectedYelpVenue: YelpVenue?
@IBAction func createVenue(_ sender: Any) {
self.view.endEditing(true)
errorBlock.isHidden = true
createVenueButton.isEnabled = false
createVenueButton.alpha = 0.5
createVenueActivityIndicator.startAnimating()
guard let name = venueName.text, isValidEntry(name),
let addr1 = address1.text, isValidEntry(addr1),
let addr2 = address2.text,
let city = city.text, isValidEntry(city),
let state = state.text, isValidEntry(state),
let zip = zip.text, isValidEntry(zip) else {
// highlight red any fields that were not valid
for del in textFieldDelegates {
del.textFieldDidEndEditing(UITextField())
}
createVenueButton.isEnabled = true
createVenueButton.alpha = 1.0
createVenueActivityIndicator.stopAnimating()
return
}
let venue = OGVenue(name: name,
street: addr1, street2: addr2,
city: city, state: state, zip: zip,
latitude: 0.0, longitude: 0.0, uuid: "")
if let yv = selectedYelpVenue {
venue.yelpId = yv.yelpId
}
let geocoder = CLGeocoder()
geocoder.geocodeAddressString(venue.address) { (placemarks, error) -> Void in
guard let placemark = placemarks?.first,
let coords = placemark.location?.coordinate else {
// unable to geocode this address, so we will show an error
self.address1.changeBorderColor(UIColor.red)
if addr2 != "" {
self.address2.changeBorderColor(UIColor.red)
}
self.city.changeBorderColor(UIColor.red)
self.state.changeBorderColor(UIColor.red)
self.zip.changeBorderColor(UIColor.red)
self.errorBlockLabel.text = "Uh oh! It looks like the address you provided isn't valid."
self.errorBlock.isHidden = false
self.errorBlock.shake()
self.createVenueButton.isEnabled = true
self.createVenueButton.alpha = 1.0
self.createVenueActivityIndicator.stopAnimating()
return
}
// we were able to geocode the address
venue.latitude = coords.latitude
venue.longitude = coords.longitude
OGCloud.sharedInstance.addVenue(venue: venue)
.then { uuid -> Void in
venue.uuid = uuid
HUD.flash(.success, delay: 1.0)
_ = self.navigationController?.popViewController(animated: true)
}.catch { error -> Void in
switch error {
case OGCloudError.authFailure:
self.errorBlockLabel.text = "Sorry, it looks like you aren't authorized to create a venue!"
self.errorBlock.isHidden = false
self.errorBlock.shake()
case OGCloudError.tokenInvalid: // this person needs to log back in
let alertController = UIAlertController(
title: "Uh oh!",
message: "It looks like your session has expired. Please log back in.",
preferredStyle: .alert)
let okAction = UIAlertAction(title: "OK", style: .default) { (action) in
OGCloud.sharedInstance.logout()
.always {
self.performSegue(withIdentifier: "fromAddVenueToRegistration", sender: nil)
}
}
alertController.addAction(okAction)
self.present(alertController, animated: true, completion: nil)
default: // otherwise unable to add the venue
self.errorBlockLabel.text = "Oh no...it looks like something went wrong adding your venue."
self.errorBlock.isHidden = false
self.errorBlock.shake()
}
self.createVenueButton.isEnabled = true
self.createVenueButton.alpha = 1.0
self.createVenueActivityIndicator.stopAnimating()
}
}
}
@IBAction func useCurrentLocation(_ sender: Any) {
yelpSearchLocation.text = curLocStr
yelpSearchLocation.textColor = useCurrentLocationButton.tintColor
checkReadyToYelp()
}
@IBAction func yelpSearchTermEditingChanged(_ sender: Any) {
checkReadyToYelp()
}
@IBAction func yelpSearchTermNext(_ sender: Any) {
yelpSearchLocation.becomeFirstResponder()
}
@IBAction func yelpSearchLocationEditingChanged(_ sender: Any) {
yelpSearchLocation.textColor = UIColor.white
checkReadyToYelp()
}
@IBAction func yelpSearchLocationNext(_ sender: Any) {
self.view.endEditing(true)
}
func checkReadyToYelp() {
guard let searchTerm = yelpSearchTerm.text, let location = yelpSearchLocation.text else {
findButton.isEnabled = false
findButton.alpha = 0.5
return
}
if searchTerm == "" || location == "" {
findButton.isEnabled = false
findButton.alpha = 0.5
return
}
findButton.isEnabled = true
fadeIn(findButton)
}
override func viewDidLoad() {
super.viewDidLoad()
locationManager.delegate = self
locationManager.requestLocation()
textFieldDelegates.append(contentsOf: [
CustomTextFieldDelegate(venueName, isValid: isValidEntry, inTableView: true),
CustomTextFieldDelegate(address1, isValid: isValidEntry, inTableView: true),
CustomTextFieldDelegate(address2, isValid: { _ in return true}, inTableView: true),
CustomTextFieldDelegate(city, isValid: isValidEntry, inTableView: true),
CustomTextFieldDelegate(state, isValid: isValidEntry, inTableView: true),
CustomTextFieldDelegate(zip, isValid: isValidEntry, inTableView: true)
])
yelpSearchTerm.useCustomBottomBorder()
let searchImageView = UIImageView()
searchImageView.image = #imageLiteral(resourceName: "ic_search_white_36pt")
searchImageView.alpha = 0.5
searchImageView.frame = CGRect(x: 5, y: 0, width: yelpSearchTerm.frame.height, height: yelpSearchTerm.frame.height)
yelpSearchTerm.leftView = searchImageView
yelpSearchTerm.leftViewMode = .always
yelpSearchLocation.useCustomBottomBorder()
let locationImageView = UIImageView()
locationImageView.image = #imageLiteral(resourceName: "ic_place_white")
locationImageView.alpha = 0.5
locationImageView.frame = CGRect(x: 5, y: 0, width: yelpSearchLocation.frame.height, height: yelpSearchLocation.frame.height)
yelpSearchLocation.leftView = locationImageView
yelpSearchLocation.leftViewMode = .always
findButton.isEnabled = false
findButton.alpha = 0.5
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let navVC = segue.destination as? UINavigationController {
let yelpVenuesVC = navVC.viewControllers.first as! FindYelpVenuesViewController
if let loc = yelpSearchLocation.text, loc == curLocStr {
if let curLoc = currentLocation {
yelpVenuesVC.searchLat = curLoc.coordinate.latitude
yelpVenuesVC.searchLong = curLoc.coordinate.longitude
}
} else {
yelpVenuesVC.searchLocation = yelpSearchLocation.text
}
yelpVenuesVC.searchTerm = yelpSearchTerm.text
yelpVenuesVC.yelpVenueDelegate = self
}
}
func isValidEntry(_ entry: String?) -> Bool {
if entry != nil && entry != "" {
return true
}
return false
}
func fadeIn(_ view: UIView){
UIView.animate(withDuration: 0.5, animations: {
view.alpha = 1.0
})
}
func fadeOut(_ view: UIView){
UIView.animate(withDuration: 0.5, animations: {
view.alpha = 0.0
})
}
}
extension CreateVenueViewController: FindYelpVenuesDelegate {
func selectYelpVenue(_ venue: YelpVenue) {
self.venueName.text = venue.name
self.address1.text = venue.address1
self.address2.text = venue.address2
self.city.text = venue.city
self.state.text = venue.state
self.zip.text = venue.zip
self.selectedYelpVenue = venue
for del in textFieldDelegates {
del.textFieldDidEndEditing(UITextField())
}
}
}
extension CreateVenueViewController: CLLocationManagerDelegate {
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
if let location = locations.first {
self.currentLocation = location
self.useCurrentLocationButton.isEnabled = true
}
}
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
log.error("failed to find user's location: \(error.localizedDescription)")
}
}