-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathCellViewModel.swift
119 lines (89 loc) · 3.41 KB
/
CellViewModel.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
//
// Created by Jesse Squires
// https://www.jessesquires.com
//
// Documentation
// https://jessesquires.github.io/ReactiveCollectionsKit
//
// GitHub
// https://github.com/jessesquires/ReactiveCollectionsKit
//
// Copyright © 2019-present Jesse Squires
//
import UIKit
/// Defines a view model that describes and configures a cell in the collection view.
public protocol CellViewModel: DiffableViewModel {
associatedtype CellType: UICollectionViewCell
var reuseIdentifier: String { get }
var nib: UINib? { get }
var shouldHighlight: Bool { get }
func registerWith(collectionView: UICollectionView)
func configure(cell: CellType)
func didSelect(with controller: UIViewController)
}
extension CellViewModel {
public var cellClass: AnyClass { CellType.self }
public var reuseIdentifier: String { "\(Self.self)" }
public var nib: UINib? { nil }
public var shouldHighlight: Bool { true }
public func registerWith(collectionView: UICollectionView) {
if let nib = self.nib {
collectionView.register(nib, forCellWithReuseIdentifier: self.reuseIdentifier)
} else {
collectionView.register(self.cellClass, forCellWithReuseIdentifier: self.reuseIdentifier)
}
}
public func dequeueAndConfigureCellFor(collectionView: UICollectionView, at indexPath: IndexPath) -> CellType {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: self.reuseIdentifier, for: indexPath) as! CellType
self.configure(cell: cell)
return cell
}
public func didSelect(with controller: UIViewController) { }
public func toAnyViewModel() -> AnyCellViewModel {
AnyCellViewModel(self)
}
}
/// A type-erased cell view model.
public struct AnyCellViewModel: CellViewModel {
// MARK: DiffableViewModel
public var id: UniqueIdentifier { self._id }
// MARK: CellViewModel
public typealias CellType = UICollectionViewCell
public var reuseIdentifier: String { self._reuseIdentifier }
public var nib: UINib? { self._nib }
public var shouldHighlight: Bool { self._shouldHighlight }
public func registerWith(collectionView: UICollectionView) {
self._register(collectionView)
}
public func configure(cell: UICollectionViewCell) {
self._configure(cell)
}
public func didSelect(with controller: UIViewController) {
self._didSelect(controller)
}
// MARK: Private
private let _id: UniqueIdentifier
private let _reuseIdentifier: String
private let _nib: UINib?
private let _shouldHighlight: Bool
private let _register: (UICollectionView) -> Void
private let _configure: (CellType) -> Void
private let _didSelect: (UIViewController) -> Void
// MARK: Init
public init<T: CellViewModel>(_ viewModel: T) {
self._id = viewModel.id
self._reuseIdentifier = viewModel.reuseIdentifier
self._nib = viewModel.nib
self._shouldHighlight = viewModel.shouldHighlight
self._register = { collectionView in
viewModel.registerWith(collectionView: collectionView)
}
self._configure = { cell in
precondition(cell is T.CellType, "Cell must be of type \(T.CellType.self). Found \(cell.self)")
viewModel.configure(cell: cell as! T.CellType)
}
self._didSelect = { controller in
viewModel.didSelect(with: controller)
}
}
}