Skip to content

Commit 07c68de

Browse files
committed
add sorted firebase collection
1 parent 0577b24 commit 07c68de

File tree

11 files changed

+312
-27
lines changed

11 files changed

+312
-27
lines changed

FirebaseDatabaseUI/FUIArray.h

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
@import FirebaseDatabase;
2222

23-
#import "FUIArrayDelegate.h"
23+
#import "FUICollection.h"
2424

2525
NS_ASSUME_NONNULL_BEGIN
2626

@@ -43,23 +43,23 @@ NS_ASSUME_NONNULL_BEGIN
4343
* query. It is useful for building custom data structures or sources, and provides the base for
4444
* FirebaseDataSource.
4545
*/
46-
@interface FUIArray : NSObject
46+
@interface FUIArray : NSObject <FUICollection>
4747

4848
/**
4949
* The delegate object that array changes are surfaced to, which conforms to the
50-
* @c FUIArrayDelegate protocol.
50+
* @c FUICollectionDelegate protocol.
5151
*/
52-
@property(weak, nonatomic, nullable) id<FUIArrayDelegate> delegate;
52+
@property (weak, nonatomic, nullable) id<FUICollectionDelegate> delegate;
5353

5454
/**
5555
* The query on a Firebase reference that provides data to populate the array.
5656
*/
57-
@property(strong, nonatomic) id<FIRDataObservable> query;
57+
@property (strong, nonatomic) id<FIRDataObservable> query;
5858

5959
/**
6060
* The number of objects in the array.
6161
*/
62-
@property(nonatomic, readonly) NSUInteger count;
62+
@property (nonatomic, readonly) NSUInteger count;
6363

6464
/**
6565
* The items currently in the array.
@@ -83,9 +83,9 @@ NS_ASSUME_NONNULL_BEGIN
8383
/**
8484
* Returns an object at a specific index in the array.
8585
* @param index The index of the item to retrieve
86-
* @return The object at the given index
86+
* @return The snapshot at the given index
8787
*/
88-
- (id)objectAtIndex:(NSUInteger)index;
88+
- (FIRDataSnapshot *)snapshotAtIndex:(NSInteger)index;
8989

9090
/**
9191
* Returns a Firebase reference for an object at a specific index in the array.

FirebaseDatabaseUI/FUIArrayDelegate.h renamed to FirebaseDatabaseUI/FUICollection.h

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,33 @@
1818

1919
// clang-format on
2020

21+
@import Foundation;
22+
2123
@class FUIArray;
24+
@protocol FUICollectionDelegate;
25+
26+
NS_ASSUME_NONNULL_BEGIN
27+
28+
@protocol FUICollection <NSObject>
29+
30+
@property (nonatomic, readonly, copy) NSArray<FIRDataSnapshot *> *items;
31+
32+
@property (weak, nonatomic, nullable) id<FUICollectionDelegate> delegate;
33+
34+
/**
35+
* The number of objects in the FirebaseArray.
36+
*/
37+
@property (nonatomic, readonly) NSUInteger count;
38+
39+
- (FIRDataSnapshot *)snapshotAtIndex:(NSInteger)index;
40+
41+
@end
2242

2343
/**
2444
* A protocol to allow instances of FUIArray to raise events through a
2545
* delegate. Raises all Firebase events except FIRDataEventTypeValue.
2646
*/
27-
@protocol FUIArrayDelegate<NSObject>
47+
@protocol FUICollectionDelegate<NSObject>
2848

2949
@optional
3050

@@ -35,7 +55,7 @@
3555
* @param object The object added to the FUIArray
3656
* @param index The index the child was added at
3757
*/
38-
- (void)array:(FUIArray *)array didAddObject:(id)object atIndex:(NSUInteger)index;
58+
- (void)array:(id<FUICollection>)array didAddObject:(id)object atIndex:(NSUInteger)index;
3959

4060
/**
4161
* Delegate method which is called whenever an object is changed in an
@@ -44,7 +64,7 @@
4464
* @param object The object that changed in the FUIArray
4565
* @param index The index the child was changed at
4666
*/
47-
- (void)array:(FUIArray *)array didChangeObject:(id)object atIndex:(NSUInteger)index;
67+
- (void)array:(id<FUICollection>)array didChangeObject:(id)object atIndex:(NSUInteger)index;
4868

4969
/**
5070
* Delegate method which is called whenever an object is removed from an
@@ -53,7 +73,7 @@
5373
* @param object The object removed from the FUIArray
5474
* @param index The index the child was removed at
5575
*/
56-
- (void)array:(FUIArray *)array didRemoveObject:(id)object atIndex:(NSUInteger)index;
76+
- (void)array:(id<FUICollection>)array didRemoveObject:(id)object atIndex:(NSUInteger)index;
5777

5878
/**
5979
* Delegate method which is called whenever an object is moved within a
@@ -63,12 +83,14 @@
6383
* @param fromIndex The index the child is being moved from
6484
* @param toIndex The index the child is being moved to
6585
*/
66-
- (void)array:(FUIArray *)array didMoveObject:(id)object fromIndex:(NSUInteger)fromIndex toIndex:(NSUInteger)toIndex;
86+
- (void)array:(id<FUICollection>)array didMoveObject:(id)object fromIndex:(NSUInteger)fromIndex toIndex:(NSUInteger)toIndex;
6787

6888
/**
6989
* Delegate method which is called whenever the backing query is canceled.
7090
* @param error the error that was raised
7191
*/
72-
- (void)array:(FUIArray *)array queryCancelledWithError:(NSError *)error;
92+
- (void)array:(id<FUICollection>)array queryCancelledWithError:(NSError *)error;
7393

7494
@end
95+
96+
NS_ASSUME_NONNULL_END

FirebaseDatabaseUI/FUICollectionViewDataSource.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ - (instancetype)initWithQuery:(FIRDatabaseQuery *)query
4040
return self;
4141
}
4242

43-
#pragma mark - FUIArrayDelegate methods
43+
#pragma mark - FUICollectionDelegate methods
4444

4545
- (void)array:(FUIArray *)array didAddObject:(id)object atIndex:(NSUInteger)index {
4646
[self.collectionView

FirebaseDatabaseUI/FUIDataSource.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
* subclasses need as well as several methods that pass through to the instance
3333
* of FirebaseArray.
3434
*/
35-
@interface FUIDataSource : NSObject<FUIArrayDelegate>
35+
@interface FUIDataSource : NSObject<FUICollectionDelegate>
3636

3737
/**
3838
* The items in the data source.
@@ -49,9 +49,9 @@
4949
- (instancetype)init NS_UNAVAILABLE;
5050

5151
/**
52-
* Pass through of [FirebaseArray objectAtIndex:].
52+
* Pass through of [FirebaseArray snapshotAtIndex:].
5353
*/
54-
- (id)objectAtIndex:(NSUInteger)index;
54+
- (FIRDataSnapshot *)objectAtIndex:(NSUInteger)index;
5555

5656
/**
5757
* Pass through of [FirebaseArray refForIndex:].

FirebaseDatabaseUI/FUIDataSource.m

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ - (NSUInteger)count {
5454
return self.array.count;
5555
}
5656

57-
- (id)objectAtIndex:(NSUInteger)index {
58-
return [self.array objectAtIndex:index];
57+
- (FIRDataSnapshot *)objectAtIndex:(NSUInteger)index {
58+
return [self.array snapshotAtIndex:index];
5959
}
6060

6161
- (FIRDatabaseReference *)refForIndex:(NSUInteger)index {
@@ -66,7 +66,7 @@ - (void)cancelWithBlock:(void (^)(NSError *))block {
6666
self.cancelBlock = block;
6767
}
6868

69-
#pragma mark - FUIArrayDelegate methods
69+
#pragma mark - FUICollectionDelegate methods
7070

7171
- (void)canceledWithError:(NSError *)error {
7272
if (self.cancelBlock != nil) {

FirebaseDatabaseUI/FUISortedArray.h

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
//
2+
// Copyright (c) 2016 Google Inc.
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
//
16+
17+
@import FirebaseDatabase;
18+
19+
#import <FirebaseDatabaseUI/FirebaseDatabaseUI.h>
20+
21+
NS_ASSUME_NONNULL_BEGIN
22+
23+
@interface FUISortedArray : NSObject <FUICollection>
24+
25+
@property (nonatomic, readonly, copy) NSArray<FIRDataSnapshot *> *items;
26+
27+
@property (nonatomic, weak, readwrite, nullable) id<FUICollectionDelegate> delegate;
28+
29+
- (instancetype)init NS_UNAVAILABLE;
30+
31+
- (instancetype)initWithQuery:(id<FIRDataObservable>)query
32+
delegate:(nullable id<FUICollectionDelegate>)delegate
33+
sortDescriptor:(NSComparisonResult (^)(FIRDataSnapshot *left,
34+
FIRDataSnapshot *right))sortDescriptor NS_DESIGNATED_INITIALIZER;
35+
36+
- (void)observeQuery;
37+
- (void)invalidate;
38+
39+
- (FIRDataSnapshot *)snapshotAtIndex:(NSInteger)index;
40+
41+
@end
42+
43+
NS_ASSUME_NONNULL_END

0 commit comments

Comments
 (0)