2017-11-23 17:29:40 +00:00
|
|
|
/**
|
|
|
|
* @flow
|
|
|
|
* AdMob representation wrapper
|
|
|
|
*/
|
2017-12-22 15:24:31 +00:00
|
|
|
import { SharedEventEmitter } from '../../utils/events';
|
|
|
|
import { getLogger } from '../../utils/log';
|
2018-01-05 17:20:02 +00:00
|
|
|
import { getNativeModule } from '../../utils/native';
|
2017-12-22 15:24:31 +00:00
|
|
|
import ModuleBase from '../../utils/ModuleBase';
|
2017-05-27 14:24:51 +00:00
|
|
|
|
2017-05-26 14:18:57 +00:00
|
|
|
import Interstitial from './Interstitial';
|
2017-05-27 16:03:09 +00:00
|
|
|
import RewardedVideo from './RewardedVideo';
|
2017-05-26 14:18:57 +00:00
|
|
|
import AdRequest from './AdRequest';
|
2017-05-31 15:33:08 +00:00
|
|
|
import VideoOptions from './VideoOptions';
|
2017-05-26 14:18:57 +00:00
|
|
|
import Banner from './Banner';
|
2017-05-31 15:33:08 +00:00
|
|
|
import NativeExpress from './NativeExpress';
|
2017-05-26 14:18:57 +00:00
|
|
|
|
2017-08-21 05:58:40 +00:00
|
|
|
import EventTypes, {
|
|
|
|
NativeExpressEventTypes,
|
2017-08-22 16:35:25 +00:00
|
|
|
RewardedVideoEventTypes,
|
|
|
|
} from './EventTypes';
|
2017-05-26 14:18:57 +00:00
|
|
|
|
2018-02-14 13:00:19 +00:00
|
|
|
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'];
|
2017-12-22 15:57:33 +00:00
|
|
|
|
2018-01-31 15:28:20 +00:00
|
|
|
export const MODULE_NAME = 'RNFirebaseAdMob';
|
2018-01-03 20:00:38 +00:00
|
|
|
export const NAMESPACE = 'admob';
|
2017-08-17 16:25:13 +00:00
|
|
|
|
2018-01-03 20:00:38 +00:00
|
|
|
export default class AdMob extends ModuleBase {
|
2017-12-06 16:42:21 +00:00
|
|
|
_appId: ?string;
|
|
|
|
_initialized: boolean;
|
|
|
|
|
2018-01-05 17:20:02 +00:00
|
|
|
constructor(app: App) {
|
|
|
|
super(app, {
|
2018-01-03 20:00:38 +00:00
|
|
|
events: NATIVE_EVENTS,
|
|
|
|
moduleName: MODULE_NAME,
|
2018-01-09 17:31:00 +00:00
|
|
|
multiApp: false,
|
2017-11-21 23:37:05 +00:00
|
|
|
hasShards: false,
|
2018-01-03 20:00:38 +00:00
|
|
|
namespace: NAMESPACE,
|
|
|
|
});
|
2017-05-27 14:24:51 +00:00
|
|
|
|
2017-06-01 09:22:15 +00:00
|
|
|
this._initialized = false;
|
2017-06-27 14:14:02 +00:00
|
|
|
this._appId = null;
|
2017-06-30 16:23:32 +00:00
|
|
|
|
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-05-26 16:55:22 +00:00
|
|
|
}
|
|
|
|
|
2017-12-06 16:42:21 +00:00
|
|
|
_onInterstitialEvent(event: NativeEvent): void {
|
2017-05-27 16:03:09 +00:00
|
|
|
const { adUnit } = event;
|
|
|
|
const jsEventType = `interstitial_${adUnit}`;
|
|
|
|
|
2018-01-31 16:05:38 +00:00
|
|
|
if (SharedEventEmitter.listeners(jsEventType).length === 0) {
|
2017-05-27 16:03:09 +00:00
|
|
|
// TODO
|
|
|
|
}
|
|
|
|
|
2017-12-22 15:24:31 +00:00
|
|
|
SharedEventEmitter.emit(jsEventType, event);
|
2017-05-27 16:03:09 +00:00
|
|
|
}
|
|
|
|
|
2017-12-06 16:42:21 +00:00
|
|
|
_onRewardedVideoEvent(event: NativeEvent): void {
|
2017-05-27 16:03:09 +00:00
|
|
|
const { adUnit } = event;
|
|
|
|
const jsEventType = `rewarded_video_${adUnit}`;
|
2017-05-26 16:55:22 +00:00
|
|
|
|
2018-01-31 16:05:38 +00:00
|
|
|
if (SharedEventEmitter.listeners(jsEventType).length === 0) {
|
2017-05-26 16:55:22 +00:00
|
|
|
// TODO
|
|
|
|
}
|
|
|
|
|
2017-12-22 15:24:31 +00:00
|
|
|
SharedEventEmitter.emit(jsEventType, event);
|
2017-05-26 16:55:22 +00:00
|
|
|
}
|
|
|
|
|
2017-12-06 16:42:21 +00:00
|
|
|
initialize(appId: string): void {
|
2017-06-01 09:22:15 +00:00
|
|
|
if (this._initialized) {
|
2017-12-22 15:24:31 +00:00
|
|
|
getLogger(this).warn('AdMob has already been initialized!');
|
2017-06-30 16:23:32 +00:00
|
|
|
} else {
|
|
|
|
this._initialized = true;
|
|
|
|
this._appId = appId;
|
2018-01-05 17:20:02 +00:00
|
|
|
getNativeModule(this).initialize(appId);
|
2017-06-01 09:22:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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!'
|
|
|
|
);
|
2017-06-30 16:23:32 +00:00
|
|
|
} else {
|
2017-12-22 15:24:31 +00:00
|
|
|
getLogger(this).info('Opening debug menu');
|
2018-01-05 17:20:02 +00:00
|
|
|
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 {
|
2017-05-26 16:55:22 +00:00
|
|
|
return new Interstitial(this, adUnit);
|
2017-05-26 14:18:57 +00:00
|
|
|
}
|
|
|
|
|
2017-12-06 16:42:21 +00:00
|
|
|
rewarded(adUnit: string): RewardedVideo {
|
2017-05-27 16:03:09 +00:00
|
|
|
return new RewardedVideo(this, adUnit);
|
|
|
|
}
|
2017-05-26 14:18:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export const statics = {
|
|
|
|
Banner,
|
2017-05-31 15:33:08 +00:00
|
|
|
NativeExpress,
|
2017-05-26 14:18:57 +00:00
|
|
|
AdRequest,
|
2017-05-31 15:33:08 +00:00
|
|
|
VideoOptions,
|
2017-08-21 05:58:40 +00:00
|
|
|
EventTypes,
|
|
|
|
RewardedVideoEventTypes,
|
2017-08-22 16:35:25 +00:00
|
|
|
NativeExpressEventTypes,
|
2017-05-26 14:18:57 +00:00
|
|
|
};
|