Skip to content
This repository has been archived by the owner on Apr 27, 2022. It is now read-only.

Commit

Permalink
Merge pull request #73 from react-native-admob/initialize-ondismissed
Browse files Browse the repository at this point in the history
fix: initialize hook values when loadOnDismissed
  • Loading branch information
wjaykim authored Nov 26, 2021
2 parents 747dacf + e28bc6b commit c89aada
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 40 deletions.
3 changes: 1 addition & 2 deletions src/ads/banner/BaseAd.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ import {
ViewStyle,
} from 'react-native';

import { GAMBannerAdProps } from '../..';
import { BannerAdProps } from '../../types';
import { BannerAdProps, GAMBannerAdProps } from '../../types';

interface BannerAdState {
style: StyleProp<ViewStyle>;
Expand Down
14 changes: 7 additions & 7 deletions src/ads/fullscreen/AppOpenAd.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {
AppOpenAdEvent,
AppOpenAdOptions,
HandlerType,
FullScreenAdEvent,
FullScreenAdHandlerType,
RequestOptions,
} from '../../types';

Expand All @@ -15,10 +15,7 @@ const defaultOptions: AppOpenAdOptions = {

let _appOpenRequest = 0;

export default class AppOpenAd extends FullScreenAd<
AppOpenAdEvent,
HandlerType
> {
export default class AppOpenAd extends FullScreenAd {
private constructor(
requestId: number,
unitId: string,
Expand Down Expand Up @@ -97,7 +94,10 @@ export default class AppOpenAd extends FullScreenAd<
* @param event Event name
* @param handler Event handler
*/
static addEventListener(event: AppOpenAdEvent, handler: HandlerType) {
static addEventListener(
event: FullScreenAdEvent,
handler: FullScreenAdHandlerType
) {
this.checkInstance();
return this.sharedInstance!.addEventListener(event, handler);
}
Expand Down
18 changes: 11 additions & 7 deletions src/ads/fullscreen/FullScreenAd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,12 @@ import {
import {
AdType,
AppOpenAdOptions,
FullScreenAdEvent,
FullScreenAdHandlerType,
FullScreenAdOptions,
RequestOptions,
RewardedAdEvent,
RewardedAdHandlerType,
} from '../../types';

const RNAdMobEvent = NativeModules.RNAdMobEvent;
Expand Down Expand Up @@ -39,14 +43,14 @@ const defaultOptions: FullScreenAdOptions = {
};

export default class FullScreenAd<
E extends string,
H extends (event?: any) => any
E extends RewardedAdEvent = FullScreenAdEvent,
H extends RewardedAdHandlerType = FullScreenAdHandlerType
> {
type: AdType;
requestId: number;
unitId: string;
listeners: EmitterSubscription[];
options: FullScreenAdOptions | AppOpenAdOptions;
readonly type: AdType;
readonly requestId: number;
readonly unitId: string;
readonly options: FullScreenAdOptions | AppOpenAdOptions;
private listeners: EmitterSubscription[];
private nativeModule: FullScreenAdInterface;

protected constructor(
Expand Down
11 changes: 2 additions & 9 deletions src/ads/fullscreen/InterstitialAd.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,10 @@
import {
FullScreenAdOptions,
HandlerType,
InterstitialAdEvent,
} from '../../types';
import { FullScreenAdOptions } from '../../types';

import FullScreenAd from './FullScreenAd';

let _interstitialRequest = 0;

export default class InterstitialAd extends FullScreenAd<
InterstitialAdEvent,
HandlerType
> {
export default class InterstitialAd extends FullScreenAd {
private constructor(
requestId: number,
unitId: string,
Expand Down
24 changes: 15 additions & 9 deletions src/hooks/useFullScreenAd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@ import {
RewardedAd,
RewardedInterstitialAd,
} from '../ads/fullscreen';
import { AdHookReturns, RequestOptions, Reward } from '../types';
import {
AdHookReturns,
FullScreenAdOptions,
RequestOptions,
Reward,
} from '../types';

export default function useFullScreenAd<
T extends
Expand Down Expand Up @@ -41,15 +46,15 @@ export default function useFullScreenAd<
(requestOptions?: RequestOptions) => {
if (ad) {
initialize();
ad.load(requestOptions);
ad.load(requestOptions).catch(() => {});
}
},
[ad]
);

const show = useCallback(() => {
if (ad) {
ad.show();
ad.show().catch(() => {});
}
}, [ad]);

Expand All @@ -58,28 +63,29 @@ export default function useFullScreenAd<
initialize();
return;
}
const isRewardedAd =
ad instanceof RewardedAd || ad instanceof RewardedInterstitialAd;
const listeners = [
ad.addEventListener('adLoaded', () => {
setAdLoaded(true);
setAdPresented(false);
}),
ad.addEventListener('adFailedToLoad', (error: Error) =>
setAdLoadError(error)
),
ad.addEventListener('adPresented', () => {
setAdPresented(true);
setAdDismissed(false);
}),
ad.addEventListener('adFailedToPresent', (error: Error) =>
setAdPresentError(error)
),
ad.addEventListener('adDismissed', () => {
setAdDismissed(true);
setAdLoaded(false);
if (
ad.type !== 'AppOpen' &&
(ad.options as FullScreenAdOptions).loadOnDismissed
) {
initialize();
}
}),
isRewardedAd
ad.type === 'Rewarded' || ad.type === 'RewardedInterstitial'
? (ad as RewardedAd | RewardedInterstitialAd).addEventListener(
'rewarded',
(r: Reward) => setReward(r)
Expand Down
10 changes: 4 additions & 6 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,15 +189,13 @@ export type FullScreenAdEvent =
| 'adLoaded'
| 'adFailedToLoad';

export type InterstitialAdEvent = FullScreenAdEvent;

export type RewardedAdEvent = FullScreenAdEvent | 'rewarded';

export type AppOpenAdEvent = FullScreenAdEvent;

export type HandlerType = (() => void) | ((error: Error) => void);
export type FullScreenAdHandlerType = (() => void) | ((error: Error) => void);

export type RewardedAdHandlerType = HandlerType | ((reward: Reward) => void);
export type RewardedAdHandlerType =
| FullScreenAdHandlerType
| ((reward: Reward) => void);

export interface Reward {
type: string;
Expand Down

0 comments on commit c89aada

Please sign in to comment.