Skip to content

Commit 6e75b83

Browse files
committed
add sorted firebase collection
1 parent 101faed commit 6e75b83

File tree

13 files changed

+314
-1012
lines changed

13 files changed

+314
-1012
lines changed

FirebaseDatabaseUI/FUIArray.h

Lines changed: 9 additions & 9 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

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

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

5656
/**
5757
* The query on a Firebase reference that provides data to populate the array.
5858
*/
59-
@property(strong, nonatomic) id<FUIDataObservable> query;
59+
@property (strong, nonatomic) id<FUIDataObservable> query;
6060

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

6666
/**
6767
* The items currently in the array.
@@ -78,7 +78,7 @@ NS_ASSUME_NONNULL_BEGIN
7878
* @return A FirebaseArray instance
7979
*/
8080
- (instancetype)initWithQuery:(id<FUIDataObservable>)query
81-
delegate:(nullable id<FUIArrayDelegate>)delegate NS_DESIGNATED_INITIALIZER;
81+
delegate:(nullable id<FUICollectionDelegate>)delegate NS_DESIGNATED_INITIALIZER;
8282

8383
/**
8484
* Initalizes FirebaseArray with a Firebase query (FIRDatabaseQuery) or database reference
@@ -102,9 +102,9 @@ NS_ASSUME_NONNULL_BEGIN
102102
/**
103103
* Returns an object at a specific index in the array.
104104
* @param index The index of the item to retrieve
105-
* @return The object at the given index
105+
* @return The snapshot at the given index
106106
*/
107-
- (id)objectAtIndex:(NSUInteger)index;
107+
- (FIRDataSnapshot *)snapshotAtIndex:(NSInteger)index;
108108

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

FirebaseDatabaseUI/FUIArray.m

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ @implementation FUIArray
3939

4040
#pragma mark - Initializer methods
4141

42-
- (instancetype)initWithQuery:(FIRDatabaseQuery *)query delegate:(id<FUIArrayDelegate>)delegate {
42+
- (instancetype)initWithQuery:(FIRDatabaseQuery *)query delegate:(id<FUICollectionDelegate>)delegate {
4343
NSParameterAssert(query != nil);
4444
self = [super init];
4545
if (self) {
@@ -174,7 +174,7 @@ - (NSUInteger)count {
174174
return [self.snapshots count];
175175
}
176176

177-
- (FIRDataSnapshot *)objectAtIndex:(NSUInteger)index {
177+
- (FIRDataSnapshot *)snapshotAtIndex:(NSInteger)index {
178178
return (FIRDataSnapshot *)[self.snapshots objectAtIndex:index];
179179
}
180180

@@ -183,7 +183,7 @@ - (FIRDatabaseReference *)refForIndex:(NSUInteger)index {
183183
}
184184

185185
- (id)objectAtIndexedSubscript:(NSUInteger)index {
186-
return [self objectAtIndex:index];
186+
return [self snapshotAtIndex:index];
187187
}
188188

189189
- (void)setObject:(id)obj atIndexedSubscript:(NSUInteger)index{

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/FUIIndexArray.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
#import "FUIIndexArray.h"
2222
#import "FUIQueryObserver.h"
2323

24-
@interface FUIIndexArray () <FUIArrayDelegate>
24+
@interface FUIIndexArray () <FUICollectionDelegate>
2525

2626
@property (nonatomic, readonly) id<FUIDataObservable> index;
2727
@property (nonatomic, readonly) id<FUIDataObservable> data;

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<FUIDataObservable>)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)