forked from asuc-octo/berkeley-mobile-ios
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathResourceGroupViewController.swift
More file actions
482 lines (391 loc) · 17.2 KB
/
ResourceGroupViewController.swift
File metadata and controls
482 lines (391 loc) · 17.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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
import UIKit
import DropDown
import Material
import Firebase
//fileprivate let kColorNavy = UIColor(red: 23/255.0, green: 85/255.0, blue: 122/255.0, alpha: 1)
fileprivate let kColorNavy = UIColor(red: 0, green: 51/255.0, blue: 102/255.0, alpha: 1)
/**
* Displays a single `Resource` type as rows of separate groups/categories.
* Each row is a horizontally scrolling carousel of tiles representing one `Resource` instance.
*/
class ResourceGroupViewController: UIViewController, IBInitializable, UITableViewDelegate, UITableViewDataSource, UISearchBarDelegate
{
// Data
// private var types: [Resource.Type]!
var types: [Resource.Type]!
private var resources = [String : [Resource]]()
var searchableItems: [SearchItem] = []
var searchResults: [SearchItem] = []
var already_loaded = false
var loading_label: UILabel?
// UI
override var preferredStatusBarStyle: UIStatusBarStyle { return .lightContent }
private weak var navbar: UINavigationBar?
private var pseudoNavbar: UIView!
private var searchBar = UISearchBar()
let searchDropDown = DropDown()
@IBOutlet private weak var tableView: UITableView!
private var activityIndicator: UIActivityIndicatorView!
var diningRes: DiningHall!
// ========================================
// MARK: - IBInitializable
// ========================================
typealias IBComponent = ResourceGroupViewController
static var componentID: String { return className(IBComponent.self) }
static func fromIB() -> IBComponent
{
return UIStoryboard.resource.instantiateViewController(withIdentifier: self.componentID) as! IBComponent
}
func setGroup(_ group: ResourceGroup)
{
guard viewIfLoaded == nil, types == nil else {
return
}
types = group.types
}
// ========================================
// MARK: - UIViewController
// ========================================
/**
* Setup any remaining UI components not done in IB,
* and fetch resources to display.
*/
override func viewDidLoad()
{
super.viewDidLoad()
self.setupNavigationBar()
self.setupSearchBar()
self.setupActivityIndicator()
// Connect tableView and load data
self.tableView.delegate = self
self.tableView.dataSource = self
self.activityIndicator.startAnimating()
types.forEach
{ type in
type.dataSource?.fetchResources
{ list in
DispatchQueue.main.async { self.activityIndicator.stopAnimating() }
guard let nonEmptyList = list else
{
// Error
return print()
}
self.resources[type.typeString] = nonEmptyList
if (self.resources.count == self.types.count && self.already_loaded != true)
{
self.already_loaded = true
self.removeLoadingView()
DispatchQueue.main.async { self.tableView.reloadData() }
}
}
}
if UIDevice.current.screenType.rawValue == "iPhone 5, iPhone 5s, iPhone 5c or iPhone SE"{
//edit
self.tableView.rowHeight = 220
}
}
/// Configure statusBar, navigationBar, and highlight the tab icon.
override func viewWillAppear(_ animated: Bool)
{
super.viewWillAppear(animated);
for some_type in self.types! {
if (some_type == berkeleyMobileiOS.Library.self) {
Analytics.logEvent("opened_library_screen", parameters: nil)
} else if (some_type == berkeleyMobileiOS.CampusResource.self) {
Analytics.logEvent("opened_resource_screen", parameters: nil)
} else if (some_type == berkeleyMobileiOS.DiningHall.self) {
Analytics.logEvent("opened_food_screen", parameters: nil)
} else if (some_type == berkeleyMobileiOS.Gym.self) {
Analytics.logEvent("opened_gym_screen", parameters: nil)
} else if (some_type == berkeleyMobileiOS.GymClass.self) {
Analytics.logEvent("opened_gym_class_screen", parameters: nil)
} else {
print("Error")
}
}
self.navbar?.hideHairline = true
self.navbar?.setTransparent(true)
setStatusBarStyle(self.preferredStatusBarStyle)
for vc in (self.pageTabBarController?.viewControllers)! {
if (type(of: vc) == BearTransitNavigationController.self) {
vc.pageTabBarItem.image = #imageLiteral(resourceName: "beartransit").withRenderingMode(.alwaysTemplate)
}
}
}
override func viewDidDisappear(_ animated: Bool) {
removeLoadingView()
}
override func viewDidAppear(_ animated: Bool) {
if (!already_loaded) {
addLoadingView()
}
}
/// Place the pseudoNavbar backdrop behind the navbar.
override func viewDidLayoutSubviews()
{
super.viewDidLayoutSubviews()
let view = self.view!
let navbarMaxY = self.navbar?.frame.maxY ?? 0
let eliminateSpace: CGFloat = 12
self.pseudoNavbar.frame = CGRect(x: 0, y: -navbarMaxY - eliminateSpace, width: view.width, height: navbarMaxY + 10)
}
// ========================================
// MARK: - Setup
// ========================================
/**
* Makes the original UINavigationBar clear and inserts another view with solid color.
* This is to prevent strage transitions when pushing/popping ViewControllers with clear navbar.
*
* - Attention: Transparent UINavigationBar and pseudoNavbar backdrop is a temporary solution.
* NavigationController and bar should be reworked into a custom class.
*/
private func setupNavigationBar()
{
// Insert a pseudo-background
self.pseudoNavbar = UIView()
self.pseudoNavbar.backgroundColor = kColorNavy
self.view!.addSubview(self.pseudoNavbar)
// Configure the navigationBar.
self.navbar = self.navigationController?.navigationBar
guard let navbar = self.navbar else {
return
}
// White content
navbar.tintColor = .white
navbar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.white]
self.navigationItem.backBarButtonItem = UIBarButtonItem.init(title: "", style: .plain, target: nil, action: nil)
}
// ========================================
// MARK: - UISearchBar Setup
// ========================================
/**
*
*/
private func setupSearchBar()
{
// Search bar
var backView = UIView(frame: CGRect(x: 0, y: 0, width: (self.navigationController?.navigationBar.frame.width)!, height: (self.navigationController?.navigationBar.frame.height)!))
let image : UIImage = #imageLiteral(resourceName: "bearsmallmed")
let imageView = UIImageView(frame: CGRect(x: ((self.navigationController?.navigationBar.frame.width)! - 36) / 2, y: 2, width: 36, height: 19))
imageView.contentMode = .scaleAspectFit
imageView.image = image
// self.navigationItem.titleView = imageView
backView.addSubview(imageView)
self.navigationItem.titleView = backView
// let iv = UIImageView.init(frame: CGRect.init(x: 0, y: 0, width: 24, height: 24))
// iv.image = #imageLiteral(resourceName: "bear")
// self.navigationItem.titleView = iv
// let textFieldInsideUISearchBar = searchBar.value(forKey: "searchField") as? UITextField
// textFieldInsideUISearchBar?.borderStyle = .none
// textFieldInsideUISearchBar?.backgroundColor = kColorNavy
// textFieldInsideUISearchBar?.textColor = UIColor.white
// searchBar.delegate = self
//// searchDropDown.frame = CGRect(x: 0, y: -20, width: searchDropDown.frame.width, height: searchDropDown.frame.height)
// searchDropDown.anchorView = self.navigationItem.titleView
// searchDropDown.bottomOffset = CGPoint(x: 0, y: 50)
// // searchDropDown.dismissMode = .manual
// searchDropDown.selectionAction = selectedRow
// searchDropDown.cancelAction = cancelDropDown
}
func selectedRow(index: Int, name: String) -> Void
{
searchDropDown.hide()
searchBar.resignFirstResponder()
let selected = searchResults[index]
selected.detailedData
{ (resource: Resource?) in
guard let res = resource else {
return
}
let ref = type(of: res).typeString + "DetailSegue"
if type(of: res).typeString == "DiningHall" {
self.diningRes = res as? DiningHall
}
self.performSegue(withIdentifier: ref, sender: res)
}
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let identifier = segue.identifier {
if identifier == "DiningHallDetailSegue" {
if let dest = segue.destination as? DiningMenuViewController {
let menuID = className(DiningMenuViewController.self)
let hall = DiningHall(name: diningRes.name, imageLink: nil, shifts: diningRes.meals)
let menuControllers = MealType.allValues.map
{ type in
let vc = storyboard!.instantiateViewController(withIdentifier: menuID) as! DiningMenuViewController
vc.setData(type: type, shift: hall.meals[type]!)
let barItem = vc.pageTabBarItem
barItem.titleColor = kColorNavy
barItem.pulseColor = kColorNavy//UIColor.white
barItem.title = type.name
barItem.titleLabel?.font = RobotoFont.regular(with: 16)
}
}
}
else if identifier == "LibraryDetailSegue" {
// if let dest = segue.destination as? SettingsViewController {
// // do stuff in the settingsVC before it loads
// }
}
else if identifier == "GymDetailSegue" {
// if segue.destination is GymDetailViewController {
// let gym = Gym(name: , address: String, imageLink: String?, openingTimeToday: Date?, closingTimeToday: Date?)
// }
}
}
}
func cancelDropDown() -> Void {
searchBar.showsCancelButton = false
searchBar.resignFirstResponder()
}
/// Configure and add the ActivityIndicator.
private func setupActivityIndicator()
{
self.activityIndicator = UIActivityIndicatorView(activityIndicatorStyle: .gray)
self.activityIndicator.hidesWhenStopped = true
self.activityIndicator.center = self.view!.center
self.activityIndicator.autoresizingMask = [.flexibleRightMargin, .flexibleBottomMargin]
// self.view.addSubview(self.activityIndicator)
}
// ========================================
// MARK: - UITableViewDataSource
// ========================================
/// Return the number of groups/categories for Dining.
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
// TODO: temp returning 1, while backend is not up to date.
return self.resources.count
}
/// Pass the DiningGroupCell a name for the category, a list of halls, and a callback handler.
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
print("Here1" )
let cell = tableView.dequeueReusableCell(withIdentifier: className(ResourceGroupCell.self)) as! ResourceGroupCell
print("Here2" )
let type = types[indexPath.row]
print("HERE3")
if let resource_data = resources[type.typeString] {
let data = (type.displayName(pluralized: true), resource_data, Optional(didSelectResource))
print("HERE4")
cell.setData(data)
} else {
print("Here5")
}
return cell
}
// ========================================
// MARK: - UITableViewDelegate
// ========================================
/// When a row (not tile) is tapped, present the corresponding `ResourceMapListViewController`.
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
{
// TODO: MapList disabled temporarily.
}
/// When a tile is tapped, present the corresponding `ResourceDetailViewController`.
func didSelectResource(_ resource: Resource)
{
guard let detailProvider = type(of: resource).detailProvider?.newInstance() else
{
return
}
detailProvider.resource = resource
let container = ResourceContainerController.fromIB()
container.detailProvider = detailProvider
container.modalPresentationStyle = .overFullScreen
present(container, animated: true, completion: nil)
}
// ========================================
// MARK: - UISearchDelegate
// ========================================
/**
* TODO: Change the dismiss behavior.
*/
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
searchBar.resignFirstResponder()
filterList(searchBar.text)
}
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
filterList(searchText)
}
func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
searchBar.showsCancelButton = true
if searchableItems.count == 0 {
SearchDataSource.fetchSearchItems
{ (_ searchItems: [SearchItem]?) in
self.searchableItems = searchItems!.filter { item in item.category != "Sports Schedule"}
self.searchableItems = self.searchableItems.filterDuplicates { $0.name == $1.name }
self.searchResults = self.searchableItems
self.searchDropDown.dataSource = self.searchResults.map { item in item.name }
self.searchDropDown.show()
}
}
else {
self.searchDropDown.dataSource = self.searchResults.map { item in item.name }
self.searchDropDown.show()
}
}
func searchBarTextDidEndEditing(_ searchBar: UISearchBar) {
searchBar.showsCancelButton = false
}
func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
searchBar.resignFirstResponder()
searchDropDown.hide()
searchBar.showsCancelButton = false
}
func filterList(_ searchText: String?) {
if searchText == nil || searchText! == "" {
searchDropDown.dataSource = searchableItems.map { item in item.name }
}
else {
let keyword = searchText!.lowercased()
searchResults = searchableItems.filter({ item in (item.name.lowercased().contains(keyword)) })
searchDropDown.dataSource = searchResults.map { item in item.name }
}
}
func removeLoadingView() {
if let l = self.loading_label {
l.removeFromSuperview()
}
}
func addLoadingView() {
self.loading_label = UILabel.init(frame: CGRect.init(x: 0, y: 0, width: 60, height: 20))
self.loading_label!.text = "Loading"
self.loading_label!.textColor = Color.grey.lighten1
self.loading_label!.font = UIFont.init(name: "System", size: 16)
self.loading_label!.sizeToFit()
self.loading_label!.center = view.center
view.addSubview(self.loading_label!)
}
}
extension UIDevice {
var iPhoneX: Bool {
return UIScreen.main.nativeBounds.height == 2436
}
var iPhone: Bool {
return UIDevice.current.userInterfaceIdiom == .phone
}
enum ScreenType: String {
case iPhone4_4S = "iPhone 4 or iPhone 4S"
case iPhones_5_5s_5c_SE = "iPhone 5, iPhone 5s, iPhone 5c or iPhone SE"
case iPhones_6_6s_7_8 = "iPhone 6, iPhone 6S, iPhone 7 or iPhone 8"
case iPhones_6Plus_6sPlus_7Plus_8Plus = "iPhone 6 Plus, iPhone 6S Plus, iPhone 7 Plus or iPhone 8 Plus"
case iPhoneX = "iPhone X"
case unknown
}
var screenType: ScreenType {
switch UIScreen.main.nativeBounds.height {
case 960:
return .iPhone4_4S
case 1136:
return .iPhones_5_5s_5c_SE
case 1334:
return .iPhones_6_6s_7_8
case 1920, 2208:
return .iPhones_6Plus_6sPlus_7Plus_8Plus
case 2436:
return .iPhoneX
default:
return .unknown
}
}
}