Skip to content

Commit 08a1bff

Browse files
committed
de-duplicate code, at the cost of exposing everything publicly
1 parent 2439dd5 commit 08a1bff

File tree

4 files changed

+156
-170
lines changed

4 files changed

+156
-170
lines changed

FirebaseDatabaseUI/FUIArray.h

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

6969
#pragma mark - Initializer methods
7070

@@ -116,6 +116,42 @@ NS_ASSUME_NONNULL_BEGIN
116116
*/
117117
- (NSUInteger)indexForKey:(NSString *)key;
118118

119+
/**
120+
* Called when the Firebase query sends a FIRDataEventTypeChildAdded event. Override this
121+
* to provide custom insertion logic.
122+
* @param snap The snapshot that was inserted.
123+
* @param previous The key of the sibling preceding the inserted snapshot.
124+
*/
125+
- (void)insertSnapshot:(FIRDataSnapshot *)snap withPreviousChildKey:(nullable NSString *)previous;
126+
127+
/**
128+
* Called when the Firebase query sends a FIRDataEventTypeChildRemoved event. Override this
129+
* to provide custom removal logic.
130+
* @param snap The snapshot that was removed.
131+
* @param previous The key of the sibling preceding the removed snapshot.
132+
*/
133+
- (void)removeSnapshot:(FIRDataSnapshot *)snap withPreviousChildKey:(nullable NSString *)previous;
134+
135+
/**
136+
* Called when the Firebase query sends a FIRDataEventTypeChildChanged event. Override this
137+
* to provide custom on change logic.
138+
* @param snap The snapshot whose value was changed.
139+
* @param previous The key of the sibling preceding the changed snapshot.
140+
*/
141+
- (void)changeSnapshot:(FIRDataSnapshot *)snap withPreviousChildKey:(nullable NSString *)previous;
142+
143+
/**
144+
* Called when the Firebase query sends a FIRDataEventTypeChildMoved event. Override this
145+
* to provide custom move logic.
146+
* @param snap The snapshot that was moved.
147+
* @param previous The key of the sibling preceding the moved snapshot at its new location.
148+
*/
149+
- (void)moveSnapshot:(FIRDataSnapshot *)snap withPreviousChildKey:(nullable NSString *)previous;
150+
151+
- (void)removeSnapshotAtIndex:(NSUInteger)index;
152+
- (void)insertSnapshot:(FIRDataSnapshot *)snap atIndex:(NSUInteger)index;
153+
- (void)addSnapshot:(FIRDataSnapshot *)snap;
154+
119155
@end
120156

121157
NS_ASSUME_NONNULL_END

FirebaseDatabaseUI/FUIArray.m

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

@@ -63,16 +63,7 @@ - (void)observeQuery {
6363
FIRDatabaseHandle handle;
6464
handle = [self.query observeEventType:FIRDataEventTypeChildAdded
6565
andPreviousSiblingKeyWithBlock:^(FIRDataSnapshot *snapshot, NSString *previousChildKey) {
66-
NSUInteger index = 0;
67-
if (previousChildKey != nil) {
68-
index = [self indexForKey:previousChildKey] + 1;
69-
}
70-
71-
[self.snapshots insertObject:snapshot atIndex:index];
72-
73-
if ([self.delegate respondsToSelector:@selector(array:didAddObject:atIndex:)]) {
74-
[self.delegate array:self didAddObject:snapshot atIndex:index];
75-
}
66+
[self insertSnapshot:snapshot withPreviousChildKey:previousChildKey];
7667
}
7768
withCancelBlock:^(NSError *error) {
7869
if ([self.delegate respondsToSelector:@selector(array:queryCancelledWithError:)]) {
@@ -83,13 +74,7 @@ - (void)observeQuery {
8374

8475
handle = [self.query observeEventType:FIRDataEventTypeChildChanged
8576
andPreviousSiblingKeyWithBlock:^(FIRDataSnapshot *snapshot, NSString *previousChildKey) {
86-
NSUInteger index = [self indexForKey:snapshot.key];
87-
88-
[self.snapshots replaceObjectAtIndex:index withObject:snapshot];
89-
90-
if ([self.delegate respondsToSelector:@selector(array:didChangeObject:atIndex:)]) {
91-
[self.delegate array:self didChangeObject:snapshot atIndex:index];
92-
}
77+
[self changeSnapshot:snapshot withPreviousChildKey:previousChildKey];
9378
}
9479
withCancelBlock:^(NSError *error) {
9580
if ([self.delegate respondsToSelector:@selector(array:queryCancelledWithError:)]) {
@@ -100,13 +85,7 @@ - (void)observeQuery {
10085

10186
handle = [self.query observeEventType:FIRDataEventTypeChildRemoved
10287
andPreviousSiblingKeyWithBlock:^(FIRDataSnapshot *snapshot, NSString *previousSiblingKey) {
103-
NSUInteger index = [self indexForKey:snapshot.key];
104-
105-
[self.snapshots removeObjectAtIndex:index];
106-
107-
if ([self.delegate respondsToSelector:@selector(array:didRemoveObject:atIndex:)]) {
108-
[self.delegate array:self didRemoveObject:snapshot atIndex:index];
109-
}
88+
[self removeSnapshot:snapshot withPreviousChildKey:previousSiblingKey];
11089
}
11190
withCancelBlock:^(NSError *error) {
11291
if ([self.delegate respondsToSelector:@selector(array:queryCancelledWithError:)]) {
@@ -117,21 +96,7 @@ - (void)observeQuery {
11796

11897
handle = [self.query observeEventType:FIRDataEventTypeChildMoved
11998
andPreviousSiblingKeyWithBlock:^(FIRDataSnapshot *snapshot, NSString *previousChildKey) {
120-
NSUInteger fromIndex = [self indexForKey:snapshot.key];
121-
[self.snapshots removeObjectAtIndex:fromIndex];
122-
123-
NSUInteger toIndex = 0;
124-
if (previousChildKey != nil) {
125-
NSUInteger prevIndex = [self indexForKey:previousChildKey];
126-
if (prevIndex != NSNotFound) {
127-
toIndex = prevIndex + 1;
128-
}
129-
}
130-
[self.snapshots insertObject:snapshot atIndex:toIndex];
131-
132-
if ([self.delegate respondsToSelector:@selector(array:didMoveObject:fromIndex:toIndex:)]) {
133-
[self.delegate array:self didMoveObject:snapshot fromIndex:fromIndex toIndex:toIndex];
134-
}
99+
[self moveSnapshot:snapshot withPreviousChildKey:previousChildKey];
135100
}
136101
withCancelBlock:^(NSError *error) {
137102
if ([self.delegate respondsToSelector:@selector(array:queryCancelledWithError:)]) {
@@ -158,6 +123,69 @@ - (NSUInteger)indexForKey:(NSString *)key {
158123
return NSNotFound;
159124
}
160125

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

163191
- (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)