2018-01-31 16:05:38 +00:00
|
|
|
import { Platform } from 'react-native';
|
2017-05-26 16:54:09 +00:00
|
|
|
import { statics } from './';
|
2017-06-07 11:37:32 +00:00
|
|
|
import AdRequest from './AdRequest';
|
2017-12-22 15:24:31 +00:00
|
|
|
import { SharedEventEmitter } from '../../utils/events';
|
2018-01-31 16:05:38 +00:00
|
|
|
import { getNativeModule } from '../../utils/native';
|
2017-05-26 16:54:09 +00:00
|
|
|
import { nativeToJSError } from '../../utils';
|
2018-01-31 16:05:38 +00:00
|
|
|
import type AdMob from './';
|
2017-05-26 14:18:57 +00:00
|
|
|
|
2017-07-06 09:21:32 +00:00
|
|
|
let subscriptions = [];
|
|
|
|
|
2017-05-26 14:18:57 +00:00
|
|
|
export default class Interstitial {
|
2018-01-31 16:05:38 +00:00
|
|
|
_admob: AdMob;
|
|
|
|
|
|
|
|
constructor(admob: AdMob, adUnit: string) {
|
2017-07-04 14:39:56 +00:00
|
|
|
// Interstitials on iOS require a new instance each time
|
|
|
|
if (Platform.OS === 'ios') {
|
2018-01-31 16:05:38 +00:00
|
|
|
getNativeModule(admob).clearInterstitial(adUnit);
|
2017-07-04 14:39:56 +00:00
|
|
|
}
|
|
|
|
|
2017-07-06 09:21:32 +00:00
|
|
|
for (let i = 0, len = subscriptions.length; i < len; i++) {
|
|
|
|
subscriptions[i].remove();
|
|
|
|
}
|
|
|
|
subscriptions = [];
|
|
|
|
|
2018-01-31 16:05:38 +00:00
|
|
|
this._admob = admob;
|
2017-05-27 16:03:09 +00:00
|
|
|
this.adUnit = adUnit;
|
2017-05-26 16:54:09 +00:00
|
|
|
this.loaded = false;
|
2017-12-22 15:24:31 +00:00
|
|
|
SharedEventEmitter.removeAllListeners(`interstitial_${adUnit}`);
|
2018-01-25 18:25:39 +00:00
|
|
|
SharedEventEmitter.addListener(
|
|
|
|
`interstitial_${adUnit}`,
|
|
|
|
this._onInterstitialEvent
|
|
|
|
);
|
2017-05-26 14:18:57 +00:00
|
|
|
}
|
|
|
|
|
2017-05-26 16:54:09 +00:00
|
|
|
/**
|
|
|
|
* Handle a JS emit event
|
|
|
|
* @param event
|
|
|
|
* @private
|
|
|
|
*/
|
2018-01-25 18:25:39 +00:00
|
|
|
_onInterstitialEvent = event => {
|
2017-05-26 16:54:09 +00:00
|
|
|
const eventType = `interstitial:${this.adUnit}:${event.type}`;
|
|
|
|
|
|
|
|
let emitData = Object.assign({}, event);
|
|
|
|
|
|
|
|
switch (event.type) {
|
|
|
|
case 'onAdLoaded':
|
|
|
|
this.loaded = true;
|
|
|
|
break;
|
|
|
|
case 'onAdFailedToLoad':
|
|
|
|
emitData = nativeToJSError(event.payload.code, event.payload.message);
|
|
|
|
emitData.type = event.type;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
}
|
2017-05-26 14:18:57 +00:00
|
|
|
|
2017-12-22 15:24:31 +00:00
|
|
|
SharedEventEmitter.emit(eventType, emitData);
|
|
|
|
SharedEventEmitter.emit(`interstitial:${this.adUnit}:*`, emitData);
|
2017-07-06 09:21:32 +00:00
|
|
|
};
|
2017-05-26 14:18:57 +00:00
|
|
|
|
2017-05-26 16:54:09 +00:00
|
|
|
/**
|
|
|
|
* Load an ad with an instance of AdRequest
|
|
|
|
* @param request
|
|
|
|
* @returns {*}
|
|
|
|
*/
|
2017-06-07 11:37:32 +00:00
|
|
|
loadAd(request?: AdRequest) {
|
|
|
|
let adRequest = request;
|
|
|
|
|
|
|
|
if (!adRequest || !Object.keys(adRequest)) {
|
|
|
|
adRequest = new AdRequest().addTestDevice().build();
|
|
|
|
}
|
|
|
|
|
2018-01-31 16:05:38 +00:00
|
|
|
return getNativeModule(this._admob).interstitialLoadAd(
|
|
|
|
this.adUnit,
|
|
|
|
adRequest
|
|
|
|
);
|
2017-05-26 14:18:57 +00:00
|
|
|
}
|
|
|
|
|
2017-05-26 16:54:09 +00:00
|
|
|
/**
|
|
|
|
* Return a local instance of isLoaded
|
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
2017-05-26 14:18:57 +00:00
|
|
|
isLoaded() {
|
2017-05-26 16:54:09 +00:00
|
|
|
return this.loaded;
|
2017-05-26 14:18:57 +00:00
|
|
|
}
|
|
|
|
|
2017-05-26 16:54:09 +00:00
|
|
|
/**
|
|
|
|
* Show the advert - will only show if loaded
|
|
|
|
* @returns {*}
|
|
|
|
*/
|
2017-05-26 14:18:57 +00:00
|
|
|
show() {
|
2017-05-26 16:54:09 +00:00
|
|
|
if (this.loaded) {
|
2018-01-31 16:05:38 +00:00
|
|
|
getNativeModule(this._admob).interstitialShowAd(this.adUnit);
|
2017-05-26 16:54:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Listen to an Ad event
|
|
|
|
* @param eventType
|
|
|
|
* @param listenerCb
|
|
|
|
* @returns {null}
|
|
|
|
*/
|
|
|
|
on(eventType, listenerCb) {
|
|
|
|
if (!statics.EventTypes[eventType]) {
|
2018-01-25 18:25:39 +00:00
|
|
|
console.warn(
|
|
|
|
`Invalid event type provided, must be one of: ${Object.keys(
|
|
|
|
statics.EventTypes
|
|
|
|
).join(', ')}`
|
|
|
|
);
|
2017-05-26 16:54:09 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-01-25 18:25:39 +00:00
|
|
|
const sub = SharedEventEmitter.addListener(
|
|
|
|
`interstitial:${this.adUnit}:${eventType}`,
|
|
|
|
listenerCb
|
|
|
|
);
|
2017-07-06 09:21:32 +00:00
|
|
|
subscriptions.push(sub);
|
|
|
|
return sub;
|
2017-05-26 14:18:57 +00:00
|
|
|
}
|
|
|
|
}
|