2017-07-04 14:39:56 +00:00
|
|
|
import { NativeModules, 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-05-26 16:54:09 +00:00
|
|
|
import { nativeToJSError } from '../../utils';
|
2017-05-26 14:18:57 +00:00
|
|
|
|
2017-06-07 11:37:32 +00:00
|
|
|
const FirebaseAdMob = NativeModules.RNFirebaseAdMob;
|
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 {
|
|
|
|
|
2017-05-27 16:03:09 +00:00
|
|
|
constructor(admob: Object, adUnit: string) {
|
2017-07-04 14:39:56 +00:00
|
|
|
// Interstitials on iOS require a new instance each time
|
|
|
|
if (Platform.OS === 'ios') {
|
|
|
|
FirebaseAdMob.clearInterstitial(adUnit);
|
|
|
|
}
|
|
|
|
|
2017-07-06 09:21:32 +00:00
|
|
|
for (let i = 0, len = subscriptions.length; i < len; i++) {
|
|
|
|
subscriptions[i].remove();
|
|
|
|
}
|
|
|
|
subscriptions = [];
|
|
|
|
|
2017-05-26 14:18:57 +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-07-06 09:21:32 +00:00
|
|
|
this.admob.removeAllListeners(`interstitial_${adUnit}`);
|
|
|
|
this.admob.on(`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
|
|
|
|
*/
|
2017-07-06 09:21:32 +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-05-26 16:54:09 +00:00
|
|
|
this.admob.emit(eventType, emitData);
|
|
|
|
this.admob.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();
|
|
|
|
}
|
|
|
|
|
|
|
|
return FirebaseAdMob.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) {
|
|
|
|
FirebaseAdMob.interstitialShowAd(this.adUnit);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Listen to an Ad event
|
|
|
|
* @param eventType
|
|
|
|
* @param listenerCb
|
|
|
|
* @returns {null}
|
|
|
|
*/
|
|
|
|
on(eventType, listenerCb) {
|
|
|
|
if (!statics.EventTypes[eventType]) {
|
|
|
|
console.warn(`Invalid event type provided, must be one of: ${Object.keys(statics.EventTypes).join(', ')}`);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2017-07-06 09:21:32 +00:00
|
|
|
const sub = this.admob.on(`interstitial:${this.adUnit}:${eventType}`, listenerCb);
|
|
|
|
subscriptions.push(sub);
|
|
|
|
return sub;
|
2017-05-26 14:18:57 +00:00
|
|
|
}
|
|
|
|
}
|