Skip to content

Added support for using NSSort Descriptors & NSPredicate & Sections #12

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

Closed
wants to merge 5 commits into from
Closed
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
89 changes: 78 additions & 11 deletions FirebaseUI/API/FirebaseArray.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
* The delegate object that array changes are surfaced to.
*/
@property(strong, nonatomic) NSMutableArray __GENERIC(FDataSnapshot *) * snapshots;
@property(strong, nonatomic) NSMutableOrderedSet __GENERIC(id) * sectionValues;

#pragma mark -
#pragma mark Initializer methods
Expand All @@ -79,6 +80,60 @@
*/
- (instancetype)initWithQuery:(FQuery *)query;

/**
* Initializes FirebaseArray with a Firebase query (FQuery), an array of NSSortDescriptors, and an
* NSPredicate.
* Use this if you would like the array to be sorted after being received from the server, or if
* you would like more complex sorting or filtering behavior than an FQuery can provide.
* @param query A query on a Firebase reference which provides filtered data to FirebaseArray
* @param predicate The predicate by which the snapshots are filtered. If predicate is nil, the array
* reflects all results from the Firebase Query or Reference.
* @return The instance of FirebaseArray
*/
- (instancetype)initWithQuery:(FQuery *)query
predicate:(NSPredicate *)predicate;


/**
* Initializes FirebaseArray with a Firebase query (FQuery), an array of NSSortDescriptors, and an
* NSPredicate.
* Use this if you would like the array to be sorted after being received from the server, or if
* you would like more complex sorting or filtering behavior than an FQuery can provide.
* @param query A query on a Firebase reference which provides filtered data to FirebaseArray
* @param sortDescriptors The sort descriptors by which the array should be ordered. If the array is
* empty or nil, the array is ordered by [snapshot1.key compare:snapshot2.key]
* @param predicate The predicate by which the snapshots are filtered. If predicate is nil, the array
* reflects all results from the Firebase Query or Reference.
* @return The instance of FirebaseArray
*/
- (instancetype)initWithQuery:(FQuery *)query
sortDescriptors:(NSArray *)sortDescriptors
predicate:(NSPredicate *)predicate;

/**
* Initializes FirebaseArray with a standard Firebase reference and an array of NSSortDescriptors.
* Use this if you would like the array to be sorted after being received from the server, or if
* you would like more complex sorting behavior.
* @param ref The Firebase reference which provides data to FirebaseArray
* @param sortDescriptors The sort descriptors by which the array should be ordered. If the array is
* empty or nil, the array is ordered by [snapshot1.key compare:snapshot2.key]
* @return The instance of FirebaseArray
*/
-(instancetype)initWithRef:(Firebase *)ref sortDescriptors:(NSArray *)sortDescriptors;

/**
* Initializes FirebaseArray with a Firebase query (FQuery) and an array of NSSortDescriptors.
* Use this if you would like the array to be sorted after being received from the server, or if
* you would like more complex sorting behavior than an FQuery can provide.
* It is recommended that you use FQuery to filter, rather than sort, for use with this initializer.
* E.G. query only objects that have false for their hidden flag, then sort using Sort Descriptors.
* @param query A query on a Firebase reference which provides filtered data to FirebaseArray
* @param sortDescriptors The sort descriptors by which the array should be ordered. If the array is
* empty or nil, the array is ordered by [snapshot1.key compare:snapshot2.key]
* @return The instance of FirebaseArray
*/
-(instancetype)initWithQuery:(FQuery *)query sortDescriptors:(NSArray *)sortDescriptors;

#pragma mark -
#pragma mark Public API methods

Expand All @@ -93,27 +148,39 @@
* @param index The index of the item to retrieve
* @return The object at the given index
*/
- (id)objectAtIndex:(NSUInteger)index;
- (FDataSnapshot *)objectAtIndexPath:(NSIndexPath *)indexPath;

/**
* Returns a Firebase reference for an object at a specific index in the FirebaseArray.
* @param index The index of the item to retrieve a reference for
* @return A Firebase reference for the object at the given index
*/
- (Firebase *)refForIndex:(NSUInteger)index;
- (Firebase *)refForIndexPath:(NSIndexPath *)indexPath;

/**
* The sort descriptors by which the array should be ordered. If the array is empty or nil, the
* array is ordered by [snapshot1.key compare:snapshot2.key]
*/
@property (strong, nonatomic) NSArray * sortDescriptors;

- (NSIndexPath *)indexPathOfObject:(FDataSnapshot *)snapshot;

#pragma mark -
#pragma mark Private API methods

- (NSIndexPath *)indexPathForKey:(NSString *)key;

/**
* Returns an index for a given object's key (that matches the object's key in the corresponding
* Firebase reference).
* @param key The key of the desired object
* @return The index of the object for which the key matches or -1 if the key is null
* @exception FirebaseArrayKeyNotFoundException Thrown when the desired key is not in the
* FirebaseArray, likely indicating that the FirebaseArray is no longer being properly synchronized
* with the Firebase database.
*/
- (NSUInteger)indexForKey:(NSString *)key;
* The predicate by which the snapshots are filtered. If predicate is nil, the array reflects all
* results from the Firebase Query or Reference.
*/
@property (strong, nonatomic) NSPredicate * predicate;

@property (strong, nonatomic) NSString * sectionKeyPath;
@property (nonatomic) BOOL sectionsOrderedAscending;

- (NSArray *)sectionAtIndex:(NSUInteger)sectionIndex;

- (NSUInteger)numberOfSections;

@end
29 changes: 20 additions & 9 deletions FirebaseUI/API/FirebaseArrayDelegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,39 +44,50 @@
* [FEventTypeChildAdded](https://www.firebase.com/docs/ios/guide/retrieving-data.html#section-event-types)
* event being raised.
* @param object The object added to the FirebaseArray
* @param index The index the child was added at
* @param indexPath The indexPath the child was added at
*/
- (void)childAdded:(id)object atIndex:(NSUInteger)index;
- (void)childAdded:(id)obj atIndexPath:(NSIndexPath *)indexPath;

/**
* Delegate method which is called whenever an object is chinged in a FirebaseArray. On a
* FirebaseArray synchronized to a Firebase reference, this corresponds to an
* [FEventTypeChildChanged](https://www.firebase.com/docs/ios/guide/retrieving-data.html#section-event-types)
* event being raised.
* @param object The object that changed in the FirebaseArray
* @param index The index the child was changed at
* @param indexPath The indexPath the child was changed at
*/
- (void)childChanged:(id)object atIndex:(NSUInteger)index;
- (void)childChanged:(id)obj atIndexPath:(NSIndexPath *)indexPath;

/**
* Delegate method which is called whenever an object is removed from a FirebaseArray. On a
* FirebaseArray synchronized to a Firebase reference, this corresponds to an
* [FEventTypeChildRemoved](https://www.firebase.com/docs/ios/guide/retrieving-data.html#section-event-types)
* event being raised.
* @param object The object removed from the FirebaseArray
* @param index The index the child was removed at
* @param indexPath The indexPath the child was removed at
*/
- (void)childRemoved:(id)object atIndex:(NSUInteger)index;
- (void)childRemoved:(id)obj atIndexPath:(NSIndexPath *)indexPath;

/**
* Delegate method which is called whenever an object is moved within a FirebaseArray. On a
* FirebaseArray synchronized to a Firebase reference, this corresponds to an
* [FEventTypeChildMoved](https://www.firebase.com/docs/ios/guide/retrieving-data.html#section-event-types)
* event being raised.
* @param object The object that has moved locations in the FirebaseArray
* @param fromIndex The index the child is being moved from
* @param toIndex The index the child is being moved to
* @param fromIndexPath The indexPath the child is being moved from
* @param toIndexPath The indexPath the child is being moved to
*/
- (void)childMoved:(id)object fromIndex:(NSUInteger)fromIndex toIndex:(NSUInteger)toIndex;
- (void)childMoved:(id)obj
fromIndexPath:(NSIndexPath *)fromIndexPath
toIndexPath:(NSIndexPath *)toIndexPath;

- (void)sectionsAddedAtIndexes:(NSIndexSet *)indexes;

- (void)sectionAddedAtSectionIndex:(NSUInteger)section;

- (void)sectionRemovedAtSectionIndex:(NSUInteger)section;

- (void)beginUpdates;
- (void)endUpdates;

@end
Loading