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

import UIKit
10
import RealmSwift
11
12
13

class FiltersTableViewController: UITableViewController {

Zac Wood's avatar
Zac Wood committed
14
15
    var updateFacilities: (() -> Void)!
    
16
17
18
19
	override var preferredStatusBarStyle: UIStatusBarStyle {
		return .default
	}
	
20
21
22
23
	@IBAction func doneButton(_ sender: Any) {
		self.dismiss(animated: true, completion: nil)
	}
	
Zach Knox's avatar
Zach Knox committed
24
	@IBAction func resetButton(_ sender: Any) {
Zach Knox's avatar
Zach Knox committed
25
26
27
28
29
30
31
32
		var c = filters.onlyFromCategories
		var l = filters.onlyFromLocations
		for v in c {
			c.updateValue(true, forKey: v.key)
		}
		for v in l {
			l.updateValue(true, forKey: v.key)
		}
33
34
35
36
		filters.openFirst = true
		filters.showClosed = true
		filters.showOpen = true
		filters.sortBy = .alphabetical
Zach Knox's avatar
Zach Knox committed
37
38
		filters.onlyFromCategories = c
		filters.onlyFromLocations = l
Zach Knox's avatar
Zach Knox committed
39
		tableView.reloadData()
Zac Wood's avatar
Zac Wood committed
40
        updateFacilities()
Zach Knox's avatar
Zach Knox committed
41
	}
42
	var filters: Filters!
43
	var facilities: List<Facility>!
Zach Knox's avatar
Zach Knox committed
44
45
	//var allLocations: [Locations] = [Locations]()
	//var allCategories: [Categories]! = [Categories]()
46
	
47
	var showOpen, showClosed, openFirst: SwitchingTableViewCell!
48
49
50
51
52
	var sortOptions: [CheckingTableViewCell] = []
	var onlyOne: OnlyOneChecked!
	
	override func viewWillAppear(_ animated: Bool) {
		onlyOne = OnlyOneChecked(tableView: self, tableCellChecked: -1)
Zach Knox's avatar
Zach Knox committed
53
		tableView.reloadData()
54
55
	}
	
56
57
    override func viewDidLoad() {
        super.viewDidLoad()
Zac Wood's avatar
Zac Wood committed
58
        
Zac Wood's avatar
Zac Wood committed
59
		tableView.estimatedRowHeight = 50
Zach Knox's avatar
Zach Knox committed
60
        tableView.rowHeight = UITableView.automaticDimension
Zach Knox's avatar
Zach Knox committed
61
		/*
62
63
64
65
66
67
68
69
		for f in facilities {
			if(!allLocations.contains(f.facilityLocation!)) {
				allLocations.append(f.facilityLocation!)
			}
			if(!allCategories.contains(f.category!)) {
				allCategories.append(f.category!)
			}
		}
Zach Knox's avatar
Zach Knox committed
70
		*/
71
		
72
73
74
75
76
77
        // Uncomment the following line to preserve selection between presentations
        // self.clearsSelectionOnViewWillAppear = false

        // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
        // self.navigationItem.rightBarButtonItem = self.editButtonItem()
    }
Zac Wood's avatar
Zac Wood committed
78
79
80
81
82
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        updateFacilities?()
    }
83
84
85
86
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
Zach Knox's avatar
Zach Knox committed
87
	
88
89
90
91
    // MARK: - Table view data source

    override func numberOfSections(in tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
92
        return 4
93
94
95
96
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
97
98
		switch section {
		case 0:
99
			return 2
100
101
102
103
104
		case 1:
			return 1
		case 2:
			return SortMethod.count
		case 3:
Zach Knox's avatar
Zach Knox committed
105
			return 2
106
		default:
107
108
			return 0
		}
109
110
    }

Zach Knox's avatar
Zach Knox committed
111
112
113
114
115
116
117
	override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
		switch section {
		case 0, 1:
			return nil
		case 2:
			return "Sort Facilities"
		case 3:
118
			return "Show Only Specified"
Zach Knox's avatar
Zach Knox committed
119
120
121
122
123
		default:
			return nil
		}
	}
	
124
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
125
		switch indexPath.section {
126
		  case 0:
127
			let cell: SwitchingTableViewCell
128
129
			switch indexPath.row {
			  case 0:
130
				cell = tableView.dequeueReusableCell(withIdentifier: "Switching", for: indexPath) as! SwitchingTableViewCell
131
				cell.textLabel!.text = "Show Open Locations"
132
				cell.switchControl.isOn = filters.showOpen
Zac Wood's avatar
Zac Wood committed
133
134
135
136
137
                cell.toggleFunc = { [unowned self] isOn in
                    let result = self.updateOpenFirstEnabledState(isOn)
                    self.updateFacilities()
                    return result
                }
138
				//self.showOpen = cell
139
			  case 1:
140
				cell = tableView.dequeueReusableCell(withIdentifier: "Switching", for: indexPath) as! SwitchingTableViewCell
141
				cell.textLabel!.text = "Show Closed Locations"
142
				cell.switchControl.isOn = filters.showClosed
Zac Wood's avatar
Zac Wood committed
143
144
145
146
147
                cell.toggleFunc = { [unowned self] isOn in
                    let result = self.filters.setShowClosed(isOn)
                    self.updateFacilities()
                    return result
                }
148
				//self.showClosed = cell
149
			  default:
150
				cell = UITableViewCell() as! SwitchingTableViewCell //this is bad don't let this happen
151
			}
Zach Knox's avatar
Zach Knox committed
152
			return cell
153
		  case 1:
154
155
156
			let cell = tableView.dequeueReusableCell(withIdentifier: "Switching", for: indexPath) as! SwitchingTableViewCell
			cell.textLabel!.text = "Show Open Facilities First"
			cell.switchControl.isEnabled = filters.showOpen
Zach Knox's avatar
Zach Knox committed
157
			cell.switchControl.isOn = filters.openFirst
Zac Wood's avatar
Zac Wood committed
158
159
160
161
162
            cell.toggleFunc = { [unowned self] isOn in
                let result = self.filters.setOpenFirst(isOn)
                self.updateFacilities()
                return result
            }
Zach Knox's avatar
Zach Knox committed
163
			return cell
164
		  case 2:
Zach Knox's avatar
Zach Knox committed
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
			let method: SortMethod
			let cellText: String
			switch indexPath.row {
			case 0:
				method = SortMethod.alphabetical
				cellText = "Alphabetically (A-Z)"
			case 1:
				method = SortMethod.reverseAlphabetical
				cellText = "Reverse Alphabetically (Z-A)"
			case 2:
				method = SortMethod.byLocation
				cellText = "By Location Name (A-Z)"
			default:
				method = SortMethod.alphabetical
				cellText = "Alphabetically (A-Z)"
			}
181
182
183
184
			let cell: CheckingTableViewCell
			cell = tableView.dequeueReusableCell(withIdentifier: "Checkbox Filter", for: indexPath) as! CheckingTableViewCell
			cell.onlyOne = self.onlyOne
			cell.cellIndex = indexPath.row
Zach Knox's avatar
Zach Knox committed
185
186
187
188
189
190
191
192
193
			cell.selectingEnum = method
			cell.selectFunc = onlyCheckOne
			if(filters.sortBy == method) {
				cell.accessoryType = .checkmark
			}
			else {
				cell.accessoryType = .none
			}
			cell.textLabel!.text = cellText
194
			sortOptions.append(cell)
195
			return cell
196
		  case 3:
197
198
			let cell = tableView.dequeueReusableCell(withIdentifier: "toSelection", for: indexPath)
			cell.accessoryType = .disclosureIndicator
199
200
			cell.textLabel?.font = UIFont.preferredFont(forTextStyle: .body)
			cell.detailTextLabel?.font = UIFont.preferredFont(forTextStyle: .footnote)
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
			switch indexPath.row {
			case 0:
				cell.textLabel?.text = "Categories"
				var i = 0
				for c in filters.onlyFromCategories {
					if(c.value == true) {
						i += 1
					}
				}
				var detail: String
				if(i == filters.onlyFromCategories.count) {
					detail = "All Selected"
				}
				else {
					detail = "\(i) Selected"
				}
Zach Knox's avatar
Zach Knox committed
217
				cell.detailTextLabel?.text = detail
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
			case 1:
				cell.textLabel?.text = "Locations"
				var i = 0
				for c in filters.onlyFromLocations {
					if(c.value == true) {
						i += 1
					}
				}
				var detail: String
				if(i == filters.onlyFromLocations.count) {
					detail = "All Selected"
				}
				else {
					detail = "\(i) Selected"
				}
Zach Knox's avatar
Zach Knox committed
233
				cell.detailTextLabel?.text = detail
234
235
			default:
				return cell
Zach Knox's avatar
Zach Knox committed
236
			}
237
			return cell
238
		  default:
239
240
			let cell = UITableViewCell() //this is bad don't let this happen
			return cell
241
		}
242

243
		//return UITableViewCell() //shouldn't come to this
244
245
        // Configure the cell...
    }
246
	
247
248
249
250
	override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
		let cell = tableView.cellForRow(at: indexPath)
		cell?.isSelected = false
		
Zac Wood's avatar
Zac Wood committed
251
        updateFacilities()
252
253
254
		//nothing is selected forever
	}
	
255
	func updateOpenFirstEnabledState(_ to: Bool) -> Bool {
256
		_ = filters.setShowOpen(to)
257
		let index = IndexPath(row: 0, section: 1)
Zach Knox's avatar
Zach Knox committed
258
259
260
261
262
263
		tableView.reloadRows(at: [index], with: .automatic)
		return true
	}
	
	func onlyCheckOne(_ method: Any?) -> Bool {
		filters.sortBy = method as! SortMethod // Be careful when calling this
264
265
266
267
		tableView.reloadData()
		return true
	}
	
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
295
296
297
298
299
300
301
302
    /*
    // Override to support conditional editing of the table view.
    override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
        // Return false if you do not want the specified item to be editable.
        return true
    }
    */

    /*
    // Override to support editing the table view.
    override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
        if editingStyle == .delete {
            // Delete the row from the data source
            tableView.deleteRows(at: [indexPath], with: .fade)
        } else if editingStyle == .insert {
            // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
        }    
    }
    */

    /*
    // Override to support rearranging the table view.
    override func tableView(_ tableView: UITableView, moveRowAt fromIndexPath: IndexPath, to: IndexPath) {

    }
    */

    /*
    // Override to support conditional rearranging of the table view.
    override func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
        // Return false if you do not want the item to be re-orderable.
        return true
    }
    */

303
	
304
305
306
307
308
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
309
		
310
		//Using this code means there won't be live updates, but that only matters on the iPad with a popover
311
312
		//filters.showOpen = showOpen.switchControl.isOn
		//filters.showClosed = showClosed.switchControl.isOn
313
		
314
		if(segue.identifier == "toFilters") {
315
			let destination = segue.destination as! FacilitiesListViewController
316
			destination.filters = self.filters
Zac Wood's avatar
Zac Wood committed
317
            updateFacilities()
318
		}
319
320
321
		else if(segue.identifier == "toSelection") {
			let destination = segue.destination as! FilterSelectionTableViewController
			destination.navigationItem.title = (sender as! UITableViewCell).textLabel?.text!
Zac Wood's avatar
Zac Wood committed
322
			destination.updateFacilities = updateFacilities
Zach Knox's avatar
Zach Knox committed
323
			
324
325
326
327
			func get() -> [String: Bool] {
				if((sender as! UITableViewCell).textLabel?.text! == "Categories") {
					return filters.onlyFromCategories
				}
Zach Knox's avatar
Zach Knox committed
328
329
330
				else if(sender as! UITableViewCell).textLabel?.text! == "Alerts" {
					return filters.showAlerts
				}
331
332
333
				else if(sender as! UITableViewCell).textLabel?.text! == "Campuses" {
					return filters.onlyFromCampuses
				}
334
335
336
337
338
339
340
341
				else {
					return filters.onlyFromLocations
				}
			}
			func selectFunc(_ key: String, value: Bool) -> Bool {
				if((sender as! UITableViewCell).textLabel?.text! == "Categories") {
					filters.onlyFromCategories[key] = value
				}
Zach Knox's avatar
Zach Knox committed
342
343
344
				else if(sender as! UITableViewCell).textLabel?.text! == "Alerts" {
					filters.showAlerts[key] = value
				}
345
346
347
				else if(sender as! UITableViewCell).textLabel?.text! == "Campuses" {
					filters.onlyFromCampuses[key] = value
				}
348
349
350
351
352
353
354
				else {
					filters.onlyFromLocations[key] = value
				}
				return true
			}
			func selectAllFunc() -> Bool {
				if((sender as! UITableViewCell).textLabel?.text! == "Categories") {
355
					var foundFalse = false
Zach Knox's avatar
Zach Knox committed
356
					for v in filters.onlyFromCategories {
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
						if !foundFalse {
							if !v.value {
								foundFalse = true
								filters.onlyFromCategories.updateValue(true, forKey: v.key)
							}
						}
						else {
							filters.onlyFromCategories.updateValue(true, forKey: v.key)
						}
					}
					if !foundFalse {
						for v in filters.onlyFromCategories {
							filters.onlyFromCategories.updateValue(false, forKey: v.key)
						}
					}
				}
				else if((sender as! UITableViewCell).textLabel?.text! == "Alerts") {
					var foundFalse = false
					for v in filters.showAlerts {
						if !foundFalse {
							if !v.value {
								foundFalse = true
								filters.showAlerts.updateValue(true, forKey: v.key)
							}
						}
						else {
							filters.showAlerts.updateValue(true, forKey: v.key)
						}
					}
					if !foundFalse {
						for v in filters.showAlerts {
							filters.showAlerts.updateValue(false, forKey: v.key)
						}
390
391
					}
				}
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
				else if((sender as! UITableViewCell).textLabel?.text! == "Campuses") {
					var foundFalse = false
					for v in filters.onlyFromCampuses {
						if !foundFalse {
							if !v.value {
								foundFalse = true
								filters.onlyFromCampuses.updateValue(true, forKey: v.key)
							}
						}
						else {
							filters.onlyFromCampuses.updateValue(true, forKey: v.key)
						}
					}
					if !foundFalse {
						for v in filters.onlyFromCampuses {
							filters.onlyFromCampuses.updateValue(false, forKey: v.key)
						}
					}
				}
411
				else {
412
					var foundFalse = false
Zach Knox's avatar
Zach Knox committed
413
					for v in filters.onlyFromLocations {
414
415
416
417
418
419
420
421
422
423
424
425
426
427
						if !foundFalse {
							if !v.value {
								foundFalse = true
								filters.onlyFromLocations.updateValue(true, forKey: v.key)
							}
						}
						else {
							filters.onlyFromLocations.updateValue(true, forKey: v.key)
						}
					}
					if !foundFalse {
						for v in filters.onlyFromLocations {
							filters.onlyFromLocations.updateValue(false, forKey: v.key)
						}
428
429
430
431
432
433
434
435
436
437
438
					}
				}
				return true
			}
			
			destination.getFunc = get
			destination.selectFunc = selectFunc
			destination.selectAllFunc = selectAllFunc
			
			
		}
439
		
440
441
442
443
        // Pass the selected object to the new view controller.
    }

}