-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathCollectionViewDriver.swift
345 lines (299 loc) · 14 KB
/
CollectionViewDriver.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
//
// 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 Combine
import Foundation
import UIKit
/// Represents the main entry-point to the library, and underlying `UICollectionView` components.
/// A `CollectionViewDriver` is responsible for "driving" the collection view.
/// It handles all layout, data source, delegate, and diffing operations.
@MainActor
public final class CollectionViewDriver: NSObject {
/// A closure type used to notify callers of collection view updates.
public typealias DidUpdate = @MainActor (CollectionViewDriver) -> Void
/// The collection view.
public let view: UICollectionView
/// A set of options to customize behavior of the driver.
public let options: CollectionViewDriverOptions
/// The collection view model.
@Published public private(set) var viewModel: CollectionViewModel
private let _emptyViewProvider: EmptyViewProvider?
private var _currentEmptyView: UIView?
// Avoiding a strong reference to prevent a possible retain cycle.
// This is typically the view controller that owns `self` (the driver).
// The caller is responsible for retaining this object for the lifetime of the driver.
private weak var _cellEventCoordinator: CellEventCoordinator?
private(set) var _dataSource: DiffableDataSource
private var _cachedRegistrations = Set<ViewRegistration>()
// MARK: Init
/// Initializes a new `CollectionViewDriver`.
///
/// - Parameters:
/// - view: The collection view.
/// - viewModel: The collection view model.
/// - options: A set of options to customize behavior of the driver.
/// - emptyViewProvider: An empty view provider.
/// - cellEventCoordinator: The cell event coordinator,
/// if you wish to handle cell events outside of your cell view models.
/// **Note: This object is not retained by the driver.**
///
/// - Warning: The driver **does not** retain the `cellEventCoordinator`,
/// because this object is typically the view controller that owns the driver.
/// Thus, the caller is responsible for retaining and keeping alive the `cellEventCoordinator`
/// for the entire lifetime of the driver.
public init(view: UICollectionView,
viewModel: CollectionViewModel = .empty,
options: CollectionViewDriverOptions = .init(),
emptyViewProvider: EmptyViewProvider? = nil,
cellEventCoordinator: CellEventCoordinator? = nil) {
self.view = view
self.viewModel = viewModel
self.options = options
self._emptyViewProvider = emptyViewProvider
self._cellEventCoordinator = cellEventCoordinator
// workaround for swift initialization rules.
// the "real" init is below.
self._dataSource = DiffableDataSource(view: view, diffOnBackgroundQueue: false)
super.init()
// because view model is a value type, we cannot capture it directly.
// it is constantly updated, and a capture would prevent updates to the data source.
//
// thus, we must capture `self` (the driver), which is a reference type.
// then we can dequeue can configure cells from the latest `self.viewModel`.
//
// using `unowned` for each closure breaks a potential cycle, and is safe to use here.
// `self` owns the `_dataSource`, so we know that `self` will always exist.
self._dataSource = DiffableDataSource(
view: view,
diffOnBackgroundQueue: options.diffOnBackgroundQueue,
cellProvider: { [unowned self] view, indexPath, itemIdentifier in
self._cellProvider(
collectionView: view,
indexPath: indexPath,
identifier: itemIdentifier
)
},
supplementaryViewProvider: { [unowned self] view, elementKind, indexPath in
self._supplementaryViewProvider(
collectionView: view,
elementKind: elementKind,
indexPath: indexPath
)
})
self.view.dataSource = self._dataSource
self.view.delegate = self
self._updateViewModel(from: .empty, to: viewModel, animated: false, completion: nil)
}
// MARK: State Information
/// The number of sections displayed by the collection view.
public var numberOfSections: Int {
self.viewModel.sections.count
}
/// Returns the count of items in the specified section.
/// - Parameter section: The index of the section for which you want a count of the items.
/// - Returns: The number of items in the specified section.
public func numberOfItems(in section: Int) -> Int {
self.viewModel.sections[section].cells.count
}
// MARK: Applying Updates
/// Updates the collection with the provided `viewModel`.
/// This method will trigger a diff between the previous view model and the newly provided view model.
///
/// - Parameters:
/// - viewModel: The new collection view model.
/// - animated: Whether or not to animate updates.
/// - completion: A closure to call when the driver finishes diffing and updating the collection view.
/// The driver passes itself to the closure. It is always called on the main thread.
///
/// - Warning: If you provide a `viewModel` with an `id` different from the previous one,
/// this is considered a *replacement*. By default, the driver will animate the diff between the view models.
/// You can customize this behavior via the ``options`` for the driver.
public func update(viewModel new: CollectionViewModel, animated: Bool = true, completion: DidUpdate? = nil) {
self._updateViewModel(
from: self.viewModel,
to: new,
animated: animated,
completion: completion
)
}
/// An async version of ``update(viewModel:animated:completion:)``.
///
/// Updates the collection with the provided `viewModel`.
/// This method will trigger a diff between the previous view model and the newly provided view model.
///
/// - Parameters:
/// - viewModel: The new collection view model.
/// - animated: Whether or not to animate updates.
///
/// - Warning: If you provide a `viewModel` with an `id` different from the previous one,
/// this is considered a *replacement*. By default, the driver will animate the diff between the view models.
/// You can customize this behavior via the ``options`` for the driver.
public func update(viewModel new: CollectionViewModel, animated: Bool = true) async {
await withCheckedContinuation { continuation in
self.update(viewModel: new, animated: animated)
continuation.resume()
}
}
// MARK: Private
private func _registerAllViews(for viewModel: CollectionViewModel) {
let allRegistrations = viewModel.allRegistrations()
let newRegistrations = allRegistrations.subtracting(self._cachedRegistrations)
newRegistrations.forEach {
$0.registerWith(collectionView: self.view)
}
self._cachedRegistrations.formUnion(newRegistrations)
}
private func _updateViewModel(
from old: CollectionViewModel,
to new: CollectionViewModel,
animated: Bool,
completion: DidUpdate?
) {
self.viewModel = new
self._registerAllViews(for: new)
if self.options.reloadDataOnReplacingViewModel {
// if given a totally new model, simply reload instead of diff
guard new.id == old.id else {
self._dataSource.reload(self.viewModel) { [weak self] in
// Note: UIKit guarantees this closure is called on the main queue.
self?._displayEmptyViewIfNeeded(animated: animated, completion: completion)
}
return
}
}
self._dataSource.applySnapshot(
from: old,
to: new,
animated: animated
) { [weak self] in
// Note: UIKit guarantees this closure is called on the main queue.
self?._displayEmptyViewIfNeeded(animated: animated, completion: completion)
}
}
private func _displayEmptyViewIfNeeded(animated: Bool, completion: DidUpdate?) {
if self.viewModel.isEmpty {
// view model is empty, but we are already displaying the empty state view
guard self._currentEmptyView == nil else {
completion?(self)
return
}
// empty state view was not provided
guard let emptyView = self._emptyViewProvider?.view else {
completion?(self)
return
}
emptyView.frame = self.view.frame
emptyView.translatesAutoresizingMaskIntoConstraints = false
emptyView.alpha = 0
self.view.addSubview(emptyView)
NSLayoutConstraint.activate([
emptyView.topAnchor.constraint(equalTo: self.view.superview!.topAnchor),
emptyView.bottomAnchor.constraint(equalTo: self.view.superview!.bottomAnchor),
emptyView.leadingAnchor.constraint(equalTo: self.view.superview!.leadingAnchor),
emptyView.trailingAnchor.constraint(equalTo: self.view.superview!.trailingAnchor)
])
self._currentEmptyView = emptyView
self._animateEmptyView(isHidden: false, animated: animated, completion: completion)
self.view.isScrollEnabled = false
} else {
self._animateEmptyView(isHidden: true, animated: animated, completion: completion)
self.view.isScrollEnabled = true
}
}
private func _animateEmptyView(isHidden: Bool, animated: Bool, completion: DidUpdate?) {
guard animated else {
if isHidden {
self._currentEmptyView?.removeFromSuperview()
self._currentEmptyView = nil
} else {
self._currentEmptyView?.alpha = 1
}
completion?(self)
return
}
UIView.animate(withDuration: 0.3, delay: 0, options: .curveEaseInOut) {
self._currentEmptyView?.alpha = isHidden ? 0 : 1
} completion: { _ in
if isHidden {
self._currentEmptyView?.removeFromSuperview()
self._currentEmptyView = nil
}
completion?(self)
}
}
private func _cellProvider(
collectionView: UICollectionView,
indexPath: IndexPath,
identifier: UniqueIdentifier
) -> UICollectionViewCell {
let cell = self.viewModel.cellViewModel(for: identifier)
precondition(cell != nil, "Inconsistent state. Cell with identifier \(identifier) does not exist.")
return cell!.dequeueAndConfigureCellFor(collectionView: collectionView, at: indexPath)
}
private func _supplementaryViewProvider(
collectionView: UICollectionView,
elementKind: String,
indexPath: IndexPath
) -> UICollectionReusableView? {
let supplementaryView = self.viewModel.supplementaryViewModel(ofKind: elementKind, at: indexPath)
return supplementaryView?.dequeueAndConfigureViewFor(collectionView: collectionView, at: indexPath)
}
}
// MARK: UICollectionViewDelegate
extension CollectionViewDriver: UICollectionViewDelegate {
// MARK: Managing the selected cells
/// :nodoc:
public func collectionView(_ collectionView: UICollectionView,
didSelectItemAt indexPath: IndexPath) {
self.viewModel.cellViewModel(at: indexPath).didSelect(with: self._cellEventCoordinator)
}
// MARK: Managing cell highlighting
/// :nodoc:
public func collectionView(_ collectionView: UICollectionView,
shouldHighlightItemAt indexPath: IndexPath) -> Bool {
self.viewModel.cellViewModel(at: indexPath).shouldHighlight
}
// MARK: Managing context menus
/// :nodoc:
public func collectionView(_ collectionView: UICollectionView,
contextMenuConfigurationForItemAt indexPath: IndexPath,
point: CGPoint) -> UIContextMenuConfiguration? {
self.viewModel.cellViewModel(at: indexPath).contextMenuConfiguration
}
// MARK: Tracking the addition and removal of views
/// :nodoc:
public func collectionView(_ collectionView: UICollectionView,
willDisplay cell: UICollectionViewCell,
forItemAt indexPath: IndexPath) {
self.viewModel._safeCellViewModel(at: indexPath)?.willDisplay()
}
/// :nodoc:
public func collectionView(_ collectionView: UICollectionView,
willDisplaySupplementaryView view: UICollectionReusableView,
forElementKind elementKind: String,
at indexPath: IndexPath) {
self.viewModel._safeSupplementaryViewModel(ofKind: elementKind, at: indexPath)?.willDisplay()
}
/// :nodoc:
public func collectionView(_ collectionView: UICollectionView,
didEndDisplaying cell: UICollectionViewCell,
forItemAt indexPath: IndexPath) {
self.viewModel._safeCellViewModel(at: indexPath)?.didEndDisplaying()
}
/// :nodoc:
public func collectionView(_ collectionView: UICollectionView,
didEndDisplayingSupplementaryView view: UICollectionReusableView,
forElementOfKind elementKind: String,
at indexPath: IndexPath) {
self.viewModel._safeSupplementaryViewModel(ofKind: elementKind, at: indexPath)?.didEndDisplaying()
}
}