-
Notifications
You must be signed in to change notification settings - Fork 15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add TableView and CollectionView protocol #139
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,7 +35,7 @@ open class TableViewDriver: NSObject { | |
} | ||
|
||
/// The table view to which the `TableViewModel` is rendered. | ||
public let tableView: UITableView | ||
let tableView: TableView | ||
|
||
private var _tableViewModel: TableViewModel? | ||
|
||
|
@@ -72,6 +72,8 @@ open class TableViewDriver: NSObject { | |
|
||
private let _automaticDiffingEnabled: Bool | ||
|
||
private let _registerViews: (TableViewModel) -> Void | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is needed to avoid |
||
|
||
/// Initializes a data source that drives a `UITableView` based on a `TableViewModel`. | ||
/// | ||
/// - Parameters: | ||
|
@@ -82,15 +84,44 @@ open class TableViewDriver: NSObject { | |
/// - automaticDiffingEnabled: defines whether or not this data source updates the table | ||
/// view automatically when cells/sections are moved/inserted/deleted. | ||
/// Defaults to `true`. | ||
public init( | ||
public convenience init( | ||
tableView: UITableView, | ||
tableViewModel: TableViewModel? = nil, | ||
shouldDeselectUponSelection: Bool = true, | ||
automaticDiffingEnabled: Bool = true) { | ||
self.init( | ||
tableView: tableView, | ||
registerViews: tableView.registerViews(for:), | ||
tableViewModel: tableViewModel, | ||
shouldDeselectUponSelection: shouldDeselectUponSelection, | ||
automaticDiffingEnabled: automaticDiffingEnabled | ||
) | ||
} | ||
|
||
/// Initializes a data source that drives a `TableView` based on a `TableViewModel`. | ||
/// Externally, this is a `UITableView`. Internally, this can be a mock. | ||
/// | ||
/// - Parameters: | ||
/// - tableView: the table view to which this data source will render its view models. | ||
/// - registerViews: a closure that registers the views for a given `TableViewModel` | ||
/// - tableViewModel: the view model that describes the initial state of this table view. | ||
/// - shouldDeselectUponSelection: indicates if selected cells should immediately be | ||
/// deselected. Defaults to `true`. | ||
/// - automaticDiffingEnabled: defines whether or not this data source updates the table | ||
/// view automatically when cells/sections are moved/inserted/deleted. | ||
/// Defaults to `true`. | ||
init( | ||
tableView: TableView, | ||
registerViews: @escaping (TableViewModel) -> Void, | ||
tableViewModel: TableViewModel? = nil, | ||
shouldDeselectUponSelection: Bool = true, | ||
automaticDiffingEnabled: Bool = true | ||
) { | ||
self._tableViewModel = tableViewModel | ||
self.tableView = tableView | ||
self._automaticDiffingEnabled = automaticDiffingEnabled | ||
self._shouldDeselectUponSelection = shouldDeselectUponSelection | ||
self._registerViews = registerViews | ||
super.init() | ||
tableView.dataSource = self | ||
tableView.delegate = self | ||
|
@@ -163,7 +194,7 @@ open class TableViewDriver: NSObject { | |
} | ||
|
||
if let newModel = newModel { | ||
self.tableView.registerViews(for: newModel) | ||
self._registerViews(newModel) | ||
} | ||
|
||
let previousStateNilOrEmpty = (oldModel == nil || oldModel!.isEmpty) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
// | ||
// PlanGrid | ||
// https://www.plangrid.com | ||
// https://medium.com/plangrid-technology | ||
// | ||
// Documentation | ||
// https://plangrid.github.io/ReactiveLists | ||
// | ||
// GitHub | ||
// https://github.com/plangrid/ReactiveLists | ||
// | ||
// License | ||
// Copyright © 2018-present PlanGrid, Inc. | ||
// Released under an MIT license: https://opensource.org/licenses/MIT | ||
// | ||
|
||
import DifferenceKit | ||
|
||
/// Protocol that allows ReactiveLists to use a UITableView without knowing about the concrete type | ||
/// This is useful for testing, and it's a step toward having one | ||
protocol TableView: class { | ||
|
||
// MARK: UITableView methods | ||
|
||
var indexPathsForVisibleRows: [IndexPath]? { get } | ||
func beginUpdates() | ||
func endUpdates() | ||
func reloadData() | ||
func cellForRow(at indexPath: IndexPath) -> UITableViewCell? | ||
var dataSource: UITableViewDataSource? { get set } | ||
var delegate: UITableViewDelegate? { get set } | ||
func headerView(forSection section: Int) -> UITableViewHeaderFooterView? | ||
func footerView(forSection section: Int) -> UITableViewHeaderFooterView? | ||
|
||
// MARK: DifferenceKit UITableView extensions | ||
|
||
//swiftlint:disable:next function_parameter_count | ||
func reload<C>( | ||
using stagedChangeset: StagedChangeset<C>, | ||
deleteSectionsAnimation: @autoclosure () -> UITableViewRowAnimation, | ||
insertSectionsAnimation: @autoclosure () -> UITableViewRowAnimation, | ||
reloadSectionsAnimation: @autoclosure () -> UITableViewRowAnimation, | ||
deleteRowsAnimation: @autoclosure () -> UITableViewRowAnimation, | ||
insertRowsAnimation: @autoclosure () -> UITableViewRowAnimation, | ||
reloadRowsAnimation: @autoclosure () -> UITableViewRowAnimation, | ||
interrupt: ((Changeset<C>) -> Bool)?, | ||
setData: (C) -> Void | ||
) | ||
} | ||
|
||
extension TableView { | ||
|
||
//swiftlint:disable:next function_parameter_count | ||
func reload<C>( | ||
using stagedChangeset: StagedChangeset<C>, | ||
deleteSectionsAnimation: @autoclosure () -> UITableViewRowAnimation, | ||
insertSectionsAnimation: @autoclosure () -> UITableViewRowAnimation, | ||
reloadSectionsAnimation: @autoclosure () -> UITableViewRowAnimation, | ||
deleteRowsAnimation: @autoclosure () -> UITableViewRowAnimation, | ||
insertRowsAnimation: @autoclosure () -> UITableViewRowAnimation, | ||
reloadRowsAnimation: @autoclosure () -> UITableViewRowAnimation, | ||
setData: (C) -> Void | ||
) { | ||
self.reload( | ||
using: stagedChangeset, | ||
deleteSectionsAnimation: deleteSectionsAnimation, | ||
insertSectionsAnimation: insertRowsAnimation, | ||
reloadSectionsAnimation: reloadSectionsAnimation, | ||
deleteRowsAnimation: deleteRowsAnimation, | ||
insertRowsAnimation: insertRowsAnimation, | ||
reloadRowsAnimation: reloadRowsAnimation, | ||
interrupt: nil, | ||
setData: setData | ||
) | ||
} | ||
} | ||
|
||
extension UITableView: TableView {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,7 +16,7 @@ | |
|
||
import UIKit | ||
|
||
extension UITableView { | ||
extension TableView where Self: CellContainerViewProtocol, Self.CellType: UITableViewCell { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. protocol extension with generic constraints! |
||
|
||
func configuredCell(for model: TableCellViewModel, at indexPath: IndexPath) -> UITableViewCell { | ||
let cell = self.dequeueReusableCellFor(identifier: model.registrationInfo.reuseIdentifier, indexPath: indexPath) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seemed okay to do?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I haven't tried it internally, but I think in most cases, you'll have another pointer to this view anyway.