-
Notifications
You must be signed in to change notification settings - Fork 23
/
CollectionViewModelDataSource.swift
1279 lines (1067 loc) · 55.2 KB
/
CollectionViewModelDataSource.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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#if os(iOS)
import UIKit
#elseif os(macOS)
import AppKit
#endif
import Pilot
import QuartzCore
/// Can be used to provide reuse ids for collection view cells.
public protocol CollectionViewCellReuseIdProvider {
func reuseIdForViewModel(_ viewModel: ViewModel, viewType: View.Type) -> String
}
open class DefaultCollectionViewCellReuseIdProvider: CollectionViewCellReuseIdProvider {
public init() {
}
// MARK: CollectionViewCellReuseIdProvider
/// The default implementation, if a provider isn't specified in the initializer.
open func reuseIdForViewModel(_ viewModel: ViewModel, viewType: View.Type) -> String {
return "\(NSStringFromClass(viewType))-\(type(of: viewModel))"
}
}
private enum CollectionViewState {
/// Don't know exactly what the CollectionView thinks the world looks like until it asks for the section count.
case loading
/// Told CollectionView to performBatchUpdates but it hasn't completed yet.
case animating
/// If the underlying ModelCollection has changed while animating, it must be updated again when the animation
/// completes.
case animatingWithPendingChanges
/// The CollectionView and ModelCollection are in sync with each other.
case synced
}
public final class CurrentCollection: SectionedModelCollection, ProxyingCollectionEventObservable {
// MARK: Init
public init(_ collectionId: ModelCollectionId) {
self.collectionId = collectionId
}
// MARK: ModelCollection
public let collectionId: ModelCollectionId
public var state: ModelCollectionState {
// Dynamic getter because `sectionedState` is the source-of-truth for this class.
return sectionedState.flattenedState()
}
public var proxiedObservable: Observable<CollectionEvent> { return observers }
private let observers = ObserverList<CollectionEvent>()
// MARK: SectionedModelCollection
public private(set) var sectionedState: [ModelCollectionState] = [] {
didSet {
observers.notify(.didChangeState(state))
}
}
// MARK: Private
fileprivate func beginUpdate(_ collection: ModelCollection) -> (CollectionEventUpdates, () -> Void){
// If the collection is already sectioned, this will honor those sections. Otherwise, it will
// provide a single section wrapping the original collection.
let sectionedCollection = collection.asSectioned()
let updates = diffEngine.update(sectionedCollection.sections, debug: false)
let commitSectionedState = sectionedCollection.sectionedState
return (updates, {
self.sectionedState = commitSectionedState
})
}
fileprivate func update(_ collection: ModelCollection) {
let (_, commitCollectionChanges) = beginUpdate(collection)
commitCollectionChanges()
}
private var diffEngine = DiffEngine()
}
/// Data source for collection views which handles all the necessary binding between models -> view models, and view
/// models -> view types. It handles observing the underlying model and handling all required updates to the collection
/// view.
public class CollectionViewModelDataSource: NSObject, ProxyingObservable {
// MARK: Init
public init(
model: ModelCollection,
modelBinder: ViewModelBindingProvider,
viewBinder: ViewBindingProvider,
context: Context,
reuseIdProvider: CollectionViewCellReuseIdProvider
) {
self.underlyingCollection = model
self.currentCollection = CurrentCollection("CVMDS-Current")
self.modelBinder = modelBinder
self.viewBinder = viewBinder
self.context = context
self.reuseIdProvider = reuseIdProvider
self.collectionViewState = .loading
super.init()
self.collectionObserver = self.underlyingCollection.observe { [weak self] event in
self?.handleCollectionEvent(event)
}
registerForNotifications()
}
deinit {
unregisterForNotifications()
}
// MARK: Public
public enum Event {
/// `willUpdateItems` is triggered when the underlying ModelCollection has changed its data, but the
/// updates have not been applied to either the CollectionView or `currentCollection`.
/// While handling this event, `currentCollection` contains the collection's contents before the change.
case willUpdateItems(CollectionEventUpdates)
/// `didUpdateItems` is triggered after the ModelCollection has changed and the CollectionView has
/// finished animating to the new state.
/// Implementation note: In the case of a reload, this event can fire before the CollectionView has updated
/// with the new contents.
case didUpdateItems(CollectionEventUpdates)
}
/// A ModelCollection that provides access to the data as far as the CollectionView knows. If the CollectionView
/// is in the process of being updated, this data might be slightly behind the ModelCollection passed as the constructor.
/// The data in `currentCollection` is updated between the `willUpdateItems` and `didUpdateItems` events.
public let currentCollection: CurrentCollection
/// Underlying model context.
public private(set) var context: Context
/// Associated collection view for this data source. The owner of the `CollectionViewModelDataSource` should set
/// this property after initialization.
public weak var collectionView: PlatformCollectionView? {
willSet {
precondition(newValue != nil)
// TODO(wkiefer) T126138: NestedModelCollectionView binds and unbinds to a data source. We'll have to make
// this class support attach/detatch from a collection view properly.
// precondition(collectionView == nil)
}
}
/// Adds a `ViewModelBindingProvider` for any supplementary views of `kind`. If not set the
/// `DefaultViewModelBindingProvider` is used.
open func setViewModelBinder(_ binder: ViewModelBindingProvider, forSupplementaryElementOfKind kind: String) {
supplementaryViewModelBinderMap[kind] = binder
}
/// Removes any `ViewModelBindingProvider` for supplementary views of `kind`. Once cleared the
/// `DefaultViewModelBindingProvider` will be used.
open func clearViewModelBinder(forSupplementaryElementOfKind kind: String) {
supplementaryViewModelBinderMap[kind] = nil
}
/// Adds a `ViewBindingProvider` to provide views for any supplementary views of the given `kind`.
open func setViewBinder(_ viewBinder: ViewBindingProvider, forSupplementaryElementOfKind kind: String) {
supplementaryViewBinderMap[kind] = viewBinder
}
/// Removes a previously-added `ViewBindingProvider` for supplementary views.
open func clearViewBinderForSupplementaryElementOfKind(_ kind: String) {
supplementaryViewBinderMap[kind] = nil
}
/// Adds an `IndexedModelProvider` for any supplementary views of `kind`. If not set,
/// supplementary views default to the ModelType provided by the ModelCollection for the given
/// index path, or a CollectionZeroItemModel otherwise.
public func setModelProvider(provider: IndexedModelProvider, forSupplementaryElementOfKind kind: String) {
supplementaryModelProviderMap[kind] = provider
}
/// Removes a previously-added `IndexedModelProvider` for supplementary views.
public func clearModelProviderForSupplementaryElementOfKind(kind: String) {
supplementaryModelProviderMap[kind] = nil
}
#if os(iOS)
/// Configures the default background color for all host cells.
open var collectionViewBackgroundColor = UIColor.white
#endif
/// Method to return the preferred layout for a given item. Typically collection view controllers would implement
/// any layout delegate methods that need a size, and call into this to fetch the desired size for an item.
/// TODO(ca): change this to take a ModelPath instead of the heavier IndexPath
open func preferredLayoutForItemAtIndexPath(
_ indexPath: IndexPath,
availableSize: AvailableSize) -> PreferredLayout {
guard let modelItem: Model = currentCollection.atIndexPath(indexPath) else { return .none }
var cachedViewModel = self.cachedViewModel(for: modelItem)
if let layout = cachedViewModel.preferredLayout.layout {
if layout.validCache(forViewModel: cachedViewModel.viewModel) {
return cachedViewModel.preferredLayout
}
}
cachedViewModel.preferredLayout = viewBinder.preferredLayout(
fitting: availableSize,
for: cachedViewModel.viewModel,
context: context)
viewModelCache[modelItem.modelId] = cachedViewModel
return cachedViewModel.preferredLayout
}
/// Same as `preferredLayoutForItemAtIndexPath:availableSize:` but for supplementary view size estimation.
open func preferredLayoutForSupplementaryElementAtIndexPath(
_ indexPath: IndexPath,
kind: String,
availableSize: AvailableSize
) -> PreferredLayout {
guard let supplementaryViewBinder = supplementaryViewBinderMap[kind] else {
Log.fatal(message: "Request for supplementary kind (\(kind)) that has no registered view binder.")
}
let viewModel = viewModelForSupplementaryElementAtIndexPath(kind, indexPath: indexPath)
// TODO:(wkiefer) Cache supplementary sizes too.
return supplementaryViewBinder.preferredLayout(
fitting: availableSize,
for: viewModel,
context: context)
}
#if os(iOS)
/// Attempts to reload a supplementary element at index path by re-binding the hosted view to the view model
/// Note: This function makes no attempt to determine whether layout needs to be invalidated, so if you're making an
/// update that should trigger the collection view layout being invalidated make sure to do that separately.
open func reloadSupplementaryElementAtIndexPath(_ indexPath: IndexPath, kind: String) {
guard let cv = collectionView else { return }
let supplementaryView = cv.supplementaryView(forElementKind: kind, at: indexPath)
guard let hostView = supplementaryView as? CollectionViewHostReusableView else { return }
guard let hostedView = hostView.hostedView else { return }
let viewModel = viewModelForSupplementaryElementAtIndexPath(kind, indexPath: indexPath)
hostedView.bindToViewModel(viewModel)
hostView.hostedView = hostedView
}
#elseif os(OSX)
/// Attempts to reload a supplementary element at index path by re-binding the hosted view to the view model
/// Note: This function makes no attempt to determine whether layout needs to be invalidated, so if you're making an
/// update that should trigger the collection view layout being invalidated make sure to do that separately.
public func reloadSupplementaryElementAtIndexPath(indexPath: IndexPath, kind: String) {
guard let cv = collectionView else { return }
guard let supplementaryViewBinder = supplementaryViewBinderMap[kind] else { return }
let supplementaryView = cv.supplementaryView(forElementKind: kind, at: indexPath)
guard let hostView = supplementaryView as? CollectionViewHostReusableView else { return }
guard var hostedView = hostView.hostedView else { return }
let viewModel = viewModelForSupplementaryElementAtIndexPath(kind, indexPath: indexPath)
hostedView = supplementaryViewBinder.view(for: viewModel, context: context, reusing: hostedView, layout: nil)
hostView.hostedView = hostedView
hostedView.bindToViewModel(viewModel)
}
#endif
/// Returns a bound `ViewModel` for the given `indexPath` or nil if the index path is not valid.
open func viewModelAtIndexPath(_ indexPath: IndexPath) -> ViewModel? {
guard let modelItem: Model = currentCollection.atIndexPath(indexPath as IndexPath) else { return nil }
return cachedViewModel(for: modelItem).viewModel
}
/// Block which is invoked anytime a view model will be rebound to a view (typically during updates that don't
/// require a reload of the cell).
open var willRebindViewModel: (ViewModel) -> Void = { _ in }
/// Clears any internally-cached preferred item size calculations. Should typically be called when the size of the
/// collection view will change.
open func clearCachedItemSizes() {
var mutatedViewModelCache: [ModelId: CachedViewModel] = [:]
for (key, cachedViewModel) in viewModelCache {
var updatedCachedViewModel = cachedViewModel
updatedCachedViewModel.preferredLayout = .none
mutatedViewModelCache[key] = updatedCachedViewModel
}
viewModelCache = mutatedViewModelCache
}
/// Possible styles for any model update animations.
public enum UpdateAnimationStyle {
/// All model update changes are animated.
case always
/// The given update is animated depending on the result of the associated closure.
case conditional((CollectionEventUpdates) -> Bool)
/// No updates are animated, but updates are incremental.
case none
/// No updates are animated, and all updates reload all data.
case noneReloadOnly
}
/// Current update animation style. For details see `UpdateAnimationStyle`.
open var updateAnimationStyle = UpdateAnimationStyle.always
/// Returns whether the given `CollectionEventUpdates` should animate depending on the current value of
/// `updateAnimationStyle` and the contents of the updates.
open func shouldAnimateUpdates(_ updates: CollectionEventUpdates) -> Bool {
switch updateAnimationStyle {
case .always:
return true
case .none, .noneReloadOnly:
return false
case .conditional(let shouldAnimate):
return shouldAnimate(updates)
}
}
/// Provider for collection reuse ids. This is set with a default based on the names of the
/// ViewModel and View, but can be overridden to optimize reuse ids for a collection.
public let reuseIdProvider: CollectionViewCellReuseIdProvider
// MARK: ObservableType
public var proxiedObservable: Observable<Event> { return observers }
private let observers = ObserverList<Event>()
// MARK: Private
private let modelBinder: ViewModelBindingProvider
private let viewBinder: ViewBindingProvider
/// The collection whose contents are synchronized to this CollectionView.
/// The underlyingCollection's data may be newer than the CollectionView's understanding of the world.
private let underlyingCollection: ModelCollection
private var collectionViewState: CollectionViewState
private var collectionObserver: Observer?
/// Cache of view models and sizing information.
private var viewModelCache: [ModelId: CachedViewModel] = [:]
/// Map from supplementary element kind (as `String`) to binding providers for supplementary views.
private var supplementaryViewBinderMap: [String: ViewBindingProvider] = [:]
/// Map from supplementary element kind (as `String`) to binding providers for supplementary view models.
private var supplementaryViewModelBinderMap: [String: ViewModelBindingProvider] = [:]
/// Map from supplementary element kind (as `String`) to model provider for supplementary elements.
private var supplementaryModelProviderMap: [String: IndexedModelProvider] = [:]
private var notificationTokens: [NSObjectProtocol] = []
private var inBackground = false
private func registerForNotifications() {
#if os(iOS)
let nc = NotificationCenter.default
// Note: Rather than observer `UIApplicationWillEnterForegroundNotification`, didBecomeActive is watched instead
// because typically it's better for collection view updates to batch until actually active (instead of multiple
// animations as the application is transitioning).
notificationTokens.append(nc.addObserver(
forName: UIApplication.didBecomeActiveNotification,
object: nil,
queue: OperationQueue.main) { [weak self] _ in
self?.inBackground = false
})
notificationTokens.append(nc.addObserver(
forName: UIApplication.didEnterBackgroundNotification,
object: nil,
queue: OperationQueue.main) { [weak self] _ in
self?.inBackground = true
})
#endif // os(iOS)
}
private func unregisterForNotifications() {
let nc = NotificationCenter.default
notificationTokens.forEach { nc.removeObserver($0) }
notificationTokens.removeAll()
}
private func modelForSupplementaryIndexPath(_ indexPath: IndexPath, ofKind kind: String) -> Model {
if let provider = supplementaryModelProviderMap[kind] {
// If a provider was set, and that provider provides a model, use it.
if let model = provider.model(for: indexPath, context: context) {
return model
}
} else if let modelItem: Model = currentCollection.atIndexPath(indexPath) {
// If no provider is set but the collection model provides an item at this index path,
// use that one.
return modelItem
}
// If either a provider is set but provides no item, or there is no provider set and the
// collection model does provides no item (such as when a section is empty),
// return a "zero" item.
return CollectionZeroItemModel(indexPath: indexPath)
}
private func viewModelForSupplementaryElementAtIndexPath(_ kind: String, indexPath: IndexPath) -> ViewModel {
let model = modelForSupplementaryIndexPath(indexPath, ofKind: kind)
if let bindingProvider = supplementaryViewModelBinderMap[kind] {
// Supplementary items don't necessarily have an item associated with them (think section headers for empty
// sections) - so handle the "zero" case here.
if let zeroModel = model as? CollectionZeroItemModel {
return CachedViewModel(
viewModel: CollectionZeroItemViewModel(indexPath: zeroModel.indexPath),
preferredLayout: .none).viewModel
}
return bindingProvider.viewModel(for: model, context: context)
} else {
return cachedViewModel(for: model).viewModel
}
}
private func cachedViewModel(for model: Model) -> CachedViewModel {
// Supplementary items don't necessarily have an item associated with them (think section headers for empty
// sections) - so handle the "zero" case here.
if let zeroModel = model as? CollectionZeroItemModel {
return CachedViewModel(
viewModel: CollectionZeroItemViewModel(indexPath: zeroModel.indexPath),
preferredLayout: .none)
}
// Return cached view model if there is one.
if let viewModel = viewModelCache[model.modelId] {
return viewModel
}
// Do binding.
let viewModel = modelBinder.viewModel(for: model, context: context)
let cachedViewModel = CachedViewModel(viewModel: viewModel, preferredLayout: .none)
viewModelCache[model.modelId] = cachedViewModel
return cachedViewModel
}
private func handleCollectionEvent(_ event: CollectionEvent) {
switch collectionViewState {
case .loading:
// The collection changed, but the CollectionView hasn't asked for any information yet, so there's nothing to do.
break
case .animating:
// CollectionView is currently animating, so indicate further updates are required when it's done.
collectionViewState = .animatingWithPendingChanges
case .animatingWithPendingChanges:
// More changes? No problem.
break
case .synced:
// Everyone is idle so kick off an update.
applyCurrentDataToCollectionView()
}
}
private func applyCurrentDataToCollectionView() {
precondition(collectionViewState == .synced)
let (updates, commitCollectionChanges) = currentCollection.beginUpdate(underlyingCollection)
guard updates.hasUpdates else {
// Still synced - no need to fire a collection view update pass.
// However, if there are no updates, the underlying case of any section may still change
// (e.g. .loading(_) -> .error(_)), so a commit is still needed.
for (underlying, current) in zip(
underlyingCollection.asSectioned().sectionedState,
currentCollection.sectionedState
) {
if underlying.isDifferentCase(than: current) {
commitCollectionChanges()
break
}
}
return
}
observers.notify(.willUpdateItems(updates))
commitCollectionChanges()
handleUpdateItems(updates) { [weak self] in
self?.observers.notify(.didUpdateItems(updates))
}
}
private var collectionViewSectionCount: Int {
return collectionView?.numberOfSections ?? 0
}
/// Clears any applicable cached view models (in `viewModelCache`) based on a set of `CollectionEventUpdates`.
/// This method should only be called once the `CollectionEventUpdates` have been committed to `currentCollection`,
/// typically in `handleUpdateItems(...)` as it assumes the updates are already reflected in the current collection.
/// Returns the invalidated view models.
private func invalidateViewModelCache(for updates: CollectionEventUpdates) -> [ModelId: CachedViewModel] {
// Removals.
for invalidatedModelId in updates.removedModelIds {
viewModelCache[invalidatedModelId] = nil
}
var invalidatedViewModelCache: [ModelId: CachedViewModel] = [:]
// At this point, `currentCollection` has been updated, and update/move.to index paths are based on post-removal
// indices. So can clear based on state of current collection.
for updatedPath in updates.updatedModelPaths {
if let model: Model = currentCollection.atModelPath(updatedPath) {
if let invalidViewModel = viewModelCache.removeValue(forKey: model.modelId) {
invalidatedViewModelCache[model.modelId] = invalidViewModel
}
}
}
for move in updates.movedModelPaths {
if let model: Model = currentCollection.atModelPath(move.to) {
if let invalidViewModel = viewModelCache.removeValue(forKey: model.modelId) {
invalidatedViewModelCache[model.modelId] = invalidViewModel
}
}
}
return invalidatedViewModelCache
}
}
#if os(iOS)
// MARK: - iOS Data and Batch Updates
extension CollectionViewModelDataSource: UICollectionViewDataSource {
// MARK: Private
private func updatesShouldFallbackOnFullReload(_ updates: CollectionEventUpdates) -> Bool {
// If `NoneReloadOnly` is specified, always reload.
if case .noneReloadOnly = updateAnimationStyle {
return true
}
// There is a long-standing `UICollectionView` bug where adding the first item or removing the last item within
// a section can cause an internal exception. This method detects those cases and returns `true` if the update
// should use a full data reload.
return updates.containsFirstAddInSection || updates.containsLastRemoveInSection
}
public func handleUpdateItems(_ updates: CollectionEventUpdates, completion: @escaping () -> Void) {
// preconditions
precondition(collectionViewState == .synced) // but not for long!
precondition(updates.hasUpdates)
guard let collectionView = collectionView else {
Log.fatal(message: "handleUpdateItems should never be called without a collectionView")
}
let invalidatedViewModelCache = invalidateViewModelCache(for: updates)
// Workaround classic collection view bugs where some updates require using a full reload. This includes
// reloading when the collection view is not part of the window hierarchy or the application is in the
// background.
let fullReloadFallback =
updatesShouldFallbackOnFullReload(updates) || collectionView.window == nil || inBackground
// Determine if this should be animated.
let shouldAnimate = shouldAnimateUpdates(updates) && !fullReloadFallback
// Set transaction start if animated.
if !shouldAnimate {
CATransaction.begin()
CATransaction.setDisableActions(true)
}
// Define the post-update completion block.
let completionHandler = { [weak self] (finished: Bool) in
if !shouldAnimate {
CATransaction.commit()
}
if let strongSelf = self {
switch strongSelf.collectionViewState {
case .loading:
Log.fatal(message: "Precondition failure - state cannot transition from animating to loading")
case .animating:
strongSelf.collectionViewState = .synced
case .animatingWithPendingChanges:
strongSelf.collectionViewState = .synced // applyCurrentDataToCollectionView will update
strongSelf.applyCurrentDataToCollectionView()
case .synced:
Log.fatal(message: "Precondition failure - state cannot transition from animating to synced")
}
}
completion()
}
// If a full reload is needed, do so and exit early.
guard !fullReloadFallback else {
// On iOS `reloadData` doesn't always dequeue new cells, so remove and add all sections here.
let oldSectionCount = collectionViewSectionCount
let newSectionCount = currentCollection.sections.count
collectionViewState = .animating
collectionView.performBatchUpdates({
collectionView.deleteSections(IndexSet(integersIn: 0..<oldSectionCount))
collectionView.insertSections(IndexSet(integersIn: 0..<newSectionCount))
}, completion: completionHandler)
return
}
// Do actual batch updates.
collectionViewState = .animating
collectionView.performBatchUpdates({
// Note: The ordering below is important and should not change. See note in
// `CollectionEventUpdates`
let removedSections = updates.removedSections
if !removedSections.isEmpty {
collectionView.deleteSections(IndexSet(removedSections))
}
let addedSections = updates.addedSections
if !addedSections.isEmpty {
collectionView.insertSections(IndexSet(addedSections))
}
let removed = updates.removedModelPaths
if !removed.isEmpty {
collectionView.deleteItems(at: removed.map { $0.indexPath })
}
let added = updates.addedModelPaths
if !added.isEmpty {
collectionView.insertItems(at: added.map { $0.indexPath })
}
for move in updates.movedModelPaths {
collectionView.moveItem(at: move.from.indexPath, to: move.to.indexPath)
}
}, completion: completionHandler)
// Note that reloads are done outside of the batch update call because they're basically unsupported
// alongside other complicated batch updates. Because reload actually does a delete and insert under
// the hood, the collectionview will throw an exception if that index path is touched in any other way.
// Splitting the call out here ensures this is avoided.
let updated = updates.updatedModelPaths
if !updated.isEmpty {
var indexPathsToReload: [IndexPath] = []
let size = collectionView.bounds
updated.forEach { indexPath in
var oldCachedViewModel: CachedViewModel?
var newCachedViewModel: CachedViewModel?
// Create new view models from the updated models.
if let model: Model = currentCollection.atModelPath(indexPath) {
oldCachedViewModel = invalidatedViewModelCache[model.modelId]
_ = cachedViewModel(for: model)
// Update the size.
// TODO:(wkiefer) Probably should cache last known available size - this makes some assumptions
// about available size.
let availableSize = AvailableSize(CGSize(width: size.width, height: CGSize.maxWindowSize.height))
_ = preferredLayoutForItemAtIndexPath(indexPath.indexPath, availableSize: availableSize)
newCachedViewModel = viewModelCache[model.modelId]
}
// If the size hasn't changed, simply rebind the view rather than perform a full cell reload.
if
let old = oldCachedViewModel,
let new = newCachedViewModel,
type(of: old.viewModel) == type(of: new.viewModel) &&
old.preferredLayout == new.preferredLayout
{
rebindViewAtIndexPath(indexPath.indexPath, toViewModel: new.viewModel)
} else {
indexPathsToReload.append(indexPath.indexPath)
}
}
if !indexPathsToReload.isEmpty {
collectionView.reloadItems(at: indexPathsToReload)
}
}
}
private func rebindViewAtIndexPath(_ indexPath: IndexPath, toViewModel viewModel: ViewModel) {
guard let collectionView = collectionView else { return }
guard let cell = collectionView.cellForItem(at: indexPath) as? CollectionViewHostCell else { return }
willRebindViewModel(viewModel)
cell.hostedView?.rebindToViewModel(viewModel)
}
// MARK: UICollectionViewDataSource
public func numberOfSections(in collectionView: UICollectionView) -> Int {
switch collectionViewState {
case .loading:
_ = currentCollection.update(underlyingCollection)
collectionViewState = .synced
default:
break
}
return currentCollection.sections.count
}
public func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
precondition(collectionViewState != .loading)
if currentCollection.sections.indices.contains(section) {
return currentCollection.sections[section].count
}
return 0
}
public func collectionView(
_ collectionView: UICollectionView,
cellForItemAt indexPath: IndexPath
) -> UICollectionViewCell {
precondition(collectionViewState != .loading)
// Fetch the model item and view model.
let modelItem = currentCollection.sections[indexPath.section][indexPath.item]
var cachedViewModel = self.cachedViewModel(for: modelItem)
let viewModel = cachedViewModel.viewModel
// Get the view class that will bind to the view model.
let viewType = viewBinder.viewTypeForViewModel(viewModel, context: context)
// Register the view/model pair to optimize reuse.
let reuseId = reuseIdProvider.reuseIdForViewModel(viewModel, viewType: viewType)
collectionView.register(CollectionViewHostCell.self, forCellWithReuseIdentifier: reuseId)
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseId, for: indexPath)
cell.backgroundColor = collectionViewBackgroundColor
if let hostCell = cell as? CollectionViewHostCell {
var reuseView: View?
// Determine if there is already a hosted view that matches the view class - if so, it can be reused as is.
if let hostUIView = hostCell.hostedView as? UIView , type(of: hostUIView) == viewType {
reuseView = hostCell.hostedView
}
// Bust the layout cache if it's stale.
if let layout = cachedViewModel.preferredLayout.layout , !layout.validCache(forViewModel: viewModel) {
// TODO:(danielh) This has the same issue as the update where it assumes available size.
let maxSize = CGSize(width: collectionView.bounds.width, height: CGSize.maxWindowSize.height)
let availableSize = AvailableSize(maxSize)
cachedViewModel.preferredLayout = viewBinder.preferredLayout(
fitting: availableSize,
for: viewModel,
context: context)
viewModelCache[modelItem.modelId] = cachedViewModel
}
let view = viewBinder.view(
for: viewModel,
context: context,
reusing: reuseView,
layout: cachedViewModel.preferredLayout.layout)
hostCell.hostedView = view
}
return cell
}
public func collectionView(
_ collectionView: UICollectionView,
viewForSupplementaryElementOfKind kind: String,
at indexPath: IndexPath
) -> UICollectionReusableView {
guard let supplementaryViewBinder = supplementaryViewBinderMap[kind] else {
Log.fatal(message: "Request for supplementary kind (\(kind)) that has no registered view binder.")
}
let viewModel = viewModelForSupplementaryElementAtIndexPath(kind, indexPath: indexPath)
let viewType = supplementaryViewBinder.viewTypeForViewModel(viewModel, context: context)
let reuseId = reuseIdProvider.reuseIdForViewModel(viewModel, viewType: viewType)
collectionView.register(
CollectionViewHostReusableView.self,
forSupplementaryViewOfKind: kind,
withReuseIdentifier: reuseId)
let supplementaryView = collectionView.dequeueReusableSupplementaryView(
ofKind: kind,
withReuseIdentifier: reuseId,
for: indexPath)
if let supplementaryView = supplementaryView as? CollectionViewHostReusableView {
var reuseView: View?
if let hostUIView = supplementaryView.hostedView as? UIView , type(of: hostUIView) == viewType {
reuseView = supplementaryView.hostedView
}
let view = supplementaryViewBinder.view(
for: viewModel, context: context, reusing: reuseView, layout: nil)
supplementaryView.hostedView = view
}
return supplementaryView
}
}
// MARK: - macOS Data and Batch Updates
#elseif os(OSX)
private struct OSInfo {
static let isAtLeastSierra: Bool = {
let sierra = OperatingSystemVersion(majorVersion: 10, minorVersion: 12, patchVersion: 0)
return ProcessInfo.processInfo.isOperatingSystemAtLeast(sierra)
}()
}
private class EmptyCollectionViewItem: NSCollectionViewItem {
public init() {
super.init(nibName: nil, bundle: nil)
}
required init(coder: NSCoder) {
Log.fatal(message: "Unsupported initializer")
}
override func loadView() {
view = NSView(frame: .zero)
}
}
extension CollectionViewModelDataSource: NSCollectionViewDataSource {
// MARK: Private
public func handleUpdateItems(_ updates: CollectionEventUpdates, completion: @escaping () -> Void) {
// preconditions
precondition(collectionViewState == .synced) // but not for long!
precondition(updates.hasUpdates)
guard let collectionView = collectionView else {
Log.fatal(message: "handleUpdateItems should never run without a collectionView")
}
// The standard collection view hierarchy is `NSScrollView`->`NSClipView`->`NSCollectionView`, so finding
// the scroll view via super view chaining is expected.
guard let scrollView = collectionView.superview?.superview as? NSScrollView else {
Log.fatal(message: "CollectionViewModelDataSource requires an outer scrollview.")
}
log(event: "HandleUpdateItems", updates: updates)
let didProcessUpdatesWithLog: (CollectionEventUpdates) -> Void = { [weak self] updates in
self?.log(event: "DidProcessUpdates", updates: updates)
completion()
}
let invalidatedViewModelCache = invalidateViewModelCache(for: updates)
if shouldPerformFullReload(withCollectionView: collectionView, scrollView: scrollView, updates: updates) {
guard !OSInfo.isAtLeastSierra else {
let oldSectionCount = collectionView.numberOfSections
let newSectionCount = numberOfSections(in: collectionView)
precondition(Thread.isMainThread)
collectionViewState = .animating
collectionView.performBatchUpdates({
precondition(Thread.isMainThread)
let existingSections = IndexSet(integersIn: 0..<oldSectionCount)
collectionView.deleteSections(existingSections)
collectionView.insertSections(IndexSet(integersIn: 0..<newSectionCount))
}, completionHandler: { [weak self] _ in
precondition(Thread.isMainThread)
didProcessUpdatesWithLog(updates)
self?.advanceCollectionViewStateAfterPerformUpdates()
})
return
}
// CollectionView.reloadData does not synchronously fetch new information from the data source, so
// don't call performBatchUpdates until it does.
collectionViewState = .loading
collectionView.reloadData()
//precondition(collectionViewState != .loading)
// NOTE: This notifies observers of .didUpdateItems before the CollectionView has been updated.
// Presumably this means code that tries to preserve selection across changes will not work if a reload
// is triggered. One option would be to wait until the CollectionView asks for the section count and
// fire the .didUpdateItems event then, but if the CollectionView is hidden that may not happen.
// Not sure what the best option is, besides complaining about how hard it is to use CollectionView.
// Another option: trigger a different event type indicating a reload has started.
didProcessUpdatesWithLog(updates)
return
}
// Determine if this should be animated.
let shouldAnimate = shouldAnimateUpdates(updates)
if shouldAnimate {
// BUGFIX: If the collection view is deallocated during the timespan of the animation, NSCollectionView
// internals will end up calling the non-zeroing weak delegate back and crashing. So retain both the
// delegate and the collection view until the animation completes.
// This is easy to repro by removing the completion handler and increasing the animation duration.
let retainedDelegate = collectionView.delegate
NSAnimationContext.beginGrouping()
NSAnimationContext.current.completionHandler = { [collectionView, retainedDelegate] in
_ = collectionView
_ = retainedDelegate
}
NSAnimationContext.current.duration = 0.2
}
// `performBatchUpdates` doesn't animate away deletes, so as a workaround the deleted items are set to
// alpha of 0 and then restored at the end of the batch.
// TODO(wkiefer): There's likely a better solution here, but not fading them out looks bad.
var forceHiddenItems: [NSCollectionViewItem] = []
collectionViewState = .animating
let cv = shouldAnimate ? collectionView.animator() : collectionView
cv.performBatchUpdates({
// Note: The ordering below is important and should not change. See note in
// `CollectionEventUpdates`
let removedSections = updates.removedSections
if !removedSections.isEmpty {
collectionView.deleteSections(IndexSet(removedSections))
}
let addedSections = updates.addedSections
if !addedSections.isEmpty {
collectionView.insertSections(IndexSet(addedSections))
}
let removed = updates.removedModelPaths
if !removed.isEmpty {
let removedIndexPathSet = Set(removed.map { $0.indexPath })
let visibleIndexPaths = collectionView.indexPathsForVisibleItems()
// Workaround for `NSCollectionView` not removing the items until the end.
for indexPath in removedIndexPathSet.intersection(visibleIndexPaths) {
if let item = collectionView.item(at: indexPath) {
item.view.alphaValue = 0
forceHiddenItems.append(item)
}
}
collectionView.deleteItems(at: removedIndexPathSet)
}
let added = updates.addedModelPaths
if !added.isEmpty {
collectionView.insertItems(at: Set(added.map { $0.indexPath } ))
}
let movedModels = updates.movedModelPaths
if !movedModels.isEmpty {
let usingFakeMoves = shouldFakeMoves(updates: updates)
for move in movedModels {
if usingFakeMoves {
collectionView.deleteItems(at: [move.from.indexPath])
collectionView.insertItems(at: [move.to.indexPath])
} else {
collectionView.moveItem(at: move.from.indexPath, to: move.to.indexPath)
}
}
}
}) { [weak self] (finished: Bool) in
// Restore alpha on item views.
forceHiddenItems.forEach { $0.view.animator().alphaValue = 1.0 }
didProcessUpdatesWithLog(updates)
self?.advanceCollectionViewStateAfterPerformUpdates()
}
// Note that reloads are done outside of the batch update call because they're basically unsupported
// alongside other complicated batch updates. Because reload actually does a delete and insert under
// the hood, the collectionview will throw an exception if that index path is touched in any other way.
// Splitting the call out here ensures this is avoided.
let updated = updates.updatedModelPaths
if !updated.isEmpty {
var reloadSet = Set<IndexPath>()
let size = collectionView.bounds
for indexPath in updated {
var oldCachedViewModel: CachedViewModel?
var newCachedViewModel: CachedViewModel?
// Create new view models from the updated models.
if let model: Model = currentCollection.atModelPath(indexPath) {
oldCachedViewModel = invalidatedViewModelCache[model.modelId]
_ = cachedViewModel(for: model)
// Update the size.
// TODO:(wkiefer) Probably should cache last known available size - this makes some assumptions
// about available size.
let availableSize = AvailableSize(CGSize(width: size.width, height: CGSize.maxWindowSize.height))
_ = preferredLayoutForItemAtIndexPath(indexPath.indexPath, availableSize: availableSize)
newCachedViewModel = viewModelCache[model.modelId]
}
// If the size hasn't changed, simply rebind the view rather than perform a full cell reload.
if
let old = oldCachedViewModel,
let new = newCachedViewModel,