-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathPrototypeCell.swift
169 lines (138 loc) · 5.46 KB
/
PrototypeCell.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#if os(iOS)
import UIKit
public protocol PrototypeViewCell: ListViewCell {
/// The TableView or CollectionView that owns this cell.
var referenceView: ListContainerView? { get set }
/// Apply the model.
/// - Note: This is used internally from the infra to set the model.
func applyModel(_ model: Any?)
/// Constructor.
init(reuseIdentifier: String)
}
open class PrototypeTableViewCell: UITableViewCell, PrototypeViewCell {
/// The wrapped view.
open var view: UIView!
/// The associated model.
open var model: Any? {
didSet { didSetModelClosure?(model) }
}
/// The *UICollectionView/UITableView* for this prototype cell.
open weak var referenceView: ListContainerView?
/// Internal state.
fileprivate var didInitializeCell = false
fileprivate var didSetModelClosure: ((Any?) -> Void)?
public required init(reuseIdentifier: String) {
super.init(style: .default, reuseIdentifier: reuseIdentifier)
}
required public init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
open func initializeCellIfNecessary(
_ view: UIView,
didSetModel: ((Any?) -> Void)? = nil
) {
assert(Thread.isMainThread)
// make sure this happens just once.
if self.didInitializeCell { return }
self.didInitializeCell = true
self.view = view
self.contentView.addSubview(view)
self.didSetModelClosure = didSetModel
}
open func applyModel(_ model: Any?) {
guard let model = model else { return }
self.model = model
}
/// Asks the view to calculate and return the size that best fits the specified size.
/// - parameter size: The size for which the view should calculate its best-fitting size.
/// - returns: A new size that fits the receiver’s subviews.
open override func sizeThatFits(_ size: CGSize) -> CGSize {
let size = view.sizeThatFits(size)
return size
}
/// Returns the natural size for the receiving view, considering only properties of the
/// view itself.
/// - returns: A size indicating the natural size for the receiving view based on its
/// intrinsic properties.
open override var intrinsicContentSize: CGSize {
return view.intrinsicContentSize
}
}
open class PrototypeCollectionViewCell: UICollectionViewCell, PrototypeViewCell {
///The wrapped view.
open var view: UIView!
///The model for this cell.
/// - Note: This is propagated to the associted.
open var model: Any? {
didSet { didSetModelClosure?(model) }
}
/// The *UICollectionView/UITableView* for this prototype cell.
weak open var referenceView: ListContainerView?
/// Internal state.
fileprivate var didInitializeCell = false
fileprivate var didSetModelClosure: ((Any?) -> Void)?
public required init(reuseIdentifier: String) {
super.init(frame: CGRect.zero)
}
required public init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
open func initializeCellIfNecessary(
_ view: UIView,
didSetModel: ((Any?) -> Void)? = nil
) {
assert(Thread.isMainThread)
// make sure this happens just once
if self.didInitializeCell { return }
self.didInitializeCell = true
self.view = view
self.contentView.addSubview(view)
self.didSetModelClosure = didSetModel
}
open func applyModel(_ model: Any?) {
self.model = model
}
/// Asks the view to calculate and return the size that best fits the specified size.
/// - parameter size: The size for which the view should calculate its best-fitting size.
/// - returns: A new size that fits the receiver’s subviews.
open override func sizeThatFits(_ size: CGSize) -> CGSize {
let size = view.sizeThatFits(size)
return size
}
/// Returns the natural size for the receiving view, considering only properties of the
/// view itself.
/// - returns: A size indicating the natural size for the receiving view based on its
/// intrinsic properties.
open override var intrinsicContentSize: CGSize {
return view.intrinsicContentSize
}
}
public enum Prototypes {
fileprivate static var registeredPrototypes = [String: PrototypeViewCell]()
/// Wether there's a prototype registered for a given reusedIdentifier.
public static func isPrototypeCellRegistered(_ reuseIdentifier: String) -> Bool {
guard let _ = Prototypes.registeredPrototypes[reuseIdentifier] else { return false }
return true
}
/// Register a cell a prototype for a given reuse identifer.
public static func registerPrototypeCell(_ reuseIdentifier: String, cell: PrototypeViewCell) {
Prototypes.registeredPrototypes[reuseIdentifier] = cell
}
/// Computes the size for the cell registered as prototype associate to the item passed
/// as argument.
/// - parameter item: The target item for size calculation.
public static func prototypeCellSize(_ item: ListItemType) -> CGSize {
guard let cell = Prototypes.registeredPrototypes[item.reuseIdentifier] else {
fatalError("Unregistered prototype with reuse identifier \(item.reuseIdentifier).")
}
cell.applyModel(item.modelRef)
cell.referenceView = item.referenceView
item.configure(cell: cell)
if let cell = cell as? UIView {
return cell.bounds.size
} else {
return CGSize.zero
}
}
}
#endif