-
Notifications
You must be signed in to change notification settings - Fork 84
/
Copy pathTable.swift
459 lines (378 loc) · 15.4 KB
/
Table.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
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
//
// Table.swift
// Proton
//
// Created by Rajdeep Kwatra on 4/8/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
protocol TableDelegate: AnyObject {
var viewport: CGRect { get }
var cellsInViewport: [TableCell] { get }
func table(_ table: Table, shouldChangeColumnWidth proposedWidth: CGFloat, for columnIndex: Int) -> Bool
}
class Table {
private let config: GridConfiguration
private let cellStore: TableCellStore
private let editorInitializer: TableCell.EditorInitializer?
private var cellXPositions = [Int: CGFloat]()
private var cellYPositions = [Int: CGFloat]()
var rowHeights = [GridRowDimension]()
var columnWidths = [GridColumnDimension]()
weak var delegate: TableDelegate?
var currentRowHeights: [CGFloat] {
rowHeights.map { $0.calculatedHeight }
}
var cells: [TableCell] {
cellStore.cells
}
var numberOfColumns: Int {
columnWidths.count
}
var numberOfRows: Int {
rowHeights.count
}
var viewport: CGRect? {
delegate?.viewport
}
var size: CGSize {
guard let lastCell = cellAt(rowIndex: numberOfRows - 1, columnIndex: numberOfColumns - 1) else {
return .zero
}
let width = lastCell.frame.maxX
let height = lastCell.frame.maxY
return CGSize(width: width, height: height)
}
init(config: GridConfiguration, cells: [TableCell], editorInitializer: TableCell.EditorInitializer? = nil) {
self.config = config
self.editorInitializer = editorInitializer
for column in config.columnsConfiguration {
self.columnWidths.append(GridColumnDimension(width: column.width, collapsedWidth: config.collapsedColumnWidth))
}
for row in config.rowsConfiguration {
self.rowHeights.append(GridRowDimension(rowConfiguration: row, collapsedHeight: config.collapsedRowHeight))
}
self.cellStore = TableCellStore(cells: cells)
}
func cellAt(rowIndex: Int, columnIndex: Int) -> TableCell? {
return cellStore.cellAt(rowIndex: rowIndex, columnIndex: columnIndex)
}
func heightForCell(_ cell: TableCell) -> CGFloat {
cell.rowSpan.reduce(0) { $0 + rowHeights[$1].currentHeight }
}
func calculateTableDimensions(basedOn size: CGSize) {
let viewportWidth = viewport?.width ?? size.width
var cumulativeX: CGFloat = 0
for (i, colWidth) in columnWidths.enumerated() {
let width = colWidth.value(basedOn: size.width, viewportWidth: viewportWidth)
cellXPositions[i] = cumulativeX
cumulativeX += width
}
var cumulativeY: CGFloat = 0
for (i, rowHeight) in currentRowHeights.enumerated() {
cellYPositions[i] = cumulativeY
cumulativeY += rowHeight
}
}
func updateCellFrames(size: CGSize, onCellFrameUpdate: ((TableCell) -> Void)) {
calculateTableDimensions(basedOn: size)
for cell in cells {
let frame = frameForCell(cell, basedOn: size)
cell.frame = frame
onCellFrameUpdate(cell)
}
}
func cellsIn(rect: CGRect, offset: CGPoint) -> [TableCell] {
let sortedKeysX = cellXPositions.sorted { $0.value < $1.value }.map { $0.key }
let sortedKeysY = cellYPositions.sorted { $0.value < $1.value }.map { $0.key }
guard let colMin = sortedKeysX.last(where: { cellXPositions[$0] ?? 0 <= rect.minX }),
let colMax = sortedKeysX.first(where: { cellXPositions[$0] ?? 0 >= rect.maxX }),
let rowMin = sortedKeysY.last(where: { cellYPositions[$0] ?? 0 <= rect.minY }),
let rowMax = sortedKeysY.first(where: { cellYPositions[$0] ?? 0 >= rect.maxY }) else {
return []
}
let columns = Array(colMin...colMax)
let rows = Array((rowMin - 1)..<rowMax)
return cells.filter({
$0.rowSpan.contains { rows.contains($0) }
&& $0.columnSpan.contains { columns.contains($0) }
})
}
func frameForCell(_ cell: TableCell, basedOn size: CGSize) -> CGRect {
var x: CGFloat = 0
var y: CGFloat = 0
guard let minColumnSpan = cell.columnSpan.min(),
let minRowSpan = cell.rowSpan.min() else {
return .zero
}
let viewportWidth = viewport?.width ?? size.width
if minColumnSpan > 0 {
x = cellXPositions[minColumnSpan] ?? 0
}
if minRowSpan > 0 {
y = cellYPositions[minRowSpan] ?? 0
}
let width = cell.columnSpan.reduce(into: 0.0) { result, col in
result += columnWidths[col].value(basedOn: size.width, viewportWidth: viewportWidth)
}
let height = cell.rowSpan.reduce(into: 0.0) { result, row in
result += rowHeights[row].currentHeight
}
return CGRect(x: x, y: y, width: width, height: height)
}
func sizeThatFits(size: CGSize) -> CGSize {
let viewportWidth = viewport?.width ?? size.width
let width = columnWidths.reduce(0.0) { $0 + $1.value(basedOn: size.width, viewportWidth: viewportWidth)}
let height = currentRowHeights.reduce(0.0, +)
return CGSize(width: width, height: height)
}
func maxContentHeightCellForRow(at index: Int) -> TableCell? {
//TODO: account for merged rows
let cells = cellStore.cells.filter { $0.rowSpan.contains(index) }
return cells.max(by: {$0.contentSize.height < $1.contentSize.height })
// var maxHeightCell: TableCell?
// for cell in cells {
// if cell.contentSize.height > maxHeightCell?.contentSize.height ?? 0 {
// maxHeightCell = cell
// }
// }
// return maxHeightCell
}
func isMergeable(cells: [TableCell]) -> Bool {
guard cells.count > 1 else { return false }
let columns = Set(cells.flatMap { $0.columnSpan })
let rows = Set(cells.flatMap { $0.rowSpan })
for r in rows {
for c in columns {
if cells.first(where: { $0.rowSpan.contains(r) && $0.columnSpan.contains(c)}) == nil {
return false
}
}
}
return true
}
func changeColumnWidth(index: Int, totalWidth: CGFloat, delta: CGFloat) {
let viewportWidth = viewport?.width ?? totalWidth
let proposedWidth = columnWidths[index].value(basedOn: totalWidth, viewportWidth: viewportWidth) + delta
guard index < columnWidths.count,
delegate?.table(self, shouldChangeColumnWidth: proposedWidth, for: index) ?? true else { return }
columnWidths[index].width = .fixed(columnWidths[index].value(basedOn: totalWidth, viewportWidth: viewportWidth) + delta)
}
func changeRowHeight(index: Int, delta: CGFloat) {
guard index < rowHeights.count else { return }
guard rowHeights[index].rowConfiguration.initialHeight > rowHeights[index].currentHeight + delta else {
return
}
rowHeights[index].currentHeight = rowHeights[index].currentHeight + delta
}
@discardableResult
func merge(cells: [TableCell]) -> TableCell? {
guard isMergeable(cells: cells) else { return nil }
let firstCell = cells[0]
for i in 1..<cells.count {
merge(cell: firstCell, other: cells[i])
}
return firstCell
}
private func merge(cell: TableCell, other: TableCell) {
guard let _ = cellStore.cells.firstIndex(where: { $0.id == cell.id }),
let otherIndex = cellStore.cells.firstIndex(where: { $0.id == other.id }) else {
return
}
cell.rowSpan = Array(Set(cell.rowSpan).union(other.rowSpan)).sorted()
cell.columnSpan = Array(Set(cell.columnSpan).union(other.columnSpan)).sorted()
let updatedText = NSMutableAttributedString(attributedString: cell.attributedText ?? NSAttributedString())
updatedText.append(NSAttributedString(string: " "))
updatedText.append(other.attributedText ?? NSAttributedString())
cell.attributedText = updatedText
cellStore.deleteCellAt(index: otherIndex)
}
@discardableResult
func split(cell: TableCell) -> [TableCell] {
guard cell.isSplittable,
cells.contains(where: { $0.id == cell.id }) else { return []}
guard let minRowIndex = cell.rowSpan.min(),
let maxRowIndex = cell.rowSpan.max(),
let minColumnIndex = cell.columnSpan.min(),
let maxColumnIndex = cell.columnSpan.max() else { return []}
var newCells = [TableCell]()
for row in minRowIndex ... maxRowIndex {
for col in minColumnIndex ... maxColumnIndex {
let c = TableCell(
rowSpan: [row],
columnSpan: [col],
initialHeight: cell.initialHeight,
editorInitializer: editorInitializer
)
c.delegate = cell.delegate
newCells.append(c)
}
}
let firstCell = newCells.remove(at: 0)
cell.rowSpan = firstCell.rowSpan
cell.columnSpan = firstCell.columnSpan
cellStore.addCells(newCells)
return newCells
}
@discardableResult
func insertRow(at index: Int, frozenRowMaxIndex: Int?, config: GridRowConfiguration, cellDelegate: TableCellDelegate?) -> Result<[TableCell], TableViewError> {
var sanitizedIndex = index
if sanitizedIndex < 0 {
sanitizedIndex = 0
} else if sanitizedIndex > numberOfRows {
sanitizedIndex = numberOfRows
}
if let frozenRowMaxIndex = frozenRowMaxIndex,
sanitizedIndex <= frozenRowMaxIndex {
return .failure(.failedToInsertInFrozenRows)
}
if sanitizedIndex < numberOfRows {
cellStore.moveCellRowIndex(from: sanitizedIndex, by: 1)
}
rowHeights.insert(GridRowDimension(rowConfiguration: config, collapsedHeight: self.config.collapsedRowHeight), at: sanitizedIndex)
var cells = [TableCell]()
for c in 0..<numberOfColumns {
let cell = TableCell(
rowSpan: [sanitizedIndex],
columnSpan: [c],
initialHeight: config.initialHeight,
style: config.style,
editorInitializer: editorInitializer)
if cellAt(rowIndex: sanitizedIndex, columnIndex: c) != nil {
continue
}
cell.delegate = cellDelegate
cellStore.addCell(cell)
cells.append(cell)
}
return .success(cells)
}
@discardableResult
func insertColumn(at index: Int, frozenColumnMaxIndex: Int?, config: GridColumnConfiguration, cellDelegate: TableCellDelegate?) -> Result<[TableCell], TableViewError> {
var sanitizedIndex = index
if sanitizedIndex < 0 {
sanitizedIndex = 0
} else if sanitizedIndex > numberOfColumns {
sanitizedIndex = numberOfColumns
}
if let frozenColumnMaxIndex = frozenColumnMaxIndex,
sanitizedIndex <= frozenColumnMaxIndex {
return .failure(.failedToInsertInFrozenColumns)
}
if sanitizedIndex < numberOfColumns {
cellStore.moveCellColumnIndex(from: sanitizedIndex, by: 1)
}
columnWidths.insert(GridColumnDimension(width: config.width, collapsedWidth: self.config.collapsedColumnWidth), at: sanitizedIndex)
var cells = [TableCell]()
for r in 0..<numberOfRows {
let cell = TableCell(
rowSpan: [r],
columnSpan: [sanitizedIndex],
initialHeight: rowHeights[r].rowConfiguration.initialHeight,
style: config.style,
editorInitializer: editorInitializer
)
if cellAt(rowIndex: r, columnIndex: sanitizedIndex) != nil {
continue
}
cell.delegate = cellDelegate
cellStore.addCell(cell)
cells.append(cell)
}
return .success(cells)
}
func deleteRow(at index: Int) {
guard rowHeights.count > 1 else { return }
var cellsToRemove = [TableCell]()
var cellsToUpdate = [TableCell]()
for cell in cells {
if cell.rowSpan.contains(index) {
if cell.isSplittable {
cellsToUpdate.append(cell)
} else {
cellsToRemove.append(cell)
}
}
}
cellsToRemove.forEach { $0.removeContentView() }
cellStore.deleteCells(cellsToRemove)
for cell in cellsToUpdate {
cell.rowSpan.removeAll{ $0 == index }
cell.rowSpan = cell.rowSpan.map { $0 < index ? $0 : $0 - 1 }
}
rowHeights.remove(at: index)
if index < numberOfRows {
cellStore.moveCellRowIndex(from: index + 1, by: -1)
}
}
func deleteColumn(at index: Int) {
guard columnWidths.count > 1 else { return }
var cellsToRemove = [TableCell]()
var cellsToUpdate = [TableCell]()
for cell in cells {
if cell.columnSpan.contains(index) {
if cell.isSplittable {
cellsToUpdate.append(cell)
} else {
cellsToRemove.append(cell)
}
}
}
cellsToRemove.forEach { $0.removeContentView() }
cellStore.deleteCells(cellsToRemove)
for cell in cellsToUpdate {
cell.columnSpan.removeAll{ $0 == index }
cell.columnSpan = cell.columnSpan.map { $0 < index ? $0 : $0 - 1 }
}
columnWidths.remove(at: index)
if index < numberOfColumns {
cellStore.moveCellColumnIndex(from: index + 1, by: -1)
}
}
func collapseRow(at index: Int) {
rowHeights[index].isCollapsed = true
}
func expandRow(at index: Int) {
rowHeights[index].isCollapsed = false
}
func collapseColumn(at index: Int) {
columnWidths[index].isCollapsed = true
// Hide editor failing which resizing column ends up elongating column based on content in cell
cells.forEach {
if $0.columnSpan.contains(index) {
$0.hideEditor()
}
}
}
func expandColumn(at index: Int) {
columnWidths[index].isCollapsed = false
cells.forEach {
if $0.columnSpan.contains(index) {
$0.showEditor()
}
}
}
func getCollapsedRowIndices() -> [Int] {
return rowHeights.indices.filter { rowHeights[$0].isCollapsed }
}
func getCollapsedColumnIndices() -> [Int] {
return columnWidths.indices.filter { columnWidths[$0].isCollapsed }
}
}
public enum TableViewError: Error {
case failedToInsertInFrozenRows
case failedToInsertInFrozenColumns
}