Repository navigation
Expand file tree
/
Copy pathOWSTableViewController.swift
More file actions
326 lines (255 loc) · 11.1 KB
/
Copy pathOWSTableViewController.swift
File metadata and controls
326 lines (255 loc) · 11.1 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
//
// Copyright 2023 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
//
import PureLayout
import SignalServiceKit
public protocol OWSTableViewControllerDelegate: AnyObject {
func tableViewWillBeginDragging(_ tableView: UITableView)
}
open class OWSTableViewController: OWSViewController {
public weak var delegate: OWSTableViewControllerDelegate?
public var contents = OWSTableContents() {
didSet {
if oldValue !== contents {
applyContents()
}
}
}
public let tableView = UITableView(frame: .zero, style: .grouped)
public var shouldAvoidKeyboard: Bool = false
public var layoutMarginsRelativeTableContent: Bool = false
private enum Constants {
static let cellReuseIdentifier = "OWSTableCellIdentifier"
}
public override func loadView() {
super.loadView()
tableView.delegate = self
tableView.dataSource = self
tableView.tableFooterView = UIView(frame: .zero)
view.addSubview(tableView)
if shouldAvoidKeyboard {
tableView.autoPinEdgesToSuperviewEdges(with: .zero, excludingEdge: .bottom)
tableView.autoPinEdge(.bottom, to: .bottom, of: keyboardLayoutGuideView)
} else {
tableView.autoPinEdgesToSuperviewEdges()
}
tableView.register(UITableViewCell.self, forCellReuseIdentifier: Constants.cellReuseIdentifier)
configureTableViewLayoutMargins()
applyContents()
applyTheme()
}
open override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
tableView.tableFooterView = UIView(frame: .zero)
}
/// Reloads table contents when content size category changes.
///
/// Does not reload header/footer views. Subclasses that use header/footer
/// views that need to update in response to content size category changes
/// should override this method to do so manually.
public override func contentSizeCategoryDidChange() {
super.contentSizeCategoryDidChange()
// Reload when content size might need to change.
applyContents()
}
// MARK: Appearance
/// Applies theme and reloads table contents.
///
/// Does not reload header/footer views. Subclasses that use header/footer
/// views that need to update in response to theme changes should override
/// this method to do so manually.
public override func themeDidChange() {
super.themeDidChange()
applyTheme()
tableView.reloadData()
}
open func applyTheme() {
view.backgroundColor = Theme.backgroundColor
tableView.backgroundColor = Theme.backgroundColor
tableView.separatorColor = Theme.cellSeparatorColor
}
private func configureTableViewLayoutMargins() {
guard layoutMarginsRelativeTableContent else { return }
tableView.preservesSuperviewLayoutMargins = true
tableView.layoutMargins = .zero
}
private static var sectionHeaderFooterTextFont: UIFont {
return UIFont.preferredFont(forTextStyle: .caption1)
}
// MARK: Contents
private func applyContents() {
if let title = contents.title?.nilIfEmpty {
self.title = title
}
tableView.reloadData()
}
private func sectionForIndex(_ index: Int) -> OWSTableSection {
return contents.sections[index]
}
private func itemForIndexPath(_ indexPath: IndexPath) -> OWSTableItem {
return sectionForIndex(indexPath.section).items[indexPath.item]
}
// MARK: Presentation
public func present(fromViewController viewController: UIViewController) {
let navigationController = OWSNavigationController(rootViewController: self)
navigationItem.leftBarButtonItem = .systemItem(.stop) { [weak self] in
self?.dismiss(animated: true)
}
viewController.present(navigationController, animated: true)
}
}
extension OWSTableViewController: UITableViewDataSource, UITableViewDelegate {
public func numberOfSections(in tableView: UITableView) -> Int {
return contents.sections.count
}
public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return sectionForIndex(section).itemCount
}
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let item = itemForIndexPath(indexPath)
item.tableViewController = self
if let customCell = item.getOrBuildCustomCell(tableView) {
return customCell
}
let cell = tableView.dequeueReusableCell(withIdentifier: Constants.cellReuseIdentifier, for: indexPath)
OWSTableItem.configureCell(cell)
cell.textLabel?.text = item.title
return cell
}
public func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if let customHeight = itemForIndexPath(indexPath).customRowHeight {
return customHeight
}
return UITableView.automaticDimension
}
public func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let section = sectionForIndex(section)
if let customHeaderView = section.customHeaderView {
return customHeaderView
}
let hasPlainTextTitle = !section.headerTitle.isEmptyOrNil
let hasAttributedTextTitle = !(section.headerAttributedTitle?.string ?? "").isEmpty
if hasPlainTextTitle || hasAttributedTextTitle {
let textView = LinkingTextView()
textView.textColor = Theme.secondaryTextAndIconColor
textView.font = OWSTableViewController.sectionHeaderFooterTextFont
if hasAttributedTextTitle {
textView.attributedText = section.headerAttributedTitle
} else {
textView.text = section.headerTitle?.uppercased()
}
let sectionHeaderView = UIView()
sectionHeaderView.addSubview(textView)
textView.autoPinHeightToSuperview()
if layoutMarginsRelativeTableContent {
sectionHeaderView.preservesSuperviewLayoutMargins = true
textView.autoPinWidthToSuperviewMargins()
textView.textContainerInset = UIEdgeInsets(top: 16, leading: 0, bottom: 6, trailing: 0)
} else {
textView.autoPinWidthToSuperview()
let tableEdgeInset: CGFloat = UIDevice.current.isPlusSizePhone ? 20 : 16
textView.textContainerInset = UIEdgeInsets(top: 16, leading: tableEdgeInset, bottom: 6, trailing: tableEdgeInset)
}
return sectionHeaderView
}
return nil
}
public func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
let tableSection = sectionForIndex(section)
if let customFooterView = tableSection.customFooterView {
return customFooterView
}
let hasPlainTextTitle = !tableSection.footerTitle.isEmptyOrNil
let hasAttributedTextTitle = !(tableSection.footerAttributedTitle?.string ?? "").isEmpty
if hasPlainTextTitle || hasAttributedTextTitle {
let textView = LinkingTextView()
textView.textColor = .ows_gray45
textView.font = OWSTableViewController.sectionHeaderFooterTextFont
textView.linkTextAttributes = [
.foregroundColor: Theme.accentBlueColor,
.underlineStyle: NSUnderlineStyle(),
.font: OWSTableViewController.sectionHeaderFooterTextFont
]
if hasAttributedTextTitle {
textView.attributedText = tableSection.footerAttributedTitle
} else {
textView.text = tableSection.footerTitle
}
let sectionFooterView = UIView()
sectionFooterView.addSubview(textView)
textView.autoPinHeightToSuperview()
if layoutMarginsRelativeTableContent {
sectionFooterView.preservesSuperviewLayoutMargins = true
textView.autoPinWidthToSuperviewMargins()
textView.textContainerInset = UIEdgeInsets(top: 16, leading: 0, bottom: 6, trailing: 0)
} else {
textView.autoPinWidthToSuperview()
let tableEdgeInset: CGFloat = UIDevice.current.isPlusSizePhone ? 20 : 16
textView.textContainerInset = UIEdgeInsets(top: 16, leading: tableEdgeInset, bottom: 6, trailing: tableEdgeInset)
}
return sectionFooterView
}
return nil
}
public func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
let tableSection = sectionForIndex(section)
if let customHeight = tableSection.customHeaderHeight {
owsAssertDebug(customHeight > 0 || customHeight == UITableView.automaticDimension)
return customHeight
}
if self.tableView(tableView, viewForHeaderInSection: section) != nil {
return UITableView.automaticDimension
}
return 0
}
public func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
let tableSection = sectionForIndex(section)
if let customHeight = tableSection.customFooterHeight {
owsAssertDebug(customHeight > 0 || customHeight == UITableView.automaticDimension)
return customHeight
}
if self.tableView(tableView, viewForFooterInSection: section) != nil {
return UITableView.automaticDimension
}
return 0
}
public func tableView(_ tableView: UITableView, willDeselectRowAt indexPath: IndexPath) -> IndexPath? {
let item = itemForIndexPath(indexPath)
guard item.actionBlock != nil else { return nil }
return indexPath
}
public func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if let actionBlock = itemForIndexPath(indexPath).actionBlock {
actionBlock()
}
}
// MARK: Index
public func tableView(_ tableView: UITableView, sectionForSectionIndexTitle title: String, at index: Int) -> Int {
if let sectionForSectionIndexTitleBlock = contents.sectionForSectionIndexTitleBlock {
return sectionForSectionIndexTitleBlock(title, index)
}
return 0
}
public func sectionIndexTitles(for tableView: UITableView) -> [String]? {
if let sectionIndexTitlesForTableViewBlock = contents.sectionIndexTitlesForTableViewBlock {
return sectionIndexTitlesForTableViewBlock()
}
return nil
}
// MARK: Editing
public override func setEditing(_ editing: Bool, animated: Bool) {
super.setEditing(editing, animated: animated)
tableView.setEditing(editing, animated: animated)
}
public override var isEditing: Bool {
didSet {
tableView.isEditing = isEditing
}
}
}
extension OWSTableViewController: UIScrollViewDelegate {
public func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
delegate?.tableViewWillBeginDragging(tableView)
}
}