-
Notifications
You must be signed in to change notification settings - Fork 6
/
CaseIterableViewController.swift
209 lines (175 loc) · 5.79 KB
/
CaseIterableViewController.swift
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
//
// CaseIterableViewController.swift
// iOSDemo
//
// Created by Ellen Shapiro (Work) on 9/21/18.
//
import UIKit
import SweetUIKit
enum Section: String, CaseIterable {
case user
case app
case logout
var title: String {
return rawValue.uppercased()
}
}
enum UserRow: CaseIterable {
case username
case emailAddress
var title: String {
switch self {
case .username:
return "User name"
case .emailAddress:
return "Email address"
}
}
var value: String {
switch self {
case .username:
return "foobar"
case .emailAddress:
return "[email protected]"
}
}
}
enum AppRow: CaseIterable {
case currentVersion
case contactDeveloper
case leaveReview
var title: String {
switch self {
case .currentVersion:
return Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString") as? String ?? "(Version unknown)"
case .contactDeveloper:
return "Contact the developer"
case .leaveReview:
return "Leave a review"
}
}
var accessoryType: UITableViewCell.AccessoryType {
switch self {
case .currentVersion:
return .none
case .contactDeveloper,
.leaveReview:
return .disclosureIndicator
}
}
}
enum LogoutRow: String, CaseIterable {
case reset
case logout
var title: String {
return rawValue.capitalized
}
var textColor: UIColor {
switch self {
case .reset:
return .darkText
case .logout:
return .red
}
}
}
class CaseIterableViewController: UIViewController {
private lazy var cellIdentitfier = String(describing: self)
lazy var tableView: UITableView = {
let tableView = UITableView(frame: .zero, style: .grouped)
tableView.translatesAutoresizingMaskIntoConstraints = false
tableView.dataSource = self
tableView.delegate = self
tableView.register(UITableViewCell.self, forCellReuseIdentifier: self.cellIdentitfier)
return tableView
}()
override func viewDidLoad() {
super.viewDidLoad()
title = "CaseIterable Example"
view.addSubview(tableView)
NSLayoutConstraint.activate([
self.tableView.topAnchor.constraint(equalTo: self.view.topAnchor),
self.tableView.leftAnchor.constraint(equalTo: self.view.leftAnchor),
self.tableView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor),
self.tableView.rightAnchor.constraint(equalTo: self.view.rightAnchor),
])
}
}
extension CaseIterableViewController: UITableViewDataSource {
func numberOfSections(in tableView: UITableView) -> Int {
return Section.allCases.count
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
switch Section.forIndex(section) {
case .user:
return UserRow.allCases.count
case .app:
return AppRow.allCases.count
case .logout:
return LogoutRow.allCases.count
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentitfier, for: indexPath)
cell.accessoryType = .none
cell.textLabel?.textColor = .darkText
switch Section.forSection(at: indexPath) {
case .user:
let row = UserRow.forRow(at: indexPath)
cell.textLabel?.text = row.title
cell.detailTextLabel?.text = row.value
case .app:
let row = AppRow.forRow(at: indexPath)
cell.textLabel?.text = row.title
cell.accessoryType = row.accessoryType
case .logout:
let row = LogoutRow.forRow(at: indexPath)
cell.textLabel?.text = row.title
cell.textLabel?.textColor = row.textColor
}
return cell
}
}
extension CaseIterableViewController: UITableViewDelegate {
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
switch Section.forSection(at: indexPath) {
case .user:
switch UserRow.forRow(at: indexPath) {
case .username:
showAlert(with: "Show username edit")
case .emailAddress:
showAlert(with: "Show email address edit")
}
case .app:
switch AppRow.forRow(at: indexPath) {
case .currentVersion:
showAlert(with: "Tapped Current Version")
case .contactDeveloper:
showAlert(with: "Start an email to the developer")
case .leaveReview:
showAlert(with: "Send user to app store to leave a review")
}
case .logout:
switch LogoutRow.forRow(at: indexPath) {
case .reset:
showAlert(with: "Reset user settings but don't log them out")
case .logout:
showAlert(with: "Log out the user!")
}
}
}
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return Section.forIndex(section).title
}
private func showAlert(with title: String) {
let alert = UIAlertController(title: title,
message: nil,
preferredStyle: .alert)
let okAction = UIAlertAction(title: "OK",
style: .default,
handler: nil)
alert.addAction(okAction)
present(alert, animated: true)
}
}