2017-05-27 16:03:40 +00:00
|
|
|
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';
|
2018-01-31 16:05:38 +00:00
|
|
|
import { getNativeModule } from '../../utils/native';
|
2017-05-27 16:03:40 +00:00
|
|
|
import { nativeToJSError } from '../../utils';
|
2018-01-31 16:05:38 +00:00
|
|
|
import type AdMob from './';
|
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 {
|
2018-01-31 16:05:38 +00:00
|
|
|
_admob: AdMob;
|
|
|
|
|
|
|
|
constructor(admob: AdMob, adUnit: string) {
|
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-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}`);
|
2018-01-25 18:25:39 +00:00
|
|
|
SharedEventEmitter.addListener(
|
|
|
|
`rewarded_video_${adUnit}`,
|
|
|
|
this._onRewardedVideoEvent
|
|
|
|
);
|
2017-05-27 16:03:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle a JS emit event
|
|
|
|
* @param event
|
|
|
|
* @private
|
|
|
|
*/
|
2018-01-25 18:25:39 +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();
|
|
|
|
}
|
|
|
|
|
2018-01-31 16:05:38 +00:00
|
|
|
return getNativeModule(this._admob).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) {
|
2018-01-31 16:05:38 +00:00
|
|
|
getNativeModule(this._admob).rewardedVideoShowAd(this.adUnit);
|
2017-05-27 16:03:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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]) {
|
2018-01-25 18:25:39 +00:00
|
|
|
console.warn(
|
|
|
|
`Invalid event type provided, must be one of: ${Object.keys(types).join(
|
|
|
|
', '
|
|
|
|
)}`
|
|
|
|
);
|
2017-05-27 16:03:40 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-01-25 18:25:39 +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
|
|
|
}
|
|
|
|
}
|