|
| 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 FirebaseIndexArray to raise events through a |
| 29 | + * delegate. Raises all Firebase events except @c FIRDataEventTypeValue. |
| 30 | + */ |
| 31 | +@protocol FirebaseIndexArrayDelegate <NSObject> |
| 32 | + |
| 33 | +@optional |
| 34 | + |
| 35 | +/** |
| 36 | + * Delegate method called when the database reference at an index has |
| 37 | + * finished loading its contents. |
| 38 | + * @param array The array containing the reference. |
| 39 | + * @param ref The reference that was loaded. |
| 40 | + * @param object The database reference's contents. |
| 41 | + * @param index The index of the reference that was loaded. |
| 42 | + */ |
| 43 | +- (void)array:(FirebaseIndexArray *)array |
| 44 | + reference:(FIRDatabaseReference *)ref |
| 45 | +didLoadObject:(FIRDataSnapshot *)object |
| 46 | + atIndex:(NSUInteger)index; |
| 47 | + |
| 48 | +/** |
| 49 | + * Delegate method called when the database reference at an index has |
| 50 | + * failed to load contents. |
| 51 | + * @param array The array containing the reference. |
| 52 | + * @param ref The reference that failed to load. |
| 53 | + * @param index The index in the array of the reference that failed to load. |
| 54 | + * @param error The error that occurred. |
| 55 | + */ |
| 56 | +- (void)array:(FirebaseIndexArray *)array |
| 57 | + reference:(FIRDatabaseReference *)ref |
| 58 | + atIndex:(NSUInteger)index |
| 59 | +didFailLoadWithError:(NSError *)error; |
| 60 | + |
| 61 | +/** |
| 62 | + * Delegate method which is called whenever an object is added to a |
| 63 | + * FirebaseArray. On a FirebaseArray synchronized to a Firebase reference, |
| 64 | + * this corresponds to a @c FIRDataEventTypeChildAdded event being raised. |
| 65 | + * @param ref The database reference added to the array |
| 66 | + * @param index The index the reference was added at |
| 67 | + */ |
| 68 | +- (void)array:(FirebaseIndexArray *)array didAddReference:(FIRDatabaseReference *)ref atIndex:(NSUInteger)index; |
| 69 | + |
| 70 | +/** |
| 71 | + * Delegate method which is called whenever an object is changed in a |
| 72 | + * FirebaseArray. On a FirebaseArray synchronized to a Firebase reference, |
| 73 | + * this corresponds to a @c FIRDataEventTypeChildChanged event being raised. |
| 74 | + * @param ref The database reference that changed in the array |
| 75 | + * @param index The index the reference was changed at |
| 76 | + */ |
| 77 | +- (void)array:(FirebaseIndexArray *)array didChangeReference:(FIRDatabaseReference *)ref atIndex:(NSUInteger)index; |
| 78 | + |
| 79 | +/** |
| 80 | + * Delegate method which is called whenever an object is removed from a |
| 81 | + * FirebaseArray. On a FirebaseArray synchronized to a Firebase reference, |
| 82 | + * this corresponds to a @c FIRDataEventTypeChildRemoved event being raised. |
| 83 | + * @param ref The database reference removed from the array |
| 84 | + * @param index The index the reference was removed at |
| 85 | + */ |
| 86 | +- (void)array:(FirebaseIndexArray *)array didRemoveReference:(FIRDatabaseReference *)ref atIndex:(NSUInteger)index; |
| 87 | + |
| 88 | +/** |
| 89 | + * Delegate method which is called whenever an object is moved within a |
| 90 | + * FirebaseArray. On a FirebaseArray synchronized to a Firebase reference, |
| 91 | + * this corresponds to a @c FIRDataEventTypeChildMoved event being raised. |
| 92 | + * @param ref The database reference that has moved locations |
| 93 | + * @param fromIndex The index the reference is being moved from |
| 94 | + * @param toIndex The index the reference is being moved to |
| 95 | + */ |
| 96 | +- (void)array:(FirebaseIndexArray *)array didMoveReference:(FIRDatabaseReference *)ref fromIndex:(NSUInteger)fromIndex toIndex:(NSUInteger)toIndex; |
| 97 | + |
| 98 | +/** |
| 99 | + * Delegate method which is called whenever the backing query is canceled. This error is fatal |
| 100 | + * and the index array will become unusable afterward, so please handle it appropriately |
| 101 | + * (i.e. by displaying a modal error explaining why there's no content). |
| 102 | + * @param error the error that was raised |
| 103 | + */ |
| 104 | +- (void)array:(FirebaseIndexArray *)array queryCancelledWithError:(NSError *)error; |
| 105 | + |
| 106 | +@end |
| 107 | + |
| 108 | +/** |
| 109 | + * A FirebaseIndexArray instance uses a query's contents to query children of |
| 110 | + * a separate database reference, which is useful for displaying an indexed list |
| 111 | + * of data as described in https://firebase.google.com/docs/database/ios/structure-data |
| 112 | + */ |
| 113 | +@interface FirebaseIndexArray : NSObject |
| 114 | + |
| 115 | +/** |
| 116 | + * An immutable copy of the loaded contents in the array. Returns an |
| 117 | + * empty array if no contents have loaded yet. |
| 118 | + */ |
| 119 | +@property(nonatomic, copy, readonly) NSArray<FIRDataSnapshot *> *items; |
| 120 | + |
| 121 | +/** |
| 122 | + * The delegate that this array should forward events to. |
| 123 | + */ |
| 124 | +@property(nonatomic, weak) id<FirebaseIndexArrayDelegate> delegate; |
| 125 | + |
| 126 | +/** |
| 127 | + * Returns the number of items in the array. |
| 128 | + */ |
| 129 | +@property(nonatomic, readonly) NSUInteger count; |
| 130 | + |
| 131 | +- (instancetype)init NS_UNAVAILABLE; |
| 132 | + |
| 133 | +/** |
| 134 | + * Initializes a FirebaseIndexArray with an index query and a data query. |
| 135 | + * The array expects the keys of the children of the index query to match exactly children |
| 136 | + * of the data query. |
| 137 | + * @param index A Firebase database query whose childrens' keys are all children |
| 138 | + * of the data query. |
| 139 | + * @param data A Firebase database reference whose children will be fetched and used |
| 140 | + * to populate the array's contents according to the index query. |
| 141 | + * @param delegate The delegate that events should be forwarded to. |
| 142 | + */ |
| 143 | +- (instancetype)initWithIndex:(id<FIRDataObservable>)index |
| 144 | + data:(id<FIRDataObservable>)data |
| 145 | + delegate:(nullable id<FirebaseIndexArrayDelegate>)delegate NS_DESIGNATED_INITIALIZER; |
| 146 | + |
| 147 | +/** |
| 148 | + * Initializes a FirebaseIndexArray with an index query and a data query. |
| 149 | + * The array expects the keys of the children of the index query to be children |
| 150 | + * of the data query. |
| 151 | + * @param index A Firebase database query whose childrens' keys are all children |
| 152 | + * of the data query. |
| 153 | + * @param data A Firebase database reference whose children will be fetched and used |
| 154 | + * to populate the array's contents according to the index query. |
| 155 | + */ |
| 156 | +- (instancetype)initWithIndex:(id<FIRDataObservable>)index |
| 157 | + data:(id<FIRDataObservable>)data; |
| 158 | + |
| 159 | +/** |
| 160 | + * Returns the snapshot at the given index, if it has loaded. |
| 161 | + * Raises a fatal error if the index is out of bounds. |
| 162 | + * @param index The index of the requested snapshot. |
| 163 | + * @return A snapshot, or nil if one has not yet been loaded. |
| 164 | + */ |
| 165 | +- (nullable FIRDataSnapshot *)objectAtIndex:(NSUInteger)index; |
| 166 | + |
| 167 | +/** |
| 168 | + * Removes all observers from all queries managed by this array and renders this array |
| 169 | + * unusable. |
| 170 | + */ |
| 171 | +- (void)invalidate; |
| 172 | + |
| 173 | +@end |
| 174 | + |
| 175 | +NS_ASSUME_NONNULL_END |
0 commit comments