Skip to content

Commit 133e2eb

Browse files
committed
add data source classes
1 parent acb137b commit 133e2eb

7 files changed

+332
-987
lines changed

FirebaseDatabaseUI/FirebaseIndexArray.h

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ didLoadObject:(FIRDataSnapshot *)object
5353
* [FIRDataEventTypeChildAdded](https://www.firebase.com/docs/ios/guide/retrieving-data.html#section-event-types)
5454
* event being raised.
5555
* @param ref The database reference added to the array
56-
* @param index The index the child was added at
56+
* @param index The index the reference was added at
5757
*/
5858
- (void)array:(FirebaseIndexArray *)array didAddReference:(FIRDatabaseReference *)ref atIndex:(NSUInteger)index;
5959

@@ -64,7 +64,7 @@ didLoadObject:(FIRDataSnapshot *)object
6464
* [FIRDataEventTypeChildChanged](https://www.firebase.com/docs/ios/guide/retrieving-data.html#section-event-types)
6565
* event being raised.
6666
* @param object The database reference that changed in the array
67-
* @param index The index the child was changed at
67+
* @param index The index the reference was changed at
6868
*/
6969
- (void)array:(FirebaseIndexArray *)array didChangeReference:(FIRDatabaseReference *)ref atIndex:(NSUInteger)index;
7070

@@ -75,7 +75,7 @@ didLoadObject:(FIRDataSnapshot *)object
7575
* [FIRDataEventTypeChildRemoved](https://www.firebase.com/docs/ios/guide/retrieving-data.html#section-event-types)
7676
* event being raised.
7777
* @param object The database reference removed from the array
78-
* @param index The index the child was removed at
78+
* @param index The index the reference was removed at
7979
*/
8080
- (void)array:(FirebaseIndexArray *)array didRemoveReference:(FIRDatabaseReference *)ref atIndex:(NSUInteger)index;
8181

@@ -86,8 +86,8 @@ didLoadObject:(FIRDataSnapshot *)object
8686
* [FIRDataEventTypeChildMoved](https://www.firebase.com/docs/ios/guide/retrieving-data.html#section-event-types)
8787
* event being raised.
8888
* @param object The database reference that has moved locations
89-
* @param fromIndex The index the child is being moved from
90-
* @param toIndex The index the child is being moved to
89+
* @param fromIndex The index the reference is being moved from
90+
* @param toIndex The index the reference is being moved to
9191
*/
9292
- (void)array:(FirebaseIndexArray *)array didMoveReference:(FIRDatabaseReference *)ref fromIndex:(NSUInteger)fromIndex toIndex:(NSUInteger)toIndex;
9393

@@ -134,6 +134,13 @@ didLoadObject:(FIRDataSnapshot *)object
134134
- (instancetype)initWithIndex:(id<FIRDataObservable>)index
135135
data:(id<FIRDataObservable>)data;
136136

137+
/**
138+
* Returns the snapshot at the given index, if it has loaded.
139+
* @param index The index of the requested snapshot.
140+
* @return A snapshot, or nil if one has not yet been loaded.
141+
*/
142+
- (nullable FIRDataSnapshot *)objectAtIndex:(NSUInteger)index;
143+
137144
/**
138145
* Removes all observers from all queries managed by this array and renders this array
139146
* unusable.

FirebaseDatabaseUI/FirebaseIndexArray.m

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,10 @@ - (void)invalidate {
8585
_observers = nil;
8686
}
8787

88+
- (FIRDataSnapshot *)objectAtIndex:(NSUInteger)index {
89+
return self.observers[index].contents;
90+
}
91+
8892
- (void)dealloc {
8993
[self invalidate];
9094
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
//
2+
// Copyright (c) 2016 Google Inc.
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
//
16+
17+
@import UIKit;
18+
19+
@import FirebaseDatabase;
20+
21+
NS_ASSUME_NONNULL_BEGIN
22+
23+
@interface FirebaseIndexCollectionViewDataSource : NSObject
24+
25+
- (instancetype)init NS_UNAVAILABLE;
26+
27+
- (instancetype)initWithIndex:(FIRDatabaseQuery *)indexQuery
28+
data:(FIRDatabaseReference *)dataQuery
29+
collectionView:(UICollectionView *)collectionView
30+
identifier:(NSString *)cellIdentifier
31+
populateCell:(void (^)(UICollectionViewCell *cell,
32+
FIRDataSnapshot *_Nullable))populateCell NS_DESIGNATED_INITIALIZER;
33+
34+
@end
35+
36+
@interface FirebaseIndexCollectionViewDataSource (CollectionViewDataSource) <UICollectionViewDataSource>
37+
@end
38+
39+
NS_ASSUME_NONNULL_END
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
//
2+
// Copyright (c) 2016 Google Inc.
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
//
16+
17+
#import "FirebaseIndexCollectionViewDataSource.h"
18+
19+
#import "FirebaseIndexArray.h"
20+
21+
@interface FirebaseIndexCollectionViewDataSource () <FirebaseIndexArrayDelegate>
22+
23+
@property (nonatomic, readonly, nonnull) FirebaseIndexArray *array;
24+
@property (nonatomic, readonly, weak) UICollectionView *collectionView;
25+
@property (nonatomic, readonly, copy) NSString *identifier;
26+
27+
@property (nonatomic, readonly, copy) void (^populateCell)(UICollectionViewCell *, FIRDataSnapshot *);
28+
29+
@end
30+
31+
@implementation FirebaseIndexCollectionViewDataSource
32+
33+
- (instancetype)initWithIndex:(FIRDatabaseQuery *)indexQuery
34+
data:(FIRDatabaseReference *)dataQuery
35+
collectionView:(UICollectionView *)collectionView
36+
identifier:(NSString *)cellIdentifier
37+
populateCell:(void (^)(UICollectionViewCell *,
38+
FIRDataSnapshot *))populateCell {
39+
self = [super init];
40+
if (self != nil) {
41+
_array = [[FirebaseIndexArray alloc] initWithIndex:indexQuery
42+
data:dataQuery
43+
delegate:self];
44+
_collectionView = collectionView;
45+
_collectionView.dataSource = self;
46+
_identifier = [cellIdentifier copy];
47+
_populateCell = populateCell;
48+
}
49+
return self;
50+
}
51+
52+
#pragma mark - FirebaseIndexArrayDelegate
53+
54+
- (void)array:(FirebaseIndexArray *)array queryCancelledWithError:(NSError *)error {
55+
// TODO: implement actual error handling
56+
NSLog(@"%@ Error: Firebase query cancelled with error %@", self, error);
57+
}
58+
59+
- (void)array:(FirebaseIndexArray *)array
60+
didAddReference:(FIRDatabaseReference *)ref
61+
atIndex:(NSUInteger)index {
62+
[self.collectionView
63+
insertItemsAtIndexPaths:@[ [NSIndexPath indexPathForItem:index inSection:0] ]];
64+
}
65+
66+
- (void)array:(FirebaseIndexArray *)array
67+
didChangeReference:(FIRDatabaseReference *)ref
68+
atIndex:(NSUInteger)index {
69+
[self.collectionView
70+
reloadItemsAtIndexPaths:@[ [NSIndexPath indexPathForItem:index inSection:0] ]];
71+
}
72+
73+
- (void)array:(FirebaseIndexArray *)array
74+
didRemoveReference:(FIRDatabaseReference *)ref
75+
atIndex:(NSUInteger)index {
76+
[self.collectionView
77+
deleteItemsAtIndexPaths:@[ [NSIndexPath indexPathForItem:index inSection:0] ]];
78+
}
79+
80+
- (void)array:(FirebaseIndexArray *)array
81+
didMoveReference:(FIRDatabaseReference *)ref
82+
fromIndex:(NSUInteger)fromIndex
83+
toIndex:(NSUInteger)toIndex {
84+
[self.collectionView moveItemAtIndexPath:[NSIndexPath indexPathForItem:fromIndex inSection:0]
85+
toIndexPath:[NSIndexPath indexPathForItem:toIndex inSection:0]];
86+
}
87+
88+
- (void)array:(FirebaseIndexArray *)array
89+
reference:(FIRDatabaseReference *)ref
90+
didLoadObject:(FIRDataSnapshot *)object
91+
atIndex:(NSUInteger)index {
92+
NSIndexPath *path = [NSIndexPath indexPathForRow:index inSection:0];
93+
[self.collectionView reloadItemsAtIndexPaths:@[path]];
94+
}
95+
96+
@end
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
//
2+
// Copyright (c) 2016 Google Inc.
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
//
16+
17+
@import UIKit;
18+
19+
@import FirebaseDatabase;
20+
21+
NS_ASSUME_NONNULL_BEGIN
22+
23+
@interface FirebaseIndexTableViewDataSource : NSObject
24+
25+
- (instancetype)init NS_UNAVAILABLE;
26+
27+
- (instancetype)initWithIndex:(FIRDatabaseQuery *)indexQuery
28+
data:(FIRDatabaseReference *)dataQuery
29+
tableView:(UITableView *)tableView
30+
cellReuseIdentifier:(NSString *)cellIdentifier
31+
populateCell:(void (^)(UITableViewCell *cell,
32+
FIRDataSnapshot *_Nullable))populateCell NS_DESIGNATED_INITIALIZER;
33+
34+
@end
35+
36+
@interface FirebaseIndexTableViewDataSource (TableViewDataSource) <UITableViewDataSource>
37+
@end
38+
39+
NS_ASSUME_NONNULL_END
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
//
2+
// Copyright (c) 2016 Google Inc.
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
//
16+
17+
#import "FirebaseIndexTableViewDataSource.h"
18+
19+
#import "FirebaseIndexArray.h"
20+
21+
@interface FirebaseIndexTableViewDataSource () <FirebaseIndexArrayDelegate>
22+
23+
@property (nonatomic, readonly, nonnull) FirebaseIndexArray *array;
24+
@property (nonatomic, readonly, weak) UITableView *tableView;
25+
@property (nonatomic, readonly, copy) NSString *identifier;
26+
27+
@property (nonatomic, readonly, copy) void (^populateCell)(UITableViewCell *, FIRDataSnapshot *);
28+
29+
@end
30+
31+
@implementation FirebaseIndexTableViewDataSource
32+
33+
- (instancetype)init {
34+
NSException *e =
35+
[NSException exceptionWithName:@"FIRUnavailableMethodException"
36+
reason:@"-init is unavailable. Please use the designated initializer instead."
37+
userInfo:nil];
38+
@throw e;
39+
}
40+
41+
- (instancetype)initWithIndex:(FIRDatabaseQuery *)indexQuery
42+
data:(FIRDatabaseReference *)dataQuery
43+
tableView:(UITableView *)tableView
44+
cellReuseIdentifier:(NSString *)cellIdentifier
45+
populateCell:(nonnull void (^)(UITableViewCell *cell,
46+
FIRDataSnapshot *data))populateCell {
47+
self = [super init];
48+
if (self != nil) {
49+
_array = [[FirebaseIndexArray alloc] initWithIndex:indexQuery
50+
data:dataQuery
51+
delegate:self];
52+
_tableView = tableView;
53+
tableView.dataSource = self;
54+
_identifier = [cellIdentifier copy];
55+
_populateCell = populateCell;
56+
}
57+
return self;
58+
}
59+
60+
#pragma mark - FirebaseIndexArrayDelegate
61+
62+
- (void)array:(FirebaseIndexArray *)array queryCancelledWithError:(NSError *)error {
63+
// TODO: implement actual error handling
64+
NSLog(@"%@ Error: Firebase query cancelled with error %@", self, error);
65+
}
66+
67+
- (void)array:(FirebaseIndexArray *)array
68+
didAddReference:(FIRDatabaseReference *)ref
69+
atIndex:(NSUInteger)index {
70+
[self.tableView beginUpdates];
71+
[self.tableView insertRowsAtIndexPaths:@[ [NSIndexPath indexPathForRow:index inSection:0] ]
72+
withRowAnimation:UITableViewRowAnimationAutomatic];
73+
[self.tableView endUpdates];
74+
}
75+
76+
- (void)array:(FirebaseIndexArray *)array
77+
didChangeReference:(FIRDatabaseReference *)ref
78+
atIndex:(NSUInteger)index {
79+
[self.tableView beginUpdates];
80+
[self.tableView reloadRowsAtIndexPaths:@[ [NSIndexPath indexPathForRow:index inSection:0] ]
81+
withRowAnimation:UITableViewRowAnimationAutomatic];
82+
[self.tableView endUpdates];
83+
}
84+
85+
- (void)array:(FirebaseIndexArray *)array
86+
didRemoveReference:(FIRDatabaseReference *)ref
87+
atIndex:(NSUInteger)index {
88+
[self.tableView beginUpdates];
89+
[self.tableView deleteRowsAtIndexPaths:@[ [NSIndexPath indexPathForRow:index inSection:0] ]
90+
withRowAnimation:UITableViewRowAnimationAutomatic];
91+
[self.tableView endUpdates];
92+
}
93+
94+
- (void)array:(FirebaseIndexArray *)array
95+
didMoveReference:(FIRDatabaseReference *)ref
96+
fromIndex:(NSUInteger)fromIndex
97+
toIndex:(NSUInteger)toIndex {
98+
[self.tableView beginUpdates];
99+
[self.tableView moveRowAtIndexPath:[NSIndexPath indexPathForRow:fromIndex inSection:0]
100+
toIndexPath:[NSIndexPath indexPathForRow:toIndex inSection:0]];
101+
[self.tableView endUpdates];
102+
}
103+
104+
- (void)array:(FirebaseIndexArray *)array
105+
reference:(FIRDatabaseReference *)ref
106+
didLoadObject:(FIRDataSnapshot *)object
107+
atIndex:(NSUInteger)index {
108+
NSIndexPath *path = [NSIndexPath indexPathForRow:index inSection:0];
109+
[self.tableView reloadRowsAtIndexPaths:@[path] withRowAnimation:UITableViewRowAnimationAutomatic];
110+
}
111+
112+
#pragma mark - UITableViewDataSource
113+
114+
- (UITableViewCell *)tableView:(UITableView *)tableView
115+
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
116+
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:self.identifier];
117+
FIRDataSnapshot *snap = [self.array objectAtIndex:indexPath.row];
118+
self.populateCell(cell, snap);
119+
return cell;
120+
}
121+
122+
@end

0 commit comments

Comments
 (0)