react-native-firebase/lib/modules/admob/index.js

122 lines
2.8 KiB
JavaScript
Raw Normal View History

2017-11-23 17:29:40 +00:00
/**
* @flow
* AdMob representation wrapper
*/
import { SharedEventEmitter } from '../../utils/events';
import { getLogger } from '../../utils/log';
import { getNativeModule } from '../../utils/native';
import ModuleBase from '../../utils/ModuleBase';
2017-05-27 14:24:51 +00:00
import Interstitial from './Interstitial';
import RewardedVideo from './RewardedVideo';
import AdRequest from './AdRequest';
import VideoOptions from './VideoOptions';
import Banner from './Banner';
import NativeExpress from './NativeExpress';
import EventTypes, {
NativeExpressEventTypes,
2017-08-22 16:35:25 +00:00
RewardedVideoEventTypes,
} from './EventTypes';
import type App from '../core/app';
2017-12-06 16:42:21 +00:00
type NativeEvent = {
adUnit: string,
payload: Object,
type: string,
2018-01-25 18:25:39 +00:00
};
2017-12-06 16:42:21 +00:00
2018-01-25 18:25:39 +00:00
const NATIVE_EVENTS = ['interstitial_event', 'rewarded_video_event'];
2018-01-31 15:28:20 +00:00
export const MODULE_NAME = 'RNFirebaseAdMob';
export const NAMESPACE = 'admob';
export default class AdMob extends ModuleBase {
2017-12-06 16:42:21 +00:00
_appId: ?string;
_initialized: boolean;
constructor(app: App) {
super(app, {
events: NATIVE_EVENTS,
moduleName: MODULE_NAME,
multiApp: false,
hasShards: false,
namespace: NAMESPACE,
});
2017-05-27 14:24:51 +00:00
this._initialized = false;
2017-06-27 14:14:02 +00:00
this._appId = null;
2018-01-25 18:25:39 +00:00
SharedEventEmitter.addListener(
'interstitial_event',
this._onInterstitialEvent.bind(this)
);
SharedEventEmitter.addListener(
'rewarded_video_event',
this._onRewardedVideoEvent.bind(this)
);
}
2017-12-06 16:42:21 +00:00
_onInterstitialEvent(event: NativeEvent): void {
const { adUnit } = event;
const jsEventType = `interstitial_${adUnit}`;
2018-01-31 16:05:38 +00:00
if (SharedEventEmitter.listeners(jsEventType).length === 0) {
// TODO
}
SharedEventEmitter.emit(jsEventType, event);
}
2017-12-06 16:42:21 +00:00
_onRewardedVideoEvent(event: NativeEvent): void {
const { adUnit } = event;
const jsEventType = `rewarded_video_${adUnit}`;
2018-01-31 16:05:38 +00:00
if (SharedEventEmitter.listeners(jsEventType).length === 0) {
// TODO
}
SharedEventEmitter.emit(jsEventType, event);
}
2017-12-06 16:42:21 +00:00
initialize(appId: string): void {
if (this._initialized) {
getLogger(this).warn('AdMob has already been initialized!');
} else {
this._initialized = true;
this._appId = appId;
getNativeModule(this).initialize(appId);
}
}
2017-12-06 16:42:21 +00:00
openDebugMenu(): void {
2017-06-27 14:14:02 +00:00
if (!this._initialized) {
2018-01-25 18:25:39 +00:00
getLogger(this).warn(
'AdMob needs to be initialized before opening the dev menu!'
);
} else {
getLogger(this).info('Opening debug menu');
getNativeModule(this).openDebugMenu(this._appId);
2017-06-27 14:14:02 +00:00
}
}
2017-12-06 16:42:21 +00:00
interstitial(adUnit: string): Interstitial {
return new Interstitial(this, adUnit);
}
2017-12-06 16:42:21 +00:00
rewarded(adUnit: string): RewardedVideo {
return new RewardedVideo(this, adUnit);
}
}
export const statics = {
Banner,
NativeExpress,
AdRequest,
VideoOptions,
EventTypes,
RewardedVideoEventTypes,
2017-08-22 16:35:25 +00:00
NativeExpressEventTypes,
};