Skip to content

Commit

Permalink
Merge pull request #85 from algolia/feat/cell-template-for-missing-hit
Browse files Browse the repository at this point in the history
Release v5.1.0
  • Loading branch information
VladislavFitz committed Sep 5, 2019
2 parents 34f9487 + b887486 commit 23144df
Show file tree
Hide file tree
Showing 23 changed files with 666 additions and 90 deletions.
2 changes: 1 addition & 1 deletion InstantSearch.podspec
@@ -1,7 +1,7 @@
Pod::Spec.new do |s|
s.name = "InstantSearch"
s.module_name = 'InstantSearch'
s.version = "5.0.0"
s.version = "5.1.0"
s.summary = "A library of widgets and helpers to build instant-search applications on iOS."
s.homepage = "https://github.com/algolia/instantsearch-ios"
s.license = { type: 'Apache 2.0', file: 'LICENSE.md' }
Expand Down
2 changes: 1 addition & 1 deletion InstantSearchTestsHost/Info.plist
Expand Up @@ -15,7 +15,7 @@
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>4.0.0</string>
<string>5.1.0</string>
<key>CFBundleVersion</key>
<string>1</string>
<key>LSRequiresIPhoneOS</key>
Expand Down
Expand Up @@ -22,14 +22,14 @@ public class HitsCollectionController<Source: HitsSource>: NSObject, HitsControl

public weak var hitsSource: Source?

private var dataSource: HitsCollectionViewDataSource<Source>? {
public var dataSource: HitsCollectionViewDataSource<Source>? {
didSet {
dataSource?.hitsSource = hitsSource
collectionView.dataSource = dataSource
}
}

private var delegate: HitsCollectionViewDelegate<Source>? {
public var delegate: HitsCollectionViewDelegate<Source>? {
didSet {
delegate?.hitsSource = hitsSource
collectionView.delegate = delegate
Expand Down
Expand Up @@ -11,21 +11,36 @@ import UIKit
open class HitsCollectionViewDataSource<DataSource: HitsSource>: NSObject, UICollectionViewDataSource {

public var cellConfigurator: CollectionViewCellConfigurator<DataSource.Record>
public var templateCellProvider: () -> UICollectionViewCell
public weak var hitsSource: DataSource?

public init(cellConfigurator: @escaping CollectionViewCellConfigurator<DataSource.Record>) {
self.cellConfigurator = cellConfigurator
self.templateCellProvider = { return .init() }
}

open func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return hitsSource?.numberOfHits() ?? 0

guard let hitsSource = hitsSource else {
fatalError("Missing hits source")
}

return hitsSource.numberOfHits()

}

open func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
guard let hit = hitsSource?.hit(atIndex: indexPath.row) else {
return UICollectionViewCell()

guard let hitsSource = hitsSource else {
fatalError("Missing hits source")
}

guard let hit = hitsSource.hit(atIndex: indexPath.row) else {
return templateCellProvider()
}

return cellConfigurator(collectionView, hit, indexPath)

}

}
Expand Up @@ -18,10 +18,16 @@ open class HitsCollectionViewDelegate<DataSource: HitsSource>: NSObject, UIColle
}

open func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
guard let hit = hitsSource?.hit(atIndex: indexPath.row) else {

guard let hitsSource = hitsSource else {
fatalError("Missing hits source")
}

guard let hit = hitsSource.hit(atIndex: indexPath.row) else {
return
}
clickHandler(collectionView, hit, indexPath)

}

}
21 changes: 18 additions & 3 deletions Sources/Controller/Hits/TableView/HitsTableViewDataSource.swift
Expand Up @@ -11,21 +11,36 @@ import UIKit
open class HitsTableViewDataSource<DataSource: HitsSource>: NSObject, UITableViewDataSource {

public var cellConfigurator: TableViewCellConfigurator<DataSource.Record>
public var templateCellProvider: () -> UITableViewCell
public weak var hitsSource: DataSource?

public init(cellConfigurator: @escaping TableViewCellConfigurator<DataSource.Record>) {
self.cellConfigurator = cellConfigurator
self.templateCellProvider = { return .init() }
}

open func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return hitsSource?.numberOfHits() ?? 0

guard let hitsSource = hitsSource else {
fatalError("Missing hits source")
}

return hitsSource.numberOfHits()

}

open func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let hit = hitsSource?.hit(atIndex: indexPath.row) else {
return UITableViewCell()

guard let hitsSource = hitsSource else {
fatalError("Missing hits source")
}

guard let hit = hitsSource.hit(atIndex: indexPath.row) else {
return templateCellProvider()
}

return cellConfigurator(tableView, hit, indexPath)

}

}
Expand Up @@ -18,10 +18,16 @@ open class HitsTableViewDelegate<DataSource: HitsSource>: NSObject, UITableViewD
}

open func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
guard let hit = hitsSource?.hit(atIndex: indexPath.row) else {

guard let hitsSource = hitsSource else {
fatalError("Missing hits source")
}

guard let hit = hitsSource.hit(atIndex: indexPath.row) else {
return
}
clickHandler(tableView, hit, indexPath)

}

}
Expand Up @@ -16,21 +16,21 @@ public class MultiIndexHitsCollectionController: NSObject, MultiIndexHitsControl

public weak var hitsSource: MultiIndexHitsSource? {
didSet {
dataSource?.hitsDataSource = hitsSource
delegate?.hitsDataSource = hitsSource
dataSource?.hitsSource = hitsSource
delegate?.hitsSource = hitsSource
}
}

public var dataSource: MultiIndexHitsCollectionViewDataSource? {
didSet {
dataSource?.hitsDataSource = hitsSource
dataSource?.hitsSource = hitsSource
collectionView.dataSource = dataSource
}
}

public var delegate: MultiIndexHitsCollectionViewDelegate? {
didSet {
delegate?.hitsDataSource = hitsSource
delegate?.hitsSource = hitsSource
collectionView.delegate = delegate
}
}
Expand Down
Expand Up @@ -12,7 +12,7 @@ open class MultiIndexHitsCollectionViewDataSource: NSObject {

private typealias CellConfigurator = (UICollectionView, Int) throws -> UICollectionViewCell

public weak var hitsDataSource: MultiIndexHitsSource?
public weak var hitsSource: MultiIndexHitsSource?

private var cellConfigurators: [Int: CellConfigurator]

Expand All @@ -21,14 +21,23 @@ open class MultiIndexHitsCollectionViewDataSource: NSObject {
super.init()
}

public func setCellConfigurator<Hit: Codable>(forSection section: Int, _ cellConfigurator: @escaping CollectionViewCellConfigurator<Hit>) {
public func setCellConfigurator<Hit: Codable>(forSection section: Int,
templateCellProvider: @escaping () -> UICollectionViewCell = { return .init() },
_ cellConfigurator: @escaping CollectionViewCellConfigurator<Hit>) {
cellConfigurators[section] = { [weak self] (collectionView, row) in
guard let dataSource = self?.hitsDataSource else { return UICollectionViewCell() }
guard let hit: Hit = try dataSource.hit(atIndex: row, inSection: section) else {
assertionFailure("Invalid state: Attempt to deqeue a cell for a missing hit in a hits Interactor")
return UICollectionViewCell()
guard let dataSource = self else {
return .init()
}
return cellConfigurator(collectionView, hit, IndexPath(item: row, section: section))

guard let hitsSource = dataSource.hitsSource else {
fatalError("Missing hits source")
}

guard let hit: Hit = try hitsSource.hit(atIndex: row, inSection: section) else {
return templateCellProvider()
}

return cellConfigurator(collectionView, hit, IndexPath(row: row, section: section))
}
}

Expand All @@ -37,22 +46,25 @@ open class MultiIndexHitsCollectionViewDataSource: NSObject {
extension MultiIndexHitsCollectionViewDataSource: UICollectionViewDataSource {

open func numberOfSections(in collectionView: UICollectionView) -> Int {
guard let numberOfSections = hitsDataSource?.numberOfSections() else {
return 0
guard let hitsSource = hitsSource else {
fatalError("Missing hits source")
}
return numberOfSections
return hitsSource.numberOfSections()
}

open func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
guard let numberOfRows = hitsDataSource?.numberOfHits(inSection: section) else {
return 0
guard let hitsSource = hitsSource else {
fatalError("Missing hits source")
}
return numberOfRows
return hitsSource.numberOfHits(inSection: section)
}

open func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
guard let cellConfigurator = cellConfigurators[indexPath.section] else {
fatalError("No cell configurator found for section \(indexPath.section)")
}
do {
return try cellConfigurators[indexPath.section]?(collectionView, indexPath.row) ?? UICollectionViewCell()
return try cellConfigurator(collectionView, indexPath.row)
} catch let error {
fatalError("\(error)")
}
Expand Down
Expand Up @@ -12,7 +12,7 @@ open class MultiIndexHitsCollectionViewDelegate: NSObject {

typealias ClickHandler = (UICollectionView, Int) throws -> Void

public weak var hitsDataSource: MultiIndexHitsSource?
public weak var hitsSource: MultiIndexHitsSource?

private var clickHandlers: [Int: ClickHandler]

Expand All @@ -23,11 +23,18 @@ open class MultiIndexHitsCollectionViewDelegate: NSObject {

public func setClickHandler<Hit: Codable>(forSection section: Int, _ clickHandler: @escaping CollectionViewClickHandler<Hit>) {
clickHandlers[section] = { [weak self] (collectionView, row) in
guard let hit: Hit = try self?.hitsDataSource?.hit(atIndex: row, inSection: section) else {
assertionFailure("Invalid state: Attempt to process a click of a cell for a missing hit in a hits Interactor")
guard let delegate = self else { return }

guard let hitsSource = delegate.hitsSource else {
fatalError("Missing hits source")
}

guard let hit: Hit = try hitsSource.hit(atIndex: row, inSection: section) else {
return
}

clickHandler(collectionView, hit, IndexPath(item: row, section: section))

}
}

Expand All @@ -36,8 +43,11 @@ open class MultiIndexHitsCollectionViewDelegate: NSObject {
extension MultiIndexHitsCollectionViewDelegate: UICollectionViewDelegate {

open func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
guard let clickHandler = clickHandlers[indexPath.section] else {
fatalError("No click handler found for section \(indexPath.section)")
}
do {
try clickHandlers[indexPath.section]?(collectionView, indexPath.row)
try clickHandler(collectionView, indexPath.row)
} catch let error {
fatalError("\(error)")
}
Expand Down
Expand Up @@ -21,12 +21,22 @@ open class MultiIndexHitsTableViewDataSource: NSObject {
super.init()
}

public func setCellConfigurator<Hit: Codable>(forSection section: Int, _ cellConfigurator: @escaping TableViewCellConfigurator<Hit>) {
public func setCellConfigurator<Hit: Codable>(forSection section: Int,
templateCellProvider: @escaping () -> UITableViewCell = { return .init() },
_ cellConfigurator: @escaping TableViewCellConfigurator<Hit>) {
cellConfigurators[section] = { [weak self] (tableView, row) in
guard let hit: Hit = try self?.hitsSource?.hit(atIndex: row, inSection: section) else {
assertionFailure("Invalid state: Attempt to deqeue a cell for a missing hit in a hits Interactor")
return UITableViewCell()
guard let dataSource = self else {
return .init()
}

guard let hitsSource = dataSource.hitsSource else {
fatalError("Missing hits source")
}

guard let hit: Hit = try hitsSource.hit(atIndex: row, inSection: section) else {
return templateCellProvider()
}

return cellConfigurator(tableView, hit, IndexPath(row: row, section: section))
}
}
Expand All @@ -36,22 +46,25 @@ open class MultiIndexHitsTableViewDataSource: NSObject {
extension MultiIndexHitsTableViewDataSource: UITableViewDataSource {

open func numberOfSections(in tableView: UITableView) -> Int {
guard let numberOfSections = hitsSource?.numberOfSections() else {
return 0
guard let hitsSource = hitsSource else {
fatalError("Missing hits source")
}
return numberOfSections
return hitsSource.numberOfSections()
}

open func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
guard let numberOfRows = hitsSource?.numberOfHits(inSection: section) else {
return 0
guard let hitsSource = hitsSource else {
fatalError("Missing hits source")
}
return numberOfRows
return hitsSource.numberOfHits(inSection: section)
}

open func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cellConfigurator = cellConfigurators[indexPath.section] else {
fatalError("No cell configurator found for section \(indexPath.section)")
}
do {
return try cellConfigurators[indexPath.section]?(tableView, indexPath.row) ?? UITableViewCell()
return try cellConfigurator(tableView, indexPath.row)
} catch let error {
fatalError("\(error)")
}
Expand Down
Expand Up @@ -23,11 +23,18 @@ open class MultiIndexHitsTableViewDelegate: NSObject {

public func setClickHandler<Hit: Codable>(forSection section: Int, _ clickHandler: @escaping TableViewClickHandler<Hit>) {
clickHandlers[section] = { [weak self] (tableView, row) in
guard let hit: Hit = try self?.hitsSource?.hit(atIndex: row, inSection: section) else {
assertionFailure("Invalid state: Attempt to process a click of a cell for a missing hit in a hits Interactor")
guard let delegate = self else { return }

guard let hitsSource = delegate.hitsSource else {
fatalError("Missing hits source")
}

guard let hit: Hit = try hitsSource.hit(atIndex: row, inSection: section) else {
return
}
clickHandler(tableView, hit, IndexPath(row: row, section: section))

clickHandler(tableView, hit, IndexPath(item: row, section: section))

}
}

Expand All @@ -36,8 +43,11 @@ open class MultiIndexHitsTableViewDelegate: NSObject {
extension MultiIndexHitsTableViewDelegate: UITableViewDelegate {

open func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
guard let clickHandler = clickHandlers[indexPath.section] else {
fatalError("No click handler found for section \(indexPath.section)")
}
do {
try clickHandlers[indexPath.section]?(tableView, indexPath.row)
try clickHandler(tableView, indexPath.row)
} catch let error {
fatalError("\(error)")
}
Expand Down

0 comments on commit 23144df

Please sign in to comment.