Skip to content

Commit 717d394

Browse files
committed
rename firebase database classes to use objc prefix convention
1 parent ae93621 commit 717d394

File tree

4 files changed

+251
-1010
lines changed

4 files changed

+251
-1010
lines changed

FirebaseDatabaseUI/FUIArrayDelegate.h

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,45 +21,54 @@
2121
@class FUIArray;
2222

2323
/**
24-
* A protocol to allow instances of FUIArray to raise events through a
25-
* delegate. Raises all Firebase events except FIRDataEventTypeValue.
24+
* A protocol to allow instances of FirebaseArray to raise events through a
25+
* delegate. Raises all
26+
* Firebase events except FIRDataEventTypeValue.
2627
*/
2728
@protocol FUIArrayDelegate<NSObject>
2829

2930
@optional
3031

3132
/**
32-
* Delegate method which is called whenever an object is added to an FUIArray.
33-
* On a FUIArray synchronized to a Firebase reference, this corresponds to a
34-
* @c FIRDataEventTypeChildAdded event being raised.
35-
* @param object The object added to the FUIArray
33+
* Delegate method which is called whenever an object is added to a
34+
* FirebaseArray. On a
35+
* FirebaseArray synchronized to a Firebase reference, this corresponds to an
36+
* [FIRDataEventTypeChildAdded](https://www.firebase.com/docs/ios/guide/retrieving-data.html#section-event-types)
37+
* event being raised.
38+
* @param object The object added to the FirebaseArray
3639
* @param index The index the child was added at
3740
*/
3841
- (void)array:(FUIArray *)array didAddObject:(id)object atIndex:(NSUInteger)index;
3942

4043
/**
41-
* Delegate method which is called whenever an object is changed in an
42-
* FUIArray. On a FUIArray synchronized to a Firebase reference, this
43-
* corresponds to a @c FIRDataEventTypeChildChanged event being raised.
44-
* @param object The object that changed in the FUIArray
44+
* Delegate method which is called whenever an object is chinged in a
45+
* FirebaseArray. On a
46+
* FirebaseArray synchronized to a Firebase reference, this corresponds to an
47+
* [FIRDataEventTypeChildChanged](https://www.firebase.com/docs/ios/guide/retrieving-data.html#section-event-types)
48+
* event being raised.
49+
* @param object The object that changed in the FirebaseArray
4550
* @param index The index the child was changed at
4651
*/
4752
- (void)array:(FUIArray *)array didChangeObject:(id)object atIndex:(NSUInteger)index;
4853

4954
/**
50-
* Delegate method which is called whenever an object is removed from an
51-
* FUIArray. On a FUIArray synchronized to a Firebase reference, this
52-
* corresponds to a @c FIRDataEventTypeChildRemoved event being raised.
53-
* @param object The object removed from the FUIArray
55+
* Delegate method which is called whenever an object is removed from a
56+
* FirebaseArray. On a
57+
* FirebaseArray synchronized to a Firebase reference, this corresponds to an
58+
* [FIRDataEventTypeChildRemoved](https://www.firebase.com/docs/ios/guide/retrieving-data.html#section-event-types)
59+
* event being raised.
60+
* @param object The object removed from the FirebaseArray
5461
* @param index The index the child was removed at
5562
*/
5663
- (void)array:(FUIArray *)array didRemoveObject:(id)object atIndex:(NSUInteger)index;
5764

5865
/**
5966
* Delegate method which is called whenever an object is moved within a
60-
* FUIArray. On a FUIArray synchronized to a Firebase reference, this
61-
* corresponds to a @c FIRDataEventTypeChildMoved event being raised.
62-
* @param object The object that has moved locations in the FUIArray
67+
* FirebaseArray. On a
68+
* FirebaseArray synchronized to a Firebase reference, this corresponds to an
69+
* [FIRDataEventTypeChildMoved](https://www.firebase.com/docs/ios/guide/retrieving-data.html#section-event-types)
70+
* event being raised.
71+
* @param object The object that has moved locations in the FirebaseArray
6372
* @param fromIndex The index the child is being moved from
6473
* @param toIndex The index the child is being moved to
6574
*/
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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 FirebaseDatabaseUI;
22+
23+
NS_ASSUME_NONNULL_BEGIN
24+
25+
// Dumb object holding a pair of blocks and a data event type.
26+
@interface FUIDataEventHandler: NSObject
27+
@property (nonatomic, assign) FIRDataEventType event;
28+
@property (nonatomic, copy) void (^success)(FIRDataSnapshot *_Nonnull, NSString *_Nullable);
29+
@property (nonatomic, copy) void (^cancelled)(NSError *_Nonnull);
30+
@end
31+
32+
// Horrible abuse of ObjC type system, since FirebaseArray is unfortunately coupled to
33+
// FIRDataSnapshot
34+
@interface FUIFakeSnapshot: NSObject
35+
- (instancetype)initWithKey:(NSString *)key value:(NSString *)value;
36+
@property (nonatomic, copy) NSString *key;
37+
@property (nonatomic, copy) NSString *value;
38+
@end
39+
40+
// A dummy observable so we can test this without relying on an internet connection.
41+
@interface FUITestObservable: NSObject <FIRDataObservable>
42+
43+
// Map of handles to observers.
44+
@property (nonatomic, readonly) NSMutableDictionary<NSNumber *, FUIDataEventHandler *> *observers;
45+
46+
// Incremented to generate unique handles.
47+
@property (nonatomic, readonly, assign) FIRDatabaseHandle current;
48+
49+
- (void)removeAllObservers;
50+
51+
// Sends an event to the observable's observers.
52+
- (void)sendEvent:(FIRDataEventType)event
53+
withObject:(nullable FUIFakeSnapshot *)object
54+
previousKey:(nullable NSString *)string
55+
error:(nullable NSError *)error;
56+
57+
// Inserts sequentially with data provided by the `generator` block. Snapshot keys
58+
// are increasing integers as strings, snapshot values are strings returned by the
59+
// `generator` block.
60+
- (void)populateWithCount:(NSUInteger)count generator:(NSString *(^)(NSUInteger))generator;
61+
62+
// Sends a bunch of insertion events with snapshot keys as integer strings (i.e. @"0") of increasing
63+
// order, starting from 0.
64+
- (void)populateWithCount:(NSUInteger)count;
65+
66+
@end
67+
68+
@interface FUIFirebaseArrayTestDelegate : NSObject <FUIArrayDelegate>
69+
@property (nonatomic, copy) void (^queryCancelled)(FUIArray *array, NSError *error);
70+
@property (nonatomic, copy) void (^didAddObject)(FUIArray *array, id object, NSUInteger index);
71+
@property (nonatomic, copy) void (^didChangeObject)(FUIArray *array, id object, NSUInteger index);
72+
@property (nonatomic, copy) void (^didRemoveObject)(FUIArray *array, id object, NSUInteger index);
73+
@property (nonatomic, copy) void (^didMoveObject)(FUIArray *array, id object, NSUInteger fromIndex, NSUInteger toIndex);
74+
@end
75+
76+
NS_ASSUME_NONNULL_END
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
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 "FirebaseDatabaseTestUtils.h"
22+
23+
@implementation FUIDataEventHandler
24+
@end
25+
26+
@implementation FUIFakeSnapshot
27+
- (instancetype)initWithKey:(NSString *)key value:(NSString *)value {
28+
self = [super init];
29+
if (self != nil) {
30+
_key = [key copy];
31+
_value = [value copy];
32+
}
33+
return self;
34+
}
35+
@end
36+
37+
@implementation FUITestObservable
38+
39+
- (instancetype)init {
40+
self = [super init];
41+
if (self != nil) {
42+
_observers = [NSMutableDictionary dictionary];
43+
}
44+
return self;
45+
}
46+
47+
- (void)removeObserverWithHandle:(FIRDatabaseHandle)handle {
48+
[self.observers removeObjectForKey:@(handle)];
49+
}
50+
51+
- (void)removeAllObservers {
52+
_observers = [NSMutableDictionary dictionary];
53+
}
54+
55+
- (FIRDatabaseHandle)observeEventType:(FIRDataEventType)eventType
56+
andPreviousSiblingKeyWithBlock:(void (^)(FIRDataSnapshot * _Nonnull, NSString * _Nullable))block
57+
withCancelBlock:(void (^)(NSError * _Nonnull))cancelBlock {
58+
FUIDataEventHandler *handler = [[FUIDataEventHandler alloc] init];
59+
handler.event = eventType;
60+
handler.success = block;
61+
handler.cancelled = cancelBlock;
62+
63+
NSNumber *key = @(self.current);
64+
_current++;
65+
self.observers[key] = handler;
66+
return key.unsignedIntegerValue;
67+
}
68+
69+
- (void)sendEvent:(FIRDataEventType)event
70+
withObject:(FUIFakeSnapshot *)object
71+
previousKey:(NSString *)string
72+
error:(NSError *)error {
73+
NSArray *allKeys = self.observers.allKeys;
74+
for (NSNumber *key in allKeys) {
75+
FUIDataEventHandler *handler = self.observers[key];
76+
if (handler.event == event) {
77+
if (error != nil) { handler.cancelled(error); }
78+
else { handler.success((FIRDataSnapshot *)object, string); }
79+
}
80+
}
81+
}
82+
83+
- (void)populateWithCount:(NSUInteger)count generator:(NSString *(^)(NSUInteger))generator {
84+
NSString *previous = nil;
85+
for (NSUInteger i = 0; i < count; i++) {
86+
FUIFakeSnapshot *snap = [[FUIFakeSnapshot alloc] init];
87+
snap.value = generator(i);
88+
snap.key = @(i).stringValue;
89+
[self sendEvent:FIRDataEventTypeChildAdded withObject:snap previousKey:previous error:nil];
90+
previous = snap.key;
91+
}
92+
}
93+
94+
- (void)populateWithCount:(NSUInteger)count {
95+
[self populateWithCount:count generator:^NSString *(NSUInteger index) {
96+
return @(index).stringValue;
97+
}];
98+
}
99+
100+
@end
101+
102+
@implementation FUIFirebaseArrayTestDelegate
103+
104+
- (void)array:(FUIArray *)array didAddObject:(id)object atIndex:(NSUInteger)index {
105+
if (self.didAddObject != NULL) {
106+
self.didAddObject(array, object, index);
107+
}
108+
}
109+
110+
- (void)array:(FUIArray *)array didChangeObject:(id)object atIndex:(NSUInteger)index {
111+
if (self.didChangeObject != NULL) {
112+
self.didChangeObject(array, object, index);
113+
}
114+
}
115+
116+
- (void)array:(FUIArray *)array didRemoveObject:(id)object atIndex:(NSUInteger)index {
117+
if (self.didRemoveObject != NULL) {
118+
self.didRemoveObject(array, object, index);
119+
}
120+
}
121+
122+
- (void)array:(FUIArray *)array didMoveObject:(id)object
123+
fromIndex:(NSUInteger)fromIndex toIndex:(NSUInteger)toIndex {
124+
if (self.didMoveObject != NULL) {
125+
self.didMoveObject(array, object, fromIndex, toIndex);
126+
}
127+
}
128+
129+
- (void)array:(FUIArray *)array queryCancelledWithError:(NSError *)error {
130+
if (self.queryCancelled != NULL) {
131+
self.queryCancelled(array, error);
132+
}
133+
}
134+
135+
@end

0 commit comments

Comments
 (0)