Skip to content

Commit d51fca6

Browse files
committed
WIP: firestack reloading
1 parent c9ca92c commit d51fca6

File tree

3 files changed

+100
-153
lines changed

3 files changed

+100
-153
lines changed

ios/Firestack/AppDelegate+Firestack.m

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,14 @@ - (void) dealloc
5151
- (void) setupListeners
5252
{
5353
// Post notification that we've initialized Firebase
54-
[[NSNotificationCenter defaultCenter]
55-
postNotificationName:kFirestackInitialized
56-
object:nil];
54+
// [[NSNotificationCenter defaultCenter]
55+
// postNotificationName:kFirestackInitialized
56+
// object:nil];
57+
58+
[[NSNotificationCenter defaultCenter] addObserver:self
59+
selector:@selector(firestackConfigured:)
60+
name:kFirestackInitialized
61+
object:nil];
5762

5863
// Add listener for when firestack the app reloads
5964
[[NSNotificationCenter defaultCenter] addObserver:self
@@ -67,4 +72,9 @@ - (void) reloadFirestack
6772
// TODO:
6873
}
6974

75+
- (void) firestackConfigured:(NSDictionary *) configuration
76+
{
77+
NSLog(@"firestackConfigured: %@", configuration);
78+
}
79+
7080
@end

ios/Firestack/Firestack.m

Lines changed: 85 additions & 150 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ + (void) initializeFirestack:(Firestack *) instance
4242

4343
_sharedInstance = instance;
4444

45+
[[NSNotificationCenter defaultCenter]
46+
postNotificationName:kFirestackInitialized
47+
object:[instance getConfig]];
48+
4549
[[NSNotificationCenter defaultCenter] addObserver:self
4650
selector:@selector(reloadFirestack)
4751
name:RCTReloadNotification
@@ -81,166 +85,97 @@ - (FIRApp *) firebaseApp
8185
RCT_EXPORT_METHOD(configureWithOptions:(NSDictionary *) opts
8286
callback:(RCTResponseSenderBlock)callback)
8387
{
84-
NSLog(@"dispatch once on configure");
85-
dispatch_async(dispatch_get_main_queue(),^{
86-
// Are we debugging, yo?
87-
self.debug = [opts valueForKey:@"debug"] != nil ? YES : NO;
88-
NSLog(@"options passed into configureWithOptions: %@", [opts valueForKey:@"debug"]);
89-
88+
// Are we debugging, yo?
89+
self.debug = [opts valueForKey:@"debug"] != nil ? YES : NO;
90+
NSDictionary *props = [self pluckOptions:opts];
91+
92+
@try {
93+
if (self.debug) {
94+
NSLog(@"debugging: %@", [props valueForKey:@"debug"]);
95+
NSLog(@"cloudMessaging: %@", [props valueForKey:@"cloudMessaging"]);
96+
}
97+
98+
NSLog(@"Configuring firestack instance: %@", self);
99+
// Save configuration option
100+
NSDictionary *cfg = [self getConfig];
101+
[cfg setValuesForKeysWithDictionary:props];
102+
self.configuration = cfg;
103+
104+
[Firestack initializeFirestack:self];
105+
self.configured = YES;
106+
callback(@[[NSNull null], props]);
107+
}
108+
@catch (NSException *exception) {
109+
NSLog(@"Exception occurred while configuring: %@", exception);
110+
[self debugLog:@"Configuring error"
111+
msg:[NSString stringWithFormat:@"An error occurred while configuring: %@", [exception debugDescription]]];
112+
NSDictionary *errProps = @{
113+
@"error": [exception name],
114+
@"description": [exception debugDescription]
115+
};
116+
callback(@[errProps]);
117+
}
118+
}
119+
120+
/**
121+
* Pluck the options that are given by JS
122+
**/
123+
- (NSDictionary *) pluckOptions:(NSDictionary *) opts
124+
{
90125
NSDictionary *keyMapping = @{
91-
@"GOOGLE_APP_ID": @[
92-
@"appId",
93-
@"googleAppId",
94-
@"applicationId"
95-
],
96-
@"BUNDLE_ID": @[
97-
@"bundleId",
98-
@"bundleID"
99-
],
100-
@"GCM_SENDER_ID": @[
101-
@"gcmSenderID",
102-
@"GCMSenderID"
103-
],
104-
@"API_KEY": @[
105-
@"apiKey"
106-
],
107-
@"CLIENT_ID": @[
108-
@"clientId",
109-
@"clientID"
110-
],
111-
@"TRACKING_ID": @[
112-
@"trackingID",
113-
@"trackingId"
114-
],
115-
@"ANDROID_CLIENT_ID": @[
116-
@"applicationId",
117-
@"clientId",
118-
@"clientID",
119-
@"androidClientID",
120-
@"androidClientId"
121-
],
122-
@"DATABASE_URL": @[
123-
@"databaseUrl",
124-
@"databaseURL"
125-
],
126-
@"STORAGE_BUCKET": @[
127-
@"storageBucket"
128-
],
129-
@"PROJECT_ID": @[
130-
@"projectId",
131-
@"projectID"
132-
],
133-
@"TRACKING_ID": @[
134-
@"trackingID",
135-
@"trackingId"
136-
],
137-
@"DEEP_LINK_SCHEME": @[
138-
@"deepLinkScheme"
139-
],
140-
@"MESSAGING_SENDER_ID": @[
141-
@"messagingSenderId",
142-
@"messagingSenderID"
143-
]
144-
};
145-
NSArray *optionKeys = [keyMapping allKeys];
146-
147-
NSMutableDictionary *props;
148-
149-
NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"GoogleService-Info" ofType:@"plist"];
150-
151-
if ([[NSFileManager defaultManager] fileExistsAtPath:plistPath]) {
152-
// If the Firebase plist is included
153-
props = [NSMutableDictionary dictionaryWithContentsOfFile:plistPath];
154-
} else {
155-
props = [[NSMutableDictionary alloc] initWithCapacity:[optionKeys count]];
126+
@"cloudMessaging": @[
127+
@"messaging",
128+
]
129+
};
130+
NSArray *optionKeys = [keyMapping allKeys];
131+
NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"Info" ofType:@"plist"];
132+
133+
NSDictionary *defaultOptions = @{
134+
@"debug": @NO,
135+
@"cloudMessaging": @{
136+
@"enabled": @YES
156137
}
157-
158-
// Bundle ID either from options OR from the main bundle
159-
NSString *bundleID;
160-
if ([opts valueForKey:@"bundleID"]) {
161-
bundleID = [opts valueForKey:@"bundleID"];
162-
} else {
163-
bundleID = [[NSBundle mainBundle] bundleIdentifier];
138+
};
139+
140+
NSMutableDictionary *props = [defaultOptions copy];
141+
142+
if ([[NSFileManager defaultManager] fileExistsAtPath:plistPath]) {
143+
// If the Info plist is included
144+
NSDictionary *infoProps = [NSMutableDictionary dictionaryWithContentsOfFile:plistPath];
145+
NSDictionary *firestackOptions = [infoProps valueForKey:@"firestack"];
146+
if (firestackOptions != nil) {
147+
props = [firestackOptions copy];
164148
}
165-
[props setValue:bundleID forKey:@"BUNDLE_ID"];
166-
167-
// Prefer the user configuration options over the default options
168-
for (int i=0; i < [optionKeys count]; i++) {
169-
// Traditional for loop here
170-
@try {
171-
NSString *key = [optionKeys objectAtIndex:i];
172-
// If the name is capitalized
173-
if ([opts valueForKey:key] != nil) {
174-
NSString *value = [opts valueForKey:key];
149+
}
150+
151+
// Prefer the user configuration options over the default options
152+
for (int i=0; i < [optionKeys count]; i++) {
153+
// Traditional for loop here
154+
@try {
155+
NSString *key = [optionKeys objectAtIndex:i];
156+
157+
// If the name is the key name
158+
NSMutableArray *possibleNames = [NSMutableArray arrayWithArray:[keyMapping objectForKey:key]];
159+
[possibleNames addObject:key];
160+
161+
for (NSString *name in possibleNames) {
162+
if ([opts valueForKey:name] != nil) {
163+
// The user passed this option in
164+
NSString *value = [opts valueForKey:name];
175165
[props setValue:value forKey:key];
176166
}
177-
178-
NSArray *possibleNames = [keyMapping objectForKey:key];
179-
180-
for (NSString *name in possibleNames) {
181-
if ([opts valueForKey:name] != nil) {
182-
// The user passed this option in
183-
NSString *value = [opts valueForKey:name];
184-
[props setValue:value forKey:key];
185-
}
186-
}
187-
}
188-
@catch (NSException *err) {
189-
// Uh oh?
190-
NSLog(@"An error occurred: %@", err);
191167
}
192168
}
193-
194-
@try {
195-
if (self.debug) {
196-
NSLog(@"props ->: %@", props);
197-
NSLog(@"GOOGLE_APP_ID: %@", [props valueForKey:@"GOOGLE_APP_ID"]);
198-
NSLog(@"BUNDLE_ID: %@", [props valueForKey:@"BUNDLE_ID"]);
199-
NSLog(@"GCM_SENDER_ID: %@", [props valueForKey:@"GCM_SENDER_ID"]);
200-
NSLog(@"API_KEY: %@", [props valueForKey:@"API_KEY"]);
201-
NSLog(@"CLIENT_ID: %@", [props valueForKey:@"CLIENT_ID"]);
202-
NSLog(@"TRACKING_ID: %@", [props valueForKey:@"TRACKING_ID"]);
203-
NSLog(@"ANDROID_CLIENT_ID: %@", [props valueForKey:@"ANDROID_CLIENT_ID"]);
204-
NSLog(@"DATABASE_URL: %@", [props valueForKey:@"DATABASE_URL"]);
205-
NSLog(@"STORAGE_BUCKET: %@", [props valueForKey:@"STORAGE_BUCKET"]);
206-
NSLog(@"DEEP_LINK_SCHEME: %@", [props valueForKey:@"DEEP_LINK_SCHEME"]);
207-
}
208-
209-
FIROptions *finalOptions = [[FIROptions alloc]
210-
initWithGoogleAppID:[props valueForKey:@"GOOGLE_APP_ID"]
211-
bundleID:[props valueForKey:@"BUNDLE_ID"]
212-
GCMSenderID:[props valueForKey:@"GCM_SENDER_ID"]
213-
APIKey:[props valueForKey:@"API_KEY"]
214-
clientID:[props valueForKey:@"CLIENT_ID"]
215-
trackingID:[props valueForKey:@"TRACKING_ID"]
216-
androidClientID:[props valueForKey:@"ANDROID_CLIENT_ID"]
217-
databaseURL:[props valueForKey:@"DATABASE_URL"]
218-
storageBucket:[props valueForKey:@"STORAGE_BUCKET"]
219-
deepLinkURLScheme:[props valueForKey:@"DEEP_LINK_SCHEME"]];
220-
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;
229-
callback(@[[NSNull null], props]);
230-
}
231-
@catch (NSException *exception) {
232-
NSLog(@"Exception occurred while configuring: %@", exception);
233-
[self debugLog:@"Configuring error"
234-
msg:[NSString stringWithFormat:@"An error occurred while configuring: %@", [exception debugDescription]]];
235-
NSDictionary *errProps = @{
236-
@"error": [exception name],
237-
@"description": [exception debugDescription]
238-
};
239-
callback(@[errProps]);
169+
@catch (NSException *err) {
170+
// Uh oh?
171+
NSLog(@"An error occurred: %@", err);
240172
}
241-
});
173+
}
174+
175+
return props;
242176
}
243177

178+
244179
RCT_EXPORT_METHOD(configure:(RCTResponseSenderBlock)callback)
245180
{
246181
NSDictionary *props = @{};

ios/Firestack/FirestackCloudMessaging.m

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,8 @@ - (void) handleTokenRefresh
190190
}]);
191191
}
192192

193+
194+
193195
RCT_EXPORT_METHOD(sendLocal:(UILocalNotification *)notification
194196
callback:(RCTResponseSenderBlock) callback)
195197
{

0 commit comments

Comments
 (0)