|
4 | 4 | // Copyright © 2016 Facebook. All rights reserved. |
5 | 5 | // |
6 | 6 |
|
| 7 | +#import <objc/runtime.h> |
| 8 | + |
7 | 9 | #import "Firestack.h" |
8 | 10 | #import "FirestackErrors.h" |
9 | 11 | #import "FirestackEvents.h" |
10 | | -#import "FirestackCloudMessaging.h" |
| 12 | +#import "FirestackAnalytics.h" |
| 13 | +// #import "FirestackCloudMessaging.h" |
11 | 14 |
|
12 | 15 | @import Firebase; |
13 | 16 |
|
| 17 | +static Firestack *_sharedInstance = nil; |
| 18 | + |
14 | 19 | @implementation Firestack |
| 20 | + |
15 | 21 | typedef void (^UserWithTokenResponse)(NSDictionary *, NSError *); |
16 | 22 |
|
17 | 23 | - (void)dealloc |
18 | 24 | { |
19 | | - NSLog(@"Dealloc called on Firestack"); |
20 | | - [[FIRApp defaultApp] deleteApp:^(BOOL success) { |
21 | | - if (success) { |
22 | | - NSLog(@"Successfully removed app"); |
23 | | - } else { |
24 | | - NSLog(@"Unsuccessfully cleaned up all"); |
25 | | - } |
26 | | - }]; |
| 25 | + NSLog(@"Dealloc called on Firestack: %@", self); |
| 26 | + [[NSNotificationCenter defaultCenter] removeObserver:self]; |
| 27 | +} |
| 28 | + |
| 29 | +- (instancetype) init { |
| 30 | + self = [super init]; |
| 31 | + if (self) { |
| 32 | + NSLog(@"Initializing Firestack: %@", self); |
| 33 | + [Firestack initializeFirestack:self]; |
| 34 | + } |
| 35 | + return self; |
27 | 36 | } |
28 | 37 |
|
29 | | -+ (void) setup:(UIApplication *) application |
| 38 | ++ (void) initializeFirestack:(Firestack *) instance |
30 | 39 | { |
31 | | - [FirestackCloudMessaging setup:application]; |
| 40 | + NSLog(@"Shared instance created on Firestack: %@", instance); |
| 41 | + // [FIRApp configureWithOptions:finalOptions]; |
| 42 | + |
| 43 | + _sharedInstance = instance; |
| 44 | + |
| 45 | + [[NSNotificationCenter defaultCenter] addObserver:self |
| 46 | + selector:@selector(reloadFirestack) |
| 47 | + name:RCTReloadNotification |
| 48 | + object:nil]; |
| 49 | + |
| 50 | + // [[NSNotificationCenter defaultCenter] |
| 51 | + // postNotificationName:RCTReloadNotification |
| 52 | + // object:nil |
| 53 | + // userInfo:nil]; |
| 54 | + |
| 55 | + // [[NSNotificationCenter defaultCenter] addObserver:self |
| 56 | + // selector:@selector(firestackConfigured:) |
| 57 | + // name:kFirestackInitialized |
| 58 | + // object:instance]; |
32 | 59 | } |
33 | 60 |
|
| 61 | ++ (instancetype) sharedInstance |
| 62 | +{ |
| 63 | + return _sharedInstance; |
| 64 | +} |
| 65 | + |
| 66 | ++ (void) reloadFirestack |
| 67 | +{ |
| 68 | + // Reloading firestack |
| 69 | + [[Firestack sharedInstance] debugLog:@"Firestack" |
| 70 | + msg:@"Reloading firestack"]; |
| 71 | +} |
| 72 | + |
| 73 | +- (FIRApp *) firebaseApp |
| 74 | +{ |
| 75 | + return [FIRApp defaultApp]; |
| 76 | +} |
| 77 | + |
| 78 | + |
34 | 79 | RCT_EXPORT_MODULE(Firestack); |
35 | 80 |
|
36 | 81 | RCT_EXPORT_METHOD(configureWithOptions:(NSDictionary *) opts |
37 | 82 | callback:(RCTResponseSenderBlock)callback) |
38 | 83 | { |
| 84 | + NSLog(@"dispatch once on configure"); |
39 | 85 | dispatch_async(dispatch_get_main_queue(),^{ |
40 | 86 | // Are we debugging, yo? |
41 | 87 | self.debug = [opts valueForKey:@"debug"] != nil ? YES : NO; |
@@ -172,13 +218,14 @@ + (void) setup:(UIApplication *) application |
172 | 218 | storageBucket:[props valueForKey:@"STORAGE_BUCKET"] |
173 | 219 | deepLinkURLScheme:[props valueForKey:@"DEEP_LINK_SCHEME"]]; |
174 | 220 |
|
175 | | - // Save configuration option |
176 | | - // NSDictionary *cfg = [self getConfig]; |
177 | | - // [cfg setValuesForKeysWithDictionary:props]; |
178 | | - |
179 | | - // if (!self.configured) { |
180 | | - |
181 | | - [FIRApp configureWithOptions:finalOptions]; |
| 221 | + NSLog(@"Configuring firestack instance: %@", self); |
| 222 | + // Save configuration option |
| 223 | + // NSDictionary *cfg = [self getConfig]; |
| 224 | + // [cfg setValuesForKeysWithDictionary:props]; |
| 225 | + // self.configuration = cfg; |
| 226 | + |
| 227 | + [Firestack initializeFirestack:self]; |
| 228 | + self.configured = YES; |
182 | 229 | callback(@[[NSNull null], props]); |
183 | 230 | } |
184 | 231 | @catch (NSException *exception) { |
@@ -312,17 +359,12 @@ - (void) debugLog:(NSString *)title |
312 | 359 | { |
313 | 360 | if (self.debug) { |
314 | 361 | NSLog(@"%@: %@", title, msg); |
315 | | - // [self sendJSEvent:DEBUG_EVENT |
316 | | - // props:@{ |
317 | | - // @"name": title, |
318 | | - // @"message": msg |
319 | | - // }]; |
320 | 362 | } |
321 | 363 | } |
322 | 364 |
|
323 | 365 | // Not sure how to get away from this... yet |
324 | 366 | - (NSArray<NSString *> *)supportedEvents { |
325 | | - return @[DEBUG_EVENT, AUTH_CHANGED_EVENT]; |
| 367 | + return @[INITIALIZED_EVENT, DEBUG_EVENT, AUTH_CHANGED_EVENT]; |
326 | 368 | } |
327 | 369 |
|
328 | 370 | - (void) sendJSEvent:(NSString *)title |
|
0 commit comments