2222
2323@import FirebaseDatabase;
2424
25- @interface FirebaseCollectionViewDataSource ()
26- @property (strong , nonatomic , readwrite ) void (^populateCell)
27- (__kindof UICollectionViewCell *cell, __kindof NSObject *object);
28- @end
29-
3025@implementation FirebaseCollectionViewDataSource
3126
3227#pragma mark - FirebaseDataSource initializer methods
3328
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-
17429- (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 {
19134 FirebaseArray *array = [[FirebaseArray alloc ] initWithQuery: query];
19235 self = [super initWithArray: array];
19336 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;
23539 }
23640 return self;
23741}
@@ -263,19 +67,9 @@ - (void)array:(FirebaseArray *)array didMoveObject:(id)object
26367
26468- (nonnull UICollectionViewCell *)collectionView : (nonnull UICollectionView *)collectionView
26569 cellForItemAtIndexPath : (nonnull NSIndexPath *)indexPath {
266- id cell = [self .collectionView dequeueReusableCellWithReuseIdentifier: self .reuseIdentifier
267- forIndexPath: indexPath];
268-
26970 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);
27973
28074 return cell;
28175}
@@ -289,27 +83,18 @@ - (NSInteger)collectionView:(nonnull UICollectionView *)collectionView
28983 return self.count ;
29084}
29185
292- - (void )populateCellWithBlock : (void (^)(__kindof UICollectionViewCell *cell,
293- __kindof NSObject *object))callback {
294- self.populateCell = callback;
295- }
86+ @end
29687
297- # pragma mark - Accessors
88+ @implementation UICollectionView (FirebaseCollectionViewDataSource)
29889
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;
31398}
31499
315100@end
0 commit comments