Skip to content

Commit 91ef2e1

Browse files
committed
cheat at subclassing to expose less public mutability
1 parent 9367ced commit 91ef2e1

File tree

10 files changed

+43
-59
lines changed

10 files changed

+43
-59
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
}

samples/objc/FirebaseUI-demo-objc/FUISamplesViewController.m

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,8 @@ - (void)populateSamples {
4949
return controller;
5050
}]];
5151

52-
<<<<<<< HEAD:samples/objc/FirebaseUI-demo-objc/FUISamplesViewController.m
5352
[samples addObject:[FUISample sampleWithTitle:@"Chat"
54-
=======
55-
[samples addObject:[FIRSample sampleWithTitle:@"Chat"
56-
>>>>>>> change renamed classes in other files, break swift sample:samples/objc/FirebaseUI-demo-objc/FIRSamplesViewController.m
57-
sampleDescription:@"Demonstrates using a FUICollectionViewDataSource to load data from Firebase Database into a UICollectionView for a basic chat app."
53+
sampleDescription:@"Demonstrates using a FUICollectionViewDataSource to load data from Firebase Database into a UICollectionView for a basic chat app."
5854
controller:^UIViewController *{
5955
UIViewController *controller =
6056
[[UIStoryboard storyboardWithName:@"Main"
@@ -97,7 +93,7 @@ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(N
9793
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
9894
FUISample *sample = _samplesContainer[indexPath.row];
9995
UIViewController *viewController = sample.controllerBlock();
100-
96+
10197
[self.navigationController pushViewController:viewController animated:YES];
10298
}
10399

samples/objc/FirebaseUI-demo-objc/Samples/Chat/FUIChatViewController.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,7 @@
1717
@import UIKit;
1818
@import FirebaseDatabase;
1919

20-
<<<<<<< HEAD:samples/objc/FirebaseUI-demo-objc/Samples/Chat/FUIChatViewController.h
2120
#import <FirebaseDatabaseUI/FirebaseDatabaseUI.h>
22-
=======
23-
#import <FirebaseDatabaseUI/FUITableViewDataSource.h>
24-
>>>>>>> change renamed classes in other files, break swift sample:samples/objc/FirebaseUI-demo-objc/Samples/Chat/FIRChatViewController.h
2521

2622
@interface FUIChatViewController : UIViewController <UITableViewDelegate, UITextFieldDelegate>
2723

samples/swift/FirebaseUI-demo-swift/Samples/Chat/ChatViewController.swift

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,7 @@ class ChatViewController: UIViewController, UICollectionViewDelegateFlowLayout {
2626
// All of the error handling in this controller is done with `fatalError`;
2727
// please don't copy paste it into your production code.
2828

29-
<<<<<<< HEAD:samples/swift/FirebaseUI-demo-swift/Samples/Chat/ChatViewController.swift
3029
fileprivate static let reuseIdentifier = "ChatCollectionViewCell"
31-
=======
32-
fileprivate static let reuseIdentifier = "FIRChatCollectionViewCell"
33-
>>>>>>> change renamed classes in other files, break swift sample:samples/swift/FirebaseUI-demo-swift/Samples/Chat/FIRChatViewController.swift
3430

3531
@IBOutlet fileprivate var collectionView: UICollectionView!
3632
@IBOutlet fileprivate var textView: UITextView! {
@@ -69,13 +65,8 @@ class ChatViewController: UIViewController, UICollectionViewDelegateFlowLayout {
6965
FUICollectionViewDataSource(query: self.query!,
7066
view: self.collectionView,
7167
populateCell: { (view, indexPath, snap) -> UICollectionViewCell in
72-
<<<<<<< HEAD:samples/swift/FirebaseUI-demo-swift/Samples/Chat/ChatViewController.swift
7368
let cell = view.dequeueReusableCell(withReuseIdentifier: ChatViewController.reuseIdentifier,
7469
for: indexPath) as! ChatCollectionViewCell
75-
=======
76-
let cell = view.dequeueReusableCell(withReuseIdentifier: FIRChatViewController.reuseIdentifier,
77-
for: indexPath) as! FIRChatCollectionViewCell
78-
>>>>>>> change renamed classes in other files, break swift sample:samples/swift/FirebaseUI-demo-swift/Samples/Chat/FIRChatViewController.swift
7970
let chat = Chat(snapshot: snap)!
8071
cell.populateCellWithChat(chat, user: self.user, maxWidth: self.view.frame.size.width)
8172
return cell
@@ -198,11 +189,7 @@ class ChatViewController: UIViewController, UICollectionViewDelegateFlowLayout {
198189
let blob = self.collectionViewDataSource.object(at: UInt((indexPath as NSIndexPath).row)) as! FIRDataSnapshot
199190
let text = Chat(snapshot: blob)!.text
200191

201-
<<<<<<< HEAD:samples/swift/FirebaseUI-demo-swift/Samples/Chat/ChatViewController.swift
202192
let rect = ChatCollectionViewCell.boundingRectForText(text, maxWidth: width)
203-
=======
204-
let rect = FIRChatCollectionViewCell.boundingRectForText(text, maxWidth: width)
205-
>>>>>>> change renamed classes in other files, break swift sample:samples/swift/FirebaseUI-demo-swift/Samples/Chat/FIRChatViewController.swift
206193

207194
let height = CGFloat(ceil(Double(rect.size.height))) + heightPadding
208195
return CGSize(width: width, height: height)

0 commit comments

Comments
 (0)