Created
November 14, 2017 01:59
-
-
Save JonFir/5ce4adf14dfef0228b2193dcaddbf11a to your computer and use it in GitHub Desktop.
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
import UIKit | |
protocol CellViewModelActionable { | |
typealias Action = () -> Void | |
var onClick: Action? { get } | |
} | |
protocol CellViewModel { | |
static var cellAnyType: UIView.Type { get } | |
func setupAny(cell: UIView) | |
} | |
protocol CellViewModelFaceless: CellViewModel { | |
associatedtype CellType: UIView | |
func setup(cell: CellType) | |
} | |
extension CellViewModelFaceless { | |
static var cellAnyType: UIView.Type { | |
return CellType.self | |
} | |
func setupAny(cell: UIView) { | |
if let cell = cell as? CellType { | |
setup(cell: cell) | |
} else { | |
assertionFailure() | |
} | |
} | |
} | |
extension UITableView { | |
func dequeueReusableCell(withModel model: CellViewModel, for indexPath: IndexPath) -> UITableViewCell { | |
let indetifier = String(describing: type(of: model).cellAnyType) | |
let cell = self.dequeueReusableCell(withIdentifier: indetifier, for: indexPath) | |
model.setupAny(cell: cell) | |
return cell | |
} | |
func register(nibModels: [CellViewModel.Type]) { | |
for model in nibModels { | |
let identifier = String(describing: model.cellAnyType) | |
let bundle = Bundle(for: model.cellAnyType) | |
let nib = UINib(nibName: identifier, bundle: bundle) | |
self.register(nib, forCellReuseIdentifier: identifier) | |
} | |
} | |
} | |
extension UITableView { | |
func dequeueReusableHeaderFooterView(withModel model: CellViewModel) -> UITableViewHeaderFooterView? { | |
let indetifier = String(describing: type(of: model).cellAnyType) | |
guard let cell = self.dequeueReusableHeaderFooterView(withIdentifier: indetifier) else { | |
return nil | |
} | |
model.setupAny(cell: cell) | |
return cell | |
} | |
func register(nibModelsForHeaderFooter nibModels: [CellViewModel.Type]) { | |
for model in nibModels { | |
let identifier = String(describing: model.cellAnyType) | |
let bundle = Bundle(for: model.cellAnyType) | |
let nib = UINib(nibName: identifier, bundle: bundle) | |
self.register(nib, forHeaderFooterViewReuseIdentifier: identifier) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment