-
Notifications
You must be signed in to change notification settings - Fork 128
Expand file tree
/
Copy pathFirestack.m
More file actions
251 lines (209 loc) · 8.62 KB
/
Firestack.m
File metadata and controls
251 lines (209 loc) · 8.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
//
// Firestack.m
// Created by Ari Lerner on 5/31/16.
// Copyright © 2016 Facebook. All rights reserved.
//
#import "Firestack.h"
#import "FirestackErrors.h"
#import "FirestackEvents.h"
#import "FirestackMessaging.h"
@import Firebase;
@implementation Firestack
typedef void (^UserWithTokenResponse)(NSDictionary *, NSError *);
RCT_EXPORT_MODULE(Firestack);
RCT_EXPORT_METHOD(configureWithOptions:(NSDictionary *) opts
callback:(RCTResponseSenderBlock)callback)
{
// Are we debugging, yo?
self.debug = [opts valueForKey:@"debug"] != nil ? YES : NO;
FIROptions *firestackOptions = [FIROptions defaultOptions];
// Bundle ID either from options OR from the main bundle
NSString *bundleID;
if ([opts valueForKey:@"bundleID"]) {
bundleID = [opts valueForKey:@"bundleID"];
} else {
bundleID = [[NSBundle mainBundle] bundleIdentifier];
}
// Prefer the user configuration options over the default options
NSArray *keyOptions = @[@"APIKey", @"clientID", @"trackingID",
@"GCMSenderID", @"androidClientID",
@"googleAppID", @"databaseURL",
@"deepLinkURLScheme", @"storageBucket"];
NSMutableDictionary *props = [[NSMutableDictionary alloc] initWithCapacity:[keyOptions count]];
for (int i=0; i < [keyOptions count]; i++) {
// Traditional for loop here
@try {
NSString *key = [keyOptions objectAtIndex:i];
NSString *value = [opts valueForKey:key];
if (value != nil) {
[props setObject:value forKey:key];
} else if ([firestackOptions valueForKey:key] != nil) {
[props setObject:[firestackOptions valueForKey:key] forKey:key];
}
}
@catch (NSException *err) {
// Uh oh?
NSLog(@"An error occurred: %@", err);
}
}
// If the apiKey is lowercase
if ([opts valueForKey:@"apiKey"]) {
[props setValue:[opts valueForKey:@"apiKey"] forKey:@"APIKey"];
}
@try {
FIROptions *finalOptions = [[FIROptions alloc] initWithGoogleAppID:[props valueForKey:@"googleAppID"]
bundleID:bundleID
GCMSenderID:[props valueForKey:@"GCMSenderID"]
APIKey:[props valueForKey:@"APIKey"]
clientID:[props valueForKey:@"clientID"]
trackingID:[props valueForKey:@"trackingID"]
androidClientID:[props valueForKey:@"androidClientID"]
databaseURL:[props valueForKey:@"databaseURL"]
storageBucket:[props valueForKey:@"storageBucket"]
deepLinkURLScheme:[props valueForKey:@"deepLinkURLScheme"]];
for (NSString *key in props) {
[self debugLog:key msg:[finalOptions valueForKey:key]];
}
[self debugLog:@"bundleID" msg:bundleID];
// Save configuration option
NSDictionary *cfg = [self getConfig];
[cfg setValuesForKeysWithDictionary:props];
if (!self.configured) {
[FIRApp configureWithOptions:finalOptions];
self->_configured = YES;
}
callback(@[[NSNull null]]);
}
@catch (NSException *exception) {
NSLog(@"Exception occurred while configuring: %@", exception);
[self debugLog:@"Configuring error"
msg:[NSString stringWithFormat:@"An error occurred while configuring: %@", [exception debugDescription]]];
NSDictionary *errProps = @{
@"error": [exception name],
@"description": [exception debugDescription]
};
callback(@[errProps]);
}
}
RCT_EXPORT_METHOD(configure:(RCTResponseSenderBlock)callback)
{
NSDictionary *props = @{};
[self configureWithOptions:props
callback:callback];
}
#pragma mark - Storage
#pragma mark RemoteConfig
RCT_EXPORT_METHOD(setDefaultRemoteConfig:(NSDictionary *)props
callback:(RCTResponseSenderBlock) callback)
{
if (!self.remoteConfigInstance) {
// Create remote Config instance
self.remoteConfigInstance = [FIRRemoteConfig remoteConfig];
}
[self.remoteConfigInstance setDefaults:props];
callback(@[[NSNull null], props]);
}
RCT_EXPORT_METHOD(setDev:(RCTResponseSenderBlock) callback)
{
FIRRemoteConfigSettings *remoteConfigSettings = [[FIRRemoteConfigSettings alloc] initWithDeveloperModeEnabled:YES];
self.remoteConfigInstance.configSettings = remoteConfigSettings;
callback(@[[NSNull null], @"ok"]);
}
RCT_EXPORT_METHOD(configValueForKey:(NSString *)name
callback:(RCTResponseSenderBlock) callback)
{
if (!self.remoteConfigInstance) {
NSDictionary *err = @{
@"error": @"No configuration instance",
@"msg": @"No configuration instance set. Please call setDefaultRemoteConfig before using this feature"
};
callback(@[err]);
}
FIRRemoteConfigValue *value = [self.remoteConfigInstance configValueForKey:name];
NSString *valueStr = value.stringValue;
if (valueStr == nil) {
valueStr = @"";
}
callback(@[[NSNull null], valueStr]);
}
RCT_EXPORT_METHOD(fetchWithExpiration:(NSNumber*)expirationSeconds
callback:(RCTResponseSenderBlock) callback)
{
if (!self.remoteConfigInstance) {
NSDictionary *err = @{
@"error": @"No configuration instance",
@"msg": @"No configuration instance set. Please call setDefaultRemoteConfig before using this feature"
};
callback(@[err]);
}
NSTimeInterval expirationDuration = [expirationSeconds doubleValue];
[self.remoteConfigInstance fetchWithExpirationDuration:expirationDuration completionHandler:^(FIRRemoteConfigFetchStatus status, NSError *error) {
if (status == FIRRemoteConfigFetchStatusSuccess) {
NSLog(@"Config fetched!");
[self.remoteConfigInstance activateFetched];
callback(@[[NSNull null], @(YES)]);
} else {
NSLog(@"Error %@", error.localizedDescription);
NSDictionary *err = @{
@"error": @"No configuration instance",
@"msg": [error localizedDescription]
};
callback(@[err]);
}
}];
}
#pragma mark Database
#pragma mark Messaging
+ (void) registerForNotification:(NSString *) typeStr andToken:(NSData *)deviceToken
{
[FirestackMessaging registerForNotification:typeStr andToken:deviceToken];
}
#pragma mark Helpers
- (NSDictionary *) getConfig
{
if (self.configuration == nil) {
self.configuration = [[NSMutableDictionary alloc] initWithCapacity:20];
}
return self.configuration;
}
- (NSDictionary *) handleFirebaseError:(NSString *) name
error:(NSError *) error
withUser:(FIRUser *) user
{
return [FirestackErrors handleFirebaseError:name
error:error
withUser:user];
}
- (void) handleException:(NSException *)exception
withCallback:(RCTResponseSenderBlock)callback
{
[FirestackErrors handleException:exception
withCallback:callback];
}
- (void) debugLog:(NSString *)title
msg:(NSString *)msg
{
if (self.debug) {
// [self sendJSEvent:DEBUG_EVENT
// props:@{
// @"name": title,
// @"message": msg
// }];
}
}
// Not sure how to get away from this... yet
- (NSArray<NSString *> *)supportedEvents {
return @[DEBUG_EVENT, AUTH_CHANGED_EVENT];
}
- (void) sendJSEvent:(NSString *)title
props:(NSDictionary *)props
{
@try {
[self sendEventWithName:title
body:props];
}
@catch (NSException *err) {
NSLog(@"An error occurred in sendJSEvent: %@", [err debugDescription]);
}
}
@end