22
22
23
23
@import FirebaseDatabase;
24
24
25
- @interface FirebaseCollectionViewDataSource ()
26
- @property (strong , nonatomic , readwrite ) void (^populateCell)
27
- (__kindof UICollectionViewCell *cell, __kindof NSObject *object);
28
- @end
29
-
30
25
@implementation FirebaseCollectionViewDataSource
31
26
32
27
#pragma mark - FirebaseDataSource initializer methods
33
28
34
- - (instancetype )initWithRef : (FIRDatabaseReference *)ref
35
- cellReuseIdentifier : (NSString *)identifier
36
- view : (UICollectionView *)collectionView {
37
- return [self initWithQuery: ref
38
- modelClass: nil
39
- cellClass: nil
40
- cellReuseIdentifier: identifier
41
- view: collectionView];
42
- }
43
-
44
- - (instancetype )initWithRef : (FIRDatabaseReference *)ref
45
- prototypeReuseIdentifier : (NSString *)identifier
46
- view : (UICollectionView *)collectionView {
47
- return [self initWithQuery: ref
48
- modelClass: nil
49
- prototypeReuseIdentifier: identifier
50
- view: collectionView];
51
- }
52
-
53
- - (instancetype )initWithRef : (FIRDatabaseReference *)ref
54
- cellClass : (Class )cell
55
- cellReuseIdentifier : (NSString *)identifier
56
- view : (UICollectionView *)collectionView {
57
- return [self initWithQuery: ref
58
- modelClass: nil
59
- cellClass: cell
60
- cellReuseIdentifier: identifier
61
- view: collectionView];
62
- }
63
-
64
- - (instancetype )initWithRef : (FIRDatabaseReference *)ref
65
- nibNamed : (NSString *)nibName
66
- cellReuseIdentifier : (NSString *)identifier
67
- view : (UICollectionView *)collectionView {
68
- return [self initWithQuery: ref
69
- modelClass: nil
70
- nibNamed: nibName
71
- cellReuseIdentifier: identifier
72
- view: collectionView];
73
- }
74
-
75
- - (instancetype )initWithRef : (FIRDatabaseReference *)ref
76
- modelClass : (Class )model
77
- cellReuseIdentifier : (NSString *)identifier
78
- view : (UICollectionView *)collectionView {
79
- return [self initWithQuery: ref
80
- modelClass: model
81
- cellClass: nil
82
- cellReuseIdentifier: identifier
83
- view: collectionView];
84
- }
85
-
86
- - (instancetype )initWithRef : (FIRDatabaseReference *)ref
87
- modelClass : (Class )model
88
- prototypeReuseIdentifier : (NSString *)identifier
89
- view : (UICollectionView *)collectionView {
90
- return [self initWithQuery: ref
91
- modelClass: model
92
- prototypeReuseIdentifier: identifier
93
- view: collectionView];
94
- }
95
-
96
- - (instancetype )initWithRef : (FIRDatabaseReference *)ref
97
- modelClass : (Class )model
98
- cellClass : (Class )cell
99
- cellReuseIdentifier : (NSString *)identifier
100
- view : (UICollectionView *)collectionView {
101
- return [self initWithQuery: ref
102
- modelClass: model
103
- cellClass: cell
104
- cellReuseIdentifier: identifier
105
- view: collectionView];
106
- }
107
-
108
- - (instancetype )initWithRef : (FIRDatabaseReference *)ref
109
- modelClass : (Class )model
110
- nibNamed : (NSString *)nibName
111
- cellReuseIdentifier : (NSString *)identifier
112
- view : (UICollectionView *)collectionView {
113
- return [self initWithQuery: ref
114
- modelClass: model
115
- nibNamed: nibName
116
- cellReuseIdentifier: identifier
117
- view: collectionView];
118
- }
119
-
120
- - (instancetype )initWithQuery : (FIRDatabaseQuery *)query
121
- cellReuseIdentifier : (NSString *)identifier
122
- view : (UICollectionView *)collectionView {
123
- return [self initWithQuery: query
124
- modelClass: nil
125
- cellClass: nil
126
- cellReuseIdentifier: identifier
127
- view: collectionView];
128
- }
129
-
130
- - (instancetype )initWithQuery : (FIRDatabaseQuery *)query
131
- prototypeReuseIdentifier : (NSString *)identifier
132
- view : (UICollectionView *)collectionView {
133
- self.hasPrototypeCell = YES ;
134
- return [self initWithQuery: query
135
- modelClass: nil
136
- cellClass: nil
137
- cellReuseIdentifier: identifier
138
- view: collectionView];
139
- }
140
-
141
- - (instancetype )initWithQuery : (FIRDatabaseQuery *)query
142
- cellClass : (Class )cell
143
- cellReuseIdentifier : (NSString *)identifier
144
- view : (UICollectionView *)collectionView {
145
- return [self initWithQuery: query
146
- modelClass: nil
147
- cellClass: cell
148
- cellReuseIdentifier: identifier
149
- view: collectionView];
150
- }
151
-
152
- - (instancetype )initWithQuery : (FIRDatabaseQuery *)query
153
- nibNamed : (NSString *)nibName
154
- cellReuseIdentifier : (NSString *)identifier
155
- view : (UICollectionView *)collectionView {
156
- return [self initWithQuery: query
157
- modelClass: nil
158
- nibNamed: nibName
159
- cellReuseIdentifier: identifier
160
- view: collectionView];
161
- }
162
-
163
- - (instancetype )initWithQuery : (FIRDatabaseQuery *)query
164
- modelClass : (Class )model
165
- cellReuseIdentifier : (NSString *)identifier
166
- view : (UICollectionView *)collectionView {
167
- return [self initWithQuery: query
168
- modelClass: model
169
- cellClass: nil
170
- cellReuseIdentifier: identifier
171
- view: collectionView];
172
- }
173
-
174
29
- (instancetype )initWithQuery : (FIRDatabaseQuery *)query
175
- modelClass : (Class )model
176
- prototypeReuseIdentifier : (NSString *)identifier
177
- view : (UICollectionView *)collectionView {
178
- self.hasPrototypeCell = YES ;
179
- return [self initWithQuery: query
180
- modelClass: model
181
- cellClass: nil
182
- cellReuseIdentifier: identifier
183
- view: collectionView];
184
- }
185
-
186
- - (instancetype )initWithQuery : (FIRDatabaseQuery *)query
187
- modelClass : (Class )model
188
- cellClass : (Class )cell
189
- cellReuseIdentifier : (NSString *)identifier
190
- view : (UICollectionView *)collectionView {
30
+ view : (UICollectionView *)collectionView
31
+ populateCell : (UICollectionViewCell *(^)(UICollectionView *,
32
+ NSIndexPath *,
33
+ FIRDataSnapshot *))populateCell {
191
34
FirebaseArray *array = [[FirebaseArray alloc ] initWithQuery: query];
192
35
self = [super initWithArray: array];
193
36
if (self) {
194
- if (!model) {
195
- model = [FIRDataSnapshot class ];
196
- }
197
-
198
- if (!cell) {
199
- cell = [UICollectionViewCell class ];
200
- }
201
-
202
- self.collectionView = collectionView;
203
- self.modelClass = model;
204
- self.cellClass = cell;
205
- self.reuseIdentifier = identifier;
206
- self.populateCell = ^(id cell, id object) {};
207
-
208
- if (!self.hasPrototypeCell ) {
209
- [self .collectionView registerClass: self .cellClass
210
- forCellWithReuseIdentifier: self .reuseIdentifier];
211
- }
212
- }
213
- return self;
214
- }
215
-
216
- - (instancetype )initWithQuery : (FIRDatabaseQuery *)query
217
- modelClass : (Class )model
218
- nibNamed : (NSString *)nibName
219
- cellReuseIdentifier : (NSString *)identifier
220
- view : (UICollectionView *)collectionView {
221
- FirebaseArray *array = [[FirebaseArray alloc ] initWithQuery: query];
222
- self = [super initWithArray: array];
223
- if (self) {
224
- if (!model) {
225
- model = [FIRDataSnapshot class ];
226
- }
227
-
228
- self.collectionView = collectionView;
229
- self.modelClass = model;
230
- self.reuseIdentifier = identifier;
231
- self.populateCell = ^(id cell, id object) {};
232
-
233
- UINib *nib = [UINib nibWithNibName: nibName bundle: nil ];
234
- [self .collectionView registerNib: nib forCellWithReuseIdentifier: self .reuseIdentifier];
37
+ _collectionView = collectionView;
38
+ _populateCellAtIndexPath = populateCell;
235
39
}
236
40
return self;
237
41
}
@@ -263,19 +67,9 @@ - (void)array:(FirebaseArray *)array didMoveObject:(id)object
263
67
264
68
- (nonnull UICollectionViewCell *)collectionView : (nonnull UICollectionView *)collectionView
265
69
cellForItemAtIndexPath : (nonnull NSIndexPath *)indexPath {
266
- id cell = [self .collectionView dequeueReusableCellWithReuseIdentifier: self .reuseIdentifier
267
- forIndexPath: indexPath];
268
-
269
70
FIRDataSnapshot *snap = [self .items objectAtIndex: indexPath.row];
270
- if (![self .modelClass isSubclassOfClass: [FIRDataSnapshot class ]]) {
271
- id model = [[self .modelClass alloc ] init ];
272
- // TODO: replace setValuesForKeysWithDictionary with client API
273
- // valueAsObject method
274
- [model setValuesForKeysWithDictionary: snap.value];
275
- self.populateCell (cell, model);
276
- } else {
277
- self.populateCell (cell, snap);
278
- }
71
+
72
+ UICollectionViewCell *cell = self.populateCellAtIndexPath (collectionView, indexPath, snap);
279
73
280
74
return cell;
281
75
}
@@ -289,27 +83,18 @@ - (NSInteger)collectionView:(nonnull UICollectionView *)collectionView
289
83
return self.count ;
290
84
}
291
85
292
- - (void )populateCellWithBlock : (void (^)(__kindof UICollectionViewCell *cell,
293
- __kindof NSObject *object))callback {
294
- self.populateCell = callback;
295
- }
86
+ @end
296
87
297
- # pragma mark - Accessors
88
+ @implementation UICollectionView (FirebaseCollectionViewDataSource)
298
89
299
- - (void )setModelClass : (Class )modelClass {
300
- if (modelClass == nil ) {
301
- _modelClass = [FIRDataSnapshot class ];
302
- } else {
303
- _modelClass = modelClass;
304
- }
305
- }
306
-
307
- - (void )setCellClass : (Class )cellClass {
308
- if (cellClass == nil ) {
309
- _cellClass = [UICollectionViewCell class ];
310
- } else {
311
- _cellClass = cellClass;
312
- }
90
+ - (FirebaseCollectionViewDataSource *)bindToQuery : (FIRDatabaseQuery *)query
91
+ populateCell : (UICollectionViewCell *(^)(UICollectionView *,
92
+ NSIndexPath *,
93
+ FIRDataSnapshot *))populateCell {
94
+ FirebaseCollectionViewDataSource *dataSource =
95
+ [[FirebaseCollectionViewDataSource alloc ] initWithQuery: query view: self populateCell: populateCell];
96
+ self.dataSource = dataSource;
97
+ return dataSource;
313
98
}
314
99
315
100
@end
0 commit comments