Skip to content

Commit ff6eea6

Browse files
yarivgdidiclaude
andcommitted
fix: typed listener overloads, isAdLoaded check, remove unused factory
- Add per-event typed addListener overloads to AppOpenAdPlugin interface, matching the pattern in InterstitialDefinitions (FailedToLoad/FailedToShow receive AdMobError, others receive void) - iOS: check isAdLoaded() before attempting to show, giving a specific "App Open Ad is not loaded" rejection instead of a generic error - Remove unused createAppOpenOptions/APP_OPEN_TESTER_ID from AdOptions and their corresponding tests (App Open reads adId directly from call) Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent 53bbcd7 commit ff6eea6

5 files changed

Lines changed: 104 additions & 115 deletions

File tree

README.md

Lines changed: 86 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,11 @@ AdMob.addListener(RewardAdPluginEvents.Rewarded, async () => {
374374
* [`loadAppOpen(...)`](#loadappopen)
375375
* [`showAppOpen()`](#showappopen)
376376
* [`isAppOpenLoaded()`](#isappopenloaded)
377-
* [`addListener(AppOpenAdPluginEvents, ...)`](#addlistenerappopenadpluginevents-)
377+
* [`addListener(AppOpenAdPluginEvents.Loaded, ...)`](#addlistenerappopenadplugineventsloaded-)
378+
* [`addListener(AppOpenAdPluginEvents.FailedToLoad, ...)`](#addlistenerappopenadplugineventsfailedtoload-)
379+
* [`addListener(AppOpenAdPluginEvents.Opened, ...)`](#addlistenerappopenadplugineventsopened-)
380+
* [`addListener(AppOpenAdPluginEvents.Closed, ...)`](#addlistenerappopenadplugineventsclosed-)
381+
* [`addListener(AppOpenAdPluginEvents.FailedToShow, ...)`](#addlistenerappopenadplugineventsfailedtoshow-)
378382
* [`showBanner(...)`](#showbanner)
379383
* [`hideBanner()`](#hidebanner)
380384
* [`resumeBanner()`](#resumebanner)
@@ -541,18 +545,80 @@ Check if the App Open ad is loaded
541545
--------------------
542546

543547

544-
### addListener(AppOpenAdPluginEvents, ...)
548+
### addListener(AppOpenAdPluginEvents.Loaded, ...)
545549

546550
```typescript
547-
addListener(eventName: AppOpenAdPluginEvents, listenerFunc: (...args: any[]) => void) => Promise<PluginListenerHandle>
551+
addListener(eventName: AppOpenAdPluginEvents.Loaded, listenerFunc: () => void) => Promise<PluginListenerHandle>
548552
```
549553

550-
Add listeners for App Open events
554+
| Param | Type |
555+
| ------------------ | ------------------------------------------------------------------------------ |
556+
| **`eventName`** | <code><a href="#appopenadpluginevents">AppOpenAdPluginEvents.Loaded</a></code> |
557+
| **`listenerFunc`** | <code>() =&gt; void</code> |
551558

552-
| Param | Type |
553-
| ------------------ | ----------------------------------------------------------------------- |
554-
| **`eventName`** | <code><a href="#appopenadpluginevents">AppOpenAdPluginEvents</a></code> |
555-
| **`listenerFunc`** | <code>(...args: any[]) =&gt; void</code> |
559+
**Returns:** <code>Promise&lt;<a href="#pluginlistenerhandle">PluginListenerHandle</a>&gt;</code>
560+
561+
--------------------
562+
563+
564+
### addListener(AppOpenAdPluginEvents.FailedToLoad, ...)
565+
566+
```typescript
567+
addListener(eventName: AppOpenAdPluginEvents.FailedToLoad, listenerFunc: (error: AdMobError) => void) => Promise<PluginListenerHandle>
568+
```
569+
570+
| Param | Type |
571+
| ------------------ | ------------------------------------------------------------------------------------ |
572+
| **`eventName`** | <code><a href="#appopenadpluginevents">AppOpenAdPluginEvents.FailedToLoad</a></code> |
573+
| **`listenerFunc`** | <code>(error: <a href="#admoberror">AdMobError</a>) =&gt; void</code> |
574+
575+
**Returns:** <code>Promise&lt;<a href="#pluginlistenerhandle">PluginListenerHandle</a>&gt;</code>
576+
577+
--------------------
578+
579+
580+
### addListener(AppOpenAdPluginEvents.Opened, ...)
581+
582+
```typescript
583+
addListener(eventName: AppOpenAdPluginEvents.Opened, listenerFunc: () => void) => Promise<PluginListenerHandle>
584+
```
585+
586+
| Param | Type |
587+
| ------------------ | ------------------------------------------------------------------------------ |
588+
| **`eventName`** | <code><a href="#appopenadpluginevents">AppOpenAdPluginEvents.Opened</a></code> |
589+
| **`listenerFunc`** | <code>() =&gt; void</code> |
590+
591+
**Returns:** <code>Promise&lt;<a href="#pluginlistenerhandle">PluginListenerHandle</a>&gt;</code>
592+
593+
--------------------
594+
595+
596+
### addListener(AppOpenAdPluginEvents.Closed, ...)
597+
598+
```typescript
599+
addListener(eventName: AppOpenAdPluginEvents.Closed, listenerFunc: () => void) => Promise<PluginListenerHandle>
600+
```
601+
602+
| Param | Type |
603+
| ------------------ | ------------------------------------------------------------------------------ |
604+
| **`eventName`** | <code><a href="#appopenadpluginevents">AppOpenAdPluginEvents.Closed</a></code> |
605+
| **`listenerFunc`** | <code>() =&gt; void</code> |
606+
607+
**Returns:** <code>Promise&lt;<a href="#pluginlistenerhandle">PluginListenerHandle</a>&gt;</code>
608+
609+
--------------------
610+
611+
612+
### addListener(AppOpenAdPluginEvents.FailedToShow, ...)
613+
614+
```typescript
615+
addListener(eventName: AppOpenAdPluginEvents.FailedToShow, listenerFunc: (error: AdMobError) => void) => Promise<PluginListenerHandle>
616+
```
617+
618+
| Param | Type |
619+
| ------------------ | ------------------------------------------------------------------------------------ |
620+
| **`eventName`** | <code><a href="#appopenadpluginevents">AppOpenAdPluginEvents.FailedToShow</a></code> |
621+
| **`listenerFunc`** | <code>(error: <a href="#admoberror">AdMobError</a>) =&gt; void</code> |
556622

557623
**Returns:** <code>Promise&lt;<a href="#pluginlistenerhandle">PluginListenerHandle</a>&gt;</code>
558624

@@ -1214,6 +1280,17 @@ addListener(eventName: RewardInterstitialAdPluginEvents.Showed, listenerFunc: ()
12141280
| **`remove`** | <code>() =&gt; Promise&lt;void&gt;</code> |
12151281

12161282

1283+
#### AdMobError
1284+
1285+
For more information
1286+
https://developers.google.com/android/reference/com/google/android/gms/ads/AdError
1287+
1288+
| Prop | Type | Description |
1289+
| ------------- | ------------------- | -------------------------------------- |
1290+
| **`code`** | <code>number</code> | Gets the error's code. |
1291+
| **`message`** | <code>string</code> | Gets the message describing the error. |
1292+
1293+
12171294
#### BannerAdOptions
12181295

12191296
This interface extends <a href="#adoptions">AdOptions</a>
@@ -1239,17 +1316,6 @@ When notice listener of OnAdLoaded, you can get banner size.
12391316
| **`height`** | <code>number</code> |
12401317

12411318

1242-
#### AdMobError
1243-
1244-
For more information
1245-
https://developers.google.com/android/reference/com/google/android/gms/ads/AdError
1246-
1247-
| Prop | Type | Description |
1248-
| ------------- | ------------------- | -------------------------------------- |
1249-
| **`code`** | <code>number</code> | Gets the error's code. |
1250-
| **`message`** | <code>string</code> | Gets the message describing the error. |
1251-
1252-
12531319
#### AdmobConsentInfo
12541320

12551321
| Prop | Type | Description | Since |
@@ -1301,7 +1367,7 @@ https://developers.google.com/android/reference/com/google/android/gms/ads/AdErr
13011367

13021368
#### AdMobRewardItem
13031369

1304-
For more information
1370+
For more information
13051371
https://developers.google.com/admob/android/rewarded-video-adapters?hl=en
13061372

13071373
| Prop | Type | Description |

android/src/main/java/com/getcapacitor/community/admob/models/AdOptions.java

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ public abstract class AdOptions {
2828
public static final String INTERSTITIAL_TESTER_ID = "ca-app-pub-3940256099942544/1033173712";
2929
public static final String REWARD_VIDEO_TESTER_ID = "ca-app-pub-3940256099942544/5224354917";
3030
public static final String REWARD_INTERSTITIAL_TESTER_ID = "ca-app-pub-3940256099942544/5354046379";
31-
public static final String APP_OPEN_TESTER_ID = "ca-app-pub-3940256099942544/9257395921";
3231

3332
/**
3433
* The position of the ad, it can be TOP_CENTER,
@@ -149,15 +148,6 @@ public String getTestingId() {
149148
};
150149
}
151150

152-
public AdOptions createAppOpenOptions(PluginCall call) {
153-
return new AdOptions(call) {
154-
@Override
155-
public String getTestingId() {
156-
return AdOptions.APP_OPEN_TESTER_ID;
157-
}
158-
};
159-
}
160-
161151
public AdOptions createGenericOptions(PluginCall call, final String testingID) {
162152
return new AdOptions(call) {
163153
@Override

android/src/test/java/com/getcapacitor/community/admob/models/AdOptionsTest.java

Lines changed: 0 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -118,84 +118,5 @@ public void ssv() {
118118
assertEquals(userId, adOptions.ssvInfo.getUserId());
119119
assertEquals(customData, adOptions.ssvInfo.getCustomData());
120120
}
121-
122-
@Test
123-
public void appOpen_ad_Id() {
124-
final String expected = "Some Given AppOpen Test Id";
125-
when(pluginCallMock.getString(eq("adId"), anyString())).thenReturn(expected);
126-
127-
final AdOptions adOptions = AdOptions.getFactory().createAppOpenOptions(pluginCallMock);
128-
129-
assertEquals(expected, adOptions.adId);
130-
}
131-
132-
@Test
133-
public void appOpen_position() {
134-
final String wantedProperty = "position";
135-
final String expected = "TOP_CENTER";
136-
final String defaultValue = "BOTTOM_CENTER";
137-
when(pluginCallMock.getString(eq(wantedProperty), anyString())).thenReturn(expected);
138-
139-
final AdOptions adOptions = AdOptions.getFactory().createAppOpenOptions(pluginCallMock);
140-
141-
verify(pluginCallMock).getString(wantedProperty, defaultValue);
142-
assertEquals(expected, adOptions.position);
143-
}
144-
145-
@Test
146-
public void appOpen_margin() {
147-
final String wantedProperty = "margin";
148-
final int expected = 10;
149-
final int defaultValue = 0;
150-
when(pluginCallMock.getInt(eq(wantedProperty), anyInt())).thenReturn(expected);
151-
152-
final AdOptions adOptions = AdOptions.getFactory().createAppOpenOptions(pluginCallMock);
153-
154-
verify(pluginCallMock).getInt(wantedProperty, defaultValue);
155-
assertEquals(expected, adOptions.margin);
156-
}
157-
158-
@Test
159-
public void appOpen_isTesting() {
160-
final String wantedProperty = "isTesting";
161-
final boolean expected = true;
162-
final boolean defaultValue = false;
163-
when(pluginCallMock.getBoolean(eq(wantedProperty), anyBoolean())).thenReturn(expected);
164-
165-
final AdOptions adOptions = AdOptions.getFactory().createAppOpenOptions(pluginCallMock);
166-
167-
verify(pluginCallMock).getBoolean(wantedProperty, defaultValue);
168-
assertEquals(expected, adOptions.isTesting);
169-
}
170-
171-
@Test
172-
public void appOpen_npa() {
173-
final String wantedProperty = "npa";
174-
final boolean expected = true;
175-
final boolean defaultValue = false;
176-
lenient().when(pluginCallMock.getBoolean(eq(wantedProperty), anyBoolean())).thenReturn(expected);
177-
178-
final AdOptions adOptions = AdOptions.getFactory().createAppOpenOptions(pluginCallMock);
179-
180-
verify(pluginCallMock).getBoolean(wantedProperty, defaultValue);
181-
assertEquals(expected, adOptions.npa);
182-
}
183-
184-
@Test
185-
public void appOpen_ssv() {
186-
final String customData = "customData";
187-
final String userId = "userId";
188-
final String wantedProperty = "ssv";
189-
final JSObject expected = new JSObject();
190-
expected.put(customData, customData);
191-
expected.put(userId, userId);
192-
lenient().when(pluginCallMock.getObject(eq(wantedProperty))).thenReturn(expected);
193-
194-
final AdOptions adOptions = AdOptions.getFactory().createAppOpenOptions(pluginCallMock);
195-
196-
verify(pluginCallMock, atLeastOnce()).getObject(wantedProperty);
197-
assertEquals(userId, adOptions.ssvInfo.getUserId());
198-
assertEquals(customData, adOptions.ssvInfo.getCustomData());
199-
}
200121
}
201122
}

ios/Sources/AdMobPlugin/AppOpen/AppOpenAdPlugin.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ import UIKit
4444
notify: @escaping (String, [String: Any]) -> Void
4545
) {
4646
DispatchQueue.main.async {
47-
guard self.appOpenAdManager != nil else {
48-
call.reject("App Open Ad manager is not initialized")
47+
guard let manager = self.appOpenAdManager, manager.isAdLoaded() else {
48+
call.reject("App Open Ad is not loaded")
4949
return
5050
}
5151

src/app-open/app-open-definitions.interface.ts

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { PluginListenerHandle } from '@capacitor/core';
22

33
import type { ValidateAllEventsEnumAreImplemented } from '../private/validate-all-events-implemented.type';
4+
import type { AdMobError } from '../shared';
45

56
import type { AppOpenAdOptions } from './app-open-ad-options.interface';
67
import type { AppOpenAdPluginEvents } from './app-open-ad-plugin-events.enum';
@@ -26,8 +27,19 @@ export interface AppOpenAdPlugin {
2627
*/
2728
isAppOpenLoaded(): Promise<{ value: boolean }>;
2829

29-
/**
30-
* Add listeners for App Open events
31-
*/
32-
addListener(eventName: AppOpenAdPluginEvents, listenerFunc: (...args: any[]) => void): Promise<PluginListenerHandle>;
30+
addListener(eventName: AppOpenAdPluginEvents.Loaded, listenerFunc: () => void): Promise<PluginListenerHandle>;
31+
32+
addListener(
33+
eventName: AppOpenAdPluginEvents.FailedToLoad,
34+
listenerFunc: (error: AdMobError) => void,
35+
): Promise<PluginListenerHandle>;
36+
37+
addListener(eventName: AppOpenAdPluginEvents.Opened, listenerFunc: () => void): Promise<PluginListenerHandle>;
38+
39+
addListener(eventName: AppOpenAdPluginEvents.Closed, listenerFunc: () => void): Promise<PluginListenerHandle>;
40+
41+
addListener(
42+
eventName: AppOpenAdPluginEvents.FailedToShow,
43+
listenerFunc: (error: AdMobError) => void,
44+
): Promise<PluginListenerHandle>;
3345
}

0 commit comments

Comments
 (0)