Skip to content

Commit

Permalink
Added ability to add new columns
Browse files Browse the repository at this point in the history
  • Loading branch information
rajdeep committed May 23, 2024
1 parent e5821d1 commit 54cabed
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 13 deletions.
18 changes: 9 additions & 9 deletions Proton/Sources/Swift/Table/TableContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -251,15 +251,15 @@ class TableContentView: UIScrollView {
return result
}

// @discardableResult
// func insertColumn(at index: Int, configuration: GridColumnConfiguration) -> Result<[TableCell], GridViewError> {
// let result = grid.insertColumn(at: index, frozenColumnMaxIndex: frozenColumnMaxIndex, config: configuration, cellDelegate: self)
// if case Result.success = result {
// invalidateCellLayout()
// tableContentViewDelegate?.tableContentView(self, didAddNewColumnAt: index)
// }
// return result
// }
@discardableResult
func insertColumn(at index: Int, configuration: GridColumnConfiguration) -> Result<[TableCell], TableViewError> {
let result = table.insertColumn(at: index, frozenColumnMaxIndex: frozenColumnMaxIndex, config: configuration, cellDelegate: self)
if case Result.success = result {
invalidateCellLayout()
tableContentViewDelegate?.tableContentView(self, didAddNewColumnAt: index)
}
return result
}

func deleteRow(at index: Int) {
table.deleteRow(at: index)
Expand Down
8 changes: 4 additions & 4 deletions Proton/Sources/Swift/Table/TableView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -615,10 +615,10 @@ public class TableView: UIView {
/// If the index is out of bounds, column will be inserted at the beginning or end of the grid based on index value
/// - configuration: Configuration for the new column
/// - Returns: Result with newly added cells for `.success`, error in case of `.failure`
// @discardableResult
// public func insertColumn(at index: Int, configuration: GridColumnConfiguration) -> Result<[TableCell], TableViewError> {
// tableView.insertColumn(at: index, configuration: configuration)
// }
@discardableResult
public func insertColumn(at index: Int, configuration: GridColumnConfiguration) -> Result<[TableCell], TableViewError> {
tableView.insertColumn(at: index, configuration: configuration)
}

/// Deletes the row at given index
/// - Parameter index: Index to delete
Expand Down
87 changes: 87 additions & 0 deletions Proton/Tests/Table/TableViewAttachmentSnapshotTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1159,6 +1159,93 @@ class TableViewAttachmentSnapshotTests: SnapshotTestCase {
assertSnapshot(matching: viewController.view, as: .image, record: recordMode)
}

func testInsertsColumnInMiddle() throws {
let config = GridConfiguration(
columnsConfiguration: [
GridColumnConfiguration(width: .fixed(30)),
GridColumnConfiguration(width: .fractional(0.45)),
GridColumnConfiguration(width: .fractional(0.45)),
],
rowsConfiguration: [
GridRowConfiguration(initialHeight: 40),
GridRowConfiguration(initialHeight: 40),
])
let attachment = TableViewAttachment(config: config)
let table = attachment.view
attachment.view.delegate = delegate

editor.replaceCharacters(in: .zero, with: "Some text in editor")
editor.insertAttachment(in: editor.textEndRange, attachment: attachment)
editor.replaceCharacters(in: editor.textEndRange, with: "Text after grid")

table.insertColumn(at: 1, configuration: GridColumnConfiguration(width: .fixed(80)))

viewController.render(size: CGSize(width: 400, height: 400))
let newCell11 = try XCTUnwrap(table.cellAt(rowIndex: 1, columnIndex: 1))
newCell11.editor?.attributedText = NSAttributedString(string: "New cell")

assertSnapshot(matching: viewController.view, as: .image, record: recordMode)
}

func testInsertsColumnAtBeginning() throws {
let config = GridConfiguration(
columnsConfiguration: [
GridColumnConfiguration(width: .fixed(30)),
GridColumnConfiguration(width: .fractional(0.50)),
GridColumnConfiguration(width: .fixed(50)),
],
rowsConfiguration: [
GridRowConfiguration(initialHeight: 40),
GridRowConfiguration(initialHeight: 40),
])
let attachment = TableViewAttachment(config: config)
let table = attachment.view
attachment.view.delegate = delegate

editor.replaceCharacters(in: .zero, with: "Some text in editor")
editor.insertAttachment(in: editor.textEndRange, attachment: attachment)
editor.replaceCharacters(in: editor.textEndRange, with: "Text after grid")

table.insertColumn(at: 0, configuration: GridColumnConfiguration(width: .fractional(0.25)))

viewController.render(size: CGSize(width: 400, height: 400))
let newCell00 = try XCTUnwrap(table.cellAt(rowIndex: 0, columnIndex: 0))
newCell00.editor?.attributedText = NSAttributedString(string: "New cell")

assertSnapshot(matching: viewController.view, as: .image, record: recordMode)
}

func testInsertsColumnAtEnd() throws {
let config = GridConfiguration(
columnsConfiguration: [
GridColumnConfiguration(width: .fixed(30)),
GridColumnConfiguration(width: .fixed(60)),
GridColumnConfiguration(width: .fixed(60)),
],
rowsConfiguration: [
GridRowConfiguration(initialHeight: 40),
GridRowConfiguration(initialHeight: 40),
])
let attachment = TableViewAttachment(config: config)
let table = attachment.view
attachment.view.delegate = delegate

editor.replaceCharacters(in: .zero, with: "Some text in editor")
editor.insertAttachment(in: editor.textEndRange, attachment: attachment)
editor.replaceCharacters(in: editor.textEndRange, with: "Text after grid")

table.insertColumn(at: 3, configuration: GridColumnConfiguration(width: .fixed(150)))

viewController.render(size: CGSize(width: 400, height: 400))
let newCell21 = try XCTUnwrap(table.cellAt(rowIndex: 1, columnIndex: 3))
newCell21.editor?.attributedText = NSAttributedString(string: "New cell")

// Editor shows caret for some reason - needs further investigation
table.cellAt(rowIndex: 2, columnIndex: 0)?.editor?.isSelectable = false

assertSnapshot(matching: viewController.view, as: .image, record: recordMode)
}


private func makeTableViewAttachment(config: GridConfiguration, cells: [TableCell] = []) -> TableViewAttachment {
let attachment: TableViewAttachment
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 54cabed

Please sign in to comment.