Skip to content

fix data source init having side effects #239

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 10, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion FirebaseDatabaseUI/FUICollection.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

@import Foundation;

@class FUIArray;
@class FIRDatabaseReference, FIRDatabaseQuery, FIRDataSnapshot;
@protocol FUICollectionDelegate;

NS_ASSUME_NONNULL_BEGIN
Expand Down
33 changes: 21 additions & 12 deletions FirebaseDatabaseUI/FUICollectionViewDataSource.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,17 @@

@import UIKit;

#import "FUIDataSource.h"
#import <FirebaseDatabaseUI/FUICollection.h>

NS_ASSUME_NONNULL_BEGIN

@class FIRDatabaseReference;

/**
* FUICollectionViewDataSource provides a class that conforms to the
* UICollectionViewDataSource protocol which allows UICollectionViews to
* adopt FUICollectionViewDataSource in order to provide a UICollectionView
* synchronized to a Firebase reference or query.
*/
@interface FUICollectionViewDataSource : FUIDataSource<UICollectionViewDataSource>
@interface FUICollectionViewDataSource : NSObject <UICollectionViewDataSource>

/**
* The UICollectionView instance that operations (inserts, removals, moves,
Expand All @@ -46,16 +44,19 @@ NS_ASSUME_NONNULL_BEGIN
* The callback to populate a subclass of UICollectionViewCell with an object
* provided by the datasource.
*/
@property(strong, nonatomic, readonly) UICollectionViewCell *(^populateCellAtIndexPath)
@property (strong, nonatomic, readonly) UICollectionViewCell *(^populateCellAtIndexPath)
(UICollectionView *collectionView, NSIndexPath *indexPath, FIRDataSnapshot *object);

/**
* The number of items in the data source.
*/
@property (nonatomic, readonly) NSUInteger count;

/**
* Initialize an instance of FUICollectionViewDataSource that populates
* UICollectionViewCells with FIRDataSnapshots.
* @param collection A FUICollection that the data source uses to pull snapshots
* from Firebase Database.
* @param view An instance of a UICollectionView to bind to. This view
* is not retained by its data source.
* @param populateCell A closure used by the data source to create the cells that
* are displayed in the collection view. This closure is retained by the data
* source, so if you capture self in the closure and also claim ownership of the
Expand All @@ -64,7 +65,6 @@ NS_ASSUME_NONNULL_BEGIN
* UICollectionViewCells with FIRDataSnapshots.
*/
- (instancetype)initWithCollection:(id<FUICollection>)collection
view:(UICollectionView *)view
populateCell:(UICollectionViewCell *(^)(UICollectionView *collectionView,
NSIndexPath *indexPath,
FIRDataSnapshot *object))populateCell NS_DESIGNATED_INITIALIZER;
Expand All @@ -73,8 +73,6 @@ NS_ASSUME_NONNULL_BEGIN
* Initialize an unsorted instance of FUICollectionViewDataSource that populates
* UICollectionViewCells with FIRDataSnapshots.
* @param query A Firebase query to bind the data source to.
* @param collectionView An instance of a UICollectionView to bind to. This view
* is not retained by its data source.
* @param populateCell A closure used by the data source to create the cells that
* are displayed in the collection view. This closure is retained by the data
* source, so if you capture self in the closure and also claim ownership of the
Expand All @@ -83,12 +81,23 @@ NS_ASSUME_NONNULL_BEGIN
* UICollectionViewCells with FIRDataSnapshots.
*/
- (instancetype)initWithQuery:(FIRDatabaseQuery *)query
view:(UICollectionView *)collectionView
populateCell:(UICollectionViewCell *(^)(UICollectionView *collectionView,
NSIndexPath *indexPath,
FIRDataSnapshot *object))populateCell;

- (instancetype)initWithCollection:(id<FUICollection>)collection NS_UNAVAILABLE;
- (instancetype)init NS_UNAVAILABLE;

/**
* Attaches the data source to a collection view and begins sending updates immediately.
* @param view An instance of UICollectionView that the data source should push
* updates to.
*/
- (void)bindToView:(UICollectionView *)view;

/**
* Detaches the data source from a view and stops sending any updates.
*/
- (void)unbind;

@end

Expand Down
39 changes: 30 additions & 9 deletions FirebaseDatabaseUI/FUICollectionViewDataSource.m
Original file line number Diff line number Diff line change
Expand Up @@ -19,33 +19,54 @@
// clang-format on

#import "FUICollectionViewDataSource.h"
#import "FUIArray.h"

@import FirebaseDatabase;

@interface FUICollectionViewDataSource ()

@property (nonatomic, readonly, nonnull) id<FUICollection> collection;

@end

@implementation FUICollectionViewDataSource

#pragma mark - FUIDataSource initializer methods

- (instancetype)initWithCollection:(id<FUICollection>)collection
view:(UICollectionView *)view
populateCell:(UICollectionViewCell * (^)(UICollectionView *,
NSIndexPath *,
FIRDataSnapshot *))populateCell {
self = [super initWithCollection:collection];
self = [super init];
if (self) {
_collectionView = view;
_collection = collection;
_populateCellAtIndexPath = populateCell;
}
return self;
}

- (instancetype)initWithQuery:(FIRDatabaseQuery *)query
view:(UICollectionView *)collectionView
populateCell:(UICollectionViewCell *(^)(UICollectionView *,
NSIndexPath *,
FIRDataSnapshot *))populateCell {
FUIArray *array = [[FUIArray alloc] initWithQuery:query];
return [self initWithCollection:array view:collectionView populateCell:populateCell];
return [self initWithCollection:array populateCell:populateCell];
}

- (NSUInteger)count {
return self.collection.count;
}

- (void)bindToView:(UICollectionView *)view {
self.collectionView = view;
view.dataSource = self;
[self.collection observeQuery];
}

- (void)unbind {
self.collectionView.dataSource = nil;
self.collectionView = nil;
[self.collection invalidate];
}

#pragma mark - FUICollectionDelegate methods
Expand Down Expand Up @@ -75,7 +96,7 @@ - (void)array:(FUIArray *)array didMoveObject:(id)object

- (nonnull UICollectionViewCell *)collectionView:(nonnull UICollectionView *)collectionView
cellForItemAtIndexPath:(nonnull NSIndexPath *)indexPath {
FIRDataSnapshot *snap = [self.items objectAtIndex:indexPath.row];
FIRDataSnapshot *snap = [self.collection.items objectAtIndex:indexPath.item];

UICollectionViewCell *cell = self.populateCellAtIndexPath(collectionView, indexPath, snap);

Expand All @@ -88,7 +109,7 @@ - (NSInteger)numberOfSectionsInCollectionView:(nonnull UICollectionView *)collec

- (NSInteger)collectionView:(nonnull UICollectionView *)collectionView
numberOfItemsInSection:(NSInteger)section {
return self.count;
return self.collection.count;
}

@end
Expand All @@ -100,8 +121,8 @@ - (FUICollectionViewDataSource *)bindToQuery:(FIRDatabaseQuery *)query
NSIndexPath *,
FIRDataSnapshot *))populateCell {
FUICollectionViewDataSource *dataSource =
[[FUICollectionViewDataSource alloc] initWithQuery:query view:self populateCell:populateCell];
self.dataSource = dataSource;
[[FUICollectionViewDataSource alloc] initWithQuery:query populateCell:populateCell];
[dataSource bindToView:self];
return dataSource;
}

Expand Down
65 changes: 0 additions & 65 deletions FirebaseDatabaseUI/FUIDataSource.h

This file was deleted.

74 changes: 0 additions & 74 deletions FirebaseDatabaseUI/FUIDataSource.m

This file was deleted.

Loading