Skip to content

Commit da1c48e

Browse files
committed
allow any FUICollection to be used in data sources
1 parent c3aa174 commit da1c48e

File tree

4 files changed

+70
-14
lines changed

4 files changed

+70
-14
lines changed

FirebaseDatabaseUI/FUICollectionViewDataSource.h

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,26 @@ NS_ASSUME_NONNULL_BEGIN
5151
/**
5252
* Initialize an instance of FUICollectionViewDataSource that populates
5353
* UICollectionViewCells with FIRDataSnapshots.
54+
* @param collection A FUICollection that the data source uses to pull snapshots
55+
* from Firebase Database.
56+
* @param collectionView An instance of a UICollectionView to bind to. This view
57+
* is not retained by its data source.
58+
* @param populateCell A closure used by the data source to create the cells that
59+
* are displayed in the collection view. This closure is retained by the data
60+
* source, so if you capture self in the closure and also claim ownership of the
61+
* data source, be sure to avoid retain cycles by capturing a weak reference to self.
62+
* @return An instance of FUICollectionViewDataSource that populates
63+
* UICollectionViewCells with FIRDataSnapshots.
64+
*/
65+
- (instancetype)initWithCollection:(id<FUICollection>)collection
66+
view:(UICollectionView *)view
67+
populateCell:(UICollectionViewCell *(^)(UICollectionView *collectionView,
68+
NSIndexPath *indexPath,
69+
FIRDataSnapshot *object))populateCell NS_DESIGNATED_INITIALIZER;
70+
71+
/**
72+
* Initialize an unsorted instance of FUICollectionViewDataSource that populates
73+
* UICollectionViewCells with FIRDataSnapshots.
5474
* @param query A Firebase query to bind the data source to.
5575
* @param collectionView An instance of a UICollectionView to bind to. This view
5676
* is not retained by its data source.
@@ -65,7 +85,7 @@ NS_ASSUME_NONNULL_BEGIN
6585
view:(UICollectionView *)collectionView
6686
populateCell:(UICollectionViewCell *(^)(UICollectionView *collectionView,
6787
NSIndexPath *indexPath,
68-
FIRDataSnapshot *object))populateCell NS_DESIGNATED_INITIALIZER;
88+
FIRDataSnapshot *object))populateCell;
6989

7090
- (instancetype)initWithCollection:(id<FUICollection>)collection NS_UNAVAILABLE;
7191

FirebaseDatabaseUI/FUICollectionViewDataSource.m

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,26 @@ @implementation FUICollectionViewDataSource
2626

2727
#pragma mark - FUIDataSource initializer methods
2828

29+
- (instancetype)initWithCollection:(id<FUICollection>)collection
30+
view:(UICollectionView *)view
31+
populateCell:(UICollectionViewCell * (^)(UICollectionView *,
32+
NSIndexPath *,
33+
FIRDataSnapshot *))populateCell {
34+
self = [super initWithCollection:collection];
35+
if (self) {
36+
_collectionView = view;
37+
_populateCellAtIndexPath = populateCell;
38+
}
39+
return self;
40+
}
41+
2942
- (instancetype)initWithQuery:(FIRDatabaseQuery *)query
3043
view:(UICollectionView *)collectionView
3144
populateCell:(UICollectionViewCell *(^)(UICollectionView *,
3245
NSIndexPath *,
3346
FIRDataSnapshot *))populateCell {
3447
FUIArray *array = [[FUIArray alloc] initWithQuery:query];
35-
self = [super initWithCollection:array];
36-
if (self) {
37-
_collectionView = collectionView;
38-
_populateCellAtIndexPath = populateCell;
39-
}
40-
return self;
48+
return [self initWithCollection:array view:collectionView populateCell:populateCell];
4149
}
4250

4351
#pragma mark - FUICollectionDelegate methods

FirebaseDatabaseUI/FUITableViewDataSource.h

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,26 @@ NS_ASSUME_NONNULL_BEGIN
4848

4949
/**
5050
* Initialize an instance of FUITableViewDataSource.
51+
* @param collection An FUICollection used by the data source to pull data
52+
* from Firebase Database.
53+
* @param tableView An instance of a UITableView to bind to. This view is
54+
* not retained by the data source.
55+
* @param populateCell A closure used by the data source to create/reuse
56+
* table view cells and populate their content. This closure is retained
57+
* by the data source, so if you capture self in the closure and also claim ownership
58+
* of the data source, be sure to avoid retain cycles by capturing a weak reference to self.
59+
* @return An instance of FUITableViewDataSource.
60+
*/
61+
- (instancetype)initWithCollection:(id<FUICollection>)collection
62+
view:(UITableView *)tableView
63+
populateCell:(UITableViewCell *(^)(UITableView *tableView,
64+
NSIndexPath *indexPath,
65+
FIRDataSnapshot *object))populateCell NS_DESIGNATED_INITIALIZER;
66+
67+
68+
/**
69+
* Initialize an instance of FUITableViewDataSource with contents ordered
70+
* by the query.
5171
* @param query A Firebase query to bind the data source to.
5272
* @param tableView An instance of a UITableView to bind to. This view is
5373
* not retained by the data source.
@@ -61,7 +81,7 @@ NS_ASSUME_NONNULL_BEGIN
6181
view:(UITableView *)tableView
6282
populateCell:(UITableViewCell *(^)(UITableView *tableView,
6383
NSIndexPath *indexPath,
64-
FIRDataSnapshot *object))populateCell NS_DESIGNATED_INITIALIZER;
84+
FIRDataSnapshot *object))populateCell;
6585

6686
- (instancetype)initWithCollection:(id<FUICollection>)collection NS_UNAVAILABLE;
6787

FirebaseDatabaseUI/FUITableViewDataSource.m

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,18 +35,26 @@ @implementation FUITableViewDataSource
3535

3636
#pragma mark - FUIDataSource initializer methods
3737

38+
- (instancetype)initWithCollection:(id<FUICollection>)collection
39+
view:(UITableView *)tableView
40+
populateCell:(UITableViewCell *(^)(UITableView *,
41+
NSIndexPath *,
42+
FIRDataSnapshot *))populateCell {
43+
self = [super initWithCollection:collection];
44+
if (self != nil) {
45+
self.tableView = tableView;
46+
self.populateCell = populateCell;
47+
}
48+
return self;
49+
}
50+
3851
- (instancetype)initWithQuery:(FIRDatabaseQuery *)query
3952
view:(UITableView *)tableView
4053
populateCell:(UITableViewCell *(^)(UITableView *,
4154
NSIndexPath *,
4255
FIRDataSnapshot *))populateCell {
4356
FUIArray *array = [[FUIArray alloc] initWithQuery:query];
44-
self = [super initWithCollection:array];
45-
if (self) {
46-
self.tableView = tableView;
47-
self.populateCell = populateCell;
48-
}
49-
return self;
57+
return [self initWithCollection:array view:tableView populateCell:populateCell];
5058
}
5159

5260
#pragma mark - FUICollectionDelegate methods

0 commit comments

Comments
 (0)