Skip to content

Commit cdb1207

Browse files
committed
de-duplicate code, at the cost of exposing everything publicly
1 parent 5b52f7a commit cdb1207

File tree

4 files changed

+156
-171
lines changed

4 files changed

+156
-171
lines changed

FirebaseDatabaseUI/FUIArray.h

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ NS_ASSUME_NONNULL_BEGIN
6666
/**
6767
* The items currently in the array.
6868
*/
69-
@property(nonatomic, readonly, copy) NSArray *items;
69+
@property (nonatomic, readonly, copy) NSArray *items;
7070

7171
#pragma mark - Initializer methods
7272

@@ -135,6 +135,42 @@ NS_ASSUME_NONNULL_BEGIN
135135
*/
136136
- (NSUInteger)indexForKey:(NSString *)key;
137137

138+
/**
139+
* Called when the Firebase query sends a FIRDataEventTypeChildAdded event. Override this
140+
* to provide custom insertion logic.
141+
* @param snap The snapshot that was inserted.
142+
* @param previous The key of the sibling preceding the inserted snapshot.
143+
*/
144+
- (void)insertSnapshot:(FIRDataSnapshot *)snap withPreviousChildKey:(nullable NSString *)previous;
145+
146+
/**
147+
* Called when the Firebase query sends a FIRDataEventTypeChildRemoved event. Override this
148+
* to provide custom removal logic.
149+
* @param snap The snapshot that was removed.
150+
* @param previous The key of the sibling preceding the removed snapshot.
151+
*/
152+
- (void)removeSnapshot:(FIRDataSnapshot *)snap withPreviousChildKey:(nullable NSString *)previous;
153+
154+
/**
155+
* Called when the Firebase query sends a FIRDataEventTypeChildChanged event. Override this
156+
* to provide custom on change logic.
157+
* @param snap The snapshot whose value was changed.
158+
* @param previous The key of the sibling preceding the changed snapshot.
159+
*/
160+
- (void)changeSnapshot:(FIRDataSnapshot *)snap withPreviousChildKey:(nullable NSString *)previous;
161+
162+
/**
163+
* Called when the Firebase query sends a FIRDataEventTypeChildMoved event. Override this
164+
* to provide custom move logic.
165+
* @param snap The snapshot that was moved.
166+
* @param previous The key of the sibling preceding the moved snapshot at its new location.
167+
*/
168+
- (void)moveSnapshot:(FIRDataSnapshot *)snap withPreviousChildKey:(nullable NSString *)previous;
169+
170+
- (void)removeSnapshotAtIndex:(NSUInteger)index;
171+
- (void)insertSnapshot:(FIRDataSnapshot *)snap atIndex:(NSUInteger)index;
172+
- (void)addSnapshot:(FIRDataSnapshot *)snap;
173+
138174
@end
139175

140176
NS_ASSUME_NONNULL_END

FirebaseDatabaseUI/FUIArray.m

Lines changed: 69 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,13 @@ @interface FUIArray ()
2525
/**
2626
* The backing collection that holds all of the array's data.
2727
*/
28-
@property(strong, nonatomic) NSMutableArray<FIRDataSnapshot *> *snapshots;
28+
@property (strong, nonatomic) NSMutableArray<FIRDataSnapshot *> *snapshots;
2929

3030
/**
3131
* A set containing the query observer handles that should be released when
3232
* this array is freed.
3333
*/
34-
@property(strong, nonatomic) NSMutableSet<NSNumber *> *handles;
34+
@property (strong, nonatomic) NSMutableSet<NSNumber *> *handles;
3535

3636
@end
3737

@@ -72,16 +72,7 @@ - (void)observeQuery {
7272
FIRDatabaseHandle handle;
7373
handle = [self.query observeEventType:FIRDataEventTypeChildAdded
7474
andPreviousSiblingKeyWithBlock:^(FIRDataSnapshot *snapshot, NSString *previousChildKey) {
75-
NSUInteger index = 0;
76-
if (previousChildKey != nil) {
77-
index = [self indexForKey:previousChildKey] + 1;
78-
}
79-
80-
[self.snapshots insertObject:snapshot atIndex:index];
81-
82-
if ([self.delegate respondsToSelector:@selector(array:didAddObject:atIndex:)]) {
83-
[self.delegate array:self didAddObject:snapshot atIndex:index];
84-
}
75+
[self insertSnapshot:snapshot withPreviousChildKey:previousChildKey];
8576
}
8677
withCancelBlock:^(NSError *error) {
8778
if ([self.delegate respondsToSelector:@selector(array:queryCancelledWithError:)]) {
@@ -92,13 +83,7 @@ - (void)observeQuery {
9283

9384
handle = [self.query observeEventType:FIRDataEventTypeChildChanged
9485
andPreviousSiblingKeyWithBlock:^(FIRDataSnapshot *snapshot, NSString *previousChildKey) {
95-
NSUInteger index = [self indexForKey:snapshot.key];
96-
97-
[self.snapshots replaceObjectAtIndex:index withObject:snapshot];
98-
99-
if ([self.delegate respondsToSelector:@selector(array:didChangeObject:atIndex:)]) {
100-
[self.delegate array:self didChangeObject:snapshot atIndex:index];
101-
}
86+
[self changeSnapshot:snapshot withPreviousChildKey:previousChildKey];
10287
}
10388
withCancelBlock:^(NSError *error) {
10489
if ([self.delegate respondsToSelector:@selector(array:queryCancelledWithError:)]) {
@@ -109,13 +94,7 @@ - (void)observeQuery {
10994

11095
handle = [self.query observeEventType:FIRDataEventTypeChildRemoved
11196
andPreviousSiblingKeyWithBlock:^(FIRDataSnapshot *snapshot, NSString *previousSiblingKey) {
112-
NSUInteger index = [self indexForKey:snapshot.key];
113-
114-
[self.snapshots removeObjectAtIndex:index];
115-
116-
if ([self.delegate respondsToSelector:@selector(array:didRemoveObject:atIndex:)]) {
117-
[self.delegate array:self didRemoveObject:snapshot atIndex:index];
118-
}
97+
[self removeSnapshot:snapshot withPreviousChildKey:previousSiblingKey];
11998
}
12099
withCancelBlock:^(NSError *error) {
121100
if ([self.delegate respondsToSelector:@selector(array:queryCancelledWithError:)]) {
@@ -126,22 +105,7 @@ - (void)observeQuery {
126105

127106
handle = [self.query observeEventType:FIRDataEventTypeChildMoved
128107
andPreviousSiblingKeyWithBlock:^(FIRDataSnapshot *snapshot, NSString *previousChildKey) {
129-
NSUInteger fromIndex = [self indexForKey:snapshot.key];
130-
[self.snapshots removeObjectAtIndex:fromIndex];
131-
132-
NSUInteger toIndex = 0;
133-
if (previousChildKey != nil) {
134-
NSUInteger prevIndex = [self indexForKey:previousChildKey];
135-
if (prevIndex != NSNotFound) {
136-
toIndex = prevIndex + 1;
137-
}
138-
}
139-
140-
[self.snapshots insertObject:snapshot atIndex:toIndex];
141-
142-
if ([self.delegate respondsToSelector:@selector(array:didMoveObject:fromIndex:toIndex:)]) {
143-
[self.delegate array:self didMoveObject:snapshot fromIndex:fromIndex toIndex:toIndex];
144-
}
108+
[self moveSnapshot:snapshot withPreviousChildKey:previousChildKey];
145109
}
146110
withCancelBlock:^(NSError *error) {
147111
if ([self.delegate respondsToSelector:@selector(array:queryCancelledWithError:)]) {
@@ -168,6 +132,69 @@ - (NSUInteger)indexForKey:(NSString *)key {
168132
return NSNotFound;
169133
}
170134

135+
- (void)insertSnapshot:(FIRDataSnapshot *)snap withPreviousChildKey:(NSString *)previous {
136+
NSUInteger index = 0;
137+
if (previous != nil) {
138+
index = [self indexForKey:previous] + 1;
139+
}
140+
141+
[self.snapshots insertObject:snap atIndex:index];
142+
143+
if ([self.delegate respondsToSelector:@selector(array:didAddObject:atIndex:)]) {
144+
[self.delegate array:self didAddObject:snap atIndex:index];
145+
}
146+
}
147+
148+
- (void)removeSnapshot:(FIRDataSnapshot *)snap withPreviousChildKey:(NSString *)previous {
149+
NSUInteger index = [self indexForKey:snap.key];
150+
151+
[self.snapshots removeObjectAtIndex:index];
152+
153+
if ([self.delegate respondsToSelector:@selector(array:didRemoveObject:atIndex:)]) {
154+
[self.delegate array:self didRemoveObject:snap atIndex:index];
155+
}
156+
}
157+
158+
- (void)changeSnapshot:(FIRDataSnapshot *)snap withPreviousChildKey:(NSString *)previous {
159+
NSUInteger index = [self indexForKey:snap.key];
160+
161+
[self.snapshots replaceObjectAtIndex:index withObject:snap];
162+
163+
if ([self.delegate respondsToSelector:@selector(array:didChangeObject:atIndex:)]) {
164+
[self.delegate array:self didChangeObject:snap atIndex:index];
165+
}
166+
}
167+
168+
- (void)moveSnapshot:(FIRDataSnapshot *)snap withPreviousChildKey:(NSString *)previous {
169+
NSUInteger fromIndex = [self indexForKey:snap.key];
170+
[self.snapshots removeObjectAtIndex:fromIndex];
171+
172+
NSUInteger toIndex = 0;
173+
if (previous != nil) {
174+
NSUInteger prevIndex = [self indexForKey:previous];
175+
if (prevIndex != NSNotFound) {
176+
toIndex = prevIndex + 1;
177+
}
178+
}
179+
[self.snapshots insertObject:snap atIndex:toIndex];
180+
181+
if ([self.delegate respondsToSelector:@selector(array:didMoveObject:fromIndex:toIndex:)]) {
182+
[self.delegate array:self didMoveObject:snap fromIndex:fromIndex toIndex:toIndex];
183+
}
184+
}
185+
186+
- (void)removeSnapshotAtIndex:(NSUInteger)index {
187+
[self.snapshots removeObjectAtIndex:index];
188+
}
189+
190+
- (void)insertSnapshot:(FIRDataSnapshot *)snap atIndex:(NSUInteger)index {
191+
[self.snapshots insertObject:snap atIndex:index];
192+
}
193+
194+
- (void)addSnapshot:(FIRDataSnapshot *)snap {
195+
[self.snapshots addObject:snap];
196+
}
197+
171198
#pragma mark - Public API methods
172199

173200
- (NSArray *)items {

FirebaseDatabaseUI/FUISortedArray.h

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

2121
NS_ASSUME_NONNULL_BEGIN
2222

23-
@interface FUISortedArray : NSObject <FUICollection>
23+
@interface FUISortedArray : FUIArray <FUICollection>
2424

2525
/**
2626
* A copy of the snapshots currently in the array.
2727
*/
2828
@property (nonatomic, readonly, copy) NSArray<FIRDataSnapshot *> *items;
2929

30-
/**
31-
* The delegate that should receive events from the sorted array.
32-
*/
33-
@property (nonatomic, weak, readwrite, nullable) id<FUICollectionDelegate> delegate;
34-
35-
- (instancetype)init NS_UNAVAILABLE;
30+
- (instancetype)initWithQuery:(id<FIRDataObservable>)query NS_UNAVAILABLE;
3631

3732
/**
3833
* Initializes a sorted collection.

0 commit comments

Comments
 (0)