Skip to content

Commit 466cbfa

Browse files
committed
add syntax shorthand for data sources
1 parent 7526248 commit 466cbfa

File tree

6 files changed

+77
-11
lines changed

6 files changed

+77
-11
lines changed

FirebaseDatabaseUI/FirebaseCollectionViewDataSource.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,4 +83,24 @@ NS_ASSUME_NONNULL_BEGIN
8383

8484
@end
8585

86+
@interface UICollectionView (FirebaseCollectionViewDataSource)
87+
88+
/**
89+
* Creates a data source, attaches it to the collection view, and returns it.
90+
* The returned data source is not retained by the collection view and must be
91+
* retained or it will be deallocated while still in use by the collection view.
92+
* @param query A Firebase database query to bind the collection view to.
93+
* @param populateCell A closure used by the data source to create the cells
94+
* displayed in the collection view. The closure is retained by the returned
95+
* data source.
96+
* @return The created data source. This value must be retained while the collection
97+
* view is in use.
98+
*/
99+
- (FirebaseCollectionViewDataSource *)bindToQuery:(FIRDatabaseQuery *)query
100+
populateCell:(UICollectionViewCell *(^)(UICollectionView *collectionView,
101+
NSIndexPath *indexPath,
102+
FIRDataSnapshot *object))populateCell __attribute__((warn_unused_result));
103+
104+
@end
105+
86106
NS_ASSUME_NONNULL_END

FirebaseDatabaseUI/FirebaseCollectionViewDataSource.m

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,3 +84,17 @@ - (NSInteger)collectionView:(nonnull UICollectionView *)collectionView
8484
}
8585

8686
@end
87+
88+
@implementation UICollectionView (FirebaseCollectionViewDataSource)
89+
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;
98+
}
99+
100+
@end

FirebaseDatabaseUI/FirebaseTableViewDataSource.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,24 @@ NS_ASSUME_NONNULL_BEGIN
7474

7575
@end
7676

77+
@interface UITableView (FirebaseTableViewDataSource)
78+
79+
/**
80+
* Creates a data source, attaches it to the table view, and returns it.
81+
* The returned data source is not retained by the table view and must be
82+
* retained or it will be deallocated while still in use by the table view.
83+
* @param query A Firebase database query to bind the table view to.
84+
* @param populateCell A closure used by the data source to create the cells
85+
* displayed in the table view. The closure is retained by the returned
86+
* data source.
87+
* @return The created data source. This value must be retained while the table
88+
* view is in use.
89+
*/
90+
- (FirebaseTableViewDataSource *)bindToQuery:(FIRDatabaseQuery *)query
91+
populateCell:(UITableViewCell *(^)(UITableView *tableView,
92+
NSIndexPath *indexPath,
93+
FIRDataSnapshot *object))populateCell __attribute__((warn_unused_result));
94+
95+
@end
96+
7797
NS_ASSUME_NONNULL_END

FirebaseDatabaseUI/FirebaseTableViewDataSource.m

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,3 +94,17 @@ - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger
9494
}
9595

9696
@end
97+
98+
@implementation UITableView (FirebaseTableViewDataSource)
99+
100+
- (FirebaseTableViewDataSource *)bindToQuery:(FIRDatabaseQuery *)query
101+
populateCell:(UITableViewCell *(^)(UITableView *tableView,
102+
NSIndexPath *indexPath,
103+
FIRDataSnapshot *snap))populateCell {
104+
FirebaseTableViewDataSource *dataSource =
105+
[[FirebaseTableViewDataSource alloc] initWithQuery:query view:self populateCell:populateCell];
106+
self.dataSource = dataSource;
107+
return dataSource;
108+
}
109+
110+
@end

FirebaseDatabaseUITests/FirebaseCollectionViewDataSourceTest.m

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,14 @@ - (void)setUp {
4444
self.observable = [[FUITestObservable alloc] init];
4545
// Horrible abuse of type system, knowing that the initializer passes the observable straight to
4646
// FirebaseArray anyway.
47-
self.dataSource = [[FirebaseCollectionViewDataSource alloc] initWithQuery:(FIRDatabaseReference *)self.observable
48-
view:self.collectionView
49-
populateCell:^UICollectionViewCell *(UICollectionView *collectionView, NSIndexPath *indexPath, FIRDataSnapshot *object) {
47+
self.dataSource = [self.collectionView bindToQuery:(FIRDatabaseReference *)self.observable
48+
populateCell:^UICollectionViewCell *(UICollectionView *collectionView,
49+
NSIndexPath *indexPath,
50+
FIRDataSnapshot *object) {
5051
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kTestReuseIdentifier forIndexPath:indexPath];
5152
cell.accessibilityValue = object.key;
5253
return cell;
5354
}];
54-
self.collectionView.dataSource = self.dataSource;
5555

5656
// Removing this NSLog causes the tests to crash since `numberOfItemsInSection`
5757
// actually pulls updates from the data source or something

FirebaseDatabaseUITests/FirebaseTableViewDataSourceTest.m

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,17 +44,15 @@ - (void)setUp {
4444
self.observable = [[FUITestObservable alloc] init];
4545
// Horrible abuse of type system, knowing that the initializer passes the observable straight to
4646
// FirebaseArray anyway.
47-
self.dataSource = [[FirebaseTableViewDataSource alloc] initWithQuery:(FIRDatabaseQuery *)self.observable
48-
view:self.tableView
49-
populateCell:^UITableViewCell *(UITableView *tableView,
50-
NSIndexPath *indexPath,
51-
FIRDataSnapshot *object) {
47+
self.dataSource = [self.tableView bindToQuery:(FIRDatabaseReference *)self.observable
48+
populateCell:^UITableViewCell *(UITableView *tableView,
49+
NSIndexPath *indexPath,
50+
FIRDataSnapshot *object) {
5251
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kTestReuseIdentifier];
5352
cell.accessibilityValue = object.key;
5453
return cell;
5554
}];
56-
self.tableView.dataSource = self.dataSource;
57-
55+
5856
[self.observable populateWithCount:10];
5957
}
6058

0 commit comments

Comments
 (0)