@nativescript/google-mobile-ads
Version:
NativeScript Google mobile Ads
636 lines • 24.6 kB
JavaScript
import { ImageSource, Utils } from '@nativescript/core';
import { MobileAdsError } from '../error';
import { topViewController, toSerializeManagerRequestOptions, toSerializeRequestOptions } from '../utils';
import { AdEventType } from '../common';
import { NativeAdViewBase, MediaViewBase, mediaContentProperty, stretchProperty, MediaAspectRatio, AdChoicesPlacement, VideoStatus, NativeAdEventType } from './common';
export { AdChoicesPlacement, AdEventType, VideoStatus, MediaAspectRatio, NativeAdEventType };
export class NativeAdView extends NativeAdViewBase {
createNativeView() {
this._native = GADNativeAdView.new();
return this._native;
}
_addChildFromBuilder(name, value) {
this.content = value;
}
get adChoicesView() {
return this._adChoicesView;
}
set adChoicesView(value) {
this._adChoicesView = value;
this._native.adChoicesView = value?.nativeView;
}
get advertiserView() {
return this._advertiserView;
}
set advertiserView(value) {
this._advertiserView = value;
this._native.advertiserView = value?.nativeView;
}
get bodyView() {
return this._bodyView;
}
set bodyView(value) {
this._bodyView = value;
this._native.bodyView = value?.nativeView;
}
get callToActionView() {
return this._callToActionView;
}
set callToActionView(value) {
this._callToActionView = value;
this._native.callToActionView = value?.nativeView;
}
get headlineView() {
return this._headlineView;
}
set headlineView(value) {
this._headlineView = value;
this._native.headlineView = value?.nativeView;
}
get iconView() {
return this._iconView;
}
set iconView(value) {
this._iconView = value;
this._native.iconView = value?.nativeView;
}
get imageView() {
return this._imageView;
}
set imageView(value) {
this.imageView = value;
this._native.imageView = value?.nativeView;
}
get mediaView() {
return this._mediaView;
}
set mediaView(value) {
this._native.mediaView = value?.native;
this._mediaView = value;
}
get nativeAd() {
return this._nativeAd;
}
set nativeAd(value) {
this._native.nativeAd = value?.native;
this._nativeAd = value;
}
get priceView() {
return this._priceView;
}
set priceView(value) {
this._priceView = value;
this._native.priceView = value?.nativeView;
}
get starRatingView() {
return this._starRatingView;
}
set starRatingView(value) {
this._starRatingView = value;
this._native.starRatingView = value?.nativeView;
}
get storeView() {
return this._storeView;
}
set storeView(value) {
this._priceView = value;
this._native.storeView = value?.nativeView;
}
}
export class NativeAdLoader {
constructor(adUnitId, requestOptions = {}, nativeAdOptions = {}) {
this._adUnitId = adUnitId;
this._requestOptions = requestOptions;
this._nativeAdOptions = nativeAdOptions;
this._delegate = GADAdLoaderDelegateImpl.initWithOwner(new WeakRef(this));
}
isLoading() {
return this.native?.loading ?? false;
}
load(arg) {
const options = [];
if (this._nativeAdOptions) {
const managerOptions = this._nativeAdOptions?.adManagerAdViewOptions;
if (managerOptions) {
if (typeof managerOptions.manualImpressionsEnabled === 'boolean') {
const opt = GAMBannerViewOptions.new();
opt.enableManualImpressions = managerOptions.manualImpressionsEnabled;
options.push(opt);
}
}
const nativeAdOptions = this._nativeAdOptions?.nativeAdOptions;
if (nativeAdOptions?.adChoicesPlacement) {
const placement = GADNativeAdViewAdOptions.new();
switch (nativeAdOptions.adChoicesPlacement) {
case AdChoicesPlacement.BOTTOM_LEFT:
placement.preferredAdChoicesPosition = 3 /* GADAdChoicesPosition.BottomLeftCorner */;
break;
case AdChoicesPlacement.BOTTOM_RIGHT:
placement.preferredAdChoicesPosition = 2 /* GADAdChoicesPosition.BottomRightCorner */;
break;
case AdChoicesPlacement.TOP_LEFT:
placement.preferredAdChoicesPosition = 1 /* GADAdChoicesPosition.TopLeftCorner */;
break;
case AdChoicesPlacement.TOP_RIGHT:
placement.preferredAdChoicesPosition = 0 /* GADAdChoicesPosition.TopRightCorner */;
break;
}
options.push(placement);
}
if (typeof nativeAdOptions?.customMuteThisAd === 'boolean') {
const customMuteThisAd = GADNativeMuteThisAdLoaderOptions.new();
customMuteThisAd.customMuteThisAdRequested = nativeAdOptions.customMuteThisAd;
options.push(customMuteThisAd);
}
if (nativeAdOptions?.mediaAspectRatio) {
const mediaAspectRatio = GADNativeAdMediaAdLoaderOptions.new();
switch (nativeAdOptions.mediaAspectRatio) {
case MediaAspectRatio.LANDSCAPE:
mediaAspectRatio.mediaAspectRatio = 2 /* GADMediaAspectRatio.Landscape */;
break;
case MediaAspectRatio.PORTRAIT:
mediaAspectRatio.mediaAspectRatio = 3 /* GADMediaAspectRatio.Portrait */;
break;
case MediaAspectRatio.SQUARE:
mediaAspectRatio.mediaAspectRatio = 4 /* GADMediaAspectRatio.Square */;
break;
case MediaAspectRatio.ANY:
mediaAspectRatio.mediaAspectRatio = 1 /* GADMediaAspectRatio.Any */;
break;
}
options.push(mediaAspectRatio);
}
if (typeof nativeAdOptions?.multipleImages === 'boolean') {
const multipleImagesKey = GADNativeAdImageAdLoaderOptions.new();
multipleImagesKey.shouldRequestMultipleImages = nativeAdOptions.multipleImages;
options.push(multipleImagesKey);
}
// probably handled already
if (typeof nativeAdOptions?.returnUrlsForImageAssets === 'boolean') {
// noop
}
const videoOptions = nativeAdOptions?.videoOptions;
if (videoOptions) {
const videoOpts = GADVideoOptions.new();
if (typeof videoOptions.clickToExpandRequested === 'boolean') {
videoOpts.clickToExpandRequested = videoOptions.clickToExpandRequested;
}
if (typeof videoOptions.customControlsRequested === 'boolean') {
videoOpts.customControlsRequested = videoOptions.customControlsRequested;
}
if (typeof videoOptions.startMuted === 'boolean') {
videoOpts.startMuted = videoOptions.startMuted;
}
options.push(videoOpts);
}
}
if (typeof arg === 'number') {
const numberOfAds = GADMultipleAdsAdLoaderOptions.new();
numberOfAds.numberOfAds = arg;
options.push(numberOfAds);
}
this._native = GADAdLoader.alloc().initWithAdUnitIDRootViewControllerAdTypesOptions(this._adUnitId, topViewController(), [NSCGA.AdLoaderAdTypeToString(2 /* NSCAdLoaderAdType.Native */)], options);
this._native.delegate = this._delegate;
if (arg) {
if (typeof arg === 'number') {
this._native.loadRequest(toSerializeRequestOptions(this._requestOptions));
}
if (typeof arg === 'object') {
this._native.loadRequest(toSerializeManagerRequestOptions(arg));
}
}
else {
this._native.loadRequest(toSerializeRequestOptions(this._requestOptions));
}
}
onAdEvent(listener) {
this._listener = listener;
}
get native() {
return this._native;
}
get ios() {
return this.native;
}
}
export class NativeAd {
static fromNative(nativeAd) {
if (nativeAd instanceof GADNativeAd) {
const ad = new NativeAd();
ad._native = nativeAd;
ad._delegate = GADNativeAdDelegateImpl.initWithOwner(new WeakRef(ad));
nativeAd.delegate = ad._delegate;
return ad;
}
return null;
}
get adUnitId() {
return this._adUnitId;
}
onAdEvent(listener) {
this._listener = listener;
}
destroy() {
// noop
}
get native() {
return this._native;
}
get ios() {
return this.native;
}
get body() {
return this.native?.body;
}
get callToAction() {
return this.native?.callToAction;
}
get headline() {
return this.native?.headline;
}
get price() {
return this.native?.price;
}
get mediaContent() {
return MediaContent.fromNative(this.native?.mediaContent);
}
get customMuteThisAdAvailable() {
return this.native.customMuteThisAdAvailable;
}
isCustomClickGestureEnabled() {
return this.native?.customClickGestureEnabled;
}
enableCustomClickGesture() {
this.native?.enableCustomClickGestures?.();
}
get advertiser() {
return this.native?.advertiser;
}
recordCustomClickGesture() {
this.native?.recordCustomClickGesture?.();
}
setUnconfirmedClickListener(listener) {
this.native.unconfirmedClickDelegate;
if (this._unconfirmedClickListener) {
this.native.unconfirmedClickDelegate = null;
this._unconfirmedClickListener = null;
}
if (listener) {
this._unconfirmedClickDelegate = GADNativeAdUnconfirmedClickDelegateImpl.initWithOwner(new WeakRef(this));
this._unconfirmedClickListener = listener;
if (this.native) {
this.native.unconfirmedClickDelegate = this._unconfirmedClickDelegate;
}
}
}
get icon() {
return NativeAdImage.fromNative(this.native.icon);
}
get store() {
return this.native?.store;
}
get starRating() {
return this.native?.starRating?.floatValue;
}
get muteThisAdReasons() {
const result = [];
const reasons = this.native?.muteThisAdReasons;
if (reasons) {
const size = reasons.count;
for (let i = 0; i < size; i++) {
result.push(MuteThisAdReason.fromNative(reasons.objectAtIndex(i)));
}
}
return result;
}
muteThisAdWithReason(reason) {
if (reason?.native) {
this.native?.muteThisAdWithReason?.(reason.native);
}
}
get images() {
const result = [];
const images = this.native?.images;
if (images) {
const size = images.count;
for (let i = 0; i < size; i++) {
result.push(NativeAdImage.fromNative(images.objectAtIndex(i)));
}
}
return result;
}
}
export class MediaContent {
static fromNative(content) {
if (content instanceof GADMediaContent) {
const mediaContent = new MediaContent();
mediaContent._native = content;
return mediaContent;
}
return null;
}
get aspectRatio() {
return this.native?.aspectRatio;
}
get currentTime() {
return this.native?.currentTime;
}
get duration() {
return this.native?.duration;
}
get mainImage() {
return this.native?.mainImage;
}
set mainImage(value) {
this.native.mainImage = value;
}
get hasVideoContent() {
return this.native?.hasVideoContent;
}
get native() {
return this._native;
}
get ios() {
return this.native;
}
}
export class VideoController {
constructor() {
this._status = VideoStatus.Unstarted;
this._isMute = false;
}
static fromNative(controller) {
if (controller instanceof GADVideoController) {
const ctrl = new VideoController();
ctrl._native = controller;
ctrl._delegate = GADVideoControllerDelegateImpl.initWithOwner(new WeakRef(ctrl));
ctrl._native.delegate = ctrl._delegate;
return ctrl;
}
return null;
}
get status() {
return this._status;
}
clickToExpandEnabled() {
return this.native?.clickToExpandEnabled?.();
}
customControlsEnabled() {
return this.native?.customControlsEnabled?.();
}
pause() {
this.native?.pause?.();
}
play() {
this.native?.play?.();
}
get mute() {
return this._isMute;
}
set mute(value) {
this.native?.setMute?.(value);
}
stop() {
this.native?.stop?.();
}
get native() {
return this._native;
}
get aniosdroid() {
return this.native;
}
}
export class MediaView extends MediaViewBase {
createNativeView() {
this._contentView = GADMediaView.new();
this._contentView.contentMode = 1 /* UIViewContentMode.ScaleAspectFit */;
return this._contentView;
}
onMeasure(widthMeasureSpec, heightMeasureSpec) {
const nativeView = this.nativeView;
if (nativeView) {
const width = Utils.layout.getMeasureSpecSize(widthMeasureSpec);
const height = Utils.layout.getMeasureSpecSize(heightMeasureSpec);
this.setMeasuredDimension(width, height);
}
}
[mediaContentProperty.setNative](content) {
if (this._contentView) {
this._contentView.mediaContent = content?.native || null;
}
}
[stretchProperty.setNative](value) {
if (!this._contentView) {
return;
}
switch (value) {
case 'aspectFit':
this._contentView.contentMode = 1 /* UIViewContentMode.ScaleAspectFit */;
break;
case 'aspectFill':
this._contentView.contentMode = 2 /* UIViewContentMode.ScaleAspectFill */;
break;
case 'fill':
this._contentView.contentMode = 0 /* UIViewContentMode.ScaleToFill */;
break;
case 'none':
default:
this._contentView.contentMode = 9 /* UIViewContentMode.TopLeft */;
break;
}
}
get native() {
return this._contentView;
}
}
export class NativeAdImage {
static fromNative(image) {
if (image instanceof GADNativeAdImage) {
const img = new NativeAdImage();
img._native = image;
return img;
}
return null;
}
get image() {
return new ImageSource(this.native?.image);
}
get url() {
return this.native?.imageURL?.absoluteString;
}
get scale() {
return this.native?.scale;
}
get native() {
return this._native;
}
get ios() {
return this.native;
}
}
export class MuteThisAdReason {
static fromNative(reason) {
if (reason instanceof GADMuteThisAdReason) {
const muteThisAdReason = new MuteThisAdReason();
muteThisAdReason._native = reason;
return muteThisAdReason;
}
return null;
}
get description() {
return this.native?.reasonDescription;
}
get native() {
return this._native;
}
get ios() {
return this.native;
}
}
var GADNativeAdDelegateImpl = /** @class */ (function (_super) {
__extends(GADNativeAdDelegateImpl, _super);
function GADNativeAdDelegateImpl() {
return _super !== null && _super.apply(this, arguments) || this;
}
GADNativeAdDelegateImpl_1 = GADNativeAdDelegateImpl;
GADNativeAdDelegateImpl.initWithOwner = function (owner) {
var delegate = GADNativeAdDelegateImpl_1.new();
delegate._owner = owner;
return delegate;
};
GADNativeAdDelegateImpl.prototype.nativeAdDidDismissScreen = function (nativeAd) {
var _a, _b, _c, _d;
(_d = (_b = (_a = this._owner) === null || _a === void 0 ? void 0 : _a.get) === null || _b === void 0 ? void 0 : (_c = _b.call(_a))._listener) === null || _d === void 0 ? void 0 : _d.call(_c, AdEventType.CLOSED, null, null);
};
GADNativeAdDelegateImpl.prototype.nativeAdDidRecordClick = function (nativeAd) {
var _a, _b, _c, _d;
(_d = (_b = (_a = this._owner) === null || _a === void 0 ? void 0 : _a.get) === null || _b === void 0 ? void 0 : (_c = _b.call(_a))._listener) === null || _d === void 0 ? void 0 : _d.call(_c, AdEventType.CLICKED, null, null);
};
GADNativeAdDelegateImpl.prototype.nativeAdDidRecordImpression = function (nativeAd) {
var _a, _b, _c, _d;
(_d = (_b = (_a = this._owner) === null || _a === void 0 ? void 0 : _a.get) === null || _b === void 0 ? void 0 : (_c = _b.call(_a))._listener) === null || _d === void 0 ? void 0 : _d.call(_c, AdEventType.IMPRESSION, null, null);
};
GADNativeAdDelegateImpl.prototype.nativeAdIsMuted = function (nativeAd) {
// noop
};
GADNativeAdDelegateImpl.prototype.nativeAdWillDismissScreen = function (nativeAd) {
// noop
};
GADNativeAdDelegateImpl.prototype.nativeAdWillPresentScreen = function (nativeAd) {
// noop
};
var GADNativeAdDelegateImpl_1;
GADNativeAdDelegateImpl = GADNativeAdDelegateImpl_1 = __decorate([
ObjCClass(GADNativeAdDelegate)
], GADNativeAdDelegateImpl);
return GADNativeAdDelegateImpl;
}(NSObject));
var GADAdLoaderDelegateImpl = /** @class */ (function (_super) {
__extends(GADAdLoaderDelegateImpl, _super);
function GADAdLoaderDelegateImpl() {
return _super !== null && _super.apply(this, arguments) || this;
}
GADAdLoaderDelegateImpl_1 = GADAdLoaderDelegateImpl;
GADAdLoaderDelegateImpl.initWithOwner = function (owner) {
var delegate = GADAdLoaderDelegateImpl_1.new();
delegate._owner = owner;
return delegate;
};
GADAdLoaderDelegateImpl.prototype.adLoaderDidFailToReceiveAdWithError = function (adLoader, error) {
var _a, _b, _c, _d;
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
(_d = (_b = (_a = this._owner) === null || _a === void 0 ? void 0 : _a.get) === null || _b === void 0 ? void 0 : (_c = _b.call(_a))._listener) === null || _d === void 0 ? void 0 : _d.call(_c, AdEventType.FAILED_TO_LOAD_EVENT, MobileAdsError.fromNative(error), null);
};
GADAdLoaderDelegateImpl.prototype.adLoaderDidFinishLoading = function (adLoader) {
var _a, _b, _c, _d;
(_d = (_b = (_a = this._owner) === null || _a === void 0 ? void 0 : _a.get) === null || _b === void 0 ? void 0 : (_c = _b.call(_a))._listener) === null || _d === void 0 ? void 0 : _d.call(_c, AdEventType.LOADED);
};
GADAdLoaderDelegateImpl.prototype.adLoaderDidReceiveNativeAd = function (adLoader, nativeAd) {
var _a, _b, _c, _d;
(_d = (_b = (_a = this._owner) === null || _a === void 0 ? void 0 : _a.get) === null || _b === void 0 ? void 0 : (_c = _b.call(_a))._listener) === null || _d === void 0 ? void 0 : _d.call(_c, NativeAdEventType.LOADED, null, NativeAd.fromNative(nativeAd));
};
var GADAdLoaderDelegateImpl_1;
GADAdLoaderDelegateImpl = GADAdLoaderDelegateImpl_1 = __decorate([
ObjCClass(GADNativeAdLoaderDelegate)
], GADAdLoaderDelegateImpl);
return GADAdLoaderDelegateImpl;
}(NSObject));
var GADNativeAdUnconfirmedClickDelegateImpl = /** @class */ (function (_super) {
__extends(GADNativeAdUnconfirmedClickDelegateImpl, _super);
function GADNativeAdUnconfirmedClickDelegateImpl() {
return _super !== null && _super.apply(this, arguments) || this;
}
GADNativeAdUnconfirmedClickDelegateImpl_1 = GADNativeAdUnconfirmedClickDelegateImpl;
GADNativeAdUnconfirmedClickDelegateImpl.initWithOwner = function (owner) {
var delegate = GADNativeAdUnconfirmedClickDelegateImpl_1.new();
delegate._owner = owner;
return delegate;
};
GADNativeAdUnconfirmedClickDelegateImpl.prototype.nativeAdDidCancelUnconfirmedClick = function (nativeAd) {
var _a, _b, _c, _d;
(_d = (_c = (_b = (_a = this._owner) === null || _a === void 0 ? void 0 : _a.get) === null || _b === void 0 ? void 0 : _b.call(_a)._unconfirmedClickListener) === null || _c === void 0 ? void 0 : _c.unconfirmedClickCancelled) === null || _d === void 0 ? void 0 : _d.call(_c);
};
GADNativeAdUnconfirmedClickDelegateImpl.prototype.nativeAdDidReceiveUnconfirmedClickOnAssetID = function (nativeAd, assetID) {
var _a, _b, _c, _d;
(_d = (_c = (_b = (_a = this._owner) === null || _a === void 0 ? void 0 : _a.get) === null || _b === void 0 ? void 0 : _b.call(_a)._unconfirmedClickListener) === null || _c === void 0 ? void 0 : _c.unconfirmedClickReceived) === null || _d === void 0 ? void 0 : _d.call(_c, assetID);
};
var GADNativeAdUnconfirmedClickDelegateImpl_1;
GADNativeAdUnconfirmedClickDelegateImpl = GADNativeAdUnconfirmedClickDelegateImpl_1 = __decorate([
ObjCClass(GADNativeAdUnconfirmedClickDelegate)
], GADNativeAdUnconfirmedClickDelegateImpl);
return GADNativeAdUnconfirmedClickDelegateImpl;
}(NSObject));
var GADVideoControllerDelegateImpl = /** @class */ (function (_super) {
__extends(GADVideoControllerDelegateImpl, _super);
function GADVideoControllerDelegateImpl() {
return _super !== null && _super.apply(this, arguments) || this;
}
GADVideoControllerDelegateImpl_1 = GADVideoControllerDelegateImpl;
GADVideoControllerDelegateImpl.initWithOwner = function (owner) {
var delegate = GADVideoControllerDelegateImpl_1.new();
delegate._owner = owner;
return delegate;
};
GADVideoControllerDelegateImpl.prototype.videoControllerDidEndVideoPlayback = function (videoController) {
var _a, _b;
var owner = (_b = (_a = this._owner) === null || _a === void 0 ? void 0 : _a.get) === null || _b === void 0 ? void 0 : _b.call(_a);
if (owner) {
owner._status = VideoStatus.Ended;
}
};
GADVideoControllerDelegateImpl.prototype.videoControllerDidMuteVideo = function (videoController) {
var _a, _b;
var owner = (_b = (_a = this._owner) === null || _a === void 0 ? void 0 : _a.get) === null || _b === void 0 ? void 0 : _b.call(_a);
if (owner) {
owner._isMute = true;
}
};
GADVideoControllerDelegateImpl.prototype.videoControllerDidPauseVideo = function (videoController) {
var _a, _b;
var owner = (_b = (_a = this._owner) === null || _a === void 0 ? void 0 : _a.get) === null || _b === void 0 ? void 0 : _b.call(_a);
if (owner) {
owner._status = VideoStatus.Paused;
}
};
GADVideoControllerDelegateImpl.prototype.videoControllerDidPlayVideo = function (videoController) {
var _a, _b;
var owner = (_b = (_a = this._owner) === null || _a === void 0 ? void 0 : _a.get) === null || _b === void 0 ? void 0 : _b.call(_a);
if (owner) {
owner._status = VideoStatus.Playing;
}
};
GADVideoControllerDelegateImpl.prototype.videoControllerDidUnmuteVideo = function (videoController) {
var _a, _b;
var owner = (_b = (_a = this._owner) === null || _a === void 0 ? void 0 : _a.get) === null || _b === void 0 ? void 0 : _b.call(_a);
if (owner) {
owner._isMute = false;
}
};
var GADVideoControllerDelegateImpl_1;
GADVideoControllerDelegateImpl = GADVideoControllerDelegateImpl_1 = __decorate([
ObjCClass(GADVideoControllerDelegate)
], GADVideoControllerDelegateImpl);
return GADVideoControllerDelegateImpl;
}(NSObject));
//# sourceMappingURL=index.ios.js.map