Skip to content

Commit

Permalink
Merge pull request #7 from thefuntasty/housekeep/any-equatable-cell-m…
Browse files Browse the repository at this point in the history
…odel

Housekeep: Any equatable cell model
  • Loading branch information
mkj-is authored Sep 25, 2018
2 parents 1640f69 + 4c9dff6 commit a5d72fe
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 62 deletions.
77 changes: 17 additions & 60 deletions Sources/CellModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,6 @@ public protocol CellModel: ReusableView {
func configure(cell: AnyObject)
}

public protocol EquatableCellModel: CellModel {
func asEquatable() -> AnyEquatableCellModel
func isEqualTo(_ other: CellModel) -> Bool
}

extension EquatableCellModel where Self: Equatable {
public func asEquatable() -> AnyEquatableCellModel {
return AnyEquatableCellModel(self)
}

public func isEqualTo(_ other: CellModel) -> Bool {
guard let otherCellModel = other as? Self else { return false }
return self == otherCellModel
}
}

public extension CellModel {
var cellHeight: CGFloat {
return 44
Expand All @@ -54,60 +38,33 @@ public protocol SupplementaryViewModel: ReusableView {
func configure(cell: AnyObject)
}

public struct AnyEquatableCellModel: EquatableCellModel {
public var cellModel: EquatableCellModel

// MARK: - Reusable view properties

public var registersLazily: Bool {
return cellModel.registersLazily
}

public var usesNib: Bool {
return cellModel.usesNib
}

public var bundle: Bundle {
return cellModel.bundle
}

public var nib: UINib? {
return cellModel.nib
}

public var cellClass: AnyClass {
return cellModel.cellClass
}

public var reuseIdentifier: String {
return cellModel.reuseIdentifier
}

// MARK: - Cell model properties
public protocol EquatableCellModel: CellModel {
func isEqual(to other: CellModel) -> Bool
}

public var cellHeight: CGFloat {
return cellModel.cellHeight
extension EquatableCellModel {
func asEquatable() -> EquatableCellModelWrapper {
return EquatableCellModelWrapper(self)
}
}

public var highlighting: Bool {
return cellModel.highlighting
extension EquatableCellModel where Self: Equatable {
public func isEqual(to other: CellModel) -> Bool {
guard let otherCellModel = other as? Self else { return false }
return self == otherCellModel
}
}

public var separatorIsHidden: Bool {
return cellModel.separatorIsHidden
}
struct EquatableCellModelWrapper {
let cellModel: EquatableCellModel

init(_ cellModel: EquatableCellModel) {
self.cellModel = cellModel
}

public func configure(cell: AnyObject) {
cellModel.configure(cell: cell)
}
}

extension AnyEquatableCellModel: Equatable {
public static func ==(lhs: AnyEquatableCellModel, rhs: AnyEquatableCellModel) -> Bool {
return lhs.cellModel.isEqualTo(rhs.cellModel)
extension EquatableCellModelWrapper: Equatable {
static func ==(lhs: EquatableCellModelWrapper, rhs: EquatableCellModelWrapper) -> Bool {
return lhs.cellModel.isEqual(to: rhs.cellModel)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ open class EquatableCellModelDataSource: AbstractDataSource, DataSource {
}
}

let diffCalculator: AbstractDiffCalculator<EquatableCellModelSection, AnyEquatableCellModel>
let diffCalculator: AbstractDiffCalculator<EquatableCellModelSection, EquatableCellModelWrapper>

public init(_ tableView: UITableView, sections: [EquatableCellModelSection]) {
self.diffCalculator = TableViewDiffCalculator(tableView: tableView)
Expand Down Expand Up @@ -57,6 +57,6 @@ open class EquatableCellModelDataSource: AbstractDataSource, DataSource {
}

override func cellModel(at indexPath: IndexPath) -> CellModel {
return self.diffCalculator.value(atIndexPath: indexPath)
return self.diffCalculator.value(atIndexPath: indexPath).cellModel
}
}

0 comments on commit a5d72fe

Please sign in to comment.