forked from firebase/FirebaseUI-iOS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFUIFirestoreCollectionViewDataSource.m
More file actions
204 lines (166 loc) · 7.76 KB
/
Copy pathFUIFirestoreCollectionViewDataSource.m
File metadata and controls
204 lines (166 loc) · 7.76 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
// clang-format off
//
// Copyright (c) 2016 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// clang-format on
#import "FUIFirestoreCollectionViewDataSource.h"
@interface FUIFirestoreCollectionViewDataSource () <FUIBatchedArrayDelegate>
@property (nonatomic, readonly, nonnull) FUIBatchedArray *collection;
// We need to maintain our own count property because the self.collection.count is sometimes
// inconsistent with how the -[UICollectionView performBatchUpdates:] method expects it to work.
// -[UICollectionView performBatchUpdates:] calls -[UICollectionViewDataSource count] at the
// beginning and at the end of the method, and if the value of -[UICollectionViewDataSource count]
// after -[UICollectionView performBatchUpdates:] is not equal to the value of
// -[UICollectionViewDataSource count] before + (rows added - rows deleted) it throws an
// NSInternalInconsistencyException.
@property (nonatomic) NSUInteger count;
/**
* The callback to populate a subclass of UICollectionViewCell with an object
* provided by the datasource.
*/
@property (copy, nonatomic, readonly) UICollectionViewCell *(^populateCellAtIndexPath)
(UICollectionView *collectionView, NSIndexPath *indexPath, FIRDocumentSnapshot *object);
@end
@implementation FUIFirestoreCollectionViewDataSource
#pragma mark - FUIDataSource initializer methods
- (instancetype)initWithCollection:(FUIBatchedArray *)collection
populateCell:(UICollectionViewCell * (^)(UICollectionView *,
NSIndexPath *,
FIRDocumentSnapshot *))populateCell {
self = [super init];
if (self) {
_collection = collection;
_collection.delegate = self;
_populateCellAtIndexPath = populateCell;
_count = collection.count;
}
return self;
}
- (instancetype)initWithQuery:(FIRQuery *)query
populateCell:(UICollectionViewCell *(^)(UICollectionView *,
NSIndexPath *,
FIRDocumentSnapshot *))populateCell {
FUIBatchedArray *array = [[FUIBatchedArray alloc] initWithQuery:query delegate:self];
return [self initWithCollection:array populateCell:populateCell];
}
- (NSArray<FIRDocumentSnapshot *> *)items {
return self.collection.items;
}
- (FIRDocumentSnapshot *)snapshotAtIndex:(NSInteger)index {
return self.collection[index];
}
- (void)bindToView:(UICollectionView *)view {
self.collectionView = view;
view.dataSource = self;
[self.collection observeQuery];
}
- (void)unbind {
self.collectionView.dataSource = nil;
self.collectionView = nil;
[self.collection stopObserving];
}
- (FIRQuery *)query {
return self.collection.query;
}
- (void)setQuery:(FIRQuery *)query {
self.collection.query = query;
}
#pragma mark - FUIBatchedArrayDelegate methods
- (void)batchedArray:(FUIBatchedArray *)array
willUpdateWithDiff:(FUISnapshotArrayDiff<FIRDocumentSnapshot *> *)diff {
// This fixes an issue where UICollectionView crashes due to an invalid number of items
// https://fangpenlin.com/posts/2016/04/29/uicollectionview-invalid-number-of-items-crash-issue/
[self.collectionView numberOfItemsInSection:0];
}
- (void)batchedArray:(FUIBatchedArray *)array
didUpdateWithDiff:(FUISnapshotArrayDiff<FIRDocumentSnapshot *> *)diff {
[self.collectionView performBatchUpdates:^{
NSMutableArray *deletedIndexPaths =
[NSMutableArray arrayWithCapacity:diff.deletedIndexes.count];
for (NSNumber *deletedIndex in diff.deletedIndexes) {
NSIndexPath *deleted = [NSIndexPath indexPathForItem:deletedIndex.integerValue inSection:0];
[deletedIndexPaths addObject:deleted];
}
[self.collectionView deleteItemsAtIndexPaths:deletedIndexPaths];
NSMutableArray *changedIndexPaths =
[NSMutableArray arrayWithCapacity:diff.changedIndexes.count];
for (NSNumber *changedIndex in diff.changedIndexes) {
NSIndexPath *changed = [NSIndexPath indexPathForItem:changedIndex.integerValue inSection:0];
[changedIndexPaths addObject:changed];
}
// Use a delete and insert instead of a reload. See
// https://stackoverflow.com/questions/42147822/uicollectionview-batchupdate-edge-case-fails
[self.collectionView deleteItemsAtIndexPaths:changedIndexPaths];
[self.collectionView insertItemsAtIndexPaths:changedIndexPaths];
for (NSInteger i = 0; i < diff.movedInitialIndexes.count; i++) {
NSInteger initialIndex = diff.movedInitialIndexes[i].integerValue;
NSInteger finalIndex = diff.movedResultIndexes[i].integerValue;
NSIndexPath *initialPath = [NSIndexPath indexPathForItem:initialIndex inSection:0];
NSIndexPath *finalPath = [NSIndexPath indexPathForItem:finalIndex inSection:0];
[self.collectionView moveItemAtIndexPath:initialPath toIndexPath:finalPath];
}
NSMutableArray *insertedIndexPaths =
[NSMutableArray arrayWithCapacity:diff.insertedIndexes.count];
for (NSNumber *insertedIndex in diff.insertedIndexes) {
NSIndexPath *inserted = [NSIndexPath indexPathForItem:insertedIndex.integerValue inSection:0];
[insertedIndexPaths addObject:inserted];
}
[self.collectionView insertItemsAtIndexPaths:insertedIndexPaths];
self.count = self.collection.count;
} completion:^(BOOL finished) {
// Reload paths that have been moved.
NSMutableArray *movedIndexPaths =
[NSMutableArray arrayWithCapacity:diff.movedResultIndexes.count];
for (NSNumber *movedResultIndex in diff.movedResultIndexes) {
NSIndexPath *moved = [NSIndexPath indexPathForItem:movedResultIndex.integerValue inSection:0];
[movedIndexPaths addObject:moved];
}
[self.collectionView reloadItemsAtIndexPaths:movedIndexPaths];
}];
}
- (void)batchedArray:(FUIBatchedArray *)array queryDidFailWithError:(NSError *)error {
if (self.queryErrorHandler != nil) {
self.queryErrorHandler(error);
} else {
NSLog(@"%@ Unhandled Firestore error: %@. Set the queryErrorHandler property to debug.",
self, error);
}
}
#pragma mark - UICollectionViewDataSource methods
- (nonnull UICollectionViewCell *)collectionView:(nonnull UICollectionView *)collectionView
cellForItemAtIndexPath:(nonnull NSIndexPath *)indexPath {
FIRDocumentSnapshot *snap = [self.collection.items objectAtIndex:indexPath.item];
UICollectionViewCell *cell = self.populateCellAtIndexPath(collectionView, indexPath, snap);
return cell;
}
- (NSInteger)numberOfSectionsInCollectionView:(nonnull UICollectionView *)collectionView {
return 1;
}
- (NSInteger)collectionView:(nonnull UICollectionView *)collectionView
numberOfItemsInSection:(NSInteger)section {
return self.count;
}
@end
@implementation UICollectionView (FUIFirestoreCollectionViewDataSource)
- (FUIFirestoreCollectionViewDataSource *)bindToFirestoreQuery:(FIRQuery *)query
populateCell:(UICollectionViewCell *(^)(UICollectionView *,
NSIndexPath *,
FIRDocumentSnapshot *))populateCell {
FUIFirestoreCollectionViewDataSource *dataSource =
[[FUIFirestoreCollectionViewDataSource alloc] initWithQuery:query populateCell:populateCell];
[dataSource bindToView:self];
return dataSource;
}
@end