Skip to content

Commit ac33885

Browse files
committed
implement delegate methods, add doc comments
1 parent f5d44c2 commit ac33885

6 files changed

+69
-9
lines changed

FirebaseDatabaseUI/FirebaseIndexArray.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,10 +115,22 @@ didFailLoadWithError:(NSError *)error;
115115

116116
@interface FirebaseIndexArray : NSObject
117117

118+
/**
119+
* An immutable copy of the loaded contents in the array. Returns an
120+
* empty array if no contents have loaded yet.
121+
*/
118122
@property(nonatomic, copy, readonly) NSArray<FIRDataSnapshot *> *items;
119123

124+
/**
125+
* The delegate that this array should forward events to.
126+
*/
120127
@property(nonatomic, weak) id<FirebaseIndexArrayDelegate> delegate;
121128

129+
/**
130+
* Returns the number of items in the array.
131+
*/
132+
@property(nonatomic, readonly) NSUInteger count;
133+
122134
- (instancetype)init NS_UNAVAILABLE;
123135

124136
/**
@@ -149,6 +161,7 @@ didFailLoadWithError:(NSError *)error;
149161

150162
/**
151163
* Returns the snapshot at the given index, if it has loaded.
164+
* Raises a fatal error if the index is out of bounds.
152165
* @param index The index of the requested snapshot.
153166
* @return A snapshot, or nil if one has not yet been loaded.
154167
*/

FirebaseDatabaseUI/FirebaseIndexArray.m

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,17 @@ - (void)observeQueries {
7171
NSArray *observers = [self.observers copy];
7272
NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity:observers.count];
7373
for (FirebaseQueryObserver *observer in observers) {
74-
[array addObject:observer.contents];
74+
if (observer.contents != nil) {
75+
[array addObject:observer.contents];
76+
}
7577
}
7678
return [array copy];
7779
}
7880

81+
- (NSUInteger)count {
82+
return self.observers.count;
83+
}
84+
7985
// FirebaseIndexArray instance becomes unusable after invalidation.
8086
- (void)invalidate {
8187
for (NSInteger i = 0; i < self.observers.count; i++) {

FirebaseDatabaseUI/FirebaseIndexCollectionViewDataSource.h

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,24 @@
2020

2121
NS_ASSUME_NONNULL_BEGIN
2222

23-
@interface FirebaseIndexCollectionViewDataSource : NSObject
23+
@interface FirebaseIndexCollectionViewDataSource : NSObject <UICollectionViewDataSource>
2424

2525
- (instancetype)init NS_UNAVAILABLE;
2626

27+
/**
28+
* Initializes a collection view data source.
29+
* @param indexQuery The Firebase query containing children of the data query.
30+
* @param dataQuery The reference whose children correspond to the contents of the
31+
* index query. This reference's children's contents are served as teh contents
32+
* of the collection view that adopts this data source.
33+
* @param collectionView The collection view that is populated by this data source. The
34+
* data source pulls updates from Firebase database, so it must maintain a reference
35+
* to the collection view in order to update its contents as the database pushes updates.
36+
* The table view is not retained by its data source.
37+
* @param cellIdentifier The cell reuse identifier used to dequeue reusable cells from
38+
* the collection view.
39+
* @param populateCell The closure invoked when populating a UICollectionViewCell (or subclass).
40+
*/
2741
- (instancetype)initWithIndex:(FIRDatabaseQuery *)indexQuery
2842
data:(FIRDatabaseReference *)dataQuery
2943
collectionView:(UICollectionView *)collectionView
@@ -33,7 +47,4 @@ NS_ASSUME_NONNULL_BEGIN
3347

3448
@end
3549

36-
@interface FirebaseIndexCollectionViewDataSource (CollectionViewDataSource) <UICollectionViewDataSource>
37-
@end
38-
3950
NS_ASSUME_NONNULL_END

FirebaseDatabaseUI/FirebaseIndexCollectionViewDataSource.m

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,4 +93,19 @@ - (void)array:(FirebaseIndexArray *)array
9393
[self.collectionView reloadItemsAtIndexPaths:@[path]];
9494
}
9595

96+
#pragma mark - UICollectionViewDataSource
97+
98+
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
99+
return self.array.count;
100+
}
101+
102+
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
103+
cellForItemAtIndexPath:(NSIndexPath *)indexPath {
104+
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:self.identifier
105+
forIndexPath:indexPath];
106+
FIRDataSnapshot *snap = [self.array objectAtIndex:indexPath.item];
107+
self.populateCell(cell, snap);
108+
return cell;
109+
}
110+
96111
@end

FirebaseDatabaseUI/FirebaseIndexTableViewDataSource.h

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,24 @@
2020

2121
NS_ASSUME_NONNULL_BEGIN
2222

23-
@interface FirebaseIndexTableViewDataSource : NSObject
23+
@interface FirebaseIndexTableViewDataSource : NSObject <UITableViewDataSource>
2424

2525
- (instancetype)init NS_UNAVAILABLE;
2626

27+
/**
28+
* Initializes a table view data source.
29+
* @param indexQuery The Firebase query containing children of the data query.
30+
* @param dataQuery The reference whose children correspond to the contents of the
31+
* index query. This reference's children's contents are served as teh contents
32+
* of the table view that adopts this data source.
33+
* @param tableView The table view that is populated by this data source. The
34+
* data source pulls updates from Firebase database, so it must maintain a reference
35+
* to the table view in order to update its contents as the database pushes updates.
36+
* The table view is not retained by its data source.
37+
* @param cellIdentifier The cell reuse identifier used to dequeue reusable cells from
38+
* the table view.
39+
* @param populateCell The closure invoked when populating a UITableViewCell (or subclass).
40+
*/
2741
- (instancetype)initWithIndex:(FIRDatabaseQuery *)indexQuery
2842
data:(FIRDatabaseReference *)dataQuery
2943
tableView:(UITableView *)tableView
@@ -33,7 +47,4 @@ NS_ASSUME_NONNULL_BEGIN
3347

3448
@end
3549

36-
@interface FirebaseIndexTableViewDataSource (TableViewDataSource) <UITableViewDataSource>
37-
@end
38-
3950
NS_ASSUME_NONNULL_END

FirebaseDatabaseUI/FirebaseIndexTableViewDataSource.m

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,4 +119,8 @@ - (UITableViewCell *)tableView:(UITableView *)tableView
119119
return cell;
120120
}
121121

122+
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
123+
return self.array.count;
124+
}
125+
122126
@end

0 commit comments

Comments
 (0)