Skip to content

Commit 6b2b5b5

Browse files
committed
Update FirebaseUI database's indexed data sources
Change-Id: I32f9409a69dafbde5587505e503c2735dbe8f689
1 parent ef4626d commit 6b2b5b5

File tree

10 files changed

+141
-1481
lines changed

10 files changed

+141
-1481
lines changed

FirebaseAuthUITests/FUIAuthTest.m

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
//
1616

1717
@import XCTest;
18-
@import FirebaseAnalytics;
1918
@import FirebaseAuth;
2019
@import FirebaseAuthUI;
2120
#import <OCMock/OCMock.h>

FirebaseDatabaseUI/FUIIndexArray.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,9 +170,15 @@ didFailLoadWithError:(NSError *)error;
170170
*/
171171
- (nullable FIRDataSnapshot *)objectAtIndex:(NSUInteger)index;
172172

173+
/**
174+
* Starts observing the index array's listeners. The indexed array will pass updates to its delegate
175+
* until the `invalidate` method is called.
176+
*/
177+
- (void)observeQuery;
178+
173179
/**
174180
* Removes all observers from all queries managed by this array and renders this array
175-
* unusable.
181+
* unusable. Initialize a new array instead of reusing this array.
176182
*/
177183
- (void)invalidate;
178184

FirebaseDatabaseUI/FUIIndexArray.m

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ - (instancetype)initWithIndex:(id<FUIDataObservable>)index
5959
_data = data;
6060
_observers = [NSMutableArray array];
6161
_delegate = delegate;
62-
[self observeQueries];
6362
}
6463
return self;
6564
}
@@ -93,6 +92,10 @@ - (NSUInteger)count {
9392
return self.observers.count;
9493
}
9594

95+
- (void)observeQuery {
96+
[self observeQueries];
97+
}
98+
9699
// FUIIndexArray instance becomes unusable after invalidation.
97100
- (void)invalidate {
98101
for (NSInteger i = 0; i < self.observers.count; i++) {

FirebaseDatabaseUI/FUIIndexCollectionViewDataSource.h

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
NS_ASSUME_NONNULL_BEGIN
2121

22-
@class FUIIndexCollectionViewDataSource;
22+
@class FUIIndexCollectionViewDataSource, FUIIndexArray;
2323

2424
@protocol FUIIndexCollectionViewDataSourceDelegate <NSObject>
2525
@optional
@@ -70,6 +70,22 @@ didFailLoadAtIndex:(NSUInteger)index
7070

7171
- (instancetype)init NS_UNAVAILABLE;
7272

73+
/**
74+
* Initializes a collection view data source.
75+
* @param indexArray The FUIIndexArray whose contents will be displayed in the collection view.
76+
* @param collectionView The collection view that is populated by this data source. The
77+
* data source pulls updates from Firebase database, so it must maintain a reference
78+
* to the collection view in order to update its contents as the database pushes updates.
79+
* The collection view is not retained by its data source.
80+
* @param populateCell The closure invoked when populating a UICollectionViewCell (or subclass).
81+
*/
82+
- (instancetype)initWithIndexArray:(FUIIndexArray *)indexArray
83+
delegate:(nullable id<FUIIndexCollectionViewDataSourceDelegate>)delegate
84+
populateCell:(UICollectionViewCell *(^)(UICollectionView *collectionView,
85+
NSIndexPath *indexPath,
86+
FIRDataSnapshot *_Nullable snap))populateCell
87+
NS_DESIGNATED_INITIALIZER;
88+
7389
/**
7490
* Initializes a collection view data source.
7591
* @param indexQuery The Firebase query containing children of the data query.
@@ -84,11 +100,10 @@ didFailLoadAtIndex:(NSUInteger)index
84100
*/
85101
- (instancetype)initWithIndex:(FIRDatabaseQuery *)indexQuery
86102
data:(FIRDatabaseReference *)dataQuery
87-
collectionView:(UICollectionView *)collectionView
88103
delegate:(nullable id<FUIIndexCollectionViewDataSourceDelegate>)delegate
89104
populateCell:(UICollectionViewCell *(^)(UICollectionView *collectionView,
90105
NSIndexPath *indexPath,
91-
FIRDataSnapshot *_Nullable snap))populateCell NS_DESIGNATED_INITIALIZER;
106+
FIRDataSnapshot *_Nullable snap))populateCell;
92107

93108
/**
94109
* Returns the snapshot at the given index, if it has loaded.
@@ -98,6 +113,18 @@ didFailLoadAtIndex:(NSUInteger)index
98113
*/
99114
- (nullable FIRDataSnapshot *)snapshotAtIndex:(NSInteger)index;
100115

116+
/**
117+
* Attaches the data source to a collection view and begins sending updates immediately.
118+
* @param view An instance of UICollectionView that the data source should push
119+
* updates to.
120+
*/
121+
- (void)bindToView:(UICollectionView *)view;
122+
123+
/**
124+
* Detaches the data source from a view and stops sending any updates.
125+
*/
126+
- (void)unbind;
127+
101128
@end
102129

103130
@interface UICollectionView (FUIIndexCollectionViewDataSource)

FirebaseDatabaseUI/FUIIndexCollectionViewDataSource.m

Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
@interface FUIIndexCollectionViewDataSource () <FUIIndexArrayDelegate>
2222

2323
@property (nonatomic, readonly, nonnull) FUIIndexArray *array;
24-
@property (nonatomic, readonly, weak) UICollectionView *collectionView;
24+
@property (nonatomic, weak, nullable) UICollectionView *collectionView;
2525

2626
@property (nonatomic, readonly, copy) UICollectionViewCell *(^populateCell)
2727
(UICollectionView *collectionView, NSIndexPath *indexPath, FIRDataSnapshot *object);
@@ -32,17 +32,24 @@ @implementation FUIIndexCollectionViewDataSource
3232

3333
- (instancetype)initWithIndex:(FIRDatabaseQuery *)indexQuery
3434
data:(FIRDatabaseReference *)dataQuery
35-
collectionView:(UICollectionView *)collectionView
35+
delegate:(id<FUIIndexCollectionViewDataSourceDelegate>)delegate
36+
populateCell:(UICollectionViewCell *(^)(UICollectionView *collectionView,
37+
NSIndexPath *indexPath,
38+
FIRDataSnapshot *snap))populateCell {
39+
FUIIndexArray *array = [[FUIIndexArray alloc] initWithIndex:indexQuery
40+
data:dataQuery
41+
delegate:self];
42+
return [self initWithIndexArray:array delegate:delegate populateCell:populateCell];
43+
}
44+
45+
- (instancetype)initWithIndexArray:(FUIIndexArray *)indexArray
3646
delegate:(id<FUIIndexCollectionViewDataSourceDelegate>)delegate
3747
populateCell:(UICollectionViewCell *(^)(UICollectionView *collectionView,
3848
NSIndexPath *indexPath,
3949
FIRDataSnapshot *snap))populateCell {
4050
self = [super init];
4151
if (self != nil) {
42-
_array = [[FUIIndexArray alloc] initWithIndex:indexQuery
43-
data:dataQuery
44-
delegate:self];
45-
_collectionView = collectionView;
52+
_array = indexArray;
4653
_collectionView.dataSource = self;
4754
_populateCell = populateCell;
4855
_delegate = delegate;
@@ -58,6 +65,18 @@ - (FIRDataSnapshot *)snapshotAtIndex:(NSInteger)index {
5865
return [self.array objectAtIndex:index];
5966
}
6067

68+
- (void)bindToView:(UICollectionView *)view {
69+
self.collectionView = view;
70+
view.dataSource = self;
71+
[self.array observeQuery];
72+
}
73+
74+
- (void)unbind {
75+
[self.array invalidate];
76+
self.collectionView.dataSource = nil;
77+
self.collectionView = nil;
78+
}
79+
6180
#pragma mark - FUIIndexArrayDelegate
6281

6382
- (void)array:(FUIIndexArray *)array
@@ -115,7 +134,8 @@ - (void)array:(FUIIndexArray *)array
115134

116135
#pragma mark - UICollectionViewDataSource
117136

118-
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
137+
- (NSInteger)collectionView:(UICollectionView *)collectionView
138+
numberOfItemsInSection:(NSInteger)section {
119139
return self.array.count;
120140
}
121141

@@ -131,18 +151,17 @@ - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
131151
@implementation UICollectionView (FUIIndexCollectionViewDataSource)
132152

133153
- (FUIIndexCollectionViewDataSource *)bindToIndexedQuery:(FIRDatabaseQuery *)index
134-
data:(FIRDatabaseReference *)data
135-
delegate:(id<FUIIndexCollectionViewDataSourceDelegate>)delegate
136-
populateCell:(UICollectionViewCell *(^)(UICollectionView *,
137-
NSIndexPath *,
138-
FIRDataSnapshot *))populateCell {
154+
data:(FIRDatabaseReference *)data
155+
delegate:(id<FUIIndexCollectionViewDataSourceDelegate>)delegate
156+
populateCell:(UICollectionViewCell *(^)(UICollectionView *,
157+
NSIndexPath *,
158+
FIRDataSnapshot *))populateCell {
139159
FUIIndexCollectionViewDataSource *dataSource =
140160
[[FUIIndexCollectionViewDataSource alloc] initWithIndex:index
141-
data:data
142-
collectionView:self
143-
delegate:delegate
144-
populateCell:populateCell];
145-
self.dataSource = dataSource;
161+
data:data
162+
delegate:delegate
163+
populateCell:populateCell];
164+
[dataSource bindToView:self];
146165
return dataSource;
147166
}
148167

FirebaseDatabaseUI/FUIIndexTableViewDataSource.h

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
NS_ASSUME_NONNULL_BEGIN
2121

22-
@class FUIIndexTableViewDataSource;
22+
@class FUIIndexTableViewDataSource, FUIIndexArray;
2323

2424
@protocol FUIIndexTableViewDataSourceDelegate <NSObject>
2525
@optional
@@ -74,7 +74,7 @@ didFailLoadAtIndex:(NSUInteger)index
7474
* Initializes a table view data source.
7575
* @param indexQuery The Firebase query containing children of the data query.
7676
* @param dataQuery The reference whose children correspond to the contents of the
77-
* index query. This reference's children's contents are served as teh contents
77+
* index query. This reference's children's contents are served as the contents
7878
* of the table view that adopts this data source.
7979
* @param tableView The table view that is populated by this data source. The
8080
* data source pulls updates from Firebase database, so it must maintain a reference
@@ -84,11 +84,26 @@ didFailLoadAtIndex:(NSUInteger)index
8484
*/
8585
- (instancetype)initWithIndex:(FIRDatabaseQuery *)indexQuery
8686
data:(FIRDatabaseReference *)dataQuery
87-
tableView:(UITableView *)tableView
8887
delegate:(nullable id<FUIIndexTableViewDataSourceDelegate>)delegate
8988
populateCell:(UITableViewCell *(^)(UITableView *tableView,
9089
NSIndexPath *indexPath,
91-
FIRDataSnapshot *_Nullable snap))populateCell NS_DESIGNATED_INITIALIZER;
90+
FIRDataSnapshot *_Nullable snap))populateCell;
91+
92+
/**
93+
* Initializes a table view data source.
94+
* @param indexArray The FUIIndexArray whose contents will be displayed in the table view.
95+
* @param tableView The table view that is populated by this data source. The
96+
* data source pulls updates from Firebase database, so it must maintain a reference
97+
* to the table view in order to update its contents as the database pushes updates.
98+
* The table view is not retained by its data source.
99+
* @param populateCell The closure invoked when populating a UITableViewCell (or subclass).
100+
*/
101+
- (instancetype)initWithIndexArray:(FUIIndexArray *)indexArray
102+
delegate:(nullable id<FUIIndexTableViewDataSourceDelegate>)delegate
103+
populateCell:(UITableViewCell *(^)(UITableView *tableView,
104+
NSIndexPath *indexPath,
105+
FIRDataSnapshot *_Nullable snap))populateCell
106+
NS_DESIGNATED_INITIALIZER;
92107

93108
/**
94109
* Returns the snapshot at the given index, if it has loaded.
@@ -98,6 +113,18 @@ didFailLoadAtIndex:(NSUInteger)index
98113
*/
99114
- (nullable FIRDataSnapshot *)snapshotAtIndex:(NSInteger)index;
100115

116+
/**
117+
* Attaches the data source to a table view and begins sending updates immediately.
118+
* @param view An instance of UITableView that the data source should push
119+
* updates to.
120+
*/
121+
- (void)bindToView:(UITableView *)view;
122+
123+
/**
124+
* Detaches the data source from a view and stops sending any updates.
125+
*/
126+
- (void)unbind;
127+
101128
@end
102129

103130
@interface UITableView (FUIIndexTableViewDataSource)

FirebaseDatabaseUI/FUIIndexTableViewDataSource.m

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
@interface FUIIndexTableViewDataSource () <FUIIndexArrayDelegate>
2222

2323
@property (nonatomic, readonly, nonnull) FUIIndexArray *array;
24-
@property (nonatomic, readonly, weak) UITableView *tableView;
24+
@property (nonatomic, weak, nullable) UITableView *tableView;
2525

2626
@property (nonatomic, readonly, copy) UITableViewCell *(^populateCell)
2727
(UITableView *tableView, NSIndexPath *indexPath, FIRDataSnapshot *snap);
@@ -38,26 +38,32 @@ - (instancetype)init {
3838
@throw e;
3939
}
4040

41-
- (instancetype)initWithIndex:(FIRDatabaseQuery *)indexQuery
42-
data:(FIRDatabaseReference *)dataQuery
43-
tableView:(UITableView *)tableView
44-
delegate:(nullable id<FUIIndexTableViewDataSourceDelegate>)delegate
45-
populateCell:(UITableViewCell *(^)(UITableView *tableView,
46-
NSIndexPath *indexPath,
47-
FIRDataSnapshot *snap))populateCell {
41+
- (instancetype)initWithIndexArray:(FUIIndexArray *)indexArray
42+
delegate:(nullable id<FUIIndexTableViewDataSourceDelegate>)delegate
43+
populateCell:(UITableViewCell *(^)(UITableView *tableView,
44+
NSIndexPath *indexPath,
45+
FIRDataSnapshot *snap))populateCell {
4846
self = [super init];
4947
if (self != nil) {
50-
_array = [[FUIIndexArray alloc] initWithIndex:indexQuery
51-
data:dataQuery
52-
delegate:self];
53-
_tableView = tableView;
54-
tableView.dataSource = self;
48+
_array = indexArray;
5549
_populateCell = populateCell;
5650
_delegate = delegate;
5751
}
5852
return self;
5953
}
6054

55+
- (instancetype)initWithIndex:(FIRDatabaseQuery *)indexQuery
56+
data:(FIRDatabaseReference *)dataQuery
57+
delegate:(nullable id<FUIIndexTableViewDataSourceDelegate>)delegate
58+
populateCell:(UITableViewCell *(^)(UITableView *tableView,
59+
NSIndexPath *indexPath,
60+
FIRDataSnapshot *snap))populateCell {
61+
FUIIndexArray *array = [[FUIIndexArray alloc] initWithIndex:indexQuery
62+
data:dataQuery
63+
delegate:self];
64+
return [self initWithIndexArray:array delegate:delegate populateCell:populateCell];
65+
}
66+
6167
- (NSArray<FIRDataSnapshot *> *)indexes {
6268
return self.array.indexes;
6369
}
@@ -66,6 +72,18 @@ - (FIRDataSnapshot *)snapshotAtIndex:(NSInteger)index {
6672
return [self.array objectAtIndex:index];
6773
}
6874

75+
- (void)bindToView:(UITableView *)view {
76+
self.tableView = view;
77+
view.dataSource = self;
78+
[self.array observeQuery];
79+
}
80+
81+
- (void)unbind {
82+
[self.array invalidate];
83+
self.tableView.dataSource = nil;
84+
self.tableView = nil;
85+
}
86+
6987
#pragma mark - FUIIndexArrayDelegate
7088

7189
- (void)array:(FUIIndexArray *)array
@@ -155,10 +173,9 @@ - (FUIIndexTableViewDataSource *)bindToIndexedQuery:(FIRDatabaseQuery *)index
155173
FUIIndexTableViewDataSource *dataSource =
156174
[[FUIIndexTableViewDataSource alloc] initWithIndex:index
157175
data:data
158-
tableView:self
159176
delegate:delegate
160177
populateCell:populateCell];
161-
self.dataSource = dataSource;
178+
[dataSource bindToView:self];
162179
return dataSource;
163180
}
164181

FirebaseDatabaseUITests/FUIIndexArrayTest.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ - (void)setUp {
6464
self.arrayDelegate = [[FUIIndexArrayTestDelegate alloc] init];
6565
self.array.delegate = self.arrayDelegate;
6666
self.dict = [database() mutableCopy];
67+
[self.array observeQuery];
6768
}
6869

6970
- (void)tearDown {

0 commit comments

Comments
 (0)