Skip to content

Commit d612ac1

Browse files
committed
cheat at subclassing to expose less public mutability
1 parent cdb1207 commit d612ac1

7 files changed

+41
-36
lines changed

FirebaseDatabaseUI/FUIArray.h

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -137,40 +137,36 @@ NS_ASSUME_NONNULL_BEGIN
137137

138138
/**
139139
* Called when the Firebase query sends a FIRDataEventTypeChildAdded event. Override this
140-
* to provide custom insertion logic.
140+
* to provide custom insertion logic. Don't call this method directly.
141141
* @param snap The snapshot that was inserted.
142142
* @param previous The key of the sibling preceding the inserted snapshot.
143143
*/
144144
- (void)insertSnapshot:(FIRDataSnapshot *)snap withPreviousChildKey:(nullable NSString *)previous;
145145

146146
/**
147147
* Called when the Firebase query sends a FIRDataEventTypeChildRemoved event. Override this
148-
* to provide custom removal logic.
148+
* to provide custom removal logic. Don't call this method directly.
149149
* @param snap The snapshot that was removed.
150150
* @param previous The key of the sibling preceding the removed snapshot.
151151
*/
152152
- (void)removeSnapshot:(FIRDataSnapshot *)snap withPreviousChildKey:(nullable NSString *)previous;
153153

154154
/**
155155
* Called when the Firebase query sends a FIRDataEventTypeChildChanged event. Override this
156-
* to provide custom on change logic.
156+
* to provide custom on change logic. Don't call this method directly.
157157
* @param snap The snapshot whose value was changed.
158158
* @param previous The key of the sibling preceding the changed snapshot.
159159
*/
160160
- (void)changeSnapshot:(FIRDataSnapshot *)snap withPreviousChildKey:(nullable NSString *)previous;
161161

162162
/**
163163
* Called when the Firebase query sends a FIRDataEventTypeChildMoved event. Override this
164-
* to provide custom move logic.
164+
* to provide custom move logic. Don't call this method directly.
165165
* @param snap The snapshot that was moved.
166166
* @param previous The key of the sibling preceding the moved snapshot at its new location.
167167
*/
168168
- (void)moveSnapshot:(FIRDataSnapshot *)snap withPreviousChildKey:(nullable NSString *)previous;
169169

170-
- (void)removeSnapshotAtIndex:(NSUInteger)index;
171-
- (void)insertSnapshot:(FIRDataSnapshot *)snap atIndex:(NSUInteger)index;
172-
- (void)addSnapshot:(FIRDataSnapshot *)snap;
173-
174170
@end
175171

176172
NS_ASSUME_NONNULL_END

FirebaseDatabaseUI/FUIIndexArray.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ - (instancetype)initWithIndex:(id<FUIDataObservable>)index
7171

7272
- (void)observeQueries {
7373
_indexArray = [[FUIArray alloc] initWithQuery:self.index delegate:self];
74+
[_indexArray observeQuery];
7475
}
7576

7677
- (NSArray <FIRDataSnapshot *> *)items {

FirebaseDatabaseUI/FUIIndexTableViewDataSource.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -104,11 +104,11 @@ didFailLoadAtIndex:(NSUInteger)index
104104
* view is in use.
105105
*/
106106
- (FUIIndexTableViewDataSource *)bindToIndexedQuery:(FIRDatabaseQuery *)index
107-
data:(FIRDatabaseReference *)data
108-
delegate:(id<FUIIndexTableViewDataSourceDelegate>)delegate
109-
populateCell:(UITableViewCell *(^)(UITableView *view,
110-
NSIndexPath *indexPath,
111-
FIRDataSnapshot *_Nullable snap))populateCell;
107+
data:(FIRDatabaseReference *)data
108+
delegate:(id<FUIIndexTableViewDataSourceDelegate>)delegate
109+
populateCell:(UITableViewCell *(^)(UITableView *view,
110+
NSIndexPath *indexPath,
111+
FIRDataSnapshot *_Nullable snap))populateCell __attribute__((warn_unused_result));
112112

113113
@end
114114

FirebaseDatabaseUI/FUIIndexTableViewDataSource.m

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ - (instancetype)initWithIndex:(FIRDatabaseQuery *)indexQuery
4848
self = [super init];
4949
if (self != nil) {
5050
_array = [[FUIIndexArray alloc] initWithIndex:indexQuery
51-
data:dataQuery
52-
delegate:self];
51+
data:dataQuery
52+
delegate:self];
5353
_tableView = tableView;
5454
tableView.dataSource = self;
5555
_populateCell = populateCell;
@@ -139,17 +139,17 @@ - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger
139139
@implementation UITableView (FUIIndexTableViewDataSource)
140140

141141
- (FUIIndexTableViewDataSource *)bindToIndexedQuery:(FIRDatabaseQuery *)index
142-
data:(FIRDatabaseReference *)data
143-
delegate:(id<FUIIndexTableViewDataSourceDelegate>)delegate
144-
populateCell:(UITableViewCell *(^)(UITableView *,
145-
NSIndexPath *,
146-
FIRDataSnapshot *))populateCell {
142+
data:(FIRDatabaseReference *)data
143+
delegate:(id<FUIIndexTableViewDataSourceDelegate>)delegate
144+
populateCell:(UITableViewCell *(^)(UITableView *,
145+
NSIndexPath *,
146+
FIRDataSnapshot *))populateCell {
147147
FUIIndexTableViewDataSource *dataSource =
148148
[[FUIIndexTableViewDataSource alloc] initWithIndex:index
149-
data:data
150-
tableView:self
151-
delegate:delegate
152-
populateCell:populateCell];
149+
data:data
150+
tableView:self
151+
delegate:delegate
152+
populateCell:populateCell];
153153
self.dataSource = dataSource;
154154
return dataSource;
155155
}

FirebaseDatabaseUI/FUIQueryObserver.m

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@ - (instancetype)initWithQuery:(id<FUIDataObservable>)query {
4444
}
4545

4646
+ (FUIQueryObserver *)observerForQuery:(id<FUIDataObservable>)query
47-
completion:(void (^)(FUIQueryObserver *obs,
48-
FIRDataSnapshot *snap,
49-
NSError *error))completion {
47+
completion:(void (^)(FUIQueryObserver *obs,
48+
FIRDataSnapshot *snap,
49+
NSError *error))completion {
5050
FUIQueryObserver *obs = [[FUIQueryObserver alloc] initWithQuery:query];
5151

5252
void (^observerBlock)(FIRDataSnapshot *, NSString *) = ^(FIRDataSnapshot *snap,

FirebaseDatabaseUI/FUISortedArray.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ NS_ASSUME_NONNULL_BEGIN
2727
*/
2828
@property (nonatomic, readonly, copy) NSArray<FIRDataSnapshot *> *items;
2929

30-
- (instancetype)initWithQuery:(id<FIRDataObservable>)query NS_UNAVAILABLE;
30+
- (instancetype)initWithQuery:(id<FUIDataObservable>)query NS_UNAVAILABLE;
3131

3232
/**
3333
* Initializes a sorted collection.

FirebaseDatabaseUI/FUISortedArray.m

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ @interface FUISortedArray ()
2323
*/
2424
@property (nonatomic, copy, nonnull) NSComparisonResult (^sortDescriptor)(FIRDataSnapshot *, FIRDataSnapshot *);
2525

26+
/**
27+
* The backing collection that holds all of the array's data.
28+
*/
29+
@property (strong, nonatomic) NSMutableArray<FIRDataSnapshot *> *snapshots;
30+
2631
/**
2732
* A set containing the query observer handles that should be released when
2833
* this array is freed.
@@ -32,6 +37,9 @@ @interface FUISortedArray ()
3237
@end
3338

3439
@implementation FUISortedArray
40+
// Cheating at subclassing, but this @dynamic avoids
41+
// duplicating storage without exposing mutability publicly
42+
@dynamic snapshots;
3543

3644
- (instancetype)initWithQuery:(FIRDatabaseQuery *)query
3745
delegate:(id<FUICollectionDelegate>)delegate
@@ -57,7 +65,7 @@ - (void)removeSnapshot:(FIRDataSnapshot *)snap withPreviousChildKey:(NSString *)
5765
NSInteger index = [self indexForKey:snap.key];
5866
if (index == NSNotFound) { /* error */ return; }
5967

60-
[self removeSnapshotAtIndex:index];
68+
[self.snapshots removeObjectAtIndex:index];
6169
if ([self.delegate respondsToSelector:@selector(array:didRemoveObject:atIndex:)]) {
6270
[self.delegate array:self didRemoveObject:snap atIndex:index];
6371
}
@@ -71,7 +79,7 @@ - (void)changeSnapshot:(FIRDataSnapshot *)snap withPreviousChildKey:(NSString *)
7179

7280
// Since changes can change ordering, model changes as a deletion and an insertion.
7381
FIRDataSnapshot *removed = [self snapshotAtIndex:index];
74-
[self removeSnapshotAtIndex:index];
82+
[self.snapshots removeObjectAtIndex:index];
7583
if ([self.delegate respondsToSelector:@selector(array:didRemoveObject:atIndex:)]) {
7684
[self.delegate array:self didRemoveObject:removed atIndex:index];
7785
}
@@ -92,29 +100,29 @@ - (NSArray *)items {
92100

93101
- (NSInteger)insertSnapshot:(FIRDataSnapshot *)snapshot {
94102
if (self.count == 0) {
95-
[self addSnapshot:snapshot];
103+
[self.snapshots addObject:snapshot];
96104
return 0;
97105
}
98106
if (self.count == 1) {
99107
NSComparisonResult result = self.sortDescriptor(snapshot, [self snapshotAtIndex:0]);
100108
switch (result) {
101109
case NSOrderedDescending:
102-
[self addSnapshot:snapshot];
110+
[self.snapshots addObject:snapshot];
103111
return 1;
104112
default:
105-
[self insertSnapshot:snapshot atIndex:0];
113+
[self.snapshots insertObject:snapshot atIndex:0];
106114
return 0;
107115
}
108116
}
109117

110118
NSInteger index = self.count / 2;
111119
while (index >= 0 && index <= self.count) {
112120
if (index == 0) {
113-
[self insertSnapshot:snapshot atIndex:index];
121+
[self.snapshots insertObject:snapshot atIndex:index];
114122
return 0;
115123
}
116124
if (index == self.count) {
117-
[self addSnapshot:snapshot];
125+
[self.snapshots addObject:snapshot];
118126
return index;
119127
}
120128

@@ -136,7 +144,7 @@ - (NSInteger)insertSnapshot:(FIRDataSnapshot *)snapshot {
136144
NSAssert(NO, @"FUISortedArray %@'s sort descriptor returned inconsistent results!", self);
137145
} else {
138146
// good
139-
[self insertSnapshot:snapshot atIndex:index];
147+
[self.snapshots insertObject:snapshot atIndex:index];
140148
return index;
141149
}
142150
}

0 commit comments

Comments
 (0)