FacilitiesListViewController.swift 7.49 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
10
//

import UIKit

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

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

31
32
	@IBOutlet var LastUpdatedLabel: UIBarButtonItem!
	
Zach Knox's avatar
Zach Knox committed
33
34
35
36
37
38
39
40
41
42
43
44
	@IBAction func favoritesControlChanges(_ sender: Any) {
		switch (self.favoritesControl.selectedSegmentIndex)
		{
		case 0:
			showFavorites = false
		case 1:
			showFavorites = true
		default:
			showFavorites = false
		}
		self.LocationsList.reloadData()
	}
45
	
Zach Knox's avatar
Zach Knox committed
46
47
	let refreshControl = UIRefreshControl()
	
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
	override func viewWillLayoutSubviews() {
		let windowWidth = self.view.frame.size.width
		
		if(windowWidth > 320 && windowWidth < 640) {
			LocationsListLayout.itemSize.width = windowWidth - 20
		}
		else if(windowWidth >= 640 && windowWidth < 1024) {
			LocationsListLayout.itemSize.width = (windowWidth / 2) - 15
		}
		else if(windowWidth >= 1024) {
			LocationsListLayout.itemSize.width = (windowWidth / 3) - 15
		}
		
		LocationsListLayout.invalidateLayout()
		
	}
	
65
66
67
68
69
70
71
72
	@IBAction func RefreshButton(_ sender: Any) {
		refresh(sender)
	}
	
	override func viewWillAppear(_ animated: Bool) {
		LastUpdatedLabel.isEnabled = false
	}
	
73
74
75
    override func viewDidLoad() {
        super.viewDidLoad()
		
76
77
78
79
		if( traitCollection.forceTouchCapability == .available){
			registerForPreviewing(with: self, sourceView: self.LocationsList!)
		}
		
80
		LocationsListLayout.invalidateLayout()
Zach Knox's avatar
Zach Knox committed
81
		
82
83
84
		settingsButton.accessibilityLabel = "Settings"
		LastUpdatedLabel.accessibilityHint = ""
		
85
86
		LocationsListLayout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10)
		
Zach Knox's avatar
Zach Knox committed
87
88
89
90
		refreshControl.addTarget(self, action: #selector(refresh), for: .valueChanged)
		LocationsList.addSubview(refreshControl)
		LocationsList.alwaysBounceVertical = true
		
91
		
92
93
94
95
96
		SRCTNetworkController.performDownload { (facilities) in
			self.facilitiesArray = facilities
			//            print(self.facilitiesArray)
			DispatchQueue.main.async {
				self.LocationsList.reloadData()
97
98
				let date = Date()
				self.LastUpdatedLabel.title = "Updated: " + self.shortDateFormat(date)
99
100
			}
		}
101
102
		
	}
Zach Knox's avatar
Zach Knox committed
103
104
105
106
	
	func refresh(_ sender: Any) {
		refreshControl.beginRefreshing()
		LocationsList.reloadData()
107
108
		let date = Date()
		LastUpdatedLabel.title = "Updated: " + shortDateFormat(date)
Zach Knox's avatar
Zach Knox committed
109
110
		refreshControl.endRefreshing()
	}
111
112
113
114
115
116
117
118
119
	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
120
	
121
122
123
124
125
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
	
126
127
128
129
130
	func numberOfSections(in collectionView: UICollectionView) -> Int {
		return 2
	}
	
	func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
Zach Knox's avatar
Zach Knox committed
131
		let count = countForOpenAndClosedFacilities(getLocationArray(facilitiesArray)) //TODO could be better optimized
Zach Knox's avatar
Zach Knox committed
132
		
133
134
135
136
137
138
139
140
141
142
143
		if(section == 1) {
			return count.open
		}
		else {
			return count.closed
		}
	}
	
	
	
	func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
144
		let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CollectionCell", for: indexPath) as! FacilityCollectionViewCell
Zach Knox's avatar
Zach Knox committed
145
146
147
148
149
150
151
		/*
		let windowRect = self.view.window!.frame
		let windowWidth = windowRect.size.width
		if(windowWidth <= 320) {
			cell.frame.size.width = 280
		}
		*/
152
153
154
		
		let dataArray = placeOpenFacilitiesFirstInArray(facilitiesArray)
		let facility = dataArray[indexPath.row]
155
		cell.facility = facility
156
		cell.nameLabel.text = facility.name
157
158
159
		
		let open = Utilities.isOpen(facility: facility)
		if(open == true) {
160
161
			cell.openClosedLabel.text = "Open"
			cell.openClosedLabel.backgroundColor = UIColor(red:0.00, green:0.40, blue:0.20, alpha:1.0)
162
			cell.backgroundColor = UIColor(red:0.00, green:0.40, blue:0.20, alpha:1.0)
163
		} else {
164
165
			cell.openClosedLabel.text = "Closed"
			cell.openClosedLabel.backgroundColor = UIColor.red
166
167
			cell.backgroundColor = UIColor.red

168
169
170
171
		}
		
		cell.timeDescriptionLabel.text = Utilities.timeUntilFacility(facility)
		
172
173
174
175
		cell.accessibilityLabel = cell.nameLabel.text! + ", Currently " + cell.openClosedLabel.text! + "." + cell.timeDescriptionLabel.text!
		cell.accessibilityHint = "Double Tap to view details"

		
176
177
178
179
		self.reloadInputViews()
		return cell
	}
	
Zach Knox's avatar
Zach Knox committed
180
181
182
183
184
185
186
187
188
189
190
	func getLocationArray(_ facilitiesArray: [Facility]) -> [Facility] {
		if(!showFavorites) {
			return placeOpenFacilitiesFirstInArray(facilitiesArray)
		}
		else {
			return [] //TODO - INCOMPLETE
		}
		
		
	}
	
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
	//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)
	}
	
	
227
228
    // MARK: - Navigation

229
	//In a storyboard-based application, you will often want to do a little preparation before navigation
230
231
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
232
		if(segue.identifier == "toDetailView") {
233
234
			let destination = segue.destination as! FacilityDetailViewController
			let tapped = sender as! FacilityCollectionViewCell //this is probably a bad idea just FYI future me
235
236
237
			destination.facility = tapped.facility
			
		}
238
		else if(segue.identifier == "toFilters") {
239
240
241
			let destination = segue.destination as! UINavigationController
			let filterView = destination.topViewController as! FiltersTableViewController
			filterView.filters = self.filters
242
		}
243
		
244
245
        // Pass the selected object to the new view controller.
    }
246
	
247
248
249
250
	// MARK: - Peek and Pop
	
	func previewingContext(_ previewingContext: UIViewControllerPreviewing, viewControllerForLocation location: CGPoint) -> UIViewController? {
		guard let indexPath = LocationsList?.indexPathForItem(at: location) else { return nil }
251
252
		let cell = LocationsList?.cellForItem(at: indexPath) as! FacilityCollectionViewCell
		guard let detailView = storyboard?.instantiateViewController(withIdentifier: "detailView") as? FacilityDetailViewController else { return nil }
253
254
		
		detailView.facility = cell.facility
Zach Knox's avatar
Zach Knox committed
255

256
257
258
259
260
261
262
		return detailView
	}
	
	func previewingContext(_ previewingContext: UIViewControllerPreviewing, commit viewControllerToCommit: UIViewController) {
		self.navigationController?.show(viewControllerToCommit, sender: Any?.self)
	}
	
263
}
264