Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/components/settingsPage.scss
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ wc-page.main-settings-page {
.main-settings-list>.list-item,
.settings-section-card>.list-item {
display: flex;

&[hidden] {
display: none;
}

width: 100%;
min-height: 64px;
margin: 0;
Expand Down
10 changes: 9 additions & 1 deletion src/lib/adConsentCoordinator.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export class AdConsentCoordinator {
#state = EMPTY_PRIVACY_STATE;
#consentPromise;
#adStartPromise;
#privacyOptionsPromise;
#listeners = new Set();

constructor({ privacy, initializeAds, onError = console.error }) {
Expand All @@ -51,7 +52,14 @@ export class AdConsentCoordinator {
return (this.#consentPromise ??= this.#gatherAndStart());
}

async showPrivacyOptions() {
showPrivacyOptions() {
this.#privacyOptionsPromise ??= this.#showPrivacyOptions().finally(() => {
this.#privacyOptionsPromise = undefined;
});
return this.#privacyOptionsPromise;
}

async #showPrivacyOptions() {
const state = await this.#privacy.showOptions();
this.#setState(state);
await this.#startAdsIfAllowed();
Expand Down
99 changes: 97 additions & 2 deletions src/lib/bannerVisibilityController.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ export const BANNER_SUPPRESSION_REASON = Object.freeze({
REWARDED_PASS: "rewarded-pass",
});

const DEFAULT_RETRY_DELAY_MS = 500;
const TRANSIENT_LOAD_ERROR_CODES = new Set([0, 2, 3, 9]);

export class BannerVisibilityController {
#banner = null;
#registeredPages = new WeakSet();
Expand All @@ -20,9 +23,13 @@ export class BannerVisibilityController {
#suppressions = new Set();
#nativeVisible = false;
#scheduledVisible = false;
#desiredVisible = false;
#operation = Promise.resolve();
#revision = 0;
#onError;
#retryTimer = null;
#retryUsed = false;
#stopBannerListeners = [];

constructor({
getActivePage,
Expand All @@ -35,9 +42,13 @@ export class BannerVisibilityController {
}

setBanner(banner) {
this.#stopListeningToBanner();
this.#resetRetry();
this.#revision++;
this.#banner = banner;
this.#nativeVisible = false;
this.#scheduledVisible = false;
this.#listenToBanner(banner);
this.reconcile();
}

Expand Down Expand Up @@ -83,6 +94,12 @@ export class BannerVisibilityController {
activePage !== null &&
this.#registeredPages.has(activePage);
const shouldShow = pageRequestsBanner && !this.#keyboardVisible;
const eligibilityChanged = shouldShow !== this.#desiredVisible;
this.#desiredVisible = shouldShow;

if (!shouldShow || eligibilityChanged) {
this.#resetRetry();
}

if (this.#banner) {
this.#banner.active = pageRequestsBanner;
Expand All @@ -98,7 +115,12 @@ export class BannerVisibilityController {
dispose() {
this.#stopObserving?.();
this.#stopObserving = null;
this.#stopListeningToBanner();
this.#resetRetry();
this.#banner = null;
this.#desiredVisible = false;
this.#nativeVisible = false;
this.#scheduledVisible = false;
this.#revision++;
}

Expand All @@ -107,9 +129,16 @@ export class BannerVisibilityController {
this.#stopObserving = this.#observePageChanges(() => this.reconcile());
}

#queueNativeVisibility(shouldShow) {
#queueNativeVisibility(shouldShow, isRetry = false) {
const banner = this.#banner;
if (!banner || shouldShow === this.#scheduledVisible) return;
if (
!banner ||
shouldShow === this.#scheduledVisible ||
(shouldShow &&
(this.#retryTimer !== null || (this.#retryUsed && !isRetry)))
) {
return;
}

this.#scheduledVisible = shouldShow;
const revision = ++this.#revision;
Expand All @@ -124,14 +153,80 @@ export class BannerVisibilityController {
} else {
await banner.hide?.();
}
if (revision !== this.#revision || banner !== this.#banner) return;
this.#nativeVisible = shouldShow;
} catch (error) {
if (revision !== this.#revision || banner !== this.#banner) return;
this.#nativeVisible = !shouldShow;
this.#scheduledVisible = !shouldShow;
this.#onError(error);
if (shouldShow) this.#scheduleRetry();
}
});
}

#listenToBanner(banner) {
if (typeof banner?.on !== "function") return;

for (const [eventName, listener] of [
["load", () => this.#handleBannerLoad()],
["loadfail", (event) => this.#handleBannerLoadFailure(event)],
]) {
const stopListening = banner.on(eventName, listener);
if (typeof stopListening === "function") {
this.#stopBannerListeners.push(stopListening);
}
}
}

#stopListeningToBanner() {
for (const stopListening of this.#stopBannerListeners.splice(0)) {
stopListening();
}
}

#handleBannerLoad() {
this.#resetRetry();
if (!this.#desiredVisible) return;

this.#nativeVisible = true;
this.#scheduledVisible = true;
}

#handleBannerLoadFailure(event) {
this.#nativeVisible = false;
this.#scheduledVisible = false;
this.#onError(event);

const errorCode = Number(event?.code);
if (TRANSIENT_LOAD_ERROR_CODES.has(errorCode)) {
this.#scheduleRetry();
} else {
this.#retryUsed = true;
}
}

#scheduleRetry() {
if (!this.#desiredVisible || this.#retryTimer !== null || this.#retryUsed) {
return;
}

this.#retryUsed = true;
const banner = this.#banner;
this.#retryTimer = setTimeout(() => {
this.#retryTimer = null;
if (!this.#desiredVisible || banner !== this.#banner) return;
this.#queueNativeVisibility(true, true);
}, DEFAULT_RETRY_DELAY_MS);
}

#resetRetry() {
if (this.#retryTimer !== null) {
clearTimeout(this.#retryTimer);
this.#retryTimer = null;
}
this.#retryUsed = false;
}
}

function getActivePage() {
Expand Down
4 changes: 3 additions & 1 deletion src/lib/startAd.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@ export function subscribePrivacyState(listener) {
}

export async function showPrivacyOptions() {
if (!canUseAdmob()) return getPrivacyState();
if (!canUseAdmob()) {
throw new Error("AdMob Privacy Choices are unavailable.");
}
return getConsentCoordinator().showPrivacyOptions();
}

Expand Down
24 changes: 19 additions & 5 deletions src/plugins/admob/src/android/cordova/Privacy.kt
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,26 @@ internal class Privacy(private val plugin: AdMob) {

fun showOptions(ctx: ExecuteContext) {
plugin.activity.runOnUiThread {
UserMessagingPlatform.showPrivacyOptionsForm(plugin.activity) { formError ->
if (formError != null) {
ctx.reject("UMP ${formError.errorCode}: ${formError.message}")
} else {
ctx.resolve(currentState().toMap())
val activity = plugin.activity
val state = currentState()
if (!state.privacyOptionsRequired) {
return@runOnUiThread ctx.resolve(state.toMap())
}

if (activity.isFinishing || activity.isDestroyed) {
return@runOnUiThread ctx.reject("Privacy Choices are temporarily unavailable")
}

try {
UserMessagingPlatform.showPrivacyOptionsForm(activity) { formError ->
if (formError != null) {
ctx.reject("UMP ${formError.errorCode}: ${formError.message}")
} else {
ctx.resolve(currentState().toMap())
}
}
} catch (error: Exception) {
ctx.reject(error.message ?: "Unable to open Privacy Choices")
}
}
}
Expand Down
11 changes: 10 additions & 1 deletion src/settings/mainSettings.js
Original file line number Diff line number Diff line change
Expand Up @@ -251,10 +251,19 @@ export default function mainSettings() {
break;

case "privacyChoices":
loader.create(
strings["privacy choices"] || "Privacy Choices",
strings["loading..."] || "Loading...",
);
try {
await showPrivacyOptions();
} catch (error) {
helpers.error(error);
console.warn("Unable to open AdMob Privacy Choices:", error);
helpers.error(
"Unable to open Privacy Choices. Check your connection and try again.",
);
} finally {
loader.destroy();
}
break;

Expand Down
21 changes: 11 additions & 10 deletions src/sidebarApps/extensions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -836,11 +836,14 @@ function ListItem({
});

const { default: installPlugin } = await import("lib/installPlugin");
await installPlugin(
id,
remotePlugin.name,
purchaseToken ? purchaseToken : undefined,
);
await Promise.all([
loadAd(),
installPlugin(
id,
remotePlugin.name,
purchaseToken ? purchaseToken : undefined,
),
]);

const searchInput = container.querySelector('input[name="search-ext"]');
if (searchInput) {
Expand All @@ -862,6 +865,7 @@ function ListItem({
if (!$installed.collapsed) {
$installed.ontoggle();
}
await helpers.showInterstitialIfReady();

async function getPurchase(sku) {
const purchases = await helpers.promisify(iap.getPurchases);
Expand Down Expand Up @@ -910,14 +914,11 @@ function ListItem({
return $el;
}

async function loadAd(el) {
async function loadAd() {
if (!helpers.canShowAds()) return;
try {
if (!(await interstitialAd?.isLoaded())) {
const oldText = el.textContent;
el.textContent = strings["loading..."];
await interstitialAd?.load();
el.textContent = oldText;
}
} catch (error) {
console.error(error);
Expand All @@ -929,7 +930,7 @@ async function uninstall(id) {
const pluginDir = Url.join(PLUGIN_DIR, id);
const state = await InstallState.new(id);
await Promise.all([
loadAd(this),
loadAd(),
fsOperation(pluginDir).delete(),
state.delete(state.storeUrl),
]);
Expand Down
Loading