-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJEKScrollableSectionCollectionView.m
More file actions
851 lines (711 loc) · 36.9 KB
/
JEKScrollableSectionCollectionView.m
File metadata and controls
851 lines (711 loc) · 36.9 KB
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
//
// JEKScrollableSectionCollectionView.m
// JEKScrollableSectionCollectionView
//
// Created by Joel Ekström on 2017-08-28.
// Copyright © 2017 Joel Ekström. All rights reserved.
//
#import "JEKScrollableSectionCollectionView.h"
@class JEKScrollableCollectionViewController;
@class JEKScrollableCollectionViewBatchUpdates;
@class JEKCollectionViewWrapperCell;
@interface JEKScrollableSectionCollectionView()
@property (nonatomic, strong) JEKScrollableCollectionViewController *controller;
@property (nonatomic, strong) NSMutableDictionary<NSString *, Class> *registeredCellClasses;
@property (nonatomic, strong) NSMutableDictionary<NSString *, UINib *> *registeredCellNibs;
@property (nonatomic, assign) NSUInteger registrationHash;
@property (nonatomic, strong) NSIndexPath *queuedIndexPath;
@property (nonatomic, assign) BOOL shouldAnimateScrollToQueuedIndexPath;
@property (nonatomic, strong) JEKScrollableCollectionViewBatchUpdates *batchUpdates;
@end
@interface JEKScrollableCollectionViewController : NSObject <UICollectionViewDataSource, UICollectionViewDelegateFlowLayout, UICollectionViewDataSourcePrefetching>
@property (nonatomic, weak) JEKScrollableSectionCollectionView *collectionView;
@property (nonatomic, weak) id<UICollectionViewDataSource> externalDataSource;
@property (nonatomic, weak) id<UICollectionViewDelegateFlowLayout> externalDelegate;
@property (nonatomic, weak) id<UICollectionViewDataSourcePrefetching> externalPrefetchingDataSource;
@property (nonatomic, strong) NSMutableSet<NSIndexPath *> *selectedIndexPaths;
@property (nonatomic, strong) NSMutableDictionary<NSNumber *, NSValue *> *contentOffsetCache;
@property (nonatomic, strong) NSMutableArray<JEKCollectionViewWrapperCell *> *visibleCells;
- (instancetype)initWithCollectionView:(JEKScrollableSectionCollectionView *)collectionView;
- (JEKCollectionViewWrapperCell *)visibleCellForSection:(NSInteger)section;
@end
static NSString * const JEKCollectionViewWrapperCellIdentifier = @"JEKCollectionViewWrapperCellIdentifier";
@interface JEKWrappedCollectionView : UICollectionView
@property (nonatomic, weak) JEKCollectionViewWrapperCell *parentCell;
@property (nonatomic, weak) JEKScrollableSectionCollectionView *parentCollectionView;
@property (nonatomic, assign) NSInteger section;
@end
@interface JEKCollectionViewWrapperCell : UICollectionViewCell
@property (nonatomic, strong) JEKWrappedCollectionView *collectionView;
@property (nonatomic, assign) NSUInteger registrationHash;
- (void)registerCellClasses:(NSDictionary<NSString *, Class> *)classes nibs:(NSDictionary<NSString *, UINib *> *)nibs;
@end
@interface JEKScrollableCollectionViewBatchUpdates : NSObject
@property (nonatomic, strong) NSMutableIndexSet *deletedSections;
@property (nonatomic, strong) NSMutableIndexSet *insertedSections;
@property (nonatomic, strong) NSMutableIndexSet *reloadedSections;
@property (nonatomic, strong) NSMutableDictionary<NSNumber *, NSNumber *> *movedSections;
@property (nonatomic, strong) NSMutableArray<NSIndexPath *> *deletedItems;
@property (nonatomic, strong) NSMutableArray<NSIndexPath *> *insertedItems;
@property (nonatomic, strong) NSMutableArray<NSIndexPath *> *reloadedItems;
@property (nonatomic, strong) NSMutableDictionary<NSIndexPath *, NSIndexPath *> *movedItems;
- (void)curate;
@property (nonatomic, readonly) BOOL hasSectionUpdates;
@property (nonatomic, readonly) BOOL hasItemUpdates;
@end
@interface NSIndexPath (JEKScrollableSectionCollectionView)
// Returns a new indexPath with the same item but section 0
- (NSIndexPath *)normalize;
@end
#pragma mark -
@implementation JEKScrollableSectionCollectionView
- (instancetype)initWithFrame:(CGRect)frame collectionViewLayout:(UICollectionViewFlowLayout *)layout
{
if (self = [super initWithFrame:frame collectionViewLayout:layout]) {
[self configure];
}
return self;
}
- (instancetype)initWithCoder:(NSCoder *)aDecoder
{
if (self = [super initWithCoder:aDecoder]) {
[self configure];
[self registerStoryboardPrototypeCells];
}
return self;
}
/**
Accesses internal nib storage to register cells created in Storyboards.
*/
- (void)registerStoryboardPrototypeCells
{
id cellNibDict = [self valueForKey:@"_cellNibDict"];
if ([cellNibDict isKindOfClass:[NSDictionary class]]) {
[cellNibDict enumerateKeysAndObjectsUsingBlock:^(NSString *key, UINib *nib, BOOL *stop) {
if ([key isKindOfClass:[NSString class]] && [nib isKindOfClass:[UINib class]]) {
[self registerNib:nib forCellWithReuseIdentifier:key];
}
}];
}
}
- (void)configure
{
NSAssert([self.collectionViewLayout isKindOfClass:UICollectionViewFlowLayout.class], @"%@ must be initialized with a UICollectionViewFlowLayout", NSStringFromClass(self.class));
[(UICollectionViewFlowLayout *)self.collectionViewLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
self.controller = [[JEKScrollableCollectionViewController alloc] initWithCollectionView:self];
self.registeredCellClasses = [NSMutableDictionary new];
self.registeredCellNibs = [NSMutableDictionary new];
[super registerClass:JEKCollectionViewWrapperCell.class forCellWithReuseIdentifier:JEKCollectionViewWrapperCellIdentifier];
}
- (void)setDelegate:(id<UICollectionViewDelegate>)delegate
{
[super setDelegate:delegate ? self.controller : nil];
self.controller.externalDelegate = (id<UICollectionViewDelegateFlowLayout>)delegate;
}
- (void)setDataSource:(id<UICollectionViewDataSource>)dataSource
{
[super setDataSource:dataSource ? self.controller : nil];
self.controller.externalDataSource = dataSource;
}
- (void)setPrefetchDataSource:(id<UICollectionViewDataSourcePrefetching>)prefetchDataSource
{
self.controller.externalPrefetchingDataSource = prefetchDataSource;
}
- (void)registerClass:(Class)cellClass forCellWithReuseIdentifier:(NSString *)identifier
{
// When initializing from storyboards, Apple internally registers a class in initWithCoder:,
// which is before our own init functions has run.
// Trivia: the registered identifier is: @"com.apple.UIKit.shadowReuseCellIdentifier",
// and is the shadow used during drag and drop.
if (_registeredCellClasses == nil) {
[super registerClass:cellClass forCellWithReuseIdentifier:identifier];
return;
}
_registeredCellClasses[identifier] = cellClass;
[self updateRegistrationHash];
}
- (void)registerNib:(UINib *)nib forCellWithReuseIdentifier:(NSString *)identifier
{
_registeredCellNibs[identifier] = nib;
[self updateRegistrationHash];
}
- (void)updateRegistrationHash
{
self.registrationHash = [self.registeredCellClasses.description hash] ^ [self.registeredCellNibs.description hash];
}
- (__kindof UICollectionViewCell *)dequeueReusableCellWithReuseIdentifier:(NSString *)identifier forIndexPath:(NSIndexPath *)indexPath
{
if ([identifier isEqualToString:JEKCollectionViewWrapperCellIdentifier]) {
return [super dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
}
JEKCollectionViewWrapperCell *cell = [self.controller visibleCellForSection:indexPath.section];
return [cell.collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath.normalize];
}
- (UICollectionViewCell *)cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
JEKCollectionViewWrapperCell *wrapperCell = [self.controller visibleCellForSection:indexPath.section];
return [wrapperCell.collectionView cellForItemAtIndexPath:indexPath.normalize];
}
- (NSArray<NSIndexPath *> *)indexPathsForSelectedItems
{
return self.controller.selectedIndexPaths.allObjects;
}
- (NSArray<__kindof UICollectionViewCell *> *)visibleCells
{
NSArray *visibleCells = @[];
for (JEKCollectionViewWrapperCell *cell in self.controller.visibleCells) {
visibleCells = [visibleCells arrayByAddingObjectsFromArray:cell.collectionView.visibleCells];
}
return visibleCells;
}
- (NSArray<NSIndexPath *> *)indexPathsForVisibleItems
{
NSArray *visibleIndexPaths = @[];
for (UICollectionViewCell *cell in self.controller.visibleCells) {
visibleIndexPaths = [visibleIndexPaths arrayByAddingObject:[self indexPathForCell:cell]];
}
return visibleIndexPaths;
}
- (NSIndexPath *)indexPathForCell:(UICollectionViewCell *)cell
{
if ([cell.superview.superview isKindOfClass:JEKCollectionViewWrapperCell.class]) {
JEKCollectionViewWrapperCell *wrapperCell = (JEKCollectionViewWrapperCell *)cell.superview.superview;
NSIndexPath *indexPath = [wrapperCell.collectionView indexPathForCell:cell];
return [NSIndexPath indexPathForItem:indexPath.item inSection:wrapperCell.collectionView.section];
}
return nil;
}
- (void)scrollToItemAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(UICollectionViewScrollPosition)scrollPosition animated:(BOOL)animated
{
NSIndexPath *outerIndexPath = [NSIndexPath indexPathForItem:0 inSection:indexPath.section];
NSIndexPath *innerIndexPath = indexPath.normalize;
[super scrollToItemAtIndexPath:outerIndexPath atScrollPosition:UICollectionViewScrollPositionCenteredVertically animated:animated];
JEKCollectionViewWrapperCell *cell = [self.controller visibleCellForSection:indexPath.section];
if (cell) {
[cell.collectionView scrollToItemAtIndexPath:innerIndexPath atScrollPosition:scrollPosition animated:animated];
} else {
self.queuedIndexPath = indexPath;
self.shouldAnimateScrollToQueuedIndexPath = animated;
}
}
- (void)selectItemAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated scrollPosition:(UICollectionViewScrollPosition)scrollPosition
{
[self.controller.selectedIndexPaths addObject:indexPath];
JEKCollectionViewWrapperCell *wrapperCell = [self.controller visibleCellForSection:indexPath.section];
[wrapperCell.collectionView selectItemAtIndexPath:indexPath.normalize animated:animated scrollPosition:scrollPosition];
if (scrollPosition != UICollectionViewScrollPositionNone) {
[self scrollToItemAtIndexPath:indexPath atScrollPosition:scrollPosition animated:animated];
}
}
- (void)deselectItemAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated
{
[self.controller.selectedIndexPaths removeObject:indexPath];
JEKCollectionViewWrapperCell *wrapperCell = [self.controller visibleCellForSection:indexPath.section];
[wrapperCell.collectionView deselectItemAtIndexPath:indexPath.normalize animated:animated];
}
#pragma mark Updating content
- (void)insertSections:(NSIndexSet *)sections
{
if (self.batchUpdates) {
[self.batchUpdates.insertedSections addIndexes:sections];
} else {
[super insertSections:sections];
}
}
- (void)deleteSections:(NSIndexSet *)sections
{
if (self.batchUpdates) {
[self.batchUpdates.deletedSections addIndexes:sections];
} else {
[super deleteSections:sections];
}
}
- (void)reloadSections:(NSIndexSet *)sections
{
if (self.batchUpdates) {
[self.batchUpdates.reloadedSections addIndexes:sections];
} else {
[super reloadSections:sections];
}
}
- (void)moveSection:(NSInteger)section toSection:(NSInteger)newSection
{
if (self.batchUpdates) {
self.batchUpdates.movedSections[@(section)] = @(newSection);
} else {
[super moveSection:section toSection:newSection];
}
}
- (void)insertItemsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths
{
if (self.batchUpdates) {
[self.batchUpdates.insertedItems addObjectsFromArray:indexPaths];
} else {
[self performSelector:_cmd forItemsAtIndexPaths:indexPaths];
}
}
- (void)deleteItemsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths
{
if (self.batchUpdates) {
[self.batchUpdates.deletedItems addObjectsFromArray:indexPaths];
} else {
[self performSelector:_cmd forItemsAtIndexPaths:indexPaths];
}
}
- (void)reloadItemsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths
{
if (self.batchUpdates) {
[self.batchUpdates.reloadedItems addObjectsFromArray:indexPaths];
} else {
[self performSelector:_cmd forItemsAtIndexPaths:indexPaths];
}
}
- (void)moveItemAtIndexPath:(NSIndexPath *)indexPath toIndexPath:(NSIndexPath *)newIndexPath
{
if (self.batchUpdates) {
self.batchUpdates.movedItems[indexPath] = newIndexPath;
} else {
JEKCollectionViewWrapperCell *cell = [self.controller visibleCellForSection:indexPath.section];
if (indexPath.section == newIndexPath.section) {
// When moving within a single section, we can just forward the move to the child collection view
[cell.collectionView moveItemAtIndexPath:indexPath.normalize toIndexPath:newIndexPath.normalize];
} else {
// Otherwise, we use delete/insert instead in the respective cells
JEKCollectionViewWrapperCell *cell2 = [self.controller visibleCellForSection:indexPath.section];
[cell.collectionView deleteItemsAtIndexPaths:@[indexPath.normalize]];
[cell2.collectionView insertItemsAtIndexPaths:@[newIndexPath.normalize]];
}
}
}
/**
When performing batch updates, we need all child collection views to be in batch update mode.
This function recursively begins performing batch updates on an array of cells, and runs the
update block in the last one
*/
- (void)performBatchUpdatesInCells:(NSArray<JEKCollectionViewWrapperCell *> *)cells updates:(void (^)(void))updates
{
if (cells.count > 0) {
[cells.firstObject.collectionView performBatchUpdates:^{
[self performBatchUpdatesInCells:[cells subarrayWithRange:NSMakeRange(1, cells.count - 1)] updates:updates];
} completion:nil];
} else {
updates();
}
}
- (void)performBatchUpdates:(void (^)(void))updates completion:(void (^)(BOOL))completion
{
JEKScrollableCollectionViewBatchUpdates *batchUpdates = [JEKScrollableCollectionViewBatchUpdates new];
self.batchUpdates = batchUpdates;
updates();
self.batchUpdates = nil;
[batchUpdates curate];
// Doing both section changes and item changes is very complicated, since that needs that both the outer and inner collection views
// somehow need to synchronize their batch updates. To keep things simple, we only do one or the other, or if both,
// just reload data and a cross dissolve transition
if (batchUpdates.hasSectionUpdates && batchUpdates.hasItemUpdates) {
[self reloadData];
[UIView transitionWithView:self duration:0.3 options:UIViewAnimationOptionTransitionCrossDissolve animations:nil completion:completion];
} else if (batchUpdates.hasSectionUpdates) {
[super performBatchUpdates:^{
[self deleteSections:batchUpdates.deletedSections];
[self insertSections:batchUpdates.insertedSections];
[self reloadSections:batchUpdates.reloadedSections];
[batchUpdates.movedSections enumerateKeysAndObjectsUsingBlock:^(NSNumber *fromSection, NSNumber *toSection, BOOL *stop) {
[self moveSection:fromSection.integerValue toSection:toSection.integerValue];
}];
} completion:completion];
} else if (batchUpdates.hasItemUpdates) {
[self performBatchUpdatesInCells:self.controller.visibleCells updates:^{
[self deleteItemsAtIndexPaths:batchUpdates.deletedItems];
[self insertItemsAtIndexPaths:batchUpdates.insertedItems];
[self reloadItemsAtIndexPaths:batchUpdates.reloadedItems];
[batchUpdates.movedItems enumerateKeysAndObjectsUsingBlock:^(NSIndexPath *fromIndexPath, NSIndexPath *toIndexPath, BOOL * _Nonnull stop) {
[self moveItemAtIndexPath:fromIndexPath toIndexPath:toIndexPath];
}];
if (completion) {
completion(YES);
}
}];
}
}
/**
When inserting/deleting/reloading items, we want to do it in the relevant child collection views.
This method finds the relevat collection views and transforms the index paths for them.
*/
- (void)performSelector:(SEL)selector forItemsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths
{
[[self indexPathsGroupedBySection:indexPaths] enumerateKeysAndObjectsUsingBlock:^(NSNumber *section, NSArray<NSIndexPath *> *indexPaths, BOOL *stop) {
JEKCollectionViewWrapperCell *cell = [self.controller visibleCellForSection:section.integerValue];
if ([cell.collectionView respondsToSelector:selector]) {
IMP imp = [cell.collectionView methodForSelector:selector];
void (*functionPtr)(id, SEL, NSArray<NSIndexPath *> *) = (void *)imp;
functionPtr(cell.collectionView, selector, indexPaths);
}
}];
}
/**
Transforms an array of indexPaths into a dictionary grouped by section where each key is the
section index. The actual section is removed from the indexPath objects and replaced by 0.
*/
- (NSDictionary<NSNumber *, NSArray<NSIndexPath *> *> *)indexPathsGroupedBySection:(NSArray<NSIndexPath *> *)indexPaths
{
NSMutableDictionary *groupedIndexPaths = [NSMutableDictionary new];
indexPaths = [indexPaths sortedArrayUsingDescriptors:@[[NSSortDescriptor sortDescriptorWithKey:@"section" ascending:YES]]];
NSMutableArray<NSIndexPath *> *currentSectionArray = nil;
NSInteger currentSectionIndex = -1;
for (NSIndexPath *indexPath in indexPaths) {
if (indexPath.section != currentSectionIndex) {
currentSectionIndex = indexPath.section;
currentSectionArray = [NSMutableArray new];
groupedIndexPaths[@(currentSectionIndex)] = currentSectionArray;
}
[currentSectionArray addObject:indexPath.normalize];
}
return [groupedIndexPaths copy];
}
@end
#pragma mark -
@implementation JEKScrollableCollectionViewController
- (instancetype)initWithCollectionView:(JEKScrollableSectionCollectionView *)collectionView
{
if (self = [super init]) {
self.collectionView = collectionView;
self.selectedIndexPaths = [NSMutableSet new];
self.contentOffsetCache = [NSMutableDictionary new];
self.visibleCells = [NSMutableArray new];
}
return self;
}
- (JEKCollectionViewWrapperCell *)visibleCellForSection:(NSInteger)section
{
for (JEKCollectionViewWrapperCell *cell in self.visibleCells) {
if (cell.collectionView.section == section) {
return cell;
}
}
return nil;
}
#pragma mark - UICollectionViewDataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
if (collectionView == self.collectionView) {
return [self.externalDataSource numberOfSectionsInCollectionView:collectionView];
}
return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
if (collectionView == self.collectionView) {
return 1;
}
return [self.externalDataSource collectionView:self.collectionView numberOfItemsInSection:[(JEKWrappedCollectionView *)collectionView section]];
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(nonnull NSIndexPath *)indexPath
{
if (collectionView == self.collectionView) {
JEKCollectionViewWrapperCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:JEKCollectionViewWrapperCellIdentifier forIndexPath:indexPath];
cell.collectionView.section = indexPath.section;
cell.collectionView.parentCollectionView = self.collectionView;
if (cell.registrationHash != self.collectionView.registrationHash) {
[cell registerCellClasses:self.collectionView.registeredCellClasses nibs:self.collectionView.registeredCellNibs];
cell.registrationHash = self.collectionView.registrationHash;
}
return cell;
}
return [self.externalDataSource collectionView:self.collectionView cellForItemAtIndexPath:[NSIndexPath indexPathForItem:indexPath.item inSection:[(JEKWrappedCollectionView *)collectionView section]]];
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
if (collectionView == self.collectionView && [self.externalDataSource respondsToSelector:_cmd]) {
return [self.externalDataSource collectionView:collectionView viewForSupplementaryElementOfKind:kind atIndexPath:indexPath];
}
return nil;
}
#pragma mark UICollectionViewDataSourcePrefetching
- (void)collectionView:(UICollectionView *)collectionView prefetchItemsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths
{
if ([self.externalPrefetchingDataSource respondsToSelector:_cmd]) {
NSMutableArray *transformedIndexPaths = [NSMutableArray new];
for (NSIndexPath *indexPath in indexPaths) {
[transformedIndexPaths addObject:[NSIndexPath indexPathForItem:indexPath.section inSection:[(JEKWrappedCollectionView *)collectionView section]]];
}
[self.externalPrefetchingDataSource collectionView:self.collectionView prefetchItemsAtIndexPaths:[transformedIndexPaths copy]];
}
}
- (void)collectionView:(UICollectionView *)collectionView cancelPrefetchingForItemsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths
{
if ([self.externalPrefetchingDataSource respondsToSelector:_cmd]) {
NSMutableArray *transformedIndexPaths = [NSMutableArray new];
for (NSIndexPath *indexPath in indexPaths) {
[transformedIndexPaths addObject:[NSIndexPath indexPathForItem:indexPath.section inSection:[(JEKWrappedCollectionView *)collectionView section]]];
}
[self.externalPrefetchingDataSource collectionView:self.collectionView cancelPrefetchingForItemsAtIndexPaths:[transformedIndexPaths copy]];
}
}
#pragma mark UICollectionViewDelegate
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
if (collectionView == self.collectionView) {
UICollectionViewFlowLayout *flowLayout = (UICollectionViewFlowLayout *)collectionViewLayout;
CGSize size = CGSizeMake(CGRectGetWidth(collectionView.frame), flowLayout.itemSize.height);
if ([self.externalDelegate respondsToSelector:@selector(collectionView:heightForSectionAtIndex:)]) {
size.height = [(id)self.externalDelegate collectionView:self.collectionView heightForSectionAtIndex:indexPath.section];
}
return size;
}
if ([self.externalDelegate respondsToSelector:_cmd]) {
return [self.externalDelegate collectionView:self.collectionView
layout:self.collectionView.collectionViewLayout
sizeForItemAtIndexPath:[NSIndexPath indexPathForItem:indexPath.item inSection:[(JEKWrappedCollectionView *)collectionView section]]];
}
return [(UICollectionViewFlowLayout *)self.collectionView.collectionViewLayout itemSize];
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewFlowLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
if (collectionView == self.collectionView) {
if ([self.externalDelegate respondsToSelector:_cmd]) {
return [self.externalDelegate collectionView:self.collectionView layout:collectionViewLayout referenceSizeForHeaderInSection:section];
}
return collectionViewLayout.headerReferenceSize;
}
return CGSizeZero;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewFlowLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section
{
if (collectionView == self.collectionView) {
if ([self.externalDelegate respondsToSelector:_cmd]) {
return [self.externalDelegate collectionView:self.collectionView layout:collectionViewLayout referenceSizeForFooterInSection:section];
}
return collectionViewLayout.footerReferenceSize;
}
return CGSizeZero;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewFlowLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
if (collectionView == self.collectionView) {
return 0.0;
}
// Call the inter-item spacing function of the delegate instead, since that's what you expect from a vertical collection view.
// The inner collection views are actually horizontal, but that's unknown outside this class
if ([self.externalDelegate respondsToSelector:@selector(collectionView:layout:minimumInteritemSpacingForSectionAtIndex:)]) {
return [self.externalDelegate collectionView:self.collectionView layout:self.collectionView.collectionViewLayout minimumInteritemSpacingForSectionAtIndex:[(JEKWrappedCollectionView *)collectionView section]];
}
return [(UICollectionViewFlowLayout *)self.collectionView.collectionViewLayout minimumInteritemSpacing];
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewFlowLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
if (collectionView == self.collectionView) {
return 0.0;
}
// Use minimumLineSpacing instead. See comment in minimumLineSpacing for more info
if ([self.externalDelegate respondsToSelector:@selector(collectionView:layout:minimumLineSpacingForSectionAtIndex:)]) {
return [self.externalDelegate collectionView:self.collectionView layout:self.collectionView.collectionViewLayout minimumLineSpacingForSectionAtIndex:[(JEKWrappedCollectionView *)collectionView section]];
}
return [(UICollectionViewFlowLayout *)self.collectionView.collectionViewLayout minimumLineSpacing];
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewFlowLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
if (collectionView == self.collectionView) {
return UIEdgeInsetsZero;
}
if ([self.externalDelegate respondsToSelector:_cmd]) {
return [self.externalDelegate collectionView:collectionView layout:self.collectionView.collectionViewLayout insetForSectionAtIndex:[(JEKWrappedCollectionView *)collectionView section]];
}
return [(UICollectionViewFlowLayout *)self.collectionView.collectionViewLayout sectionInset];
}
- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath
{
if (collectionView == self.collectionView) {
JEKCollectionViewWrapperCell *wrapperCell = (JEKCollectionViewWrapperCell *)cell;
wrapperCell.collectionView.dataSource = self;
wrapperCell.collectionView.delegate = self;
wrapperCell.collectionView.prefetchDataSource = _externalPrefetchingDataSource ? self : nil;
wrapperCell.collectionView.allowsMultipleSelection = collectionView.allowsMultipleSelection;
wrapperCell.collectionView.allowsSelection = collectionView.allowsSelection;
wrapperCell.collectionView.showsHorizontalScrollIndicator = self.collectionView.showsHorizontalScrollIndicator;
[self.visibleCells addObject:wrapperCell];
[wrapperCell.collectionView reloadData];
NSValue *contentOffset = self.contentOffsetCache[@(indexPath.section)];
[wrapperCell.collectionView setContentOffset:contentOffset ? contentOffset.CGPointValue : CGPointZero animated:NO];
for (NSIndexPath *indexPath in self.selectedIndexPaths) {
if (indexPath.section == wrapperCell.collectionView.section) {
[wrapperCell.collectionView selectItemAtIndexPath:indexPath.normalize animated:NO scrollPosition:UICollectionViewScrollPositionNone];
}
}
if (self.collectionView.queuedIndexPath && self.collectionView.queuedIndexPath.section == wrapperCell.collectionView.section) {
[wrapperCell.collectionView scrollToItemAtIndexPath:self.collectionView.queuedIndexPath.normalize
atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally
animated:self.collectionView.shouldAnimateScrollToQueuedIndexPath];
self.collectionView.queuedIndexPath = nil;
}
} else if ([self.externalDelegate respondsToSelector:_cmd]) {
[self.externalDelegate collectionView:self.collectionView willDisplayCell:cell forItemAtIndexPath:[NSIndexPath indexPathForItem:indexPath.item inSection:[(JEKWrappedCollectionView *)collectionView section]]];
}
}
- (void)collectionView:(UICollectionView *)collectionView didEndDisplayingCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath
{
if (collectionView == self.collectionView) {
JEKCollectionViewWrapperCell *wrapperCell = (JEKCollectionViewWrapperCell *)cell;
NSNumber *section = @(indexPath.section);
self.contentOffsetCache[section] = [NSValue valueWithCGPoint:wrapperCell.collectionView.contentOffset];
wrapperCell.collectionView.dataSource = nil;
wrapperCell.collectionView.prefetchDataSource = nil;
for (NSIndexPath *indexPath in wrapperCell.collectionView.indexPathsForSelectedItems) {
[wrapperCell.collectionView deselectItemAtIndexPath:indexPath.normalize animated:NO];
}
[self.visibleCells removeObject:wrapperCell];
} else if ([self.externalDelegate respondsToSelector:_cmd]) {
[self.externalDelegate collectionView:self.collectionView didEndDisplayingCell:cell forItemAtIndexPath:[NSIndexPath indexPathForItem:indexPath.item inSection:[(JEKWrappedCollectionView *)collectionView section]]];
}
}
- (void)collectionView:(UICollectionView *)collectionView willDisplaySupplementaryView:(nonnull UICollectionReusableView *)view forElementKind:(nonnull NSString *)elementKind atIndexPath:(nonnull NSIndexPath *)indexPath
{
if ([self.externalDelegate respondsToSelector:_cmd]) {
[self.externalDelegate collectionView:collectionView willDisplaySupplementaryView:view forElementKind:elementKind atIndexPath:indexPath];
}
}
- (void)collectionView:(UICollectionView *)collectionView didEndDisplayingSupplementaryView:(UICollectionReusableView *)view forElementOfKind:(NSString *)elementKind atIndexPath:(NSIndexPath *)indexPath
{
if ([self.externalDelegate respondsToSelector:_cmd]) {
[self.externalDelegate collectionView:collectionView didEndDisplayingSupplementaryView:view forElementOfKind:elementKind atIndexPath:indexPath];
}
}
- (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath
{
if (collectionView == self.collectionView) {
return NO;
} else if ([self.externalDelegate respondsToSelector:_cmd]) {
return [self.externalDelegate collectionView:self.collectionView shouldHighlightItemAtIndexPath:[NSIndexPath indexPathForItem:indexPath.item inSection:[(JEKWrappedCollectionView *)collectionView section]]];
}
return YES;
}
- (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
if (collectionView == self.collectionView) {
return NO;
} else if ([self.externalDelegate respondsToSelector:_cmd]) {
return [self.externalDelegate collectionView:self.collectionView shouldSelectItemAtIndexPath:[NSIndexPath indexPathForItem:indexPath.item inSection:[(JEKWrappedCollectionView *)collectionView section]]];
}
return YES;
}
- (BOOL)collectionView:(UICollectionView *)collectionView shouldDeselectItemAtIndexPath:(NSIndexPath *)indexPath
{
if ([self.externalDelegate respondsToSelector:_cmd]) {
return [self.externalDelegate collectionView:self.collectionView shouldDeselectItemAtIndexPath:[NSIndexPath indexPathForItem:indexPath.item inSection:[(JEKWrappedCollectionView *)collectionView section]]];
}
return YES;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
NSIndexPath *externalIndexPath = [NSIndexPath indexPathForItem:indexPath.item inSection:[(JEKWrappedCollectionView *)collectionView section]];
NSIndexPath *previousSelection = [self.selectedIndexPaths anyObject];
if (!self.collectionView.allowsMultipleSelection && previousSelection && ![previousSelection isEqual:externalIndexPath]) {
[self.collectionView deselectItemAtIndexPath:previousSelection animated:NO];
}
[self.selectedIndexPaths addObject:externalIndexPath];
if ([self.externalDelegate respondsToSelector:_cmd]) {
[self.externalDelegate collectionView:self.collectionView didSelectItemAtIndexPath:externalIndexPath];
}
}
- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(nonnull NSIndexPath *)indexPath
{
NSIndexPath *externalIndexPath = [NSIndexPath indexPathForItem:indexPath.item inSection:[(JEKWrappedCollectionView *)collectionView section]];
[self.selectedIndexPaths removeObject:externalIndexPath];
if ([self.externalDelegate respondsToSelector:_cmd]) {
[self.externalDelegate collectionView:self.collectionView didDeselectItemAtIndexPath:externalIndexPath];
}
}
@end
#pragma mark -
@implementation JEKCollectionViewWrapperCell
- (instancetype)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame]) {
UICollectionViewFlowLayout *layout = [UICollectionViewFlowLayout new];
layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
self.collectionView = [[JEKWrappedCollectionView alloc] initWithFrame:self.contentView.bounds collectionViewLayout:layout];
self.collectionView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
self.collectionView.alwaysBounceHorizontal = YES;
self.collectionView.backgroundColor = [UIColor clearColor];
self.collectionView.parentCell = self;
[self.contentView addSubview:self.collectionView];
}
return self;
}
- (void)registerCellClasses:(NSDictionary<NSString *, Class> *)classes nibs:(NSDictionary<NSString *, UINib *> *)nibs
{
[classes enumerateKeysAndObjectsUsingBlock:^(NSString *identifier, Class class, BOOL * _Nonnull stop) {
[self.collectionView registerClass:class forCellWithReuseIdentifier:identifier];
}];
[nibs enumerateKeysAndObjectsUsingBlock:^(NSString *identifier, UINib *nib, BOOL * _Nonnull stop) {
[self.collectionView registerNib:nib forCellWithReuseIdentifier:identifier];
}];
}
@end
#pragma mark -
@implementation JEKWrappedCollectionView
@synthesize section = _section;
- (void)setSection:(NSInteger)section
{
_section = section;
}
- (NSInteger)section
{
NSIndexPath *indexPath = [self.parentCollectionView indexPathForCell:self.parentCell];
// Sometimes, UICollectionView will prepare cells before adding them as subviews,
// which means that wrapped collection views will request cells before they are added
// to the main collection view. This in turn means that the parent collection view will return
// nil in indexPathForCell: above.
//
// To solve this, we can used the stored section from willDisplayCell: instead, however this one
// is unsafe in other cases, because it will be outdated if a previous section is deleted or inserted.
return indexPath ? indexPath.section : _section;
}
@end
#pragma mark -
@implementation JEKScrollableCollectionViewBatchUpdates
- (instancetype)init
{
if (self = [super init]) {
self.deletedSections = [NSMutableIndexSet new];
self.insertedSections = [NSMutableIndexSet new];
self.reloadedSections = [NSMutableIndexSet new];
self.movedSections = [NSMutableDictionary new];
self.deletedItems = [NSMutableArray new];
self.insertedItems = [NSMutableArray new];
self.reloadedItems = [NSMutableArray new];
self.movedItems = [NSMutableDictionary new];
}
return self;
}
/**
Any insertions into inserted sections (or deletions from deleted sections),
can be skipped, because UICollectionView will insert all rows when a section is inserted.
This function removes all unneeded updates, because they can cause problems when collection views
are reused.
*/
- (void)curate
{
[self.deletedItems filterUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(NSIndexPath *indexPath, NSDictionary<NSString *,id> * _Nullable bindings) {
return !([self.deletedSections containsIndex:indexPath.section] || [self.reloadedSections containsIndex:indexPath.section]);
}]];
[self.insertedItems filterUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(NSIndexPath *indexPath, NSDictionary<NSString *,id> * _Nullable bindings) {
return !([self.insertedSections containsIndex:indexPath.section] || [self.reloadedSections containsIndex:indexPath.section]);
}]];
}
- (BOOL)hasSectionUpdates
{
return self.insertedSections.count > 0 || self.deletedSections.count > 0 || self.reloadedSections.count > 0 || self.movedSections.count > 0;
}
- (BOOL)hasItemUpdates
{
return self.insertedItems.count > 0 || self.deletedItems.count > 0 || self.reloadedItems.count > 0 || self.movedItems.count > 0;
}
@end
#pragma mark -
@implementation NSIndexPath (JEKScrollableSectionCollectionView)
- (NSIndexPath *)normalize
{
return [NSIndexPath indexPathForItem:self.item inSection:0];
}
@end