FacilitiesListViewController.swift 9.95 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
Zach Knox's avatar
Zach Knox committed
11
import RealmSwift
12

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

Zach Knox's avatar
Zach Knox committed
15
	var facilitiesArray = List<Facility>()
Zach Knox's avatar
Zach Knox committed
16
	var filters = Filters()
17
	
18
19
20
21
	override var preferredStatusBarStyle: UIStatusBarStyle {
		return .default
	}
	
22
	@IBOutlet var LeftButton: UIBarButtonItem!
Zach Knox's avatar
Zach Knox committed
23

24
25
26
27
	@IBAction func RightButton(_ sender: Any) {
	}
	@IBOutlet var RightButton: UIBarButtonItem!
	
28
29
	@IBOutlet var settingsButton: UIBarButtonItem!
	
30
	@IBOutlet var LocationsList: UICollectionView!
Zach Knox's avatar
Zach Knox committed
31

32
	@IBOutlet var LocationsListLayout: UICollectionViewFlowLayout!
Zach Knox's avatar
Zach Knox committed
33

34
	@IBOutlet var favoritesControl: UISegmentedControl!
Zach Knox's avatar
Zach Knox committed
35
36
	var showFavorites = false

37
	@IBOutlet var LastUpdatedLabel: UIBarButtonItem!
Zach Knox's avatar
Zach Knox committed
38

Zach Knox's avatar
Zach Knox committed
39
40
41
42
43
44
45
46
47
48
49
50
	@IBAction func favoritesControlChanges(_ sender: Any) {
		switch (self.favoritesControl.selectedSegmentIndex)
		{
		case 0:
			showFavorites = false
		case 1:
			showFavorites = true
		default:
			showFavorites = false
		}
		self.LocationsList.reloadData()
	}
Zach Knox's avatar
Zach Knox committed
51

Zach Knox's avatar
Zach Knox committed
52
	let refreshControl = UIRefreshControl()
Zach Knox's avatar
Zach Knox committed
53

54
	override func viewWillLayoutSubviews() {
Zach Knox's avatar
Zach Knox committed
55
56
57
58
59
60
		LocationsListLayout.itemSize.width = getCellWidth()
		LocationsListLayout.invalidateLayout()

	}
	
	func getCellWidth() -> CGFloat {
61
62
		let windowWidth = self.view.frame.size.width
		
Zach Knox's avatar
Zach Knox committed
63
64
		if(windowWidth < 640) {
			return windowWidth - 20
65
66
		}
		else if(windowWidth >= 640 && windowWidth < 1024) {
Zach Knox's avatar
Zach Knox committed
67
			return (windowWidth / 2) - 15
68
69
		}
		else if(windowWidth >= 1024) {
Zach Knox's avatar
Zach Knox committed
70
			return (windowWidth / 3) - 15
71
72
		}
		
Zach Knox's avatar
Zach Knox committed
73
		return 0
74
	}
Zach Knox's avatar
Zach Knox committed
75

76
77
78
	@IBAction func RefreshButton(_ sender: Any) {
		refresh(sender)
	}
Zach Knox's avatar
Zach Knox committed
79

80
81
	override func viewWillAppear(_ animated: Bool) {
		LastUpdatedLabel.isEnabled = false
Zach Knox's avatar
Zach Knox committed
82
83
84
85
		
		navigationItem.title = "What's Open?"


86
87
	}
	
88
	func tapRecognizer(_ sender: UITapGestureRecognizer) {
Zach Knox's avatar
Zach Knox committed
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
		
		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?
Zach Knox's avatar
Zach Knox committed
104
105
106
107
108
109
			destination.modalPresentationStyle = .popover
			let popoverController = destination.popoverPresentationController
			popoverController?.permittedArrowDirections = .any
			popoverController?.sourceView = destination.view
			
			present(destination, animated: true, completion: nil)
Zach Knox's avatar
Zach Knox committed
110
111
112
113
114
115
116
117
118
119
		}
		else {
			let destDelegate = DeckTransitioningDelegate()
			destination.modalPresentationStyle = .custom
			destination.transitioningDelegate = destDelegate
			present(destination, animated: true, completion: nil)
		}
	}
	
	override func viewDidLoad() {
120
121
        super.viewDidLoad()
		
122
123
124
125
		if( traitCollection.forceTouchCapability == .available){
			registerForPreviewing(with: self, sourceView: self.LocationsList!)
		}
		
Zach Knox's avatar
Zach Knox committed
126
127
128
129
		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
130
			navigationItem.hidesSearchBarWhenScrolling = true
Zach Knox's avatar
Zach Knox committed
131
132
133
134
			navigationItem.searchController?.searchBar.barTintColor = UIColor.white
			navigationItem.searchController?.searchBar.barStyle = .default
		}
		
135
		LocationsListLayout.invalidateLayout()
Zach Knox's avatar
Zach Knox committed
136
		
137
138
139
		settingsButton.accessibilityLabel = "Settings"
		LastUpdatedLabel.accessibilityHint = ""
		
140
		LocationsListLayout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10)
Zach Knox's avatar
Zach Knox committed
141

Zach Knox's avatar
Zach Knox committed
142
143
144
		refreshControl.addTarget(self, action: #selector(refresh), for: .valueChanged)
		LocationsList.addSubview(refreshControl)
		LocationsList.alwaysBounceVertical = true
Zach Knox's avatar
Zach Knox committed
145

146
		SRCTNetworkController.performDownload { (facilities) in
Zach Knox's avatar
Zach Knox committed
147
			self.facilitiesArray = List(facilities)
148
149
			DispatchQueue.main.async {
				self.LocationsList.reloadData()
150
151
				let date = Date()
				self.LastUpdatedLabel.title = "Updated: " + self.shortDateFormat(date)
152
153
			}
		}
154
155
		
	}
Zach Knox's avatar
Zach Knox committed
156
157
158
159
	
	func refresh(_ sender: Any) {
		refreshControl.beginRefreshing()
		LocationsList.reloadData()
160
161
		let date = Date()
		LastUpdatedLabel.title = "Updated: " + shortDateFormat(date)
Zach Knox's avatar
Zach Knox committed
162
163
		refreshControl.endRefreshing()
	}
164
165
166
167
168
169
170
171
172
	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
173
	
174
175
176
177
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
Zach Knox's avatar
Zach Knox committed
178

179
	func numberOfSections(in collectionView: UICollectionView) -> Int {
Zach Knox's avatar
Zach Knox committed
180
		return 1
181
	}
Zach Knox's avatar
Zach Knox committed
182

183
	func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
Zach Knox's avatar
Zach Knox committed
184
        return self.facilitiesArray.count
185
	}
Zach Knox's avatar
Zach Knox committed
186
187
188



189
	func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
190
		let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CollectionCell", for: indexPath) as! FacilityCollectionViewCell
Zach Knox's avatar
Zach Knox committed
191
192
193
194
195
196
197
		/*
		let windowRect = self.view.window!.frame
		let windowWidth = windowRect.size.width
		if(windowWidth <= 320) {
			cell.frame.size.width = 280
		}
		*/
198
199
200
		cell.tapRecognizer.addTarget(self, action: #selector(FacilitiesListViewController.tapRecognizer(_:)))
		cell.gestureRecognizers = []
		cell.gestureRecognizers?.append(cell.tapRecognizer)
201
202
		let dataArray = placeOpenFacilitiesFirstInArray(facilitiesArray)
		let facility = dataArray[indexPath.row]
203
		cell.facility = facility
Zach Knox's avatar
Zach Knox committed
204
		cell.nameLabel.text = facility.facilityName
205
        cell.categoryLabel.text = facility.category?.categoryName.uppercased()
Zach Knox's avatar
Zach Knox committed
206

207
208
		let open = Utilities.isOpen(facility: facility)
		if(open == true) {
209
			cell.openClosedLabel.text = "Open"
210
211
212
			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)
213
			cell.backgroundColor = UIColor(red:0.00, green:0.40, blue:0.20, alpha:1.0)
214
		} else {
215
			cell.openClosedLabel.text = "Closed"
216
217
218
			cell.openClosedLabel.textColor = UIColor.white
			cell.openClosedLabel.backgroundColor = UIColor.black
			//cell.openClosedLabel.backgroundColor = UIColor.red
219
220
			cell.backgroundColor = UIColor.red

221
		}
Zach Knox's avatar
Zach Knox committed
222

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

		
229
230
231
		self.reloadInputViews()
		return cell
	}
Zach Knox's avatar
Zach Knox committed
232
233

	func getLocationArray(_ facilitiesArray: List<Facility>) -> [Facility] {
Zach Knox's avatar
Zach Knox committed
234
235
236
237
238
239
		if(!showFavorites) {
			return placeOpenFacilitiesFirstInArray(facilitiesArray)
		}
		else {
			return [] //TODO - INCOMPLETE
		}
Zach Knox's avatar
Zach Knox committed
240
241


Zach Knox's avatar
Zach Knox committed
242
	}
Zach Knox's avatar
Zach Knox committed
243

244
245
246
	//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
Zach Knox's avatar
Zach Knox committed
247
	func placeOpenFacilitiesFirstInArray(_ facilitiesArray: List<Facility>) -> [Facility] {
248
249
		var open = [Facility]()
		var closed = [Facility]()
Zach Knox's avatar
Zach Knox committed
250

251
252
253
254
255
256
257
258
259
260
261
		for i in facilitiesArray {
			if(Utilities.isOpen(facility: i)) {
				open.append(i)
			}
			else {
				closed.append(i)
			}
		}
		// Test
		return open + closed
	}
Zach Knox's avatar
Zach Knox committed
262

263
264
265
	func countForOpenAndClosedFacilities(_ facilitiesArray: Array<Facility>) -> (open: Int, closed: Int) {
		var open = 0
		var closed = 0
Zach Knox's avatar
Zach Knox committed
266

267
268
269
270
271
272
273
274
		for i in facilitiesArray {
			if(Utilities.isOpen(facility: i)) {
				open += 1
			}
			else {
				closed += 1
			}
		}
Zach Knox's avatar
Zach Knox committed
275

276
277
		return (open, closed)
	}
Zach Knox's avatar
Zach Knox committed
278
279


280
281
    // MARK: - Navigation

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

311
312
313
314
		return detailView
	}
	
	func previewingContext(_ previewingContext: UIViewControllerPreviewing, commit viewControllerToCommit: UIViewController) {
Zach Knox's avatar
Zach Knox committed
315
316
317
318
319
		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)
320
321
	}
	
322
}