2017-05-27 16:03:40 +00:00
|
|
|
import { NativeModules } from 'react-native';
|
|
|
|
import { statics } from './';
|
2017-06-07 11:36:04 +00:00
|
|
|
import AdRequest from './AdRequest';
|
2017-12-22 15:24:31 +00:00
|
|
|
import { SharedEventEmitter } from '../../utils/events';
|
2017-05-27 16:03:40 +00:00
|
|
|
import { nativeToJSError } from '../../utils';
|
|
|
|
|
2017-06-07 13:19:06 +00:00
|
|
|
const FirebaseAdMob = NativeModules.RNFirebaseAdMob;
|
2017-05-27 16:03:40 +00:00
|
|
|
|
2017-07-06 09:21:32 +00:00
|
|
|
let subscriptions = [];
|
|
|
|
|
2017-05-27 16:03:40 +00:00
|
|
|
export default class RewardedVideo {
|
|
|
|
|
2017-07-06 09:21:32 +00:00
|
|
|
constructor(admob: Object, adUnit: string) {
|
|
|
|
for (let i = 0, len = subscriptions.length; i < len; i++) {
|
|
|
|
subscriptions[i].remove();
|
|
|
|
}
|
|
|
|
subscriptions = [];
|
|
|
|
|
2017-05-27 16:03:40 +00:00
|
|
|
this.admob = admob;
|
2017-07-06 09:21:32 +00:00
|
|
|
this.adUnit = adUnit;
|
2017-05-27 16:03:40 +00:00
|
|
|
this.loaded = false;
|
2017-12-22 15:24:31 +00:00
|
|
|
SharedEventEmitter.removeAllListeners(`rewarded_video_${adUnit}`);
|
|
|
|
SharedEventEmitter.addListener(`rewarded_video_${adUnit}`, this._onRewardedVideoEvent);
|
2017-05-27 16:03:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle a JS emit event
|
|
|
|
* @param event
|
|
|
|
* @private
|
|
|
|
*/
|
2017-07-06 09:21:32 +00:00
|
|
|
_onRewardedVideoEvent = (event) => {
|
2017-05-27 16:03:40 +00:00
|
|
|
const eventType = `rewarded_video:${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-12-22 15:24:31 +00:00
|
|
|
SharedEventEmitter.emit(eventType, emitData);
|
|
|
|
SharedEventEmitter.emit(`rewarded_video:${this.adUnit}:*`, emitData);
|
2017-07-06 09:21:32 +00:00
|
|
|
};
|
2017-05-27 16:03:40 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Load an ad with an instance of AdRequest
|
|
|
|
* @param request
|
|
|
|
* @returns {*}
|
|
|
|
*/
|
2017-06-07 11:36:04 +00:00
|
|
|
loadAd(request?: AdRequest) {
|
|
|
|
let adRequest = request;
|
|
|
|
|
|
|
|
if (!adRequest || !Object.keys(adRequest)) {
|
|
|
|
adRequest = new AdRequest().addTestDevice().build();
|
|
|
|
}
|
|
|
|
|
|
|
|
return FirebaseAdMob.rewardedVideoLoadAd(this.adUnit, adRequest);
|
2017-05-27 16:03:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a local instance of isLoaded
|
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
|
|
|
isLoaded() {
|
|
|
|
return this.loaded;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show the advert - will only show if loaded
|
|
|
|
* @returns {*}
|
|
|
|
*/
|
|
|
|
show() {
|
|
|
|
if (this.loaded) {
|
|
|
|
FirebaseAdMob.rewardedVideoShowAd(this.adUnit);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Listen to an Ad event
|
|
|
|
* @param eventType
|
|
|
|
* @param listenerCb
|
|
|
|
* @returns {null}
|
|
|
|
*/
|
|
|
|
on(eventType, listenerCb) {
|
2017-05-27 16:27:22 +00:00
|
|
|
const types = {
|
|
|
|
...statics.EventTypes,
|
|
|
|
...statics.RewardedVideoEventTypes,
|
|
|
|
};
|
|
|
|
|
|
|
|
if (!types[eventType]) {
|
|
|
|
console.warn(`Invalid event type provided, must be one of: ${Object.keys(types).join(', ')}`);
|
2017-05-27 16:03:40 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2017-12-22 15:24:31 +00:00
|
|
|
const sub = SharedEventEmitter.addListener(`rewarded_video:${this.adUnit}:${eventType}`, listenerCb);
|
2017-07-06 09:21:32 +00:00
|
|
|
subscriptions.push(sub);
|
|
|
|
return sub;
|
2017-05-27 16:03:40 +00:00
|
|
|
}
|
|
|
|
}
|