Skip to content

Commit 595710b

Browse files
committed
fix(admob): banner creation
1 parent 9a5cb7d commit 595710b

File tree

6 files changed

+105
-210
lines changed

6 files changed

+105
-210
lines changed

packages/firebase-admob/index.android.ts

Lines changed: 14 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -479,43 +479,28 @@ export class BannerAdSize extends BannerAdSizeBase {
479479
return BannerAdSize.fromNative(MEDIUM_RECTANGLE());
480480
}
481481

482-
static createAnchoredAdaptiveBanner(width: number | 'fullWidth' | 'autoHeight', orientation: 'portrait' | 'landscape' | 'device' = 'device'): BannerAdSize {
483-
let w;
484-
if (width === 'fullWidth') {
485-
w = FULL_WIDTH();
486-
} else if (width === 'autoHeight') {
487-
w = AUTO_HEIGHT();
488-
} else {
489-
w = width;
490-
}
491-
482+
static createAnchoredAdaptiveBanner(width: number, orientation: 'portrait' | 'landscape' | 'device' = 'device'): BannerAdSize {
492483
switch (orientation) {
493484
case 'portrait':
494-
return BannerAdSize.fromNative(com.google.android.gms.ads.AdSize.getPortraitAnchoredAdaptiveBannerAdSize(Application.android.foregroundActivity || Application.android.startActivity, w));
485+
return BannerAdSize.fromNative(com.google.android.gms.ads.AdSize.getPortraitAnchoredAdaptiveBannerAdSize(Application.android.foregroundActivity || Application.android.startActivity, width));
495486
case 'landscape':
496-
return BannerAdSize.fromNative(com.google.android.gms.ads.AdSize.getLandscapeAnchoredAdaptiveBannerAdSize(Application.android.foregroundActivity || Application.android.startActivity, w));
487+
return BannerAdSize.fromNative(com.google.android.gms.ads.AdSize.getLandscapeAnchoredAdaptiveBannerAdSize(Application.android.foregroundActivity || Application.android.startActivity, width));
497488
default:
498-
return BannerAdSize.fromNative(com.google.android.gms.ads.AdSize.getCurrentOrientationAnchoredAdaptiveBannerAdSize(Application.android.foregroundActivity || Application.android.startActivity, w));
489+
return BannerAdSize.fromNative(com.google.android.gms.ads.AdSize.getCurrentOrientationAnchoredAdaptiveBannerAdSize(Application.android.foregroundActivity || Application.android.startActivity, width));
499490
}
500491
}
501492

502-
static createInLineAdaptiveBanner(width: number | 'fullWidth' | 'autoHeight', orientation: 'portrait' | 'landscape' | 'device' = 'device'): BannerAdSize {
503-
let w;
504-
if (width === 'fullWidth') {
505-
w = FULL_WIDTH();
506-
} else if (width === 'autoHeight') {
507-
w = AUTO_HEIGHT();
508-
} else {
509-
w = width;
493+
static createInLineAdaptiveBanner(width: number, maxHeight: number = 0, orientation: 'portrait' | 'landscape' | 'device' = 'device'): BannerAdSize {
494+
if (maxHeight > 0) {
495+
BannerAdSize.fromNative((com as any).google.android.gms.ads.AdSize.getInlineAdaptiveBannerAdSize(Application.android.foregroundActivity || Application.android.startActivity, width, maxHeight));
510496
}
511-
512497
switch (orientation) {
513498
case 'portrait':
514-
return BannerAdSize.fromNative(com.google.android.gms.ads.AdSize.getPortraitInlineAdaptiveBannerAdSize(Application.android.foregroundActivity || Application.android.startActivity, w));
499+
return BannerAdSize.fromNative(com.google.android.gms.ads.AdSize.getPortraitInlineAdaptiveBannerAdSize(Application.android.foregroundActivity || Application.android.startActivity, width));
515500
case 'landscape':
516-
return BannerAdSize.fromNative(com.google.android.gms.ads.AdSize.getLandscapeInlineAdaptiveBannerAdSize(Application.android.foregroundActivity || Application.android.startActivity, w));
501+
return BannerAdSize.fromNative(com.google.android.gms.ads.AdSize.getLandscapeInlineAdaptiveBannerAdSize(Application.android.foregroundActivity || Application.android.startActivity, width));
517502
default:
518-
return BannerAdSize.fromNative(com.google.android.gms.ads.AdSize.getCurrentOrientationInlineAdaptiveBannerAdSize(Application.android.foregroundActivity || Application.android.startActivity, w));
503+
return BannerAdSize.fromNative(com.google.android.gms.ads.AdSize.getCurrentOrientationInlineAdaptiveBannerAdSize(Application.android.foregroundActivity || Application.android.startActivity, width));
519504
}
520505
}
521506

@@ -549,7 +534,9 @@ export class BannerAd extends BannerAdBase {
549534
#listener;
550535

551536
[sizeProperty.setNative](value) {
552-
this.#native.setAdSize(value?.native);
537+
if (this.#native) {
538+
this.#native.setAdSize(value?.native);
539+
}
553540
}
554541

555542
[unitIdProperty.setNative](value) {
@@ -564,6 +551,7 @@ export class BannerAd extends BannerAdBase {
564551
}
565552

566553
initNativeView() {
554+
super.initNativeView();
567555
ensureAdListener();
568556
this.#listener = new AdListener(new WeakRef(this));
569557
this.#native.setAdListener(this.#listener);

packages/firebase-admob/index.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,9 @@ export declare class BannerAdSize extends BannerAdSizeBase {
113113

114114
readonly SEARCH: BannerAdSize;
115115

116-
static createAnchoredAdaptiveBanner(width: number | 'fullWidth' | 'autoHeight', orientation: 'portrait' | 'landscape' | 'device' = 'device'): BannerAdSize;
116+
static createAnchoredAdaptiveBanner(width: number, orientation: 'portrait' | 'landscape' | 'device' = 'device'): BannerAdSize;
117117

118-
static createInLineAdaptiveBanner(width: number | 'fullWidth' | 'autoHeight', orientation: 'portrait' | 'landscape' | 'device' = 'device'): BannerAdSize;
118+
static createInLineAdaptiveBanner(width: number, maxHeight: number = 0, orientation: 'portrait' | 'landscape' | 'device' = 'device'): BannerAdSize;
119119

120120
readonly native: any;
121121
readonly android: any;

packages/firebase-admob/index.ios.ts

Lines changed: 31 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -282,103 +282,73 @@ export class RewardedItem implements IRewardedItem {
282282
export class BannerAdSize extends BannerAdSizeBase {
283283
#native: GADAdSize;
284284

285-
constructor(width?: number, height?: number) {
285+
constructor(width: number, height: number, native?) {
286286
super();
287-
if (width && height) {
287+
if (typeof width === 'number' && typeof height === 'number') {
288288
this.#native = GADAdSizeFromCGSize(CGSizeMake(width, height));
289+
} else if (arguments[2] instanceof GADAdSize) {
290+
this.#native = arguments[2];
291+
} else {
292+
this.#native = GADAdSizeInvalid;
289293
}
290294
}
291295

292296
static fromNative(size: GADAdSize) {
293-
const banner = new BannerAdSize();
294-
if (size instanceof GADAdSize) {
295-
banner.#native = size;
296-
} else {
297-
banner.#native = kGADAdSizeInvalid;
298-
}
299-
return banner;
297+
return new BannerAdSize(null, null, size);
300298
}
301299

302300
static get BANNER(): BannerAdSize {
303-
return BannerAdSize.fromNative(kGADAdSizeBanner);
301+
return BannerAdSize.fromNative(GADAdSizeBanner);
304302
}
305303

306304
static get FULL_BANNER(): BannerAdSize {
307-
return BannerAdSize.fromNative(kGADAdSizeFullBanner);
305+
return BannerAdSize.fromNative(GADAdSizeFullBanner);
308306
}
309307

310308
static get LARGE_BANNER(): BannerAdSize {
311-
return BannerAdSize.fromNative(kGADAdSizeLargeBanner);
309+
return BannerAdSize.fromNative(GADAdSizeLargeBanner);
312310
}
313311

314312
static get LEADERBOARD(): BannerAdSize {
315-
return BannerAdSize.fromNative(kGADAdSizeLeaderboard);
313+
return BannerAdSize.fromNative(GADAdSizeLeaderboard);
316314
}
317315

318316
static get MEDIUM_RECTANGLE(): BannerAdSize {
319-
return BannerAdSize.fromNative(kGADAdSizeMediumRectangle);
317+
return BannerAdSize.fromNative(GADAdSizeMediumRectangle);
320318
}
321319

322-
static createAnchoredAdaptiveBanner(width: number | 'fullWidth' | 'autoHeight', orientation: 'portrait' | 'landscape' | 'device' = 'device'): BannerAdSize {
323-
let banner;
320+
static createAnchoredAdaptiveBanner(width: number, orientation: 'portrait' | 'landscape' | 'device' = 'device'): BannerAdSize {
321+
let nativeOrientation: Orientation = Orientation.Device;
324322
if (orientation === 'portrait') {
325-
if (width === 'fullWidth') {
326-
banner = TNSGA.createAnchoredAdaptiveBanner(Width.FullWidth, Orientation.Portrait);
327-
} else if (width === 'autoHeight') {
328-
banner = TNSGA.createAnchoredAdaptiveBanner(Width.AutoHeight, Orientation.Portrait);
329-
} else {
330-
banner = TNSGA.createAnchoredAdaptiveBannerWithWidth(width, Orientation.Device);
331-
}
323+
nativeOrientation = Orientation.Portrait;
332324
} else if (orientation === 'landscape') {
333-
if (width === 'fullWidth') {
334-
banner = TNSGA.createAnchoredAdaptiveBanner(Width.FullWidth, Orientation.Landscape);
335-
} else if (width === 'autoHeight') {
336-
banner = TNSGA.createAnchoredAdaptiveBanner(Width.AutoHeight, Orientation.Landscape);
337-
} else {
338-
banner = TNSGA.createAnchoredAdaptiveBannerWithWidth(width, Orientation.Device);
339-
}
340-
} else {
341-
banner = TNSGA.createAnchoredAdaptiveBannerWithWidth(width as any, Orientation.Device);
325+
nativeOrientation = Orientation.Landscape;
342326
}
343327

344-
return BannerAdSize.fromNative(banner);
328+
return BannerAdSize.fromNative(TNSGA.createAnchoredAdaptiveBanner(width, nativeOrientation));
345329
}
346330

347-
static createInLineAdaptiveBanner(width: number | 'fullWidth' | 'autoHeight', orientation: 'portrait' | 'landscape' | 'device' = 'device'): BannerAdSize {
348-
let banner;
331+
static createInLineAdaptiveBanner(width: number, maxHeight: number = 0, orientation: 'portrait' | 'landscape' | 'device' = 'device'): BannerAdSize {
332+
let nativeOrientation: Orientation = Orientation.Device;
349333
if (orientation === 'portrait') {
350-
if (width === 'fullWidth') {
351-
banner = TNSGA.createInlineAdaptiveBanner(Width.FullWidth, Orientation.Portrait);
352-
} else if (width === 'autoHeight') {
353-
banner = TNSGA.createInlineAdaptiveBanner(Width.AutoHeight, Orientation.Portrait);
354-
} else {
355-
banner = TNSGA.createInlineAdaptiveBannerWithWidth(width, Orientation.Device);
356-
}
334+
nativeOrientation = Orientation.Portrait;
357335
} else if (orientation === 'landscape') {
358-
if (width === 'fullWidth') {
359-
banner = TNSGA.createInlineAdaptiveBanner(Width.FullWidth, Orientation.Landscape);
360-
} else if (width === 'autoHeight') {
361-
banner = TNSGA.createInlineAdaptiveBanner(Width.AutoHeight, Orientation.Landscape);
362-
} else {
363-
banner = TNSGA.createInlineAdaptiveBannerWithWidth(width, Orientation.Device);
364-
}
365-
} else {
366-
banner = TNSGA.createInlineAdaptiveBannerWithWidth(width as any, Orientation.Device);
336+
nativeOrientation = Orientation.Landscape;
367337
}
368338

369-
return BannerAdSize.fromNative(banner);
339+
return BannerAdSize.fromNative(TNSGA.createInlineAdaptiveBanner(width, maxHeight, nativeOrientation));
370340
}
371341

372342
static get FLUID(): BannerAdSize {
373-
return BannerAdSize.fromNative(kGADAdSizeFluid);
343+
return BannerAdSize.fromNative(GADAdSizeFluid);
374344
}
375345

376346
static get WIDE_SKYSCRAPER(): BannerAdSize {
377-
return BannerAdSize.fromNative(kGADAdSizeSkyscraper);
347+
return BannerAdSize.fromNative(GADAdSizeSkyscraper);
378348
}
379349

380350
static get INVALID(): BannerAdSize {
381-
return BannerAdSize.fromNative(kGADAdSizeInvalid);
351+
return BannerAdSize.fromNative(GADAdSizeInvalid);
382352
}
383353

384354
static get SEARCH(): BannerAdSize {
@@ -426,12 +396,13 @@ export class BannerAd extends BannerAdBase {
426396

427397
load(options: RequestOptions = {}) {
428398
this.#isLoading = true;
429-
this.#native?.loadRequest(toSerializeRequestOptions(options));
399+
this.#native?.loadRequest?.(toSerializeRequestOptions(options));
430400
}
431401

432402
[sizeProperty.setNative](value) {
433-
console.log(this.#native.frame.origin.x, this.#native.frame.origin.y, this.#native.frame.size.width, this.#native.frame.size.height);
434-
this.#native.adSize = value?.native;
403+
if (this.#native) {
404+
this.#native.adSize = value?.native;
405+
}
435406
}
436407

437408
[unitIdProperty.setNative](value) {
@@ -448,15 +419,6 @@ export class BannerAd extends BannerAdBase {
448419
this.setMeasuredDimension(width, height);
449420
}
450421
}
451-
452-
// // @ts-ignore
453-
// set unitId(id) {
454-
// this.#native.adUnitID = id;
455-
// }
456-
457-
// get unitId() {
458-
// return this.#native.adUnitID;
459-
// }
460422
}
461423

462424
declare const FIRApp;
@@ -521,8 +483,8 @@ export class Admob implements IAdmob {
521483
if (Array.isArray(requestConfiguration.testDevices)) {
522484
GADMobileAds.sharedInstance().requestConfiguration.testDeviceIdentifiers = requestConfiguration.testDevices.map((item) => {
523485
if (item === 'EMULATOR') {
524-
if (typeof kGADSimulatorID) {
525-
return kGADSimulatorID;
486+
if (typeof GADSimulatorID) {
487+
return GADSimulatorID;
526488
}
527489
return '';
528490
}

packages/firebase-admob/platforms/ios/src/TNSGA.swift

Lines changed: 5 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -37,34 +37,6 @@ public class TNSGA:NSObject {
3737
}
3838
}
3939

40-
@objc(Width)
41-
public enum Width:Int, RawRepresentable {
42-
43-
case FullWidth
44-
case AutoHeight
45-
public typealias RawValue = UInt32
46-
47-
public init?(rawValue: UInt32) {
48-
switch rawValue {
49-
case 0:
50-
self = .FullWidth
51-
case 1:
52-
self = .AutoHeight
53-
default:
54-
return nil
55-
}
56-
}
57-
58-
public var rawValue: RawValue {
59-
switch self {
60-
case .FullWidth:
61-
return 0
62-
case .AutoHeight:
63-
return 1
64-
}
65-
}
66-
67-
}
6840

6941
@objc(AdLoaderAdType)
7042
public enum AdLoaderAdType: Int, RawRepresentable {
@@ -102,7 +74,7 @@ public class TNSGA:NSObject {
10274
return type.rawValue
10375
}
10476

105-
public static func createAnchoredAdaptiveBanner(width: CGFloat,_ orientation: Orientation) -> GADAdSize {
77+
public static func createAnchoredAdaptiveBanner(_ width: CGFloat,_ orientation: Orientation) -> GADAdSize {
10678
switch orientation {
10779
case .Landscape:
10880
return GADLandscapeAnchoredAdaptiveBannerAdSizeWithWidth(width)
@@ -111,37 +83,12 @@ public class TNSGA:NSObject {
11183
default:
11284
return GADCurrentOrientationAnchoredAdaptiveBannerAdSizeWithWidth(width)
11385
}
114-
}
86+
}
11587

116-
117-
public static func createAnchoredAdaptiveBanner(_ width: Width,_ orientation: Orientation) -> GADAdSize {
118-
switch orientation {
119-
case .Landscape:
120-
switch width {
121-
case .AutoHeight:
122-
return GADAdSizeFullWidthLandscapeWithHeight(0)
123-
case .FullWidth:
124-
return GADAdSizeFullWidthLandscapeWithHeight(0)
125-
default:
126-
return kGADAdSizeInvalid
127-
}
128-
case .Portrait:
129-
switch width {
130-
case .AutoHeight:
131-
return GADAdSizeFullWidthPortraitWithHeight(0)
132-
case .FullWidth:
133-
return GADAdSizeFullWidthPortraitWithHeight(0)
134-
default:
135-
return kGADAdSizeInvalid
136-
}
137-
default:
138-
return kGADAdSizeInvalid
88+
public static func createInlineAdaptiveBanner(_ width: CGFloat, _ maxHeight: CGFloat,_ orientation: Orientation) -> GADAdSize {
89+
if(maxHeight > 0){
90+
return GADInlineAdaptiveBannerAdSizeWithWidthAndMaxHeight(width, maxHeight)
13991
}
140-
}
141-
142-
143-
144-
public static func createInlineAdaptiveBanner(width: CGFloat,_ orientation: Orientation) -> GADAdSize {
14592
switch orientation {
14693
case .Landscape:
14794
return GADLandscapeInlineAdaptiveBannerAdSizeWithWidth(width)
@@ -151,30 +98,4 @@ public class TNSGA:NSObject {
15198
return GADCurrentOrientationInlineAdaptiveBannerAdSizeWithWidth(width)
15299
}
153100
}
154-
155-
156-
public static func createInlineAdaptiveBanner(_ width: Width,_ orientation: Orientation) -> GADAdSize {
157-
switch orientation {
158-
case .Landscape:
159-
switch width {
160-
case .AutoHeight:
161-
return GADAdSizeFullWidthLandscapeWithHeight(0)
162-
case .FullWidth:
163-
return GADAdSizeFullWidthLandscapeWithHeight(0)
164-
default:
165-
return kGADAdSizeInvalid
166-
}
167-
case .Portrait:
168-
switch width {
169-
case .AutoHeight:
170-
return GADAdSizeFullWidthPortraitWithHeight(0)
171-
case .FullWidth:
172-
return GADAdSizeFullWidthPortraitWithHeight(0)
173-
default:
174-
return kGADAdSizeInvalid
175-
}
176-
default:
177-
return kGADAdSizeInvalid
178-
}
179-
}
180101
}

0 commit comments

Comments
 (0)