-
Notifications
You must be signed in to change notification settings - Fork 131
/
Copy pathFirestack.m
404 lines (345 loc) · 15 KB
/
Firestack.m
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
//
// 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 "FirestackAnalytics.h"
#import "FirestackCloudMessaging.h"
static Firestack *_sharedInstance = nil;
static dispatch_once_t onceToken;
@implementation Firestack
typedef void (^UserWithTokenResponse)(NSDictionary *, NSError *);
- (void)dealloc
{
NSLog(@"Dealloc called on Firestack: %@", self);
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
// TODO: Implement
+ (void) setup:(UIApplication *) application
withLaunchOptions: (NSDictionary *) launchOptions
{
NSLog(@"Called setup for firestack with application");
dispatch_once(&onceToken, ^{
[application registerForRemoteNotifications];
[[NSNotificationCenter defaultCenter]
postNotificationName:kFirestackInitialized
object:nil];
});
}
- (id) init
{
self = [super init];
if (self != nil) {
NSLog(@"Setting up Firestace instance");
[Firestack initializeFirestack:self];
}
return self;
}
+ (void) initializeFirestack:(Firestack *) instance
{
dispatch_once(&onceToken, ^{
_sharedInstance = instance;
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(reloadFirestack)
name:RCTReloadNotification
object:nil];
[[NSNotificationCenter defaultCenter]
postNotificationName:kFirestackInitialized
object:nil];
});
}
+ (instancetype) sharedInstance
{
return _sharedInstance;
}
+ (void) reloadFirestack
{
// Reloading firestack
onceToken = 0; // not sure if this is a good idea or a bad idea...
[[Firestack sharedInstance] debugLog:@"Firestack"
msg:@"Reloading firestack"];
_sharedInstance = nil;
}
- (FIRApp *) firebaseApp
{
return [FIRApp defaultApp];
}
RCT_EXPORT_MODULE(Firestack);
RCT_EXPORT_METHOD(serverValue:(RCTResponseSenderBlock) callback)
{
callback(@[[NSNull null], @{
@"TIMESTAMP": [FIRServerValue timestamp]
}]);
}
RCT_EXPORT_METHOD(configureWithOptions:(NSDictionary *) opts
callback:(RCTResponseSenderBlock)callback)
{
dispatch_async(dispatch_get_main_queue(),^{
// Are we debugging, yo?
self.debug = [opts valueForKey:@"debug"] != nil ? YES : NO;
NSLog(@"options passed into configureWithOptions: %@", [opts valueForKey:@"debug"]);
NSDictionary *keyMapping = @{
@"GOOGLE_APP_ID": @[
@"appId",
@"googleAppId",
@"applicationId"
],
@"BUNDLE_ID": @[
@"bundleId",
@"bundleID"
],
@"GCM_SENDER_ID": @[
@"gcmSenderID",
@"GCMSenderID"
],
@"API_KEY": @[
@"apiKey"
],
@"CLIENT_ID": @[
@"clientId",
@"clientID"
],
@"TRACKING_ID": @[
@"trackingID",
@"trackingId"
],
@"ANDROID_CLIENT_ID": @[
@"applicationId",
@"clientId",
@"clientID",
@"androidClientID",
@"androidClientId"
],
@"DATABASE_URL": @[
@"databaseUrl",
@"databaseURL"
],
@"STORAGE_BUCKET": @[
@"storageBucket"
],
@"PROJECT_ID": @[
@"projectId",
@"projectID"
],
@"TRACKING_ID": @[
@"trackingID",
@"trackingId"
],
@"DEEP_LINK_SCHEME": @[
@"deepLinkScheme"
],
@"MESSAGING_SENDER_ID": @[
@"messagingSenderId",
@"messagingSenderID"
]
};
NSArray *optionKeys = [keyMapping allKeys];
NSMutableDictionary *props;
NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"GoogleService-Info" ofType:@"plist"];
if ([[NSFileManager defaultManager] fileExistsAtPath:plistPath]) {
// If the Firebase plist is included
props = [NSMutableDictionary dictionaryWithContentsOfFile:plistPath];
} else {
props = [[NSMutableDictionary alloc] initWithCapacity:[optionKeys count]];
}
// 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];
}
[props setValue:bundleID forKey:@"BUNDLE_ID"];
// Prefer the user configuration options over the default options
for (int i=0; i < [optionKeys count]; i++) {
// Traditional for loop here
@try {
NSString *key = [optionKeys objectAtIndex:i];
// If the name is capitalized
if ([opts valueForKey:key] != nil) {
NSString *value = [opts valueForKey:key];
[props setValue:value forKey:key];
}
NSArray *possibleNames = [keyMapping objectForKey:key];
for (NSString *name in possibleNames) {
if ([opts valueForKey:name] != nil) {
// The user passed this option in
NSString *value = [opts valueForKey:name];
[props setValue:value forKey:key];
}
}
}
@catch (NSException *err) {
// Uh oh?
NSLog(@"An error occurred: %@", err);
}
}
@try {
if (self.debug) {
NSLog(@"props ->: %@", props);
NSLog(@"GOOGLE_APP_ID: %@", [props valueForKey:@"GOOGLE_APP_ID"]);
NSLog(@"BUNDLE_ID: %@", [props valueForKey:@"BUNDLE_ID"]);
NSLog(@"GCM_SENDER_ID: %@", [props valueForKey:@"GCM_SENDER_ID"]);
NSLog(@"API_KEY: %@", [props valueForKey:@"API_KEY"]);
NSLog(@"CLIENT_ID: %@", [props valueForKey:@"CLIENT_ID"]);
NSLog(@"TRACKING_ID: %@", [props valueForKey:@"TRACKING_ID"]);
NSLog(@"ANDROID_CLIENT_ID: %@", [props valueForKey:@"ANDROID_CLIENT_ID"]);
NSLog(@"DATABASE_URL: %@", [props valueForKey:@"DATABASE_URL"]);
NSLog(@"STORAGE_BUCKET: %@", [props valueForKey:@"STORAGE_BUCKET"]);
NSLog(@"DEEP_LINK_SCHEME: %@", [props valueForKey:@"DEEP_LINK_SCHEME"]);
}
FIROptions *finalOptions = [[FIROptions alloc]
initWithGoogleAppID:[props valueForKey:@"GOOGLE_APP_ID"]
bundleID:[props valueForKey:@"BUNDLE_ID"]
GCMSenderID:[props valueForKey:@"GCM_SENDER_ID"]
APIKey:[props valueForKey:@"API_KEY"]
clientID:[props valueForKey:@"CLIENT_ID"]
trackingID:[props valueForKey:@"TRACKING_ID"]
androidClientID:[props valueForKey:@"ANDROID_CLIENT_ID"]
databaseURL:[props valueForKey:@"DATABASE_URL"]
storageBucket:[props valueForKey:@"STORAGE_BUCKET"]
deepLinkURLScheme:[props valueForKey:@"DEEP_LINK_SCHEME"]];
// Save configuration option
// NSDictionary *cfg = [self getConfig];
// [cfg setValuesForKeysWithDictionary:props];
// if (!self.configured) {
//If you want this method could replace all of the above for the option setting for firebase
//FIROptions *options = [[FIROptions alloc] initWithContentsOfFile:plistPath];
if ([FIRApp defaultApp] == NULL) {
[FIRApp configureWithOptions:finalOptions];
}
[Firestack initializeFirestack:self];
callback(@[[NSNull null], props]);
}
@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
#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) {
NSLog(@"%@: %@", title, msg);
}
}
// Not sure how to get away from this... yet
- (NSArray<NSString *> *)supportedEvents {
return @[
INITIALIZED_EVENT,
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