-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathTableViewKitDelegate.swift
More file actions
177 lines (143 loc) · 7.33 KB
/
Copy pathTableViewKitDelegate.swift
File metadata and controls
177 lines (143 loc) · 7.33 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
import UIKit
open class TableViewKitDelegate: NSObject, UITableViewDelegate {
open unowned var manager: TableViewManager
open weak var scrollDelegate: UIScrollViewDelegate?
private var sections: ObservableArray<TableSection> { return manager.sections }
public required init(manager: TableViewManager) {
self.manager = manager
}
/// Implementation of UITableViewDelegate
open func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
guard let currentItem = manager.item(at: indexPath) as? Selectable else { return }
currentItem.didSelect()
}
/// Implementation of UITableViewDelegate
open func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return height(at: indexPath) ?? tableView.rowHeight
}
/// Implementation of UITableViewDelegate
open func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return height(for: { $0.header }, inSection: section) ?? tableView.sectionHeaderHeight
}
/// Implementation of UITableViewDelegate
open func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return height(for: { $0.footer }, inSection: section) ?? tableView.sectionFooterHeight
}
/// Implementation of UITableViewDelegate
open func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
return estimatedHeight(at: indexPath) ?? tableView.estimatedRowHeight
}
/// Implementation of UITableViewDelegate
open func tableView(_ tableView: UITableView, estimatedHeightForHeaderInSection section: Int) -> CGFloat {
return estimatedHeight(for: { $0.header }, inSection: section) ?? tableView.estimatedSectionHeaderHeight
}
/// Implementation of UITableViewDelegate
open func tableView(_ tableView: UITableView, estimatedHeightForFooterInSection section: Int) -> CGFloat {
return estimatedHeight(for: { $0.footer }, inSection: section) ?? tableView.estimatedSectionHeaderHeight
}
/// Implementation of UITableViewDelegate
open func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let view = view(for: { $0.header }, inSection: section)
if let zPosition = manager.zPositionForHeader(in: section) {
view?.layer.zPosition = zPosition
}
return view
}
/// Implementation of UITableViewDelegate
open func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
let view = view(for: { $0.footer }, inSection: section)
if let zPosition = manager.zPositionForFooter(in: section) {
view?.layer.zPosition = zPosition
}
return view
}
/// Implementation of UITableViewDelegate
open func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
guard let item = manager.item(at: indexPath) as? Editable else { return nil }
return item.actions
}
/// Implementation of UITableViewDelegate
// swiftlint:disable:next line_length
public func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
guard manager.itemExists(at: indexPath),
let item = manager.item(at: indexPath) as? Editable else { return nil }
return item.trailingConfiguration
}
/// Implementation of UITableViewDelegate
// swiftlint:disable:next line_length
public func tableView(_ tableView: UITableView, leadingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
guard manager.itemExists(at: indexPath),
let item = manager.item(at: indexPath) as? Editable else { return nil }
return item.leadingConfiguration
}
/// Implementation of UITableViewDelegate
open func tableView(_ tableView: UITableView, shouldShowMenuForRowAt indexPath: IndexPath) -> Bool {
return manager.item(at: indexPath) is ActionPerformable
}
/// Implementation of UITableViewDelegate
// swiftlint:disable:next line_length
open func tableView(_ tableView: UITableView, canPerformAction action: Selector, forRowAt indexPath: IndexPath, withSender sender: Any?) -> Bool {
guard let item = manager.item(at: indexPath) as? ActionPerformable,
let action = ItemAction(action: action) else { return false }
return item.canPerformAction(action)
}
/// Implementation of UITableViewDelegate
// swiftlint:disable:next line_length
open func tableView(_ tableView: UITableView, performAction action: Selector, forRowAt indexPath: IndexPath, withSender sender: Any?) {
guard let item = manager.item(at: indexPath) as? ActionPerformable,
let action = ItemAction(action: action) else { return }
item.performAction(action)
}
/// Implementation of UIScrollViewDelegate
open func scrollViewDidScroll(_ scrollView: UIScrollView) {
scrollDelegate?.scrollViewDidScroll?(scrollView)
}
/// Implementation of UIScrollViewDelegate
open func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
scrollDelegate?.scrollViewWillBeginDragging?(scrollView)
}
/// Implementation of UIScrollViewDelegate
// swiftlint:disable:next line_length
open func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
scrollDelegate?.scrollViewWillEndDragging?(
scrollView,
withVelocity: velocity,
targetContentOffset: targetContentOffset)
}
/// Implementation of UIScrollViewDelegate
open func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
scrollDelegate?.scrollViewDidEndDragging?(scrollView, willDecelerate: decelerate)
}
fileprivate func view(for key: (TableSection) -> HeaderFooterView, inSection section: Int) -> UIView? {
guard case .view(let item) = key(sections[section]) else { return nil }
let drawer = type(of: item).drawer
let view = drawer.view(in: manager, with: item)
drawer.draw(view, with: item)
return view
}
fileprivate func estimatedHeight(for key: (TableSection) -> HeaderFooterView, inSection section: Int) -> CGFloat? {
let item = key(sections[section])
switch item {
case .view(let view):
guard let height = view.height else { return nil }
return height.estimated
case .title:
return 1.0
default:
return nil
}
}
fileprivate func estimatedHeight(at indexPath: IndexPath) -> CGFloat? {
guard let height = manager.item(at: indexPath).height else { return nil }
return height.estimated
}
fileprivate func height(for key: (TableSection) -> HeaderFooterView, inSection section: Int) -> CGFloat? {
guard case .view(let view) = key(sections[section]), let value = view.height
else { return nil }
return value.height
}
fileprivate func height(at indexPath: IndexPath) -> CGFloat? {
guard let value = manager.item(at: indexPath).height else { return nil }
return value.height
}
}