|
| 1 | +// clang-format off |
| 2 | + |
| 3 | +// |
| 4 | +// Copyright (c) 2016 Google Inc. |
| 5 | +// |
| 6 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +// you may not use this file except in compliance with the License. |
| 8 | +// You may obtain a copy of the License at |
| 9 | +// |
| 10 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +// |
| 12 | +// Unless required by applicable law or agreed to in writing, software |
| 13 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +// See the License for the specific language governing permissions and |
| 16 | +// limitations under the License. |
| 17 | +// |
| 18 | + |
| 19 | +// clang-format on |
| 20 | + |
| 21 | +#import "FirebaseArray.h" |
| 22 | + |
| 23 | +NS_ASSUME_NONNULL_BEGIN |
| 24 | + |
| 25 | +@class FirebaseIndexArray; |
| 26 | + |
| 27 | +/** |
| 28 | + * A protocol to allow instances of FirebaseArray to raise events through a |
| 29 | + * delegate. Raises all |
| 30 | + * Firebase events except FIRDataEventTypeValue. |
| 31 | + */ |
| 32 | +@protocol FirebaseIndexArrayDelegate<NSObject> |
| 33 | + |
| 34 | +@optional |
| 35 | + |
| 36 | +/** |
| 37 | + * Delegate method called when the database reference at an index has |
| 38 | + * finished loading its contents. |
| 39 | + * @param array The array calling this method. |
| 40 | + * @param reference The reference that was loaded. |
| 41 | + * @param object The database reference's contents. |
| 42 | + * @param index The index of the reference that was loaded. |
| 43 | + */ |
| 44 | +- (void)array:(FirebaseIndexArray *)array |
| 45 | + reference:(FIRDatabaseReference *)ref |
| 46 | +didLoadObject:(FIRDataSnapshot *)object |
| 47 | + atIndex:(NSUInteger)index; |
| 48 | + |
| 49 | +/** |
| 50 | + * Delegate method which is called whenever an object is added to a |
| 51 | + * FirebaseArray. On a |
| 52 | + * FirebaseArray synchronized to a Firebase reference, this corresponds to an |
| 53 | + * [FIRDataEventTypeChildAdded](https://www.firebase.com/docs/ios/guide/retrieving-data.html#section-event-types) |
| 54 | + * event being raised. |
| 55 | + * @param ref The database reference added to the array |
| 56 | + * @param index The index the child was added at |
| 57 | + */ |
| 58 | +- (void)array:(FirebaseIndexArray *)array didAddReference:(FIRDatabaseReference *)ref atIndex:(NSUInteger)index; |
| 59 | + |
| 60 | +/** |
| 61 | + * Delegate method which is called whenever an object is chinged in a |
| 62 | + * FirebaseArray. On a |
| 63 | + * FirebaseArray synchronized to a Firebase reference, this corresponds to an |
| 64 | + * [FIRDataEventTypeChildChanged](https://www.firebase.com/docs/ios/guide/retrieving-data.html#section-event-types) |
| 65 | + * event being raised. |
| 66 | + * @param object The database reference that changed in the array |
| 67 | + * @param index The index the child was changed at |
| 68 | + */ |
| 69 | +- (void)array:(FirebaseIndexArray *)array didChangeReference:(FIRDatabaseReference *)ref atIndex:(NSUInteger)index; |
| 70 | + |
| 71 | +/** |
| 72 | + * Delegate method which is called whenever an object is removed from a |
| 73 | + * FirebaseArray. On a |
| 74 | + * FirebaseArray synchronized to a Firebase reference, this corresponds to an |
| 75 | + * [FIRDataEventTypeChildRemoved](https://www.firebase.com/docs/ios/guide/retrieving-data.html#section-event-types) |
| 76 | + * event being raised. |
| 77 | + * @param object The database reference removed from the array |
| 78 | + * @param index The index the child was removed at |
| 79 | + */ |
| 80 | +- (void)array:(FirebaseIndexArray *)array didRemoveReference:(FIRDatabaseReference *)ref atIndex:(NSUInteger)index; |
| 81 | + |
| 82 | +/** |
| 83 | + * Delegate method which is called whenever an object is moved within a |
| 84 | + * FirebaseArray. On a |
| 85 | + * FirebaseArray synchronized to a Firebase reference, this corresponds to an |
| 86 | + * [FIRDataEventTypeChildMoved](https://www.firebase.com/docs/ios/guide/retrieving-data.html#section-event-types) |
| 87 | + * event being raised. |
| 88 | + * @param object The database reference that has moved locations |
| 89 | + * @param fromIndex The index the child is being moved from |
| 90 | + * @param toIndex The index the child is being moved to |
| 91 | + */ |
| 92 | +- (void)array:(FirebaseIndexArray *)array didMoveReference:(FIRDatabaseReference *)ref fromIndex:(NSUInteger)fromIndex toIndex:(NSUInteger)toIndex; |
| 93 | + |
| 94 | +/** |
| 95 | + * Delegate method which is called whenever the backing query is canceled. This error is fatal |
| 96 | + * and the index array will become unusable afterward, so please handle it appropriately. |
| 97 | + * @param error the error that was raised |
| 98 | + */ |
| 99 | +- (void)array:(FirebaseIndexArray *)array queryCancelledWithError:(NSError *)error; |
| 100 | + |
| 101 | +@end |
| 102 | + |
| 103 | +@interface FirebaseIndexArray : NSObject |
| 104 | + |
| 105 | +@property(nonatomic, copy, readonly) NSArray<FIRDataSnapshot *> *items; |
| 106 | + |
| 107 | +@property(nonatomic, weak) id<FirebaseIndexArrayDelegate> delegate; |
| 108 | + |
| 109 | +- (instancetype)init NS_UNAVAILABLE; |
| 110 | + |
| 111 | +/** |
| 112 | + * Initializes a FirebaseIndexArray with an index query and a data query. |
| 113 | + * The array expects the keys of the children of the index query to be children |
| 114 | + * of the data query. |
| 115 | + * @param index A Firebase database query whose childrens' keys are all children |
| 116 | + * of the data query. |
| 117 | + * @param data A Firebase database reference whose children will be fetched and used |
| 118 | + * to populate the array's contents according to the index query. |
| 119 | + * @param delegate The delegate that events should be forwarded to. |
| 120 | + */ |
| 121 | +- (instancetype)initWithIndex:(id<FIRDataObservable>)index |
| 122 | + data:(id<FIRDataObservable>)data |
| 123 | + delegate:(nullable id<FirebaseIndexArrayDelegate>)delegate NS_DESIGNATED_INITIALIZER; |
| 124 | + |
| 125 | +/** |
| 126 | + * Initializes a FirebaseIndexArray with an index query and a data query. |
| 127 | + * The array expects the keys of the children of the index query to be children |
| 128 | + * of the data query. |
| 129 | + * @param index A Firebase database query whose childrens' keys are all children |
| 130 | + * of the data query. |
| 131 | + * @param data A Firebase database reference whose children will be fetched and used |
| 132 | + * to populate the array's contents according to the index query. |
| 133 | + */ |
| 134 | +- (instancetype)initWithIndex:(id<FIRDataObservable>)index |
| 135 | + data:(id<FIRDataObservable>)data; |
| 136 | + |
| 137 | +/** |
| 138 | + * Removes all observers from all queries managed by this array and renders this array |
| 139 | + * unusable. |
| 140 | + */ |
| 141 | +- (void)invalidate; |
| 142 | + |
| 143 | +@end |
| 144 | + |
| 145 | +NS_ASSUME_NONNULL_END |
0 commit comments