FacilitiesListViewController.swift 9.79 KB
Newer Older
1
2
3
4
5
//
//  LocationsListViewController.swift
//  WhatsOpen
//
//  Created by Zach Knox on 4/5/17.
Zach Knox's avatar
Zach Knox committed
6
//  Copyright © 2017 SRCT. Some rights reserved.
7
8
9
//

import UIKit
Zach Knox's avatar
Zach Knox committed
10
import DeckTransition
11

12
class FacilitiesListViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource, UIViewControllerPreviewingDelegate {
13

14
	var facilitiesArray = Array<Facility>()
Zach Knox's avatar
Zach Knox committed
15
	var filters = Filters()
16
	
17
18
19
20
	override var preferredStatusBarStyle: UIStatusBarStyle {
		return .default
	}
	
21
	@IBOutlet var LeftButton: UIBarButtonItem!
22
		
23
24
25
26
	@IBAction func RightButton(_ sender: Any) {
	}
	@IBOutlet var RightButton: UIBarButtonItem!
	
27
28
	@IBOutlet var settingsButton: UIBarButtonItem!
	
29
30
	@IBOutlet var LocationsList: UICollectionView!
	
31
	@IBOutlet var LocationsListLayout: UICollectionViewFlowLayout!
32
	
33
	@IBOutlet var favoritesControl: UISegmentedControl!
Zach Knox's avatar
Zach Knox committed
34
35
	var showFavorites = false

36
37
	@IBOutlet var LastUpdatedLabel: UIBarButtonItem!
	
Zach Knox's avatar
Zach Knox committed
38
39
40
41
42
43
44
45
46
47
48
49
	@IBAction func favoritesControlChanges(_ sender: Any) {
		switch (self.favoritesControl.selectedSegmentIndex)
		{
		case 0:
			showFavorites = false
		case 1:
			showFavorites = true
		default:
			showFavorites = false
		}
		self.LocationsList.reloadData()
	}
50
	
Zach Knox's avatar
Zach Knox committed
51
52
	let refreshControl = UIRefreshControl()
	
53
	override func viewWillLayoutSubviews() {
Zach Knox's avatar
Zach Knox committed
54
55
56
57
58
59
		LocationsListLayout.itemSize.width = getCellWidth()
		LocationsListLayout.invalidateLayout()

	}
	
	func getCellWidth() -> CGFloat {
60
61
		let windowWidth = self.view.frame.size.width
		
Zach Knox's avatar
Zach Knox committed
62
63
		if(windowWidth < 640) {
			return windowWidth - 20
64
65
		}
		else if(windowWidth >= 640 && windowWidth < 1024) {
Zach Knox's avatar
Zach Knox committed
66
			return (windowWidth / 2) - 15
67
68
		}
		else if(windowWidth >= 1024) {
Zach Knox's avatar
Zach Knox committed
69
			return (windowWidth / 3) - 15
70
71
		}
		
Zach Knox's avatar
Zach Knox committed
72
		return 0
73
74
	}
	
75
76
77
78
79
80
	@IBAction func RefreshButton(_ sender: Any) {
		refresh(sender)
	}
	
	override func viewWillAppear(_ animated: Bool) {
		LastUpdatedLabel.isEnabled = false
Zach Knox's avatar
Zach Knox committed
81
82
83
84
		
		navigationItem.title = "What's Open?"


85
86
	}
	
87
	func tapRecognizer(_ sender: UITapGestureRecognizer) {
Zach Knox's avatar
Zach Knox committed
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
		
		let tapLocation = sender.location(in: LocationsList)
		let indexPath = LocationsList.indexPathForItem(at: tapLocation)
		if((indexPath) != nil) {
			let destination = storyboard?.instantiateViewController(withIdentifier: "detailView") as? FacilityDetailViewController
			let tapped = LocationsList.cellForItem(at: indexPath!) as! FacilityCollectionViewCell
			destination!.facility = tapped.facility
			presentDetailView(destination!)
		}
	}
	
	func presentDetailView(_ destination: FacilityDetailViewController) {
		if(self.view.traitCollection.horizontalSizeClass == .regular && self.view.traitCollection.verticalSizeClass == .regular) {
			//do a popover here for the iPad
			//iPads are cool right?
		}
		else {
			let destDelegate = DeckTransitioningDelegate()
			destination.modalPresentationStyle = .custom
			destination.transitioningDelegate = destDelegate
			present(destination, animated: true, completion: nil)
		}
	}
	
	override func viewDidLoad() {
113
114
        super.viewDidLoad()
		
115
116
117
118
		if( traitCollection.forceTouchCapability == .available){
			registerForPreviewing(with: self, sourceView: self.LocationsList!)
		}
		
Zach Knox's avatar
Zach Knox committed
119
120
121
122
		let searchController = UISearchController(searchResultsController: nil) //TODO: ADD SEARCH
		if #available(iOS 11, *) {
			navigationController?.navigationBar.prefersLargeTitles = true
			navigationItem.searchController = searchController
Zach Knox's avatar
Zach Knox committed
123
			navigationItem.hidesSearchBarWhenScrolling = true
Zach Knox's avatar
Zach Knox committed
124
125
126
127
			navigationItem.searchController?.searchBar.barTintColor = UIColor.white
			navigationItem.searchController?.searchBar.barStyle = .default
		}
		
128
		LocationsListLayout.invalidateLayout()
Zach Knox's avatar
Zach Knox committed
129
		
130
131
132
		settingsButton.accessibilityLabel = "Settings"
		LastUpdatedLabel.accessibilityHint = ""
		
133
134
		LocationsListLayout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10)
		
Zach Knox's avatar
Zach Knox committed
135
136
137
138
		refreshControl.addTarget(self, action: #selector(refresh), for: .valueChanged)
		LocationsList.addSubview(refreshControl)
		LocationsList.alwaysBounceVertical = true
		
139
		
140
141
142
143
144
		SRCTNetworkController.performDownload { (facilities) in
			self.facilitiesArray = facilities
			//            print(self.facilitiesArray)
			DispatchQueue.main.async {
				self.LocationsList.reloadData()
145
146
				let date = Date()
				self.LastUpdatedLabel.title = "Updated: " + self.shortDateFormat(date)
147
148
			}
		}
149
150
		
	}
Zach Knox's avatar
Zach Knox committed
151
152
153
154
	
	func refresh(_ sender: Any) {
		refreshControl.beginRefreshing()
		LocationsList.reloadData()
155
156
		let date = Date()
		LastUpdatedLabel.title = "Updated: " + shortDateFormat(date)
Zach Knox's avatar
Zach Knox committed
157
158
		refreshControl.endRefreshing()
	}
159
160
161
162
163
164
165
166
167
	func shortDateFormat(_ date: Date) -> String {
		let dateFormatter = DateFormatter()
		dateFormatter.dateStyle = .short
		dateFormatter.timeStyle = .short

		// US English Locale (en_US)
		dateFormatter.locale = Locale(identifier: "en_US")
		return dateFormatter.string(from: date)
	}
Zach Knox's avatar
Zach Knox committed
168
	
169
170
171
172
173
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
	
174
175
176
177
178
	func numberOfSections(in collectionView: UICollectionView) -> Int {
		return 2
	}
	
	func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
Zach Knox's avatar
Zach Knox committed
179
		let count = countForOpenAndClosedFacilities(getLocationArray(facilitiesArray)) //TODO could be better optimized
Zach Knox's avatar
Zach Knox committed
180
		
181
182
183
184
185
186
187
188
189
190
191
		if(section == 1) {
			return count.open
		}
		else {
			return count.closed
		}
	}
	
	
	
	func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
192
		let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CollectionCell", for: indexPath) as! FacilityCollectionViewCell
Zach Knox's avatar
Zach Knox committed
193
194
195
196
197
198
199
		/*
		let windowRect = self.view.window!.frame
		let windowWidth = windowRect.size.width
		if(windowWidth <= 320) {
			cell.frame.size.width = 280
		}
		*/
200
201
202
		cell.tapRecognizer.addTarget(self, action: #selector(FacilitiesListViewController.tapRecognizer(_:)))
		cell.gestureRecognizers = []
		cell.gestureRecognizers?.append(cell.tapRecognizer)
203
204
		let dataArray = placeOpenFacilitiesFirstInArray(facilitiesArray)
		let facility = dataArray[indexPath.row]
205
		cell.facility = facility
206
		cell.nameLabel.text = facility.name
207
208
209
		
		let open = Utilities.isOpen(facility: facility)
		if(open == true) {
210
			cell.openClosedLabel.text = "Open"
211
212
213
			cell.openClosedLabel.textColor = UIColor.black
			cell.openClosedLabel.backgroundColor = UIColor.white
			//cell.openClosedLabel.backgroundColor = UIColor(red:0.00, green:0.40, blue:0.20, alpha:1.0)
214
			cell.backgroundColor = UIColor(red:0.00, green:0.40, blue:0.20, alpha:1.0)
215
		} else {
216
			cell.openClosedLabel.text = "Closed"
217
218
219
			cell.openClosedLabel.textColor = UIColor.white
			cell.openClosedLabel.backgroundColor = UIColor.black
			//cell.openClosedLabel.backgroundColor = UIColor.red
220
221
			cell.backgroundColor = UIColor.red

222
223
224
225
		}
		
		cell.timeDescriptionLabel.text = Utilities.timeUntilFacility(facility)
		
226
227
228
229
		cell.accessibilityLabel = cell.nameLabel.text! + ", Currently " + cell.openClosedLabel.text! + "." + cell.timeDescriptionLabel.text!
		cell.accessibilityHint = "Double Tap to view details"

		
230
231
232
233
		self.reloadInputViews()
		return cell
	}
	
Zach Knox's avatar
Zach Knox committed
234
235
236
237
238
239
240
241
242
243
244
	func getLocationArray(_ facilitiesArray: [Facility]) -> [Facility] {
		if(!showFavorites) {
			return placeOpenFacilitiesFirstInArray(facilitiesArray)
		}
		else {
			return [] //TODO - INCOMPLETE
		}
		
		
	}
	
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
	//Returns an array which has the open locations listed first
	//Could be improved in the future because currently this means you're checking
	//open status twice per cell
	func placeOpenFacilitiesFirstInArray(_ facilitiesArray: Array<Facility>) -> [Facility] {
		var open = [Facility]()
		var closed = [Facility]()
		
		for i in facilitiesArray {
			if(Utilities.isOpen(facility: i)) {
				open.append(i)
			}
			else {
				closed.append(i)
			}
		}
		// Test
		return open + closed
	}
	
	func countForOpenAndClosedFacilities(_ facilitiesArray: Array<Facility>) -> (open: Int, closed: Int) {
		var open = 0
		var closed = 0
		
		for i in facilitiesArray {
			if(Utilities.isOpen(facility: i)) {
				open += 1
			}
			else {
				closed += 1
			}
		}
		
		return (open, closed)
	}
	
	
281
282
    // MARK: - Navigation

283
	//In a storyboard-based application, you will often want to do a little preparation before navigation
284
285
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
286
		if(segue.identifier == "toDetailView") {
287
			let destination = segue.destination as! FacilityDetailViewController
Zach Knox's avatar
Zach Knox committed
288
289
			let destDelegate = DeckTransitioningDelegate()
			destination.transitioningDelegate = destDelegate
290
			let tapped = sender as! FacilityCollectionViewCell //this is probably a bad idea just FYI future me
291
			destination.facility = tapped.facility
Zach Knox's avatar
Zach Knox committed
292
			present(destination, animated: true, completion: nil)
293
		}
294
		else if(segue.identifier == "toFilters") {
295
296
297
			let destination = segue.destination as! UINavigationController
			let filterView = destination.topViewController as! FiltersTableViewController
			filterView.filters = self.filters
298
		}
299
		
300
301
        // Pass the selected object to the new view controller.
    }
302
	
303
304
305
306
	// MARK: - Peek and Pop
	
	func previewingContext(_ previewingContext: UIViewControllerPreviewing, viewControllerForLocation location: CGPoint) -> UIViewController? {
		guard let indexPath = LocationsList?.indexPathForItem(at: location) else { return nil }
307
308
		let cell = LocationsList?.cellForItem(at: indexPath) as! FacilityCollectionViewCell
		guard let detailView = storyboard?.instantiateViewController(withIdentifier: "detailView") as? FacilityDetailViewController else { return nil }
309
310
		
		detailView.facility = cell.facility
Zach Knox's avatar
Zach Knox committed
311

312
313
314
315
		return detailView
	}
	
	func previewingContext(_ previewingContext: UIViewControllerPreviewing, commit viewControllerToCommit: UIViewController) {
Zach Knox's avatar
Zach Knox committed
316
317
318
319
320
		let destDelegate = DeckTransitioningDelegate()
		viewControllerToCommit.modalPresentationStyle = .custom
		viewControllerToCommit.transitioningDelegate = destDelegate
		//If one day 3D touch comes to the iPad, this is no longer good.
		present(viewControllerToCommit, animated: true, completion: nil)
321
322
	}
	
323
}
324