Skip to content

Commit ae919ad

Browse files
committed
feat(admob): prep admob for independence
Admob isn't dependant on Firebase this starts the process of removal, we can remove it for v3 release of firebase. fixes: #117 fixes: #116
1 parent 9b89705 commit ae919ad

File tree

10 files changed

+169
-79
lines changed

10 files changed

+169
-79
lines changed

apps/demo/src/app.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { Application, Utils } from '@nativescript/core';
22
import { firebase } from '@nativescript/firebase-core';
3-
import '@nativescript/firebase-admob';
43
import '@nativescript/firebase-analytics';
54
import '@nativescript/firebase-auth';
65
import '@nativescript/firebase-crashlytics';

apps/demo/src/plugin-demos/firebase-admob.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
import { Observable, EventData, Page, View, Label } from '@nativescript/core';
22
import { DemoSharedFirebaseAdmob } from '@demo/shared';
3-
import { firebase } from '@nativescript/firebase-core';
4-
import '@nativescript/firebase-admob';
5-
import { AdEventType, InterstitialAd, RewardedInterstitialAd, RewardedAd, BannerAd, BannerAdSize, Admob, AdsConsent, NativeAd, NativeAdLoader, NativeAdView } from '@nativescript/firebase-admob';
6-
import { AdChoicesPlacement, NativeAdEventType } from '@nativescript/firebase-admob';
3+
import { AdChoicesPlacement, NativeAdEventType, AdEventType, InterstitialAd, RewardedInterstitialAd, RewardedAd, BannerAd, BannerAdSize, Admob, AdsConsent, NativeAd, NativeAdLoader, NativeAdView } from '@nativescript/firebase-admob';
74

85
export function navigatingTo(args: EventData) {
96
const page = <Page>args.object;
@@ -33,10 +30,7 @@ export class DemoModel extends DemoSharedFirebaseAdmob {
3330
} else {
3431
testDevices.push('EMULATOR');
3532
}
36-
const admob = firebase().admob();
37-
admob.setRequestConfiguration({
38-
testDevices,
39-
});
33+
Admob.getInstance().requestConfiguration = { testDevices };
4034
}
4135

4236
nativeAdLayoutChanged(event) {

packages/firebase-admob/README.md

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -506,12 +506,11 @@ Use the argument `tagForChildDirectedTreatment: undefined` or do not set this ta
506506
The following example indicates that you want your content treated as child-directed for purposes of COPPA:
507507

508508
```ts
509-
import { firebase } from '@nativescript/firebase-core'
510-
import '@nativescript/firebase-admob'
509+
import { Admob, RequestConfiguration } from '@nativescript/firebase-admob';
511510
const requestConfiguration: RequestConfiguration = {
512511
tagForChildDirectedTreatment: true
513512
}
514-
firebase().admob().setRequestConfiguration(requestConfiguration)
513+
Admob.getInstance().requestConfiguration = requestConfiguration;
515514
```
516515

517516
### Users under the age of consent
@@ -527,12 +526,11 @@ Use the argument `tagForUnderAgeOfConsent: false` to indicates that you don't wa
527526
Use the argument `tagForUnderAgeOfConsent: undefined` or do not set this tag to indicate that you have not specified whether the ad request should receive treatment for users in the European Economic Area (EEA) under the age of consent. The following example indicates that you want TFUA included in your ad request:
528527

529528
```ts
530-
import { firebase } from '@nativescript/firebase-core'
531-
import '@nativescript/firebase-admob'
529+
import { Admob, RequestConfiguration } from '@nativescript/firebase-admob';
532530
const requestConfiguration: RequestConfiguration = {
533531
tagForUnderAgeOfConsent: true
534532
}
535-
firebase().admob().setRequestConfiguration(requestConfiguration)
533+
Admob.getInstance().requestConfiguration = requestConfiguration;
536534
```
537535

538536
The tags to enable the Child-directed setting and `tagForUnderAgeOfConsent` should not both simultaneously be set to true. If they are, the child-directed setting takes precedence.
@@ -551,12 +549,11 @@ AdMob ads returned for these requests have a content rating at or below that lev
551549
The following code configures a `RequestConfiguration` object to specify that ad content returned should correspond to a digital content label designation no higher than G:
552550

553551
```ts
554-
import { firebase } from '@nativescript/firebase-core'
555-
import { MaxAdContentRating } from '@nativescript/firebase-admob'
552+
import { Admob, MaxAdContentRating, RequestConfiguration } from '@nativescript/firebase-admob';
556553
const requestConfiguration: RequestConfiguration = {
557554
maxAdContentRating: MaxAdContentRating.G
558555
}
559-
firebase().admob().setRequestConfiguration(requestConfiguration)
556+
Admob.getInstance().requestConfiguration = requestConfiguration;
560557
```
561558

562559
## License

packages/firebase-admob/index.android.ts

Lines changed: 35 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { Application, Utils } from '@nativescript/core';
22
import lazy from '@nativescript/core/utils/lazy';
3-
import { firebase, FirebaseApp, FirebaseError } from '@nativescript/firebase-core';
43
import { IAdmob, AdEventListener, RequestConfiguration, AdShowOptions, IInterstitialAd, RequestOptions, IRewardedAd, IRewardedInterstitialAd, IRewardedItem, ServerSideVerificationOptions, AdapterStatus } from '.';
54
import { AdEventType, BannerAdBase, RewardedAdEventType, MaxAdContentRating, unitIdProperty, BannerAdSizeBase, sizeProperty } from './common';
65

@@ -9,11 +8,30 @@ export { MaxAdContentRating, AdEventType };
98
export * from './adsconsent';
109
export * from './nativead';
1110

11+
export class AdmobError extends Error {
12+
#native: java.lang.Exception;
13+
static fromNative(native: java.lang.Exception, message?: string) {
14+
const error = new AdmobError(message || native?.getMessage?.());
15+
error.#native = native;
16+
return error;
17+
}
18+
19+
get native() {
20+
return this.#native;
21+
}
22+
23+
intoNative() {
24+
if (!this.#native) {
25+
return new java.lang.Exception(this.message);
26+
}
27+
return this.#native;
28+
}
29+
}
30+
1231
let defaultAdmob: Admob;
1332

14-
const fb = firebase();
15-
if (!fb.admob) {
16-
Object.defineProperty(fb, 'admob', {
33+
if (!global.__admob) {
34+
Object.defineProperty(global, '__admob', {
1735
value: () => {
1836
if (!defaultAdmob) {
1937
defaultAdmob = new Admob();
@@ -52,7 +70,7 @@ class AdListener extends com.google.android.gms.ads.AdListener {
5270
this._owner?.get?.().notify({
5371
eventName: BannerAd.onAdFailedToLoadEvent,
5472
object: this._owner?.get?.(),
55-
error: FirebaseError.fromNative(error),
73+
error: AdmobError.fromNative(error as any),
5674
});
5775
}
5876

@@ -179,7 +197,7 @@ export class InterstitialAd implements IInterstitialAd {
179197
owner?._setLoaded(false);
180198
break;
181199
case AdEventType.FAILED_TO_SHOW_FULL_SCREEN_CONTENT:
182-
owner?._onAdEvent(AdEventType.FAILED_TO_SHOW_FULL_SCREEN_CONTENT, FirebaseError.fromNative(dataOrError), owner);
200+
owner?._onAdEvent(AdEventType.FAILED_TO_SHOW_FULL_SCREEN_CONTENT, AdmobError.fromNative(dataOrError), owner);
183201
break;
184202
case AdEventType.IMPRESSION:
185203
owner?._onAdEvent(AdEventType.IMPRESSION, null, owner);
@@ -188,7 +206,7 @@ export class InterstitialAd implements IInterstitialAd {
188206
owner?._onAdEvent(AdEventType.OPENED, null, owner);
189207
break;
190208
case AdEventType.FAILED_TO_LOAD_EVENT:
191-
owner?._onAdEvent(AdEventType.FAILED_TO_LOAD_EVENT, FirebaseError.fromNative(dataOrError), owner);
209+
owner?._onAdEvent(AdEventType.FAILED_TO_LOAD_EVENT, AdmobError.fromNative(dataOrError), owner);
192210
break;
193211
}
194212
},
@@ -270,7 +288,7 @@ export class RewardedInterstitialAd implements IRewardedInterstitialAd {
270288
owner?._setLoaded(false);
271289
break;
272290
case AdEventType.FAILED_TO_SHOW_FULL_SCREEN_CONTENT:
273-
owner?._onAdEvent(AdEventType.FAILED_TO_SHOW_FULL_SCREEN_CONTENT, FirebaseError.fromNative(dataOrError), owner);
291+
owner?._onAdEvent(AdEventType.FAILED_TO_SHOW_FULL_SCREEN_CONTENT, AdmobError.fromNative(dataOrError), owner);
274292
break;
275293
case AdEventType.IMPRESSION:
276294
owner?._onAdEvent(AdEventType.IMPRESSION, null, owner);
@@ -279,7 +297,7 @@ export class RewardedInterstitialAd implements IRewardedInterstitialAd {
279297
owner?._onAdEvent(AdEventType.OPENED, null, owner);
280298
break;
281299
case AdEventType.FAILED_TO_LOAD_EVENT:
282-
owner?._onAdEvent(AdEventType.FAILED_TO_LOAD_EVENT, FirebaseError.fromNative(dataOrError), owner);
300+
owner?._onAdEvent(AdEventType.FAILED_TO_LOAD_EVENT, AdmobError.fromNative(dataOrError), owner);
283301
break;
284302
}
285303
},
@@ -386,7 +404,7 @@ export class RewardedAd implements IRewardedAd {
386404
owner?._setLoaded(false);
387405
break;
388406
case AdEventType.FAILED_TO_SHOW_FULL_SCREEN_CONTENT:
389-
owner?._onAdEvent(AdEventType.FAILED_TO_SHOW_FULL_SCREEN_CONTENT, FirebaseError.fromNative(dataOrError), owner);
407+
owner?._onAdEvent(AdEventType.FAILED_TO_SHOW_FULL_SCREEN_CONTENT, AdmobError.fromNative(dataOrError), owner);
390408
break;
391409
case AdEventType.IMPRESSION:
392410
owner?._onAdEvent(AdEventType.IMPRESSION, null, owner);
@@ -395,7 +413,7 @@ export class RewardedAd implements IRewardedAd {
395413
owner?._onAdEvent(AdEventType.OPENED, null, owner);
396414
break;
397415
case AdEventType.FAILED_TO_LOAD_EVENT:
398-
owner?._onAdEvent(AdEventType.FAILED_TO_LOAD_EVENT, FirebaseError.fromNative(dataOrError), owner);
416+
owner?._onAdEvent(AdEventType.FAILED_TO_LOAD_EVENT, AdmobError.fromNative(dataOrError), owner);
399417
break;
400418
}
401419
},
@@ -627,8 +645,6 @@ export class BannerAd extends BannerAdBase {
627645
}
628646

629647
export class Admob implements IAdmob {
630-
#app: FirebaseApp;
631-
632648
constructor() {
633649
if (defaultAdmob) {
634650
return defaultAdmob;
@@ -653,6 +669,10 @@ export class Admob implements IAdmob {
653669
});
654670
}
655671

672+
static getInstance(): Admob {
673+
return new Admob();
674+
}
675+
656676
set requestConfiguration(requestConfiguration: RequestConfiguration) {
657677
try {
658678
const parsedConfiguration: any = { ...requestConfiguration };
@@ -736,11 +756,7 @@ export class Admob implements IAdmob {
736756
return this.requestConfiguration;
737757
}
738758

739-
get app(): FirebaseApp {
740-
if (!this.#app) {
741-
// @ts-ignore
742-
this.#app = FirebaseApp.fromNative(com.google.firebase.FirebaseApp.getInstance());
743-
}
744-
return this.#app;
759+
get app() {
760+
return global?.__defaultFirebaseApp;
745761
}
746762
}

packages/firebase-admob/index.d.ts

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { Application } from '@nativescript/core';
22

3-
import { FirebaseApp } from '@nativescript/firebase-core';
43
import { IAdmob, IInterstitialAd, IRewardedAd, BannerAdBase, IRewardedInterstitialAd, RewardedAdEventType, MaxAdContentRating, ServerSideVerificationOptions } from './common';
54

65
export { MaxAdContentRating, RewardedAdEventType };
@@ -214,10 +213,12 @@ export declare class BannerAdSize extends BannerAdSizeBase {
214213
}
215214

216215
export declare class Admob implements IAdmob {
217-
readonly app: FirebaseApp;
216+
readonly app: any;
218217

219218
static init(): Promise<{ [key: string]: AdapterStatus }>;
220219

220+
static getInstance(): Admob;
221+
221222
requestConfiguration: RequestConfiguration;
222223

223224
/**
@@ -231,10 +232,6 @@ export declare class Admob implements IAdmob {
231232
getRequestConfiguration(requestConfiguration: RequestConfiguration);
232233
}
233234

234-
declare module '@nativescript/firebase-core' {
235-
export interface Firebase extends FirebaseAdmob {}
236-
}
237-
238235
export enum AdapterStatusState {
239236
NOT_READY,
240237
READY,
@@ -245,7 +242,3 @@ export interface AdapterStatus {
245242
latency: number;
246243
initializationState: AdapterStatusState;
247244
}
248-
249-
export interface FirebaseAdmob {
250-
static admob(): Admob;
251-
}

0 commit comments

Comments
 (0)