Skip to content

Commit 5814b1b

Browse files
committed
feat(core): support initializing app from path
1 parent 528e1ba commit 5814b1b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+1160
-14
lines changed

packages/firebase-core/index.android.ts

+112-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { IFirebaseOptions, FirebaseConfig } from './common';
2-
import { Utils } from '@nativescript/core';
2+
import { knownFolders, Utils, File } from '@nativescript/core';
33
export * from './utils';
44

55
export class FirebaseError extends Error {
@@ -257,6 +257,117 @@ export class Firebase {
257257

258258
return fbApp;
259259
}
260+
261+
262+
initializeAppWithPath(path: string, options: FirebaseOptions = null, config?: FirebaseConfig) {
263+
let json;
264+
const ctx = Utils.android.getApplicationContext() as android.content.Context;
265+
if (path.startsWith('res://')) {
266+
const jsonStr = (<any>org).nativescript.firebase.core.FirebaseCore.readRawAsset(ctx, path);
267+
console.log('jsonStr', jsonStr);
268+
json = JSON.parse(jsonStr);
269+
} else {
270+
if (path.startsWith('~/')) {
271+
path = knownFolders.currentApp().path + '/' + path.replace('~/', '');
272+
}
273+
json = require(path);
274+
}
275+
276+
277+
// always use first client
278+
279+
const client = json['client'][0];
280+
const oauth_clients = client['oauth_client'];
281+
const project_info = json['project_info'];
282+
const client_info = client['client_info'];
283+
284+
285+
let default_web_client_id = null;
286+
const firebase_database_url = project_info['firebase_url'] || null;
287+
const gcm_defaultSenderId = project_info['project_number'] || null;
288+
const google_api_key = client['api_key']?.['current_key'] ?? null;
289+
const google_app_id = client_info['mobilesdk_app_id'] || null;
290+
const google_crash_reporting_api_key = google_app_id;
291+
const google_storage_bucket = project_info['storage_bucket'] || null;
292+
const project_id = project_info['project_id'] || null;
293+
294+
for (let i = 0; i < oauth_clients.length; i++) {
295+
const oauth_client = oauth_clients[i];
296+
if (oauth_client.client_type === 3) {
297+
default_web_client_id = oauth_client['client_id'];
298+
}
299+
}
300+
301+
const nativeOptions = new com.google.firebase.FirebaseOptions.Builder();
302+
if (google_api_key) {
303+
nativeOptions.setApiKey(google_api_key);
304+
}
305+
306+
if (google_app_id) {
307+
nativeOptions.setApplicationId(google_app_id);
308+
}
309+
310+
if (firebase_database_url) {
311+
nativeOptions.setDatabaseUrl(firebase_database_url);
312+
}
313+
314+
if (gcm_defaultSenderId) {
315+
nativeOptions.setGcmSenderId(gcm_defaultSenderId);
316+
}
317+
318+
if (project_id) {
319+
nativeOptions.setProjectId(project_id);
320+
}
321+
322+
323+
if (google_storage_bucket) {
324+
nativeOptions.setStorageBucket(google_storage_bucket);
325+
}
326+
327+
328+
if (options?.apiKey) {
329+
nativeOptions.setApiKey(options.apiKey);
330+
}
331+
332+
if (options?.gcmSenderId) {
333+
nativeOptions.setGcmSenderId(options.gcmSenderId);
334+
}
335+
336+
if (options?.databaseURL) {
337+
nativeOptions.setDatabaseUrl(options.databaseURL);
338+
}
339+
340+
if (options?.googleAppId) {
341+
nativeOptions.setApplicationId(options.googleAppId);
342+
}
343+
344+
if (options?.projectId) {
345+
nativeOptions.setProjectId(options.projectId);
346+
}
347+
348+
if (options?.storageBucket) {
349+
nativeOptions.setStorageBucket(options.storageBucket);
350+
}
351+
352+
if (options?.trackingId) {
353+
nativeOptions.setGaTrackingId(options.trackingId);
354+
}
355+
356+
357+
const app = com.google.firebase.FirebaseApp.initializeApp(ctx, nativeOptions.build());
358+
359+
if (app && typeof config === 'object' && typeof config.automaticResourceManagement === 'boolean') {
360+
app.setAutomaticResourceManagementEnabled(config.automaticDataCollectionEnabled);
361+
}
362+
363+
364+
const fbApp = FirebaseApp.fromNative(app);
365+
366+
if (!defaultApp) {
367+
defaultApp = fbApp;
368+
}
369+
return fbApp;
370+
}
260371
}
261372

262373
export function firebase() {

packages/firebase-core/index.d.ts

+14-12
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {FirebaseConfig, IFirebaseOptions} from './common';
1+
import { FirebaseConfig, IFirebaseOptions } from './common';
22

33

44
declare function serialize(data: any): any;
@@ -12,27 +12,27 @@ declare class FirebaseOptions implements IFirebaseOptions {
1212

1313
apiKey?: string;
1414

15-
gcmSenderId?: string;
15+
gcmSenderId?: string;
1616

17-
androidClientId?: string;
17+
androidClientId?: string;
1818

19-
appGroupId?: string;
19+
appGroupId?: string;
2020

21-
bundleId?: string;
21+
bundleId?: string;
2222

23-
clientId?: string;
23+
clientId?: string;
2424

25-
databaseURL?: string;
25+
databaseURL?: string;
2626

27-
deepLinkURLScheme?: string;
27+
deepLinkURLScheme?: string;
2828

29-
googleAppId?: string;
29+
googleAppId?: string;
3030

31-
projectId?: string;
31+
projectId?: string;
3232

33-
storageBucket?: string;
33+
storageBucket?: string;
3434

35-
trackingId?: string;
35+
trackingId?: string;
3636
}
3737

3838
declare class FirebaseApp {
@@ -50,6 +50,8 @@ export interface Firebase {
5050
app(name?: string): FirebaseApp;
5151

5252
initializeApp(options?: FirebaseOptions, configOrName?: FirebaseConfig | string): FirebaseApp;
53+
54+
initializeAppWithPath(path: string, options?: FirebaseOptions, config?: FirebaseConfig): FirebaseApp;
5355
}
5456

5557
export function firebase(): Firebase;

packages/firebase-core/index.ios.ts

+75
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { knownFolders } from '@nativescript/core';
12
import { FirebaseConfig, IFirebaseOptions } from './common';
23
export * from './utils';
34

@@ -349,6 +350,80 @@ export class Firebase {
349350

350351
return fbApp;
351352
}
353+
354+
initializeAppWithPath(path: string, options: FirebaseOptions = null, config?: FirebaseConfig) {
355+
if (path.startsWith('res://')) {
356+
path = NSBundle.mainBundle.pathForResourceOfType(path.replace('res://', '').replace('.plist', ''), 'plist')
357+
} else if (path.startsWith('~/')) {
358+
path = knownFolders.currentApp().path + '/' + path.replace('~/', '');
359+
}
360+
361+
const nativeOptions = FIROptions.alloc().initWithContentsOfFile(path);
362+
363+
364+
if (options?.apiKey) {
365+
nativeOptions.APIKey = options.apiKey;
366+
}
367+
368+
if (options?.gcmSenderId) {
369+
nativeOptions.GCMSenderID = options.gcmSenderId;
370+
}
371+
372+
if (options?.androidClientId) {
373+
nativeOptions.androidClientID = options.androidClientId;
374+
}
375+
376+
if (options?.appGroupId) {
377+
nativeOptions.appGroupID = options.appGroupId;
378+
}
379+
380+
if (options?.bundleId) {
381+
nativeOptions.bundleID = options.bundleId;
382+
}
383+
384+
if (options?.clientId) {
385+
nativeOptions.clientID = options.clientId;
386+
}
387+
388+
if (options?.databaseURL) {
389+
nativeOptions.databaseURL = options.databaseURL;
390+
}
391+
392+
if (options?.deepLinkURLScheme) {
393+
nativeOptions.deepLinkURLScheme = options.deepLinkURLScheme;
394+
}
395+
396+
if (options?.googleAppId) {
397+
nativeOptions.googleAppID = options.googleAppId;
398+
}
399+
400+
if (options?.projectId) {
401+
nativeOptions.projectID = options.projectId;
402+
}
403+
404+
if (options?.storageBucket) {
405+
nativeOptions.storageBucket = options.storageBucket;
406+
}
407+
408+
if (options?.trackingId) {
409+
nativeOptions.trackingID = options.trackingId;
410+
}
411+
412+
413+
FIRApp.configureWithOptions(nativeOptions);
414+
415+
const app = FIRApp.defaultApp();
416+
417+
if (app && typeof config === 'object' && typeof config.automaticDataCollectionEnabled === 'boolean') {
418+
app.dataCollectionDefaultEnabled = config.automaticDataCollectionEnabled;
419+
}
420+
const fbApp = FirebaseApp.fromNative(app);
421+
422+
if (!defaultApp) {
423+
defaultApp = fbApp;
424+
}
425+
return fbApp;
426+
}
352427
}
353428

354429
export function firebase() {

packages/firebase-core/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@nativescript/firebase-core",
3-
"version": "1.0.0-alpha.13",
3+
"version": "1.0.0-alpha.14",
44
"description": "NativeScript Firebase - Core",
55
"main": "index",
66
"typings": "index.d.ts",
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
*.iml
2+
.gradle
3+
/local.properties
4+
/.idea/caches
5+
/.idea/libraries
6+
/.idea/modules.xml
7+
/.idea/workspace.xml
8+
/.idea/navEditor.xml
9+
/.idea/assetWizardSettings.xml
10+
.DS_Store
11+
/build
12+
/captures
13+
.externalNativeBuild
14+
.cxx
15+
local.properties

packages/firebase-core/src-native/android/.idea/.gitignore

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/firebase-core/src-native/android/.idea/.name

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/firebase-core/src-native/android/.idea/compiler.xml

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/firebase-core/src-native/android/.idea/gradle.xml

+20
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/firebase-core/src-native/android/.idea/misc.xml

+9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/firebase-core/src-native/android/.idea/vcs.xml

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
plugins {
2+
id 'com.android.application'
3+
id 'kotlin-android'
4+
}
5+
6+
android {
7+
compileSdk 31
8+
9+
defaultConfig {
10+
applicationId "org.nativescript.firebasecoredemo"
11+
minSdk 17
12+
targetSdk 31
13+
versionCode 1
14+
versionName "1.0"
15+
16+
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
17+
}
18+
19+
buildTypes {
20+
release {
21+
minifyEnabled false
22+
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
23+
}
24+
}
25+
compileOptions {
26+
sourceCompatibility JavaVersion.VERSION_1_8
27+
targetCompatibility JavaVersion.VERSION_1_8
28+
}
29+
kotlinOptions {
30+
jvmTarget = '1.8'
31+
}
32+
}
33+
34+
dependencies {
35+
36+
implementation 'androidx.core:core-ktx:1.6.0'
37+
implementation 'androidx.appcompat:appcompat:1.3.1'
38+
implementation 'com.google.android.material:material:1.4.0'
39+
implementation 'androidx.constraintlayout:constraintlayout:2.1.1'
40+
testImplementation 'junit:junit:4.+'
41+
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
42+
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
43+
}

0 commit comments

Comments
 (0)