Skip to content

Commit 1fd1610

Browse files
committed
Disable cell selection by default. Can be enabled by setting table.isCellSelectionEnabled
1 parent d7947b3 commit 1fd1610

File tree

6 files changed

+50
-10
lines changed

6 files changed

+50
-10
lines changed

Diff for: ExampleApp/ExampleApp/Views/TableViewAttachment.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public class TableViewAttachment: Attachment {
3030
public let view: TableView
3131

3232
public init(config: GridConfiguration) {
33-
view = TableView(config: config)
33+
view = TableView(config: config, isCellSelectionEnabled: true)
3434
super.init(view, size: .fullWidth)
3535
view.boundsObserver = self
3636
}

Diff for: Proton/Sources/Swift/Table/TableContentView.swift

+19
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,15 @@ class TableContentView: UIScrollView {
7878

7979
var isFreeScrollingEnabled = false
8080
var isRendered = false
81+
var isCellSelectionEnabled = false {
82+
didSet {
83+
guard oldValue != isCellSelectionEnabled else { return }
84+
setupSelectionGesture()
85+
}
86+
}
87+
88+
var selectionGestureRecognizer: UIPanGestureRecognizer?
89+
let selectionGestureRecognizerName = "_tableView_selectionGestureRecognizer"
8190

8291
// Border for outer edges are added separately to account for
8392
// half-width borders added by cells which results in thinner outer border of table
@@ -255,7 +264,17 @@ class TableContentView: UIScrollView {
255264
}
256265

257266
private func setupSelectionGesture() {
267+
if let selectionGestureRecognizer {
268+
removeGestureRecognizer(selectionGestureRecognizer)
269+
}
270+
271+
guard isCellSelectionEnabled else {
272+
return
273+
}
274+
258275
let gestureRecognizer = UIPanGestureRecognizer(target: self, action: #selector(handleSelection(_:)))
276+
gestureRecognizer.name = selectionGestureRecognizerName
277+
selectionGestureRecognizer = gestureRecognizer
259278
gestureRecognizer.delegate = self
260279
gestureRecognizer.minimumNumberOfTouches = 2
261280
gestureRecognizer.maximumNumberOfTouches = 2

Diff for: Proton/Sources/Swift/Table/TableView.swift

+12-6
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,12 @@ public class TableView: UIView {
209209
}
210210
}
211211

212+
/// Determines if cell selection using 2-finger drag gesture is enabled
213+
public var isCellSelectionEnabled: Bool {
214+
get { tableView.isCellSelectionEnabled }
215+
set { tableView.isCellSelectionEnabled = newValue }
216+
}
217+
212218
/// Bounds observer for the `TableView`. Typically, this will be the `Attachment` that hosts the `TableView`.
213219
/// - Note: In absence of a `boundObserver`, the `TableView` will not autoresize when the content in the cells
214220
/// are changed.
@@ -294,9 +300,9 @@ public class TableView: UIView {
294300
/// - config: Configuration for `TableView`
295301
/// - cellEditorInitializer: Custom initializer for `EditorView` within `TableCell`. This will also be used when creating new cells as a
296302
/// return of adding new row or column, or cells being split.
297-
public convenience init(config: GridConfiguration, cellEditorInitializer: GridCell.EditorInitializer? = nil) {
303+
public convenience init(config: GridConfiguration, cellEditorInitializer: GridCell.EditorInitializer? = nil, isCellSelectionEnabled: Bool = false) {
298304
let tableView = TableContentView(config: config, editorInitializer: cellEditorInitializer)
299-
self.init(config: config, tableView: tableView)
305+
self.init(config: config, tableView: tableView, isCellSelectionEnabled: isCellSelectionEnabled)
300306
}
301307

302308
/// Initializes `TableView` using the provided configuration.
@@ -307,12 +313,12 @@ public class TableView: UIView {
307313
/// return of adding new row or column, or cells being split.
308314
/// - Important:
309315
/// Care must be taken that the number of cells are correct per the configuration provided, failing which the `TableView` rendering may be broken.
310-
public convenience init(config: GridConfiguration, cells: [TableCell], cellEditorInitializer: TableCell.EditorInitializer? = nil) {
316+
public convenience init(config: GridConfiguration, cells: [TableCell], cellEditorInitializer: TableCell.EditorInitializer? = nil, isCellSelectionEnabled: Bool = false) {
311317
let tableView = TableContentView(config: config, cells: cells, editorInitializer: cellEditorInitializer)
312-
self.init(config: config, tableView: tableView)
318+
self.init(config: config, tableView: tableView, isCellSelectionEnabled: isCellSelectionEnabled)
313319
}
314320

315-
private init(config: GridConfiguration, tableView: TableContentView) {
321+
private init(config: GridConfiguration, tableView: TableContentView, isCellSelectionEnabled: Bool ) {
316322
self.tableView = tableView
317323
let boundsShadowColors = [
318324
config.boundsLimitShadowColors.primary.cgColor,
@@ -325,7 +331,7 @@ public class TableView: UIView {
325331
self.config = config
326332
super.init(frame: .zero)
327333
self.leadingShadowConstraint = leadingShadowView.leadingAnchor.constraint(equalTo: self.leadingAnchor)
328-
334+
self.isCellSelectionEnabled = isCellSelectionEnabled
329335
setup()
330336
}
331337

Diff for: Proton/Tests/Attachments/TableViewAttachment.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,13 @@ extension EditorContent.Name {
2828

2929
public class TableViewAttachment: Attachment {
3030
public let view: TableView
31-
public init(config: GridConfiguration) {
31+
public init(config: GridConfiguration, isCellSelectionEnabled: Bool = false) {
3232
view = TableView(config: config)
3333
super.init(view, size: .fullWidth)
3434
view.boundsObserver = self
3535
}
3636

37-
public init(config: GridConfiguration, cells: [TableCell]) {
37+
public init(config: GridConfiguration, cells: [TableCell], isCellSelectionEnabled: Bool = false) {
3838
view = TableView(config: config, cells: cells)
3939
super.init(view, size: .fullWidth)
4040
view.boundsObserver = self

Diff for: Proton/Tests/Helpers/AttachmentGenerator.swift

+2-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ struct AttachmentGenerator {
3131
numColumns: Int,
3232
columnConfig: GridColumnConfiguration? = nil,
3333
rowConfig: GridRowConfiguration? = nil,
34-
initialRowHeight: CGFloat = 50
34+
initialRowHeight: CGFloat = 50,
35+
isCellSelectionEnabled: Bool = false
3536
) -> TableViewAttachment {
3637
let columnConfiguration = columnConfig ?? GridColumnConfiguration(width: .fixed(100))
3738
let rowConfiguration = rowConfig ?? GridRowConfiguration(initialHeight: initialRowHeight)

Diff for: Proton/Tests/Table/TableViewTests.swift

+14
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,20 @@ class TableViewTests: XCTestCase {
106106
try cellIDString(from: tableView.cellsInViewport, filter: filter))
107107
}
108108

109+
func testIsCellSelectionEnabled() {
110+
let attachment = AttachmentGenerator.makeTableViewAttachment(
111+
id: 1,
112+
numRows: 20,
113+
numColumns: 5,
114+
initialRowHeight: 100
115+
)
116+
let tableView = attachment.view.tableView
117+
XCTAssertFalse(tableView.gestureRecognizers?.contains{ $0.name == tableView.selectionGestureRecognizerName } ?? false)
118+
119+
tableView.isCellSelectionEnabled = true
120+
XCTAssertTrue(tableView.gestureRecognizers?.contains{ $0.name == tableView.selectionGestureRecognizerName } ?? false)
121+
}
122+
109123
func FIXME_testChangesBoundsOfCell() {
110124
let expectation = functionExpectation()
111125
expectation.expectedFulfillmentCount = 2

0 commit comments

Comments
 (0)