| 1 | import { invoke } from "@tauri-apps/api/core"; |
| 2 | |
| 3 | export async function isPrivacyOptionsRequired(): Promise<boolean> { |
| 4 | return ( |
| 5 | await invoke<{ isPrivacyOptionsRequired: boolean }>( |
| 6 | "plugin:admob|isPrivacyOptionsRequired", |
| 7 | ) |
| 8 | ).isPrivacyOptionsRequired; |
| 9 | } |
| 10 | |
| 11 | export async function showPrivacyOptionsForm(): Promise<void> { |
| 12 | await invoke("plugin:admob|showPrivacyOptionsForm"); |
| 13 | } |
| 14 | |
| 15 | export class MobileAd<T extends MobileAdOptions = MobileAdOptions> { |
| 16 | private static allAdds: { [s: number]: MobileAd } = {}; |
| 17 | private static idCounter = 0; |
| 18 | |
| 19 | public readonly id: number; |
| 20 | |
| 21 | protected readonly opts: T; |
| 22 | |
| 23 | #created = false; |
| 24 | #init: Promise<void> | null = null; |
| 25 | |
| 26 | constructor(opts: T) { |
| 27 | this.opts = opts; |
| 28 | |
| 29 | this.id = MobileAd.nextId(); |
| 30 | MobileAd.allAdds[this.id] = this; |
| 31 | } |
| 32 | |
| 33 | private static nextId() { |
| 34 | return MobileAd.idCounter++; |
| 35 | } |
| 36 | |
| 37 | public get adUnitId() { |
| 38 | return this.opts.adUnitId; |
| 39 | } |
| 40 | |
| 41 | protected async isLoaded() { |
| 42 | await this.init(); |
| 43 | return await invoke("plugin:admob|adIsLoaded", { |
| 44 | id: this.id, |
| 45 | } as unknown as Record<string, unknown>); |
| 46 | } |
| 47 | |
| 48 | protected async load() { |
| 49 | await this.init(); |
| 50 | await invoke("plugin:admob|adLoad", { |
| 51 | ...this.opts, |
| 52 | id: this.id, |
| 53 | } as unknown as Record<string, unknown>); |
| 54 | } |
| 55 | |
| 56 | protected async show() { |
| 57 | await this.init(); |
| 58 | await invoke("plugin:admob|adShow", { |
| 59 | id: this.id, |
| 60 | }); |
| 61 | } |
| 62 | |
| 63 | protected async hide() { |
| 64 | await this.init(); |
| 65 | await invoke("plugin:admob|adHide", { |
| 66 | id: this.id, |
| 67 | }); |
| 68 | } |
| 69 | |
| 70 | protected async init() { |
| 71 | if (this.#created) return; |
| 72 | |
| 73 | if (this.#init === null) { |
| 74 | const cls = |
| 75 | (this.constructor as unknown as { cls?: string }).cls ?? |
| 76 | this.constructor.name; |
| 77 | |
| 78 | await invoke("plugin:admob|adCreate", { |
| 79 | ...this.opts, |
| 80 | id: this.id, |
| 81 | cls, |
| 82 | } as unknown as Record<string, unknown>); |
| 83 | } |
| 84 | |
| 85 | await this.#init; |
| 86 | this.#created = true; |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | export interface MobileAdOptions { |
| 91 | adUnitId: string; |
| 92 | } |