@theoplayer/react-native-ui
Version:
A React Native UI for @theoplayer/react-native
132 lines (131 loc) • 5.34 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.AdSkipButton = void 0;
var _react = _interopRequireWildcard(require("react"));
var _PlayerContext = require("../util/PlayerContext");
var _reactNative = require("react-native");
var _reactNativeTheoplayer = require("react-native-theoplayer");
var _ArrayUtils = require("../../utils/ArrayUtils");
var _AdUtils = require("../../utils/AdUtils");
var _ActionButton = require("../button/actionbutton/ActionButton");
var _SkipNext = require("../button/svg/SkipNext");
var _jsxRuntime = require("react/jsx-runtime");
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
class AdSkipButton extends _react.PureComponent {
static initialState = {
currentAd: undefined,
timeToSkip: undefined
};
constructor(props) {
super(props);
this.state = AdSkipButton.initialState;
}
componentDidMount() {
const context = this.context;
const player = context.player;
player.addEventListener(_reactNativeTheoplayer.PlayerEventType.AD_EVENT, this.onAdEvent);
if (context.adInProgress) {
void this.update();
player.addEventListener(_reactNativeTheoplayer.PlayerEventType.TIME_UPDATE, this.onTimeUpdateEvent);
}
}
componentWillUnmount() {
const player = this.context.player;
player.removeEventListener(_reactNativeTheoplayer.PlayerEventType.AD_EVENT, this.onAdEvent);
player.removeEventListener(_reactNativeTheoplayer.PlayerEventType.TIME_UPDATE, this.onTimeUpdateEvent);
}
onAdEvent = event => {
const player = this.context.player;
if (event.subType === _reactNativeTheoplayer.AdEventType.AD_BREAK_BEGIN) {
void this.update();
player.addEventListener(_reactNativeTheoplayer.PlayerEventType.TIME_UPDATE, this.onTimeUpdateEvent);
} else if (event.subType === _reactNativeTheoplayer.AdEventType.AD_BREAK_END) {
this.setState(AdSkipButton.initialState);
player.removeEventListener(_reactNativeTheoplayer.PlayerEventType.TIME_UPDATE, this.onTimeUpdateEvent);
}
};
onTimeUpdateEvent = _event => {
void this.update();
};
async update() {
const player = this.context.player;
const currentAds = await player.ads.currentAds();
const linearAd = (0, _ArrayUtils.arrayFind)(currentAds ?? [], _AdUtils.isLinearAd);
const skipOffset = linearAd?.skipOffset;
if (skipOffset === undefined || skipOffset < 0) {
this.setState({
currentAd: linearAd,
timeToSkip: undefined
});
} else {
const timeToSkip = Math.ceil(skipOffset - player.currentTime / 1000);
this.setState({
currentAd: linearAd,
timeToSkip: timeToSkip
});
}
}
onPress = () => {
const player = this.context.player;
player.ads.skip();
};
render() {
const {
currentAd,
timeToSkip
} = this.state;
const {
style,
textStyle,
icon
} = this.props;
if (timeToSkip === undefined || isNaN(timeToSkip) || currentAd && currentAd.integration === 'google-ima') {
return /*#__PURE__*/(0, _jsxRuntime.jsx)(_jsxRuntime.Fragment, {});
}
if (timeToSkip > 0) {
return /*#__PURE__*/(0, _jsxRuntime.jsx)(_PlayerContext.PlayerContext.Consumer, {
children: context => /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.Text, {
style: [context.style.text, {
color: context.style.colors.text
}, style],
children: context.locale.adSkipCounter({
seconds: timeToSkip
})
})
});
}
const skipSvg = icon ?? /*#__PURE__*/(0, _jsxRuntime.jsx)(_SkipNext.SkipNextSvg, {});
return /*#__PURE__*/(0, _jsxRuntime.jsx)(_PlayerContext.PlayerContext.Consumer, {
children: context => /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.View, {
style: [{
flexDirection: 'row',
backgroundColor: context.style.colors.adSkipBackground,
padding: 5
}, style],
children: /*#__PURE__*/(0, _jsxRuntime.jsxs)(_reactNative.TouchableOpacity, {
style: [{
flexDirection: 'row'
}, style],
onPress: () => {
this.onPress();
context.ui.onUserAction_();
},
children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.Text, {
style: [context.style.text, {
color: context.style.colors.text
}, textStyle],
children: context.locale.adSkip
}), /*#__PURE__*/(0, _jsxRuntime.jsx)(_ActionButton.ActionButton, {
touchable: false,
svg: skipSvg
})]
})
})
});
}
}
exports.AdSkipButton = AdSkipButton;
AdSkipButton.contextType = _PlayerContext.PlayerContext;
//# sourceMappingURL=AdSkipButton.js.map