Skip to content

Commit

Permalink
Merge pull request #210 from Shopify/selection-fix
Browse files Browse the repository at this point in the history
Selection fix
  • Loading branch information
mikegarfinkle committed May 17, 2022
2 parents ba4052d + 785d006 commit 9f7f44f
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ extension FunctionalTableData {
}

public func tableView(_ tableView: UITableView, willSelectRowAt indexPath: IndexPath) -> IndexPath? {

// This fixes a bug when selecting cells quickly in a table view; without this, a tableview cell can be selected on the screen, but the cell state selection status is not updated. This may be due to the fact that state status is updated when cellConfig?.actions.selectionAction function is called. This function is only called in didSelectRowAt function, and could potentially not always fire.
guard tableView.cellForRow(at: indexPath) != nil else { return nil }

if tableView.indexPathForSelectedRow == indexPath {
return nil
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,7 @@ class DiffableDataSourceFunctionalDataTests: XCTestCase {

func testCellAccessibilityIdentifiers() {
let tableData = FunctionalTableData(name: nil, diffingStrategy: .diffableDataSource)
let tableView = UITableView()

tableData.tableView = tableView
tableData.tableView = WindowWithTableViewMounted().tableView
let expectation1 = expectation(description: "rendered")
let cellConfig = TestCaseCell(key: "cellKey", state: TestCaseState(data: "red"), cellUpdater: TestCaseState.updateView)
let section = TableSection(key: "sectionKey", rows: [cellConfig])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,19 @@ import Foundation
class DiffableDataSourceTableCellReuseTests: XCTestCase {
private typealias LabelCell = HostCell<UILabel, String, LayoutMarginsTableItemLayout>

private var tableView: UITableView!
private var window: WindowWithTableViewMounted!
private var tableModel: FunctionalTableData!

override func setUp() {
super.setUp()
tableView = UITableView()
window = WindowWithTableViewMounted()
tableModel = FunctionalTableData()
tableModel.tableView = tableView
tableModel.tableView = window.tableView
}

override func tearDown() {
tableView = nil
tableModel = nil
window.tearDownWindow()
super.tearDown()
}

Expand All @@ -42,7 +42,7 @@ class DiffableDataSourceTableCellReuseTests: XCTestCase {
renderedDisclosureCell.fulfill()
}

guard let cellView = self.tableView.visibleCells.first else {
guard let cellView = self.window.tableView.visibleCells.first else {
XCTFail("Tableview has no cell views")
return
}
Expand All @@ -62,7 +62,7 @@ class DiffableDataSourceTableCellReuseTests: XCTestCase {
renderedUnstyledCell.fulfill()
}

guard let cellView = self.tableView.visibleCells.first else {
guard let cellView = self.window.tableView.visibleCells.first else {
XCTFail("Tableview has no cell views")
return
}
Expand Down
2 changes: 1 addition & 1 deletion Tests/FunctionalTableDataTests/FunctionalDataTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class FunctionalDataTests: XCTestCase {

func testCellAccessibilityIdentifiers() {
let tableData = FunctionalTableData()
let tableView = UITableView()
let tableView = WindowWithTableViewMounted().tableView

tableData.tableView = tableView
let expectation1 = expectation(description: "rendered")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import UIKit



final class WindowWithTableViewMounted {

private var rootWindow: UIWindow!
let tableView = UITableView()
private let tableViewController : TableViewControllerWithTableViewMounted

init() {
self.tableViewController = TableViewControllerWithTableViewMounted(tableView)
setUpWindowWithTableView()
presentWindow()
}

private func setUpWindowWithTableView() {
rootWindow = UIWindow(frame: UIScreen.main.bounds)
rootWindow.rootViewController = tableViewController
}

private func presentWindow() {
rootWindow.isHidden = false
tableViewController.viewWillAppear(false)
tableViewController.viewDidAppear(false)
}

func tearDownWindow() {
tableViewController.viewWillDisappear(false)
tableViewController.viewDidDisappear(false)
rootWindow.rootViewController = nil
rootWindow.isHidden = true
self.rootWindow = nil
}
}


private final class TableViewControllerWithTableViewMounted: UIViewController {
let tableView : UITableView

init(_ tableView: UITableView) {
self.tableView = tableView
super.init(nibName: nil, bundle: nil)
}

required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

override func loadView() {
super.loadView()
setupTableView()
}

func setupTableView() {
view.addSubview(tableView)
tableView.translatesAutoresizingMaskIntoConstraints = false
tableView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
tableView.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
tableView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
tableView.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
}
}
12 changes: 6 additions & 6 deletions Tests/FunctionalTableDataTests/TableCellReuseTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,18 @@ import Foundation
class TableCellReuseTests: XCTestCase {
private typealias LabelCell = HostCell<UILabel, String, LayoutMarginsTableItemLayout>

private var tableView: UITableView!
private var window: WindowWithTableViewMounted!
private var tableModel: FunctionalTableData!

override func setUp() {
super.setUp()
tableView = UITableView()
window = WindowWithTableViewMounted()
tableModel = FunctionalTableData()
tableModel.tableView = tableView
tableModel.tableView = window.tableView
}

override func tearDown() {
tableView = nil
window.tearDownWindow()
tableModel = nil
super.tearDown()
}
Expand All @@ -43,7 +43,7 @@ class TableCellReuseTests: XCTestCase {
renderedDisclosureCell.fulfill()
}

guard let cellView = self.tableView.visibleCells.first else {
guard let cellView = self.window.tableView.visibleCells.first else {
XCTFail("Tableview has no cell views")
return
}
Expand All @@ -63,7 +63,7 @@ class TableCellReuseTests: XCTestCase {
renderedUnstyledCell.fulfill()
}

guard let cellView = self.tableView.visibleCells.first else {
guard let cellView = self.window.tableView.visibleCells.first else {
XCTFail("Tableview has no cell views")
return
}
Expand Down

0 comments on commit 9f7f44f

Please sign in to comment.