@cond/simasdk 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CondSimasdk.podspec +18 -0
- package/Package.swift +29 -0
- package/README.md +508 -2
- package/android/build.gradle +61 -0
- package/android/src/main/AndroidManifest.xml +2 -0
- package/android/src/main/java/com/simasdk/SimaFirebasePlugin.java +139 -0
- package/android/src/main/java/com/simasdk/SimaHapticsPlugin.java +101 -0
- package/android/src/main/java/com/simasdk/SimaReviewPlugin.java +48 -0
- package/android/src/main/java/com/simasdk/SimaStoragePlugin.java +79 -0
- package/dist/ads.cjs +214 -0
- package/dist/ads.cjs.map +1 -0
- package/dist/ads.d.cts +98 -0
- package/dist/ads.d.ts +98 -0
- package/dist/ads.js +211 -0
- package/dist/ads.js.map +1 -0
- package/dist/back-button.cjs +95 -0
- package/dist/back-button.cjs.map +1 -0
- package/dist/back-button.d.cts +45 -0
- package/dist/back-button.d.ts +45 -0
- package/dist/back-button.js +88 -0
- package/dist/back-button.js.map +1 -0
- package/dist/cli.js +662 -0
- package/dist/firebase.cjs +102 -0
- package/dist/firebase.cjs.map +1 -0
- package/dist/firebase.d.cts +40 -0
- package/dist/firebase.d.ts +40 -0
- package/dist/firebase.js +99 -0
- package/dist/firebase.js.map +1 -0
- package/dist/haptics.cjs +81 -0
- package/dist/haptics.cjs.map +1 -0
- package/dist/haptics.d.cts +51 -0
- package/dist/haptics.d.ts +51 -0
- package/dist/haptics.js +78 -0
- package/dist/haptics.js.map +1 -0
- package/dist/index.cjs +233 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +99 -0
- package/dist/index.d.ts +99 -0
- package/dist/index.js +226 -0
- package/dist/index.js.map +1 -0
- package/dist/react.cjs +118 -0
- package/dist/react.cjs.map +1 -0
- package/dist/react.d.cts +51 -0
- package/dist/react.d.ts +51 -0
- package/dist/react.js +113 -0
- package/dist/react.js.map +1 -0
- package/dist/review.cjs +195 -0
- package/dist/review.cjs.map +1 -0
- package/dist/review.d.cts +76 -0
- package/dist/review.d.ts +76 -0
- package/dist/review.js +192 -0
- package/dist/review.js.map +1 -0
- package/dist/shell.cjs +97 -0
- package/dist/shell.cjs.map +1 -0
- package/dist/shell.d.cts +36 -0
- package/dist/shell.d.ts +36 -0
- package/dist/shell.js +91 -0
- package/dist/shell.js.map +1 -0
- package/dist/storage.cjs +140 -0
- package/dist/storage.cjs.map +1 -0
- package/dist/storage.d.cts +48 -0
- package/dist/storage.d.ts +48 -0
- package/dist/storage.js +136 -0
- package/dist/storage.js.map +1 -0
- package/ios/Sources/SimasdkPlugin/SimaFirebasePlugin.swift +188 -0
- package/ios/Sources/SimasdkPlugin/SimaHapticsPlugin.swift +61 -0
- package/ios/Sources/SimasdkPlugin/SimaReviewPlugin.swift +40 -0
- package/ios/Sources/SimasdkPlugin/SimaStoragePlugin.swift +58 -0
- package/package.json +118 -4
- package/skills/simasdk/SKILL.md +189 -0
package/dist/ads.d.cts
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
interface AdStorage {
|
|
2
|
+
getItem(key: string): string | null | Promise<string | null>;
|
|
3
|
+
setItem(key: string, value: string): void | Promise<void>;
|
|
4
|
+
removeItem?(key: string): void | Promise<void>;
|
|
5
|
+
}
|
|
6
|
+
interface AdDriver {
|
|
7
|
+
isSupported(): boolean;
|
|
8
|
+
showInterstitial(adId: string, options?: {
|
|
9
|
+
isTesting?: boolean;
|
|
10
|
+
}): Promise<void>;
|
|
11
|
+
showRewarded?(adId: string, options?: {
|
|
12
|
+
isTesting?: boolean;
|
|
13
|
+
}): Promise<{
|
|
14
|
+
rewarded: boolean;
|
|
15
|
+
}>;
|
|
16
|
+
}
|
|
17
|
+
interface AdPolicyConfig {
|
|
18
|
+
/** Unique key isolating persisted frequency/cooldown state. */
|
|
19
|
+
key: string;
|
|
20
|
+
/** Platform-specific interstitial ad unit IDs (or a single string). */
|
|
21
|
+
interstitialAdId: string | {
|
|
22
|
+
ios?: string;
|
|
23
|
+
android?: string;
|
|
24
|
+
};
|
|
25
|
+
/** Optional rewarded ad unit IDs. */
|
|
26
|
+
rewardedAdId?: string | {
|
|
27
|
+
ios?: string;
|
|
28
|
+
android?: string;
|
|
29
|
+
};
|
|
30
|
+
/** Minimum actions completed before the very first interstitial is eligible (default: `2`). */
|
|
31
|
+
minActionsBeforeFirst?: number;
|
|
32
|
+
/** Number of actions required between subsequent interstitials (default: `3`). */
|
|
33
|
+
actionsBetweenAds?: number;
|
|
34
|
+
/** Minimum seconds between two interstitials (default: `90`). */
|
|
35
|
+
cooldownSeconds?: number;
|
|
36
|
+
/** When `true`, requests test ads from the driver. */
|
|
37
|
+
isTesting?: boolean;
|
|
38
|
+
/** Disable all interstitials (e.g. for pro/ad-free users). */
|
|
39
|
+
disabled?: () => boolean;
|
|
40
|
+
/** Optional custom storage backend. */
|
|
41
|
+
storage?: AdStorage;
|
|
42
|
+
/** Optional custom AdMob driver (defaults to lazy `@capacitor-community/admob` bridge). */
|
|
43
|
+
driver?: AdDriver;
|
|
44
|
+
/** Optional clock for deterministic testing. */
|
|
45
|
+
now?: () => number;
|
|
46
|
+
/** Optional platform override for testing (`'ios' | 'android' | 'web'`). */
|
|
47
|
+
platform?: () => 'ios' | 'android' | 'web';
|
|
48
|
+
}
|
|
49
|
+
interface AdPolicyState {
|
|
50
|
+
totalActions: number;
|
|
51
|
+
actionsSinceLastAd: number;
|
|
52
|
+
lastShownAt: number | null;
|
|
53
|
+
}
|
|
54
|
+
type InterstitialResult = {
|
|
55
|
+
status: 'disabled';
|
|
56
|
+
state: AdPolicyState;
|
|
57
|
+
} | {
|
|
58
|
+
status: 'not_eligible';
|
|
59
|
+
state: AdPolicyState;
|
|
60
|
+
} | {
|
|
61
|
+
status: 'cooldown';
|
|
62
|
+
state: AdPolicyState;
|
|
63
|
+
nextEligibleAt: number;
|
|
64
|
+
} | {
|
|
65
|
+
status: 'unsupported';
|
|
66
|
+
state: AdPolicyState;
|
|
67
|
+
} | {
|
|
68
|
+
status: 'shown';
|
|
69
|
+
state: AdPolicyState;
|
|
70
|
+
} | {
|
|
71
|
+
status: 'failed';
|
|
72
|
+
state: AdPolicyState;
|
|
73
|
+
error: Error;
|
|
74
|
+
};
|
|
75
|
+
type RewardedResult = {
|
|
76
|
+
status: 'rewarded';
|
|
77
|
+
} | {
|
|
78
|
+
status: 'dismissed';
|
|
79
|
+
} | {
|
|
80
|
+
status: 'unsupported';
|
|
81
|
+
} | {
|
|
82
|
+
status: 'failed';
|
|
83
|
+
error: Error;
|
|
84
|
+
};
|
|
85
|
+
interface AdPolicy {
|
|
86
|
+
recordActionAndMaybeShow(): Promise<InterstitialResult>;
|
|
87
|
+
showRewarded(): Promise<RewardedResult>;
|
|
88
|
+
getState(): Promise<AdPolicyState>;
|
|
89
|
+
reset(): Promise<void>;
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Lazy driver for `@capacitor-community/admob` when installed in the host app.
|
|
93
|
+
* `simasdk` itself never bundles or links AdMob native SDKs.
|
|
94
|
+
*/
|
|
95
|
+
declare function capacitorAdMobDriver(): AdDriver;
|
|
96
|
+
declare function createAdPolicy(config: AdPolicyConfig): AdPolicy;
|
|
97
|
+
|
|
98
|
+
export { type AdDriver, type AdPolicy, type AdPolicyConfig, type AdPolicyState, type AdStorage, type InterstitialResult, type RewardedResult, capacitorAdMobDriver, createAdPolicy };
|
package/dist/ads.d.ts
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
interface AdStorage {
|
|
2
|
+
getItem(key: string): string | null | Promise<string | null>;
|
|
3
|
+
setItem(key: string, value: string): void | Promise<void>;
|
|
4
|
+
removeItem?(key: string): void | Promise<void>;
|
|
5
|
+
}
|
|
6
|
+
interface AdDriver {
|
|
7
|
+
isSupported(): boolean;
|
|
8
|
+
showInterstitial(adId: string, options?: {
|
|
9
|
+
isTesting?: boolean;
|
|
10
|
+
}): Promise<void>;
|
|
11
|
+
showRewarded?(adId: string, options?: {
|
|
12
|
+
isTesting?: boolean;
|
|
13
|
+
}): Promise<{
|
|
14
|
+
rewarded: boolean;
|
|
15
|
+
}>;
|
|
16
|
+
}
|
|
17
|
+
interface AdPolicyConfig {
|
|
18
|
+
/** Unique key isolating persisted frequency/cooldown state. */
|
|
19
|
+
key: string;
|
|
20
|
+
/** Platform-specific interstitial ad unit IDs (or a single string). */
|
|
21
|
+
interstitialAdId: string | {
|
|
22
|
+
ios?: string;
|
|
23
|
+
android?: string;
|
|
24
|
+
};
|
|
25
|
+
/** Optional rewarded ad unit IDs. */
|
|
26
|
+
rewardedAdId?: string | {
|
|
27
|
+
ios?: string;
|
|
28
|
+
android?: string;
|
|
29
|
+
};
|
|
30
|
+
/** Minimum actions completed before the very first interstitial is eligible (default: `2`). */
|
|
31
|
+
minActionsBeforeFirst?: number;
|
|
32
|
+
/** Number of actions required between subsequent interstitials (default: `3`). */
|
|
33
|
+
actionsBetweenAds?: number;
|
|
34
|
+
/** Minimum seconds between two interstitials (default: `90`). */
|
|
35
|
+
cooldownSeconds?: number;
|
|
36
|
+
/** When `true`, requests test ads from the driver. */
|
|
37
|
+
isTesting?: boolean;
|
|
38
|
+
/** Disable all interstitials (e.g. for pro/ad-free users). */
|
|
39
|
+
disabled?: () => boolean;
|
|
40
|
+
/** Optional custom storage backend. */
|
|
41
|
+
storage?: AdStorage;
|
|
42
|
+
/** Optional custom AdMob driver (defaults to lazy `@capacitor-community/admob` bridge). */
|
|
43
|
+
driver?: AdDriver;
|
|
44
|
+
/** Optional clock for deterministic testing. */
|
|
45
|
+
now?: () => number;
|
|
46
|
+
/** Optional platform override for testing (`'ios' | 'android' | 'web'`). */
|
|
47
|
+
platform?: () => 'ios' | 'android' | 'web';
|
|
48
|
+
}
|
|
49
|
+
interface AdPolicyState {
|
|
50
|
+
totalActions: number;
|
|
51
|
+
actionsSinceLastAd: number;
|
|
52
|
+
lastShownAt: number | null;
|
|
53
|
+
}
|
|
54
|
+
type InterstitialResult = {
|
|
55
|
+
status: 'disabled';
|
|
56
|
+
state: AdPolicyState;
|
|
57
|
+
} | {
|
|
58
|
+
status: 'not_eligible';
|
|
59
|
+
state: AdPolicyState;
|
|
60
|
+
} | {
|
|
61
|
+
status: 'cooldown';
|
|
62
|
+
state: AdPolicyState;
|
|
63
|
+
nextEligibleAt: number;
|
|
64
|
+
} | {
|
|
65
|
+
status: 'unsupported';
|
|
66
|
+
state: AdPolicyState;
|
|
67
|
+
} | {
|
|
68
|
+
status: 'shown';
|
|
69
|
+
state: AdPolicyState;
|
|
70
|
+
} | {
|
|
71
|
+
status: 'failed';
|
|
72
|
+
state: AdPolicyState;
|
|
73
|
+
error: Error;
|
|
74
|
+
};
|
|
75
|
+
type RewardedResult = {
|
|
76
|
+
status: 'rewarded';
|
|
77
|
+
} | {
|
|
78
|
+
status: 'dismissed';
|
|
79
|
+
} | {
|
|
80
|
+
status: 'unsupported';
|
|
81
|
+
} | {
|
|
82
|
+
status: 'failed';
|
|
83
|
+
error: Error;
|
|
84
|
+
};
|
|
85
|
+
interface AdPolicy {
|
|
86
|
+
recordActionAndMaybeShow(): Promise<InterstitialResult>;
|
|
87
|
+
showRewarded(): Promise<RewardedResult>;
|
|
88
|
+
getState(): Promise<AdPolicyState>;
|
|
89
|
+
reset(): Promise<void>;
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Lazy driver for `@capacitor-community/admob` when installed in the host app.
|
|
93
|
+
* `simasdk` itself never bundles or links AdMob native SDKs.
|
|
94
|
+
*/
|
|
95
|
+
declare function capacitorAdMobDriver(): AdDriver;
|
|
96
|
+
declare function createAdPolicy(config: AdPolicyConfig): AdPolicy;
|
|
97
|
+
|
|
98
|
+
export { type AdDriver, type AdPolicy, type AdPolicyConfig, type AdPolicyState, type AdStorage, type InterstitialResult, type RewardedResult, capacitorAdMobDriver, createAdPolicy };
|
package/dist/ads.js
ADDED
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
import { registerPlugin, Capacitor } from '@capacitor/core';
|
|
2
|
+
|
|
3
|
+
// src/ads.ts
|
|
4
|
+
var CapacitorAdMob = registerPlugin("AdMob");
|
|
5
|
+
function capacitorAdMobDriver() {
|
|
6
|
+
return {
|
|
7
|
+
isSupported() {
|
|
8
|
+
return typeof window !== "undefined" && Capacitor.isNativePlatform() && Capacitor.isPluginAvailable("AdMob");
|
|
9
|
+
},
|
|
10
|
+
async showInterstitial(adId, options) {
|
|
11
|
+
await CapacitorAdMob.prepareInterstitial({ adId, isTesting: options?.isTesting });
|
|
12
|
+
await CapacitorAdMob.showInterstitial();
|
|
13
|
+
},
|
|
14
|
+
async showRewarded(adId, options) {
|
|
15
|
+
await CapacitorAdMob.prepareRewardVideoAd({ adId, isTesting: options?.isTesting });
|
|
16
|
+
const reward = await CapacitorAdMob.showRewardVideoAd();
|
|
17
|
+
return { rewarded: reward !== void 0 && reward !== null };
|
|
18
|
+
}
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
var STORAGE_PREFIX = "simasdk:ads:v1:";
|
|
22
|
+
var memoryStore = /* @__PURE__ */ new Map();
|
|
23
|
+
var defaultAdStorage = {
|
|
24
|
+
getItem(key) {
|
|
25
|
+
if (typeof window !== "undefined" && typeof window.localStorage !== "undefined") {
|
|
26
|
+
try {
|
|
27
|
+
return window.localStorage.getItem(key);
|
|
28
|
+
} catch {
|
|
29
|
+
return memoryStore.get(key) ?? null;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
return memoryStore.get(key) ?? null;
|
|
33
|
+
},
|
|
34
|
+
setItem(key, value) {
|
|
35
|
+
if (typeof window !== "undefined" && typeof window.localStorage !== "undefined") {
|
|
36
|
+
try {
|
|
37
|
+
window.localStorage.setItem(key, value);
|
|
38
|
+
return;
|
|
39
|
+
} catch {
|
|
40
|
+
memoryStore.set(key, value);
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
memoryStore.set(key, value);
|
|
45
|
+
},
|
|
46
|
+
removeItem(key) {
|
|
47
|
+
if (typeof window !== "undefined" && typeof window.localStorage !== "undefined") {
|
|
48
|
+
try {
|
|
49
|
+
window.localStorage.removeItem(key);
|
|
50
|
+
return;
|
|
51
|
+
} catch {
|
|
52
|
+
memoryStore.delete(key);
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
memoryStore.delete(key);
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
var adLocks = /* @__PURE__ */ new WeakMap();
|
|
60
|
+
function withAdLock(storage, storageKey, fn) {
|
|
61
|
+
let map = adLocks.get(storage);
|
|
62
|
+
if (!map) {
|
|
63
|
+
map = /* @__PURE__ */ new Map();
|
|
64
|
+
adLocks.set(storage, map);
|
|
65
|
+
}
|
|
66
|
+
const previous = map.get(storageKey) ?? Promise.resolve();
|
|
67
|
+
let release;
|
|
68
|
+
const current = new Promise((resolve) => {
|
|
69
|
+
release = resolve;
|
|
70
|
+
});
|
|
71
|
+
const chained = previous.then(() => current);
|
|
72
|
+
map.set(storageKey, chained);
|
|
73
|
+
return previous.then(() => fn()).finally(() => {
|
|
74
|
+
release();
|
|
75
|
+
if (map?.get(storageKey) === chained) {
|
|
76
|
+
map.delete(storageKey);
|
|
77
|
+
}
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
function parseAdState(raw) {
|
|
81
|
+
if (!raw) {
|
|
82
|
+
return { totalActions: 0, actionsSinceLastAd: 0, lastShownAt: null };
|
|
83
|
+
}
|
|
84
|
+
try {
|
|
85
|
+
const parsed = JSON.parse(raw);
|
|
86
|
+
return {
|
|
87
|
+
totalActions: typeof parsed.totalActions === "number" && parsed.totalActions >= 0 ? parsed.totalActions : 0,
|
|
88
|
+
actionsSinceLastAd: typeof parsed.actionsSinceLastAd === "number" && parsed.actionsSinceLastAd >= 0 ? parsed.actionsSinceLastAd : 0,
|
|
89
|
+
lastShownAt: typeof parsed.lastShownAt === "number" && Number.isFinite(parsed.lastShownAt) ? parsed.lastShownAt : null
|
|
90
|
+
};
|
|
91
|
+
} catch {
|
|
92
|
+
return { totalActions: 0, actionsSinceLastAd: 0, lastShownAt: null };
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
function resolveAdId(idConfig, platform) {
|
|
96
|
+
if (!idConfig) {
|
|
97
|
+
return null;
|
|
98
|
+
}
|
|
99
|
+
if (typeof idConfig === "string") {
|
|
100
|
+
return idConfig;
|
|
101
|
+
}
|
|
102
|
+
if (platform === "ios") {
|
|
103
|
+
return idConfig.ios ?? null;
|
|
104
|
+
}
|
|
105
|
+
if (platform === "android") {
|
|
106
|
+
return idConfig.android ?? null;
|
|
107
|
+
}
|
|
108
|
+
return null;
|
|
109
|
+
}
|
|
110
|
+
function defaultPlatform() {
|
|
111
|
+
if (typeof window === "undefined") {
|
|
112
|
+
return "web";
|
|
113
|
+
}
|
|
114
|
+
const p = Capacitor.getPlatform();
|
|
115
|
+
if (p === "ios" || p === "android") {
|
|
116
|
+
return p;
|
|
117
|
+
}
|
|
118
|
+
return "web";
|
|
119
|
+
}
|
|
120
|
+
function createAdPolicy(config) {
|
|
121
|
+
const minActionsBeforeFirst = config.minActionsBeforeFirst ?? 2;
|
|
122
|
+
const actionsBetweenAds = config.actionsBetweenAds ?? 3;
|
|
123
|
+
const cooldownMs = (config.cooldownSeconds ?? 90) * 1e3;
|
|
124
|
+
const storage = config.storage ?? defaultAdStorage;
|
|
125
|
+
const driver = config.driver ?? capacitorAdMobDriver();
|
|
126
|
+
const now = config.now ?? (() => Date.now());
|
|
127
|
+
const getPlatform = config.platform ?? defaultPlatform;
|
|
128
|
+
const storageKey = `${STORAGE_PREFIX}${config.key}`;
|
|
129
|
+
async function readState() {
|
|
130
|
+
return parseAdState(await storage.getItem(storageKey));
|
|
131
|
+
}
|
|
132
|
+
async function writeState(state) {
|
|
133
|
+
await storage.setItem(storageKey, JSON.stringify(state));
|
|
134
|
+
}
|
|
135
|
+
return {
|
|
136
|
+
async recordActionAndMaybeShow() {
|
|
137
|
+
return withAdLock(storage, storageKey, async () => {
|
|
138
|
+
const prev = await readState();
|
|
139
|
+
if (config.disabled?.()) {
|
|
140
|
+
return { status: "disabled", state: prev };
|
|
141
|
+
}
|
|
142
|
+
const nextState = {
|
|
143
|
+
totalActions: prev.totalActions + 1,
|
|
144
|
+
actionsSinceLastAd: prev.actionsSinceLastAd + 1,
|
|
145
|
+
lastShownAt: prev.lastShownAt
|
|
146
|
+
};
|
|
147
|
+
await writeState(nextState);
|
|
148
|
+
const requiredActions = prev.lastShownAt === null ? minActionsBeforeFirst : actionsBetweenAds;
|
|
149
|
+
if (nextState.actionsSinceLastAd < requiredActions) {
|
|
150
|
+
return { status: "not_eligible", state: nextState };
|
|
151
|
+
}
|
|
152
|
+
const currentTime = now();
|
|
153
|
+
if (nextState.lastShownAt !== null) {
|
|
154
|
+
const nextEligibleAt = nextState.lastShownAt + cooldownMs;
|
|
155
|
+
if (currentTime < nextEligibleAt) {
|
|
156
|
+
return { status: "cooldown", state: nextState, nextEligibleAt };
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
const adId = resolveAdId(config.interstitialAdId, getPlatform());
|
|
160
|
+
if (!adId || !driver.isSupported()) {
|
|
161
|
+
return { status: "unsupported", state: nextState };
|
|
162
|
+
}
|
|
163
|
+
try {
|
|
164
|
+
await driver.showInterstitial(adId, { isTesting: config.isTesting });
|
|
165
|
+
const shownState = {
|
|
166
|
+
totalActions: nextState.totalActions,
|
|
167
|
+
actionsSinceLastAd: 0,
|
|
168
|
+
lastShownAt: now()
|
|
169
|
+
};
|
|
170
|
+
await writeState(shownState);
|
|
171
|
+
return { status: "shown", state: shownState };
|
|
172
|
+
} catch (err) {
|
|
173
|
+
return {
|
|
174
|
+
status: "failed",
|
|
175
|
+
state: nextState,
|
|
176
|
+
error: err instanceof Error ? err : new Error(String(err))
|
|
177
|
+
};
|
|
178
|
+
}
|
|
179
|
+
});
|
|
180
|
+
},
|
|
181
|
+
async showRewarded() {
|
|
182
|
+
const adId = resolveAdId(config.rewardedAdId, getPlatform());
|
|
183
|
+
if (!adId || !driver.isSupported() || !driver.showRewarded) {
|
|
184
|
+
return { status: "unsupported" };
|
|
185
|
+
}
|
|
186
|
+
try {
|
|
187
|
+
const res = await driver.showRewarded(adId, { isTesting: config.isTesting });
|
|
188
|
+
return { status: res.rewarded ? "rewarded" : "dismissed" };
|
|
189
|
+
} catch (err) {
|
|
190
|
+
return {
|
|
191
|
+
status: "failed",
|
|
192
|
+
error: err instanceof Error ? err : new Error(String(err))
|
|
193
|
+
};
|
|
194
|
+
}
|
|
195
|
+
},
|
|
196
|
+
getState: () => withAdLock(storage, storageKey, readState),
|
|
197
|
+
async reset() {
|
|
198
|
+
return withAdLock(storage, storageKey, async () => {
|
|
199
|
+
if (storage.removeItem) {
|
|
200
|
+
await storage.removeItem(storageKey);
|
|
201
|
+
} else {
|
|
202
|
+
await writeState({ totalActions: 0, actionsSinceLastAd: 0, lastShownAt: null });
|
|
203
|
+
}
|
|
204
|
+
});
|
|
205
|
+
}
|
|
206
|
+
};
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
export { capacitorAdMobDriver, createAdPolicy };
|
|
210
|
+
//# sourceMappingURL=ads.js.map
|
|
211
|
+
//# sourceMappingURL=ads.js.map
|
package/dist/ads.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/ads.ts"],"names":[],"mappings":";;;AA2EA,IAAM,cAAA,GAAiB,eAAmC,OAAO,CAAA;AAM1D,SAAS,oBAAA,GAAiC;AAC/C,EAAA,OAAO;AAAA,IACL,WAAA,GAAuB;AACrB,MAAA,OACE,OAAO,WAAW,WAAA,IAClB,SAAA,CAAU,kBAAiB,IAC3B,SAAA,CAAU,kBAAkB,OAAO,CAAA;AAAA,IAEvC,CAAA;AAAA,IACA,MAAM,gBAAA,CAAiB,IAAA,EAAc,OAAA,EAAkD;AACrF,MAAA,MAAM,eAAe,mBAAA,CAAoB,EAAE,MAAM,SAAA,EAAW,OAAA,EAAS,WAAW,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AAM,IAAI,KAAA,CAAM,MAAA,CAAO,GAAG,CAAC;AAAA,SAC3D;AAAA,MACF;AAAA,IACF,CAAA;AAAA,IAEA,QAAA,EAAU,MAAM,UAAA,CAAW,OAAA,EAAS,YAAY,SAAS,CAAA;AAAA,IAEzD,MAAM,KAAA,GAAuB;AAC3B,MAAA,OAAO,UAAA,CAAW,OAAA,EAAS,UAAA,EAAY,YAAY;AACjD,QAAA,IAAI,QAAQ,UAAA,EAAY;AACtB,UAAA,MAAM,OAAA,CAAQ,WAAW,UAAU,CAAA;AAAA,QACrC,CAAA,MAAO;AACL,UAAA,MAAM,UAAA,CAAW,EAAE,YAAA,EAAc,CAAA,EAAG,oBAAoB,CAAA,EAAG,WAAA,EAAa,MAAM,CAAA;AAAA,QAChF;AAAA,MACF,CAAC,CAAA;AAAA,IACH;AAAA,GACF;AACF","file":"ads.js","sourcesContent":["import { Capacitor, registerPlugin } from '@capacitor/core';\n\nexport interface AdStorage {\n getItem(key: string): string | null | Promise<string | null>;\n setItem(key: string, value: string): void | Promise<void>;\n removeItem?(key: string): void | Promise<void>;\n}\n\nexport interface AdDriver {\n isSupported(): boolean;\n showInterstitial(adId: string, options?: { isTesting?: boolean }): Promise<void>;\n showRewarded?(adId: string, options?: { isTesting?: boolean }): Promise<{ rewarded: boolean }>;\n}\n\nexport interface AdPolicyConfig {\n /** Unique key isolating persisted frequency/cooldown state. */\n key: string;\n /** Platform-specific interstitial ad unit IDs (or a single string). */\n interstitialAdId: string | { ios?: string; android?: string };\n /** Optional rewarded ad unit IDs. */\n rewardedAdId?: string | { ios?: string; android?: string };\n /** Minimum actions completed before the very first interstitial is eligible (default: `2`). */\n minActionsBeforeFirst?: number;\n /** Number of actions required between subsequent interstitials (default: `3`). */\n actionsBetweenAds?: number;\n /** Minimum seconds between two interstitials (default: `90`). */\n cooldownSeconds?: number;\n /** When `true`, requests test ads from the driver. */\n isTesting?: boolean;\n /** Disable all interstitials (e.g. for pro/ad-free users). */\n disabled?: () => boolean;\n /** Optional custom storage backend. */\n storage?: AdStorage;\n /** Optional custom AdMob driver (defaults to lazy `@capacitor-community/admob` bridge). */\n driver?: AdDriver;\n /** Optional clock for deterministic testing. */\n now?: () => number;\n /** Optional platform override for testing (`'ios' | 'android' | 'web'`). */\n platform?: () => 'ios' | 'android' | 'web';\n}\n\nexport interface AdPolicyState {\n totalActions: number;\n actionsSinceLastAd: number;\n lastShownAt: number | null;\n}\n\nexport type InterstitialResult =\n | { status: 'disabled'; state: AdPolicyState }\n | { status: 'not_eligible'; state: AdPolicyState }\n | { status: 'cooldown'; state: AdPolicyState; nextEligibleAt: number }\n | { status: 'unsupported'; state: AdPolicyState }\n | { status: 'shown'; state: AdPolicyState }\n | { status: 'failed'; state: AdPolicyState; error: Error };\n\nexport type RewardedResult =\n | { status: 'rewarded' }\n | { status: 'dismissed' }\n | { status: 'unsupported' }\n | { status: 'failed'; error: Error };\n\nexport interface AdPolicy {\n recordActionAndMaybeShow(): Promise<InterstitialResult>;\n showRewarded(): Promise<RewardedResult>;\n getState(): Promise<AdPolicyState>;\n reset(): Promise<void>;\n}\n\ninterface CapacitorAdMobLike {\n prepareInterstitial(options: { adId: string; isTesting?: boolean }): Promise<void>;\n showInterstitial(): Promise<void>;\n prepareRewardVideoAd(options: { adId: string; isTesting?: boolean }): Promise<void>;\n showRewardVideoAd(): Promise<{ type?: string; amount?: number } | void>;\n}\n\nconst CapacitorAdMob = registerPlugin<CapacitorAdMobLike>('AdMob');\n\n/**\n * Lazy driver for `@capacitor-community/admob` when installed in the host app.\n * `simasdk` itself never bundles or links AdMob native SDKs.\n */\nexport function capacitorAdMobDriver(): AdDriver {\n return {\n isSupported(): boolean {\n return (\n typeof window !== 'undefined' &&\n Capacitor.isNativePlatform() &&\n Capacitor.isPluginAvailable('AdMob')\n );\n },\n async showInterstitial(adId: string, options?: { isTesting?: boolean }): Promise<void> {\n await CapacitorAdMob.prepareInterstitial({ adId, isTesting: options?.isTesting });\n await CapacitorAdMob.showInterstitial();\n },\n async showRewarded(\n adId: string,\n options?: { isTesting?: boolean },\n ): Promise<{ rewarded: boolean }> {\n await CapacitorAdMob.prepareRewardVideoAd({ adId, isTesting: options?.isTesting });\n const reward = await CapacitorAdMob.showRewardVideoAd();\n return { rewarded: reward !== undefined && reward !== null };\n },\n };\n}\n\nconst STORAGE_PREFIX = 'simasdk:ads:v1:';\nconst memoryStore = new Map<string, string>();\n\nconst defaultAdStorage: AdStorage = {\n getItem(key: string): string | null {\n if (typeof window !== 'undefined' && typeof window.localStorage !== 'undefined') {\n try {\n return window.localStorage.getItem(key);\n } catch {\n return memoryStore.get(key) ?? null;\n }\n }\n return memoryStore.get(key) ?? null;\n },\n setItem(key: string, value: string): void {\n if (typeof window !== 'undefined' && typeof window.localStorage !== 'undefined') {\n try {\n window.localStorage.setItem(key, value);\n return;\n } catch {\n memoryStore.set(key, value);\n return;\n }\n }\n memoryStore.set(key, value);\n },\n removeItem(key: string): void {\n if (typeof window !== 'undefined' && typeof window.localStorage !== 'undefined') {\n try {\n window.localStorage.removeItem(key);\n return;\n } catch {\n memoryStore.delete(key);\n return;\n }\n }\n memoryStore.delete(key);\n },\n};\n\nconst adLocks = new WeakMap<AdStorage, Map<string, Promise<void>>>();\n\nfunction withAdLock<T>(storage: AdStorage, storageKey: string, fn: () => Promise<T>): Promise<T> {\n let map = adLocks.get(storage);\n if (!map) {\n map = new Map();\n adLocks.set(storage, map);\n }\n const previous = map.get(storageKey) ?? Promise.resolve();\n let release!: () => void;\n const current = new Promise<void>((resolve) => {\n release = resolve;\n });\n const chained = previous.then(() => current);\n map.set(storageKey, chained);\n\n return previous\n .then(() => fn())\n .finally(() => {\n release();\n if (map?.get(storageKey) === chained) {\n map.delete(storageKey);\n }\n });\n}\n\nfunction parseAdState(raw: string | null): AdPolicyState {\n if (!raw) {\n return { totalActions: 0, actionsSinceLastAd: 0, lastShownAt: null };\n }\n try {\n const parsed = JSON.parse(raw) as Partial<AdPolicyState>;\n return {\n totalActions:\n typeof parsed.totalActions === 'number' && parsed.totalActions >= 0\n ? parsed.totalActions\n : 0,\n actionsSinceLastAd:\n typeof parsed.actionsSinceLastAd === 'number' && parsed.actionsSinceLastAd >= 0\n ? parsed.actionsSinceLastAd\n : 0,\n lastShownAt:\n typeof parsed.lastShownAt === 'number' && Number.isFinite(parsed.lastShownAt)\n ? parsed.lastShownAt\n : null,\n };\n } catch {\n return { totalActions: 0, actionsSinceLastAd: 0, lastShownAt: null };\n }\n}\n\nfunction resolveAdId(\n idConfig: string | { ios?: string; android?: string } | undefined,\n platform: 'ios' | 'android' | 'web',\n): string | null {\n if (!idConfig) {\n return null;\n }\n if (typeof idConfig === 'string') {\n return idConfig;\n }\n if (platform === 'ios') {\n return idConfig.ios ?? null;\n }\n if (platform === 'android') {\n return idConfig.android ?? null;\n }\n return null;\n}\n\nfunction defaultPlatform(): 'ios' | 'android' | 'web' {\n if (typeof window === 'undefined') {\n return 'web';\n }\n const p = Capacitor.getPlatform();\n if (p === 'ios' || p === 'android') {\n return p;\n }\n return 'web';\n}\n\nexport function createAdPolicy(config: AdPolicyConfig): AdPolicy {\n const minActionsBeforeFirst = config.minActionsBeforeFirst ?? 2;\n const actionsBetweenAds = config.actionsBetweenAds ?? 3;\n const cooldownMs = (config.cooldownSeconds ?? 90) * 1000;\n const storage = config.storage ?? defaultAdStorage;\n const driver = config.driver ?? capacitorAdMobDriver();\n const now = config.now ?? (() => Date.now());\n const getPlatform = config.platform ?? defaultPlatform;\n const storageKey = `${STORAGE_PREFIX}${config.key}`;\n\n async function readState(): Promise<AdPolicyState> {\n return parseAdState(await storage.getItem(storageKey));\n }\n\n async function writeState(state: AdPolicyState): Promise<void> {\n await storage.setItem(storageKey, JSON.stringify(state));\n }\n\n return {\n async recordActionAndMaybeShow(): Promise<InterstitialResult> {\n return withAdLock(storage, storageKey, async () => {\n const prev = await readState();\n if (config.disabled?.()) {\n return { status: 'disabled', state: prev };\n }\n\n const nextState: AdPolicyState = {\n totalActions: prev.totalActions + 1,\n actionsSinceLastAd: prev.actionsSinceLastAd + 1,\n lastShownAt: prev.lastShownAt,\n };\n await writeState(nextState);\n\n const requiredActions =\n prev.lastShownAt === null ? minActionsBeforeFirst : actionsBetweenAds;\n if (nextState.actionsSinceLastAd < requiredActions) {\n return { status: 'not_eligible', state: nextState };\n }\n\n const currentTime = now();\n if (nextState.lastShownAt !== null) {\n const nextEligibleAt = nextState.lastShownAt + cooldownMs;\n if (currentTime < nextEligibleAt) {\n return { status: 'cooldown', state: nextState, nextEligibleAt };\n }\n }\n\n const adId = resolveAdId(config.interstitialAdId, getPlatform());\n if (!adId || !driver.isSupported()) {\n return { status: 'unsupported', state: nextState };\n }\n\n try {\n await driver.showInterstitial(adId, { isTesting: config.isTesting });\n const shownState: AdPolicyState = {\n totalActions: nextState.totalActions,\n actionsSinceLastAd: 0,\n lastShownAt: now(),\n };\n await writeState(shownState);\n return { status: 'shown', state: shownState };\n } catch (err) {\n return {\n status: 'failed',\n state: nextState,\n error: err instanceof Error ? err : new Error(String(err)),\n };\n }\n });\n },\n\n async showRewarded(): Promise<RewardedResult> {\n const adId = resolveAdId(config.rewardedAdId, getPlatform());\n if (!adId || !driver.isSupported() || !driver.showRewarded) {\n return { status: 'unsupported' };\n }\n try {\n const res = await driver.showRewarded(adId, { isTesting: config.isTesting });\n return { status: res.rewarded ? 'rewarded' : 'dismissed' };\n } catch (err) {\n return {\n status: 'failed',\n error: err instanceof Error ? err : new Error(String(err)),\n };\n }\n },\n\n getState: () => withAdLock(storage, storageKey, readState),\n\n async reset(): Promise<void> {\n return withAdLock(storage, storageKey, async () => {\n if (storage.removeItem) {\n await storage.removeItem(storageKey);\n } else {\n await writeState({ totalActions: 0, actionsSinceLastAd: 0, lastShownAt: null });\n }\n });\n },\n };\n}\n"]}
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var core = require('@capacitor/core');
|
|
4
|
+
|
|
5
|
+
// src/back-button.ts
|
|
6
|
+
function createBackButtonStack() {
|
|
7
|
+
let nextId = 1;
|
|
8
|
+
const entries = [];
|
|
9
|
+
return {
|
|
10
|
+
register(handler, options = {}) {
|
|
11
|
+
const entry = {
|
|
12
|
+
id: nextId++,
|
|
13
|
+
priority: options.priority ?? 0,
|
|
14
|
+
handler
|
|
15
|
+
};
|
|
16
|
+
entries.push(entry);
|
|
17
|
+
return () => {
|
|
18
|
+
const index = entries.findIndex((item) => item.id === entry.id);
|
|
19
|
+
if (index !== -1) {
|
|
20
|
+
entries.splice(index, 1);
|
|
21
|
+
}
|
|
22
|
+
};
|
|
23
|
+
},
|
|
24
|
+
async handleBackPress() {
|
|
25
|
+
const ordered = [...entries].sort((a, b) => {
|
|
26
|
+
if (b.priority !== a.priority) {
|
|
27
|
+
return b.priority - a.priority;
|
|
28
|
+
}
|
|
29
|
+
return b.id - a.id;
|
|
30
|
+
});
|
|
31
|
+
for (const entry of ordered) {
|
|
32
|
+
const handled = await entry.handler();
|
|
33
|
+
if (handled === true) {
|
|
34
|
+
return true;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
return false;
|
|
38
|
+
},
|
|
39
|
+
size() {
|
|
40
|
+
return entries.length;
|
|
41
|
+
},
|
|
42
|
+
clear() {
|
|
43
|
+
entries.length = 0;
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
var defaultBackButtonStack = createBackButtonStack();
|
|
48
|
+
function registerBackHandler(handler, options) {
|
|
49
|
+
return defaultBackButtonStack.register(handler, options);
|
|
50
|
+
}
|
|
51
|
+
function handleBackPress() {
|
|
52
|
+
return defaultBackButtonStack.handleBackPress();
|
|
53
|
+
}
|
|
54
|
+
var CapacitorApp = core.registerPlugin("App");
|
|
55
|
+
function createDoubleBackExit(options = {}) {
|
|
56
|
+
const windowMs = options.windowMs ?? 2e3;
|
|
57
|
+
const now = options.now ?? (() => Date.now());
|
|
58
|
+
let lastPressedAt = null;
|
|
59
|
+
return async () => {
|
|
60
|
+
const current = now();
|
|
61
|
+
if (lastPressedAt !== null && current - lastPressedAt <= windowMs) {
|
|
62
|
+
lastPressedAt = null;
|
|
63
|
+
if (options.onExit) {
|
|
64
|
+
await options.onExit();
|
|
65
|
+
} else if (typeof window !== "undefined" && core.Capacitor.isNativePlatform() && core.Capacitor.isPluginAvailable("App")) {
|
|
66
|
+
await CapacitorApp.exitApp();
|
|
67
|
+
}
|
|
68
|
+
return true;
|
|
69
|
+
}
|
|
70
|
+
lastPressedAt = current;
|
|
71
|
+
options.onFirstPress?.();
|
|
72
|
+
return true;
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
async function attachCapacitorBackButton(stack = defaultBackButtonStack) {
|
|
76
|
+
if (typeof window === "undefined" || !core.Capacitor.isNativePlatform() || !core.Capacitor.isPluginAvailable("App")) {
|
|
77
|
+
return async () => {
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
const listener = await CapacitorApp.addListener("backButton", () => {
|
|
81
|
+
void stack.handleBackPress();
|
|
82
|
+
});
|
|
83
|
+
return async () => {
|
|
84
|
+
await listener.remove();
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
exports.attachCapacitorBackButton = attachCapacitorBackButton;
|
|
89
|
+
exports.createBackButtonStack = createBackButtonStack;
|
|
90
|
+
exports.createDoubleBackExit = createDoubleBackExit;
|
|
91
|
+
exports.defaultBackButtonStack = defaultBackButtonStack;
|
|
92
|
+
exports.handleBackPress = handleBackPress;
|
|
93
|
+
exports.registerBackHandler = registerBackHandler;
|
|
94
|
+
//# sourceMappingURL=back-button.cjs.map
|
|
95
|
+
//# sourceMappingURL=back-button.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/back-button.ts"],"names":["registerPlugin","Capacitor"],"mappings":";;;;;AA6BO,SAAS,qBAAA,GAAyC;AACvD,EAAA,IAAI,MAAA,GAAS,CAAA;AACb,EAAA,MAAM,UAAuB,EAAC;AAE9B,EAAA,OA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cAAA,CAAU,iBAAA,CAAkB,KAAK,CAAA,EAClC;AACA,IAAA,OAAO,YAAY;AAAA,IAAC,CAAA;AAAA,EACtB;AAEA,EAAA,MAAM,QAAA,GAAW,MAAM,YAAA,CAAa,WAAA,CAAY,cAAc,MAAM;AAClE,IAAA,KAAK,MAAM,eAAA,EAAgB;AAAA,EAC7B,CAAC,CAAA;AAED,EAAA,OAAO,YAAY;AACjB,IAAA,MAAM,SAAS,MAAA,EAAO;AAAA,EACxB,CAAA;AACF","file":"back-button.cjs","sourcesContent":["import { Capacitor, registerPlugin } from '@capacitor/core';\n\n/**\n * Return `true` if the back press was handled (stops propagation to lower-priority handlers),\n * or `false` / `void` to let the next handler run.\n */\nexport type BackHandler = () => boolean | void | Promise<boolean | void>;\n\nexport interface BackHandlerOptions {\n /**\n * Higher priority runs first (e.g. modals/sheets = 100, navigation = 50, exit = 0).\n * Equal priorities run in LIFO order (most recently registered first).\n */\n priority?: number;\n}\n\nexport interface BackButtonStack {\n register(handler: BackHandler, options?: BackHandlerOptions): () => void;\n handleBackPress(): Promise<boolean>;\n size(): number;\n clear(): void;\n}\n\ninterface BackEntry {\n id: number;\n priority: number;\n handler: BackHandler;\n}\n\nexport function createBackButtonStack(): BackButtonStack {\n let nextId = 1;\n const entries: BackEntry[] = [];\n\n return {\n register(handler: BackHandler, options: BackHandlerOptions = {}): () => void {\n const entry: BackEntry = {\n id: nextId++,\n priority: options.priority ?? 0,\n handler,\n };\n entries.push(entry);\n return () => {\n const index = entries.findIndex((item) => item.id === entry.id);\n if (index !== -1) {\n entries.splice(index, 1);\n }\n };\n },\n\n async handleBackPress(): Promise<boolean> {\n const ordered = [...entries].sort((a, b) => {\n if (b.priority !== a.priority) {\n return b.priority - a.priority;\n }\n return b.id - a.id;\n });\n\n for (const entry of ordered) {\n const handled = await entry.handler();\n if (handled === true) {\n return true;\n }\n }\n\n return false;\n },\n\n size(): number {\n return entries.length;\n },\n\n clear(): void {\n entries.length = 0;\n },\n };\n}\n\nexport const defaultBackButtonStack = createBackButtonStack();\n\nexport function registerBackHandler(\n handler: BackHandler,\n options?: BackHandlerOptions,\n): () => void {\n return defaultBackButtonStack.register(handler, options);\n}\n\nexport function handleBackPress(): Promise<boolean> {\n return defaultBackButtonStack.handleBackPress();\n}\n\nexport interface DoubleBackExitOptions {\n /** Window in milliseconds within which a second back press exits (default: `2000`). */\n windowMs?: number;\n /** Called on the first back press (e.g. show a \"Press back again to exit\" toast). */\n onFirstPress?: () => void;\n /** Called on the second press within `windowMs`. Defaults to `@capacitor/app` `exitApp()`. */\n onExit?: () => void | Promise<void>;\n /** Optional clock for deterministic testing. */\n now?: () => number;\n}\n\ninterface CapacitorAppLike {\n addListener(\n eventName: 'backButton',\n listenerFunc: (event: { canGoBack: boolean }) => void,\n ): Promise<{ remove: () => Promise<void> }>;\n exitApp(): Promise<void>;\n}\n\nconst CapacitorApp = registerPlugin<CapacitorAppLike>('App');\n\n/**\n * Creates a handler for the classic Android \"press back again to exit\" pattern.\n * Register it with low priority (e.g. `priority: 0`) so modals and inner screens take precedence.\n */\nexport function createDoubleBackExit(options: DoubleBackExitOptions = {}): BackHandler {\n const windowMs = options.windowMs ?? 2000;\n const now = options.now ?? (() => Date.now());\n let lastPressedAt: number | null = null;\n\n return async () => {\n const current = now();\n if (lastPressedAt !== null && current - lastPressedAt <= windowMs) {\n lastPressedAt = null;\n if (options.onExit) {\n await options.onExit();\n } else if (\n typeof window !== 'undefined' &&\n Capacitor.isNativePlatform() &&\n Capacitor.isPluginAvailable('App')\n ) {\n await CapacitorApp.exitApp();\n }\n return true;\n }\n\n lastPressedAt = current;\n options.onFirstPress?.();\n return true;\n };\n}\n\n/**\n * Subscribes a `BackButtonStack` to Capacitor's native Android `App.addListener('backButton')` event.\n * Safely no-ops on web/SSR or when `@capacitor/app` is not installed.\n * Returns an async cleanup function.\n */\nexport async function attachCapacitorBackButton(\n stack: BackButtonStack = defaultBackButtonStack,\n): Promise<() => Promise<void>> {\n if (\n typeof window === 'undefined' ||\n !Capacitor.isNativePlatform() ||\n !Capacitor.isPluginAvailable('App')\n ) {\n return async () => {};\n }\n\n const listener = await CapacitorApp.addListener('backButton', () => {\n void stack.handleBackPress();\n });\n\n return async () => {\n await listener.remove();\n };\n}\n"]}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Return `true` if the back press was handled (stops propagation to lower-priority handlers),
|
|
3
|
+
* or `false` / `void` to let the next handler run.
|
|
4
|
+
*/
|
|
5
|
+
type BackHandler = () => boolean | void | Promise<boolean | void>;
|
|
6
|
+
interface BackHandlerOptions {
|
|
7
|
+
/**
|
|
8
|
+
* Higher priority runs first (e.g. modals/sheets = 100, navigation = 50, exit = 0).
|
|
9
|
+
* Equal priorities run in LIFO order (most recently registered first).
|
|
10
|
+
*/
|
|
11
|
+
priority?: number;
|
|
12
|
+
}
|
|
13
|
+
interface BackButtonStack {
|
|
14
|
+
register(handler: BackHandler, options?: BackHandlerOptions): () => void;
|
|
15
|
+
handleBackPress(): Promise<boolean>;
|
|
16
|
+
size(): number;
|
|
17
|
+
clear(): void;
|
|
18
|
+
}
|
|
19
|
+
declare function createBackButtonStack(): BackButtonStack;
|
|
20
|
+
declare const defaultBackButtonStack: BackButtonStack;
|
|
21
|
+
declare function registerBackHandler(handler: BackHandler, options?: BackHandlerOptions): () => void;
|
|
22
|
+
declare function handleBackPress(): Promise<boolean>;
|
|
23
|
+
interface DoubleBackExitOptions {
|
|
24
|
+
/** Window in milliseconds within which a second back press exits (default: `2000`). */
|
|
25
|
+
windowMs?: number;
|
|
26
|
+
/** Called on the first back press (e.g. show a "Press back again to exit" toast). */
|
|
27
|
+
onFirstPress?: () => void;
|
|
28
|
+
/** Called on the second press within `windowMs`. Defaults to `@capacitor/app` `exitApp()`. */
|
|
29
|
+
onExit?: () => void | Promise<void>;
|
|
30
|
+
/** Optional clock for deterministic testing. */
|
|
31
|
+
now?: () => number;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Creates a handler for the classic Android "press back again to exit" pattern.
|
|
35
|
+
* Register it with low priority (e.g. `priority: 0`) so modals and inner screens take precedence.
|
|
36
|
+
*/
|
|
37
|
+
declare function createDoubleBackExit(options?: DoubleBackExitOptions): BackHandler;
|
|
38
|
+
/**
|
|
39
|
+
* Subscribes a `BackButtonStack` to Capacitor's native Android `App.addListener('backButton')` event.
|
|
40
|
+
* Safely no-ops on web/SSR or when `@capacitor/app` is not installed.
|
|
41
|
+
* Returns an async cleanup function.
|
|
42
|
+
*/
|
|
43
|
+
declare function attachCapacitorBackButton(stack?: BackButtonStack): Promise<() => Promise<void>>;
|
|
44
|
+
|
|
45
|
+
export { type BackButtonStack, type BackHandler, type BackHandlerOptions, type DoubleBackExitOptions, attachCapacitorBackButton, createBackButtonStack, createDoubleBackExit, defaultBackButtonStack, handleBackPress, registerBackHandler };
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Return `true` if the back press was handled (stops propagation to lower-priority handlers),
|
|
3
|
+
* or `false` / `void` to let the next handler run.
|
|
4
|
+
*/
|
|
5
|
+
type BackHandler = () => boolean | void | Promise<boolean | void>;
|
|
6
|
+
interface BackHandlerOptions {
|
|
7
|
+
/**
|
|
8
|
+
* Higher priority runs first (e.g. modals/sheets = 100, navigation = 50, exit = 0).
|
|
9
|
+
* Equal priorities run in LIFO order (most recently registered first).
|
|
10
|
+
*/
|
|
11
|
+
priority?: number;
|
|
12
|
+
}
|
|
13
|
+
interface BackButtonStack {
|
|
14
|
+
register(handler: BackHandler, options?: BackHandlerOptions): () => void;
|
|
15
|
+
handleBackPress(): Promise<boolean>;
|
|
16
|
+
size(): number;
|
|
17
|
+
clear(): void;
|
|
18
|
+
}
|
|
19
|
+
declare function createBackButtonStack(): BackButtonStack;
|
|
20
|
+
declare const defaultBackButtonStack: BackButtonStack;
|
|
21
|
+
declare function registerBackHandler(handler: BackHandler, options?: BackHandlerOptions): () => void;
|
|
22
|
+
declare function handleBackPress(): Promise<boolean>;
|
|
23
|
+
interface DoubleBackExitOptions {
|
|
24
|
+
/** Window in milliseconds within which a second back press exits (default: `2000`). */
|
|
25
|
+
windowMs?: number;
|
|
26
|
+
/** Called on the first back press (e.g. show a "Press back again to exit" toast). */
|
|
27
|
+
onFirstPress?: () => void;
|
|
28
|
+
/** Called on the second press within `windowMs`. Defaults to `@capacitor/app` `exitApp()`. */
|
|
29
|
+
onExit?: () => void | Promise<void>;
|
|
30
|
+
/** Optional clock for deterministic testing. */
|
|
31
|
+
now?: () => number;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Creates a handler for the classic Android "press back again to exit" pattern.
|
|
35
|
+
* Register it with low priority (e.g. `priority: 0`) so modals and inner screens take precedence.
|
|
36
|
+
*/
|
|
37
|
+
declare function createDoubleBackExit(options?: DoubleBackExitOptions): BackHandler;
|
|
38
|
+
/**
|
|
39
|
+
* Subscribes a `BackButtonStack` to Capacitor's native Android `App.addListener('backButton')` event.
|
|
40
|
+
* Safely no-ops on web/SSR or when `@capacitor/app` is not installed.
|
|
41
|
+
* Returns an async cleanup function.
|
|
42
|
+
*/
|
|
43
|
+
declare function attachCapacitorBackButton(stack?: BackButtonStack): Promise<() => Promise<void>>;
|
|
44
|
+
|
|
45
|
+
export { type BackButtonStack, type BackHandler, type BackHandlerOptions, type DoubleBackExitOptions, attachCapacitorBackButton, createBackButtonStack, createDoubleBackExit, defaultBackButtonStack, handleBackPress, registerBackHandler };
|