-
Notifications
You must be signed in to change notification settings - Fork 85
/
Copy pathTableCell.swift
233 lines (196 loc) · 8.8 KB
/
TableCell.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
//
// TableCell.swift
// Proton
//
// Created by Rajdeep Kwatra on 8/4/2024.
// Copyright © 2024 Rajdeep Kwatra. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
import Foundation
import UIKit
import OSLog
protocol TableCellDelegate: AnyObject {
var table: Table { get }
func cell(_ cell: TableCell, didAddContentView view: TableCellContentView)
func cell(_ cell: TableCell, didRemoveContentView view: TableCellContentView?)
func cell(_ cell: TableCell, didChangeBounds bounds: CGRect, oldBounds: CGRect)
func cell(_ cell: TableCell, didReceiveFocusAt range: NSRange)
func cell(_ cell: TableCell, didLoseFocusFrom range: NSRange)
func cell(_ cell: TableCell, didTapAtLocation location: CGPoint, characterRange: NSRange?)
func cell(_ cell: TableCell, didChangeSelectionAt range: NSRange, attributes: [NSAttributedString.Key : Any], contentType: EditorContent.Name)
func cell(_ cell: TableCell, didReceiveKey key: EditorKey, at range: NSRange)
func cell(_ cell: TableCell, didChangeSelected isSelected: Bool)
func cell(_ cell: TableCell, didChangeBackgroundColor color: UIColor?, oldColor: UIColor?)
}
public class TableCell {
public typealias EditorInitializer = () -> EditorView
var id: String {
"{\(rowSpan),\(columnSpan)}"
}
public var onEditorInitialized: ((TableCell, EditorView) -> Void)?
/// Additional attributes that can be stored on Cell to identify various aspects like Header, Numbered etc.
public var additionalAttributes: [String: Any] = [:]
private var _attributedText: NSAttributedString?
public var attributedText: NSAttributedString? {
get { contentView == nil ? _attributedText : editor?.attributedText }
set {
_attributedText = newValue
editor?.attributedText = newValue ?? NSAttributedString()
}
}
/// Row indexes spanned by the cell. In case of a merged cell, this will contain all the rows= indexes which are merged.
public internal(set) var rowSpan: [Int]
/// Column indexes spanned by the cell. In case of a merged cell, this will contain all the column indexes which are merged.
public internal(set) var columnSpan: [Int]
/// Frame of the cell within `GridView`
public internal(set) var frame: CGRect = .zero {
didSet {
guard oldValue != frame else { return }
contentView?.frame = frame
}
}
public var isEditable: Bool = true {
didSet {
editor?.isEditable = isEditable
}
}
public var backgroundColor: UIColor? = nil {
didSet {
guard oldValue != backgroundColor else { return }
contentView?.backgroundColor = backgroundColor
}
}
let editorInitializer: EditorInitializer
/// Controls if the cell can be selected or not.
public var isSelectable: Bool {
get { contentView?.isSelectable ?? false }
set { contentView?.isSelectable = newValue }
}
public var isSelected: Bool {
get { contentView?.isSelected ?? false }
set { contentView?.isSelected = newValue }
}
/// Denotes if the cell can be split i.e. is a merged cell.
public var isSplittable: Bool {
rowSpan.count > 1 || columnSpan.count > 1
}
/// Content size of the cell
public var contentSize: CGSize {
contentView?.contentSize ?? .zero
}
/// Content view for the cell
public private(set) var contentView: TableCellContentView? {
didSet {
guard oldValue != contentView else { return }
contentView?.containerCell = self
contentView?.frame = frame
//TODO: get rid of editorInitializer in favor of delegate callback for editor
if let editor = contentView?.editor {
editor.attributedText = _attributedText ?? editorInitializer().attributedText
editor.isEditable = isEditable
onEditorInitialized?(self, editor)
}
contentView?.applyStyle(style)
}
}
public var editor: EditorView? {
contentView?.editor
}
public let gridStyle: GridStyle
public var style: GridCellStyle
weak var delegate: TableCellDelegate?
let initialHeight: CGFloat
/// Initializes the cell
/// - Parameters:
/// - rowSpan: Array of row indexes the cells spans. For e.g. a cell with first two rows as merged, will have a value of [0, 1] denoting 0th and 1st index.
/// - columnSpan: Array of column indexes the cells spans. For e.g. a cell with first two columns as merged, will have a value of [0, 1] denoting 0th and 1st index.
/// - initialHeight: Initial height of the cell. This will be updated based on size of editor content on load,
/// - style: Visual style of the cell
/// - gridStyle: Visual style for grid containing cell border color and width
/// - ignoresOptimizedInit: Ignores optimization to initialize editor within the cell. With optimization, the editor is not initialized until the cell is ready to be rendered on the UI thereby not incurring any overheads when creating
/// attributedText containing a `GridView` in an attachment. Defaults to `false`.
/// - editorInitializer: Closure for setting up the `EditorView` within the cell.
/// - Important:
/// Creating a `GridView` with 100s of cells can result in slow performance when creating an attributed string containing the GridView attachment. Using the closure defers the creation until the view is ready to be rendered in the UI.
/// It is recommended to setup all the parts of editor in closure where possible, or wait until after the GridView is rendered. In case, editor must be initialized before the rendering is complete and it is not possible to configure an aspect within the closure itself,
/// `setupEditor()` may be invoked. Use of `setupEditor()` is discouraged.
required public init(rowSpan: [Int],
columnSpan: [Int],
initialHeight: CGFloat = 40,
style: GridCellStyle = .init(),
gridStyle: GridStyle = .default,
editorInitializer: EditorInitializer? = nil) {
self.editorInitializer = editorInitializer ?? { EditorView(allowAutogrowing: false) }
self.rowSpan = rowSpan
self.columnSpan = columnSpan
self.gridStyle = gridStyle
self.style = style
self.initialHeight = initialHeight
}
// Clear the content of the cell
public func clear() {
attributedText = NSAttributedString()
}
/// Sets the focus in the `Editor` within the cell.
public func setFocus() {
contentView?.setFocus()
}
/// Removes the focus from the `Editor` within the cell.
public func removeFocus() {
contentView?.removeFocus()
}
func hideEditor() {
contentView?.hideEditor()
}
func showEditor() {
contentView?.showEditor()
}
func removeContentView() {
attributedText = contentView?.editor.attributedText
frame = contentView?.frame ?? frame
delegate?.cell(self, didRemoveContentView: contentView)
contentView = nil
}
func updateBackgroundColorFromParent(color: UIColor?, oldColor: UIColor?) {
// check for same color is required. In absence of that, the rendering causes
// collapsed cells to still show text even though cell is collapsed
guard backgroundColor == oldColor, backgroundColor != color else { return }
backgroundColor = color
}
func addContentView(_ contentView: TableCellContentView) {
prepareForReuse(contentView)
self.contentView = contentView
delegate?.cell(self, didAddContentView: contentView)
}
func prepareForReuse(_ contentView: TableCellContentView) {
contentView.isSelected = false
contentView.editor.clear()
contentView.editor.frame = CGRect(
origin: .zero,
size: CGSize(width: contentView.editor.frame.width,
height: initialHeight)
)
contentView.frame = contentView.editor.frame
}
}
extension TableCell: Equatable {
public static func ==(lhs: TableCell, rhs: TableCell) -> Bool {
return lhs.id == rhs.id
}
}
extension TableCell: Hashable {
public func hash(into hasher: inout Hasher) {
hasher.combine(id)
}
}