-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathMenuViewController.swift
More file actions
180 lines (138 loc) · 6.65 KB
/
MenuViewController.swift
File metadata and controls
180 lines (138 loc) · 6.65 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
//
// MenuViewController.swift
// UCLA Radio
//
// Created by Christopher Laganiere on 6/3/16.
// Copyright © 2016 UCLA Student Media. All rights reserved.
//
import Foundation
import UIKit
private let reuseIdentifier = "MenuCell"
private let headerReuseIdentifier = "MenuHeaderView"
private let sectionInset: CGFloat = 25
private let itemSpacing: CGFloat = 15
class MenuItem {
let title: String
let storyboardID: String? // storyboard ID for view controller to push when tapped (or nil)
init(title: String, storyboardID: String?) {
self.title = title
self.storyboardID = storyboardID
}
}
fileprivate let defaultItems = [
MenuItem(title: "Schedule", storyboardID: ScheduleViewController.storyboardID),
MenuItem(title: "DJs", storyboardID: DJListViewController.storyboardID),
MenuItem(title: "About", storyboardID: AboutViewController.storyboardID)
]
fileprivate let giveawayItem = MenuItem(title: "Tickets", storyboardID: EventsViewController.storyboardID)
class MenuViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
private var items = [MenuItem]()
var tableView = UITableView(frame: CGRect.zero, style: .grouped)
var triangleView: UIView!
// MARK: - ViewController Life Cycle
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = UIColor.clear
triangleView = UIView()
view.addSubview(triangleView)
triangleView.translatesAutoresizingMaskIntoConstraints = false
items = defaultItems
NotificationCenter.default.addObserver(self, selector: #selector(didUpdateGiveawaysNotification), name: RadioAPI.updatedGiveawaysNotificationName, object: nil)
tableView.dataSource = self
tableView.delegate = self
tableView.backgroundColor = UIColor.clear
tableView.alwaysBounceVertical = true
tableView.register(MenuTableViewCell.self, forCellReuseIdentifier: reuseIdentifier)
tableView.register(MenuSectionHeaderView.self, forHeaderFooterViewReuseIdentifier: headerReuseIdentifier)
tableView.backgroundColor = UIColor.clear
tableView.contentInset = UIEdgeInsets(top: 40, left: 0, bottom: 0, right: 0)
tableView.separatorStyle = .none
view.addSubview(tableView)
tableView.translatesAutoresizingMaskIntoConstraints = false
view.addConstraints(preferredConstraints())
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
if let navigationController = navigationController {
navigationController.setNavigationBarHidden(true, animated: true)
}
// set the background image
UIGraphicsBeginImageContext(self.view.frame.size)
UIImage(named: "background")?.draw(in: self.view.bounds)
if let image = UIGraphicsGetImageFromCurrentImageContext(){
UIGraphicsEndImageContext()
self.view.backgroundColor = UIColor(patternImage: image)
}
AnalyticsManager.sharedInstance.trackPageWithValue("Menu / Now Playing")
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
if let navigationController = navigationController {
navigationController.setNavigationBarHidden(false, animated: true)
}
}
// MARK: - Actions
func pushViewController(_ storyboardID: String) {
let viewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: storyboardID)
if let navigationController = navigationController {
navigationController.pushViewController(viewController, animated: true)
}
}
// MARK: - Data
@objc func didUpdateGiveawaysNotification(notification: Notification) {
if let userInfo = notification.userInfo,
let hasGiveaways = userInfo["hasGiveaways"] as? Bool,
hasGiveaways {
// should add tickets row if not already there
items = defaultItems
items.insert(giveawayItem, at: 2)
} else {
items = defaultItems
}
tableView.reloadData()
}
// MARK: - UITableViewDataSource
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return items.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
return tableView.dequeueReusableCell(withIdentifier: reuseIdentifier, for: indexPath)
}
// MARK: - UITableViewDelegate
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return MenuTableViewCell.preferredHeight()
}
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
let item = items[(indexPath as NSIndexPath).row]
if let menuCell = cell as? MenuTableViewCell {
menuCell.styleForMenuItem(item)
}
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
let item = items[(indexPath as NSIndexPath).row]
if let storyboardID = item.storyboardID {
pushViewController(storyboardID)
}
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return MenuSectionHeaderView.preferredHeight()
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
return tableView.dequeueReusableHeaderFooterView(withIdentifier: headerReuseIdentifier)
}
// MARK: - Layout
func preferredConstraints() -> [NSLayoutConstraint] {
var constraints = [NSLayoutConstraint]()
// table view
constraints += NSLayoutConstraint.constraints(withVisualFormat: "H:|[table]|", options: [], metrics: nil, views: ["table": tableView])
constraints += NSLayoutConstraint.constraints(withVisualFormat: "V:|[table]|", options: [], metrics: nil, views: ["table": tableView])
// trianglify view
constraints += NSLayoutConstraint.constraints(withVisualFormat: "H:|[triangles]|", options: [], metrics: nil, views: ["triangles": triangleView])
constraints += NSLayoutConstraint.constraints(withVisualFormat: "V:|[triangles]|", options: [], metrics: nil, views: ["triangles": triangleView])
return constraints
}
}