Skip to content

Commit

Permalink
cheat at subclassing to expose less public mutability
Browse files Browse the repository at this point in the history
  • Loading branch information
morganchen12 committed Jan 3, 2017
1 parent a97ebf4 commit 99bc90a
Show file tree
Hide file tree
Showing 10 changed files with 43 additions and 59 deletions.
12 changes: 4 additions & 8 deletions FirebaseDatabaseUI/FUIArray.h
Original file line number Diff line number Diff line change
Expand Up @@ -137,40 +137,36 @@ NS_ASSUME_NONNULL_BEGIN

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

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

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

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

- (void)removeSnapshotAtIndex:(NSUInteger)index;
- (void)insertSnapshot:(FIRDataSnapshot *)snap atIndex:(NSUInteger)index;
- (void)addSnapshot:(FIRDataSnapshot *)snap;

@end

NS_ASSUME_NONNULL_END
1 change: 1 addition & 0 deletions FirebaseDatabaseUI/FUIIndexArray.m
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ - (instancetype)initWithIndex:(id<FUIDataObservable>)index

- (void)observeQueries {
_indexArray = [[FUIArray alloc] initWithQuery:self.index delegate:self];
[_indexArray observeQuery];
}

- (NSArray <FIRDataSnapshot *> *)items {
Expand Down
10 changes: 5 additions & 5 deletions FirebaseDatabaseUI/FUIIndexTableViewDataSource.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,11 @@ didFailLoadAtIndex:(NSUInteger)index
* view is in use.
*/
- (FUIIndexTableViewDataSource *)bindToIndexedQuery:(FIRDatabaseQuery *)index
data:(FIRDatabaseReference *)data
delegate:(id<FUIIndexTableViewDataSourceDelegate>)delegate
populateCell:(UITableViewCell *(^)(UITableView *view,
NSIndexPath *indexPath,
FIRDataSnapshot *_Nullable snap))populateCell;
data:(FIRDatabaseReference *)data
delegate:(id<FUIIndexTableViewDataSourceDelegate>)delegate
populateCell:(UITableViewCell *(^)(UITableView *view,
NSIndexPath *indexPath,
FIRDataSnapshot *_Nullable snap))populateCell __attribute__((warn_unused_result));

@end

Expand Down
22 changes: 11 additions & 11 deletions FirebaseDatabaseUI/FUIIndexTableViewDataSource.m
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ - (instancetype)initWithIndex:(FIRDatabaseQuery *)indexQuery
self = [super init];
if (self != nil) {
_array = [[FUIIndexArray alloc] initWithIndex:indexQuery
data:dataQuery
delegate:self];
data:dataQuery
delegate:self];
_tableView = tableView;
tableView.dataSource = self;
_populateCell = populateCell;
Expand Down Expand Up @@ -139,17 +139,17 @@ - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger
@implementation UITableView (FUIIndexTableViewDataSource)

- (FUIIndexTableViewDataSource *)bindToIndexedQuery:(FIRDatabaseQuery *)index
data:(FIRDatabaseReference *)data
delegate:(id<FUIIndexTableViewDataSourceDelegate>)delegate
populateCell:(UITableViewCell *(^)(UITableView *,
NSIndexPath *,
FIRDataSnapshot *))populateCell {
data:(FIRDatabaseReference *)data
delegate:(id<FUIIndexTableViewDataSourceDelegate>)delegate
populateCell:(UITableViewCell *(^)(UITableView *,
NSIndexPath *,
FIRDataSnapshot *))populateCell {
FUIIndexTableViewDataSource *dataSource =
[[FUIIndexTableViewDataSource alloc] initWithIndex:index
data:data
tableView:self
delegate:delegate
populateCell:populateCell];
data:data
tableView:self
delegate:delegate
populateCell:populateCell];
self.dataSource = dataSource;
return dataSource;
}
Expand Down
6 changes: 3 additions & 3 deletions FirebaseDatabaseUI/FUIQueryObserver.m
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ - (instancetype)initWithQuery:(id<FUIDataObservable>)query {
}

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

void (^observerBlock)(FIRDataSnapshot *, NSString *) = ^(FIRDataSnapshot *snap,
Expand Down
2 changes: 1 addition & 1 deletion FirebaseDatabaseUI/FUISortedArray.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ NS_ASSUME_NONNULL_BEGIN
*/
@property (nonatomic, readonly, copy) NSArray<FIRDataSnapshot *> *items;

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

/**
* Initializes a sorted collection.
Expand Down
24 changes: 16 additions & 8 deletions FirebaseDatabaseUI/FUISortedArray.m
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ @interface FUISortedArray ()
*/
@property (nonatomic, copy, nonnull) NSComparisonResult (^sortDescriptor)(FIRDataSnapshot *, FIRDataSnapshot *);

/**
* The backing collection that holds all of the array's data.
*/
@property (strong, nonatomic) NSMutableArray<FIRDataSnapshot *> *snapshots;

/**
* A set containing the query observer handles that should be released when
* this array is freed.
Expand All @@ -32,6 +37,9 @@ @interface FUISortedArray ()
@end

@implementation FUISortedArray
// Cheating at subclassing, but this @dynamic avoids
// duplicating storage without exposing mutability publicly
@dynamic snapshots;

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

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

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

- (NSInteger)insertSnapshot:(FIRDataSnapshot *)snapshot {
if (self.count == 0) {
[self addSnapshot:snapshot];
[self.snapshots addObject:snapshot];
return 0;
}
if (self.count == 1) {
NSComparisonResult result = self.sortDescriptor(snapshot, [self snapshotAtIndex:0]);
switch (result) {
case NSOrderedDescending:
[self addSnapshot:snapshot];
[self.snapshots addObject:snapshot];
return 1;
default:
[self insertSnapshot:snapshot atIndex:0];
[self.snapshots insertObject:snapshot atIndex:0];
return 0;
}
}

NSInteger index = self.count / 2;
while (index >= 0 && index <= self.count) {
if (index == 0) {
[self insertSnapshot:snapshot atIndex:index];
[self.snapshots insertObject:snapshot atIndex:index];
return 0;
}
if (index == self.count) {
[self addSnapshot:snapshot];
[self.snapshots addObject:snapshot];
return index;
}

Expand All @@ -136,7 +144,7 @@ - (NSInteger)insertSnapshot:(FIRDataSnapshot *)snapshot {
NSAssert(NO, @"FUISortedArray %@'s sort descriptor returned inconsistent results!", self);
} else {
// good
[self insertSnapshot:snapshot atIndex:index];
[self.snapshots insertObject:snapshot atIndex:index];
return index;
}
}
Expand Down
8 changes: 2 additions & 6 deletions samples/objc/FirebaseUI-demo-objc/FUISamplesViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,8 @@ - (void)populateSamples {
return controller;
}]];

<<<<<<< HEAD:samples/objc/FirebaseUI-demo-objc/FUISamplesViewController.m
[samples addObject:[FUISample sampleWithTitle:@"Chat"
=======
[samples addObject:[FIRSample sampleWithTitle:@"Chat"
>>>>>>> change renamed classes in other files, break swift sample:samples/objc/FirebaseUI-demo-objc/FIRSamplesViewController.m
sampleDescription:@"Demonstrates using a FUICollectionViewDataSource to load data from Firebase Database into a UICollectionView for a basic chat app."
sampleDescription:@"Demonstrates using a FUICollectionViewDataSource to load data from Firebase Database into a UICollectionView for a basic chat app."
controller:^UIViewController *{
UIViewController *controller =
[[UIStoryboard storyboardWithName:@"Main"
Expand Down Expand Up @@ -97,7 +93,7 @@ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(N
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
FUISample *sample = _samplesContainer[indexPath.row];
UIViewController *viewController = sample.controllerBlock();

[self.navigationController pushViewController:viewController animated:YES];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,7 @@
@import UIKit;
@import FirebaseDatabase;

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

@interface FUIChatViewController : UIViewController <UITableViewDelegate, UITextFieldDelegate>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,7 @@ class ChatViewController: UIViewController, UICollectionViewDelegateFlowLayout {
// All of the error handling in this controller is done with `fatalError`;
// please don't copy paste it into your production code.

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

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

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

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

0 comments on commit 99bc90a

Please sign in to comment.