@ethicly/react-native 0.0.0-stage → 0.2.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/CHANGELOG.md +75 -0
- package/LICENSE +21 -0
- package/README.md +313 -2
- package/core/package.json +6 -0
- package/interstitial/package.json +6 -0
- package/lib/commonjs/core/adapters.js +287 -0
- package/lib/commonjs/core/beacons.js +70 -0
- package/lib/commonjs/core/cache.js +206 -0
- package/lib/commonjs/core/click.js +19 -0
- package/lib/commonjs/core/client.js +489 -0
- package/lib/commonjs/core/config.js +113 -0
- package/lib/commonjs/core/http.js +115 -0
- package/lib/commonjs/core/index.js +20 -0
- package/lib/commonjs/core/outbox.js +192 -0
- package/lib/commonjs/core/tracker.js +374 -0
- package/lib/commonjs/core/types.js +4 -0
- package/lib/commonjs/index.js +22 -0
- package/lib/commonjs/interstitial.js +7 -0
- package/lib/commonjs/react/EthiclyInterstitial.js +286 -0
- package/lib/commonjs/react/useEthiclyInterstitial.js +54 -0
- package/lib/commonjs/version.js +5 -0
- package/lib/module/core/adapters.js +276 -0
- package/lib/module/core/beacons.js +64 -0
- package/lib/module/core/cache.js +202 -0
- package/lib/module/core/click.js +15 -0
- package/lib/module/core/client.js +485 -0
- package/lib/module/core/config.js +104 -0
- package/lib/module/core/http.js +105 -0
- package/lib/module/core/index.js +6 -0
- package/lib/module/core/outbox.js +186 -0
- package/lib/module/core/tracker.js +370 -0
- package/lib/module/core/types.js +3 -0
- package/lib/module/index.js +4 -0
- package/lib/module/interstitial.js +3 -0
- package/lib/module/react/EthiclyInterstitial.js +260 -0
- package/lib/module/react/useEthiclyInterstitial.js +51 -0
- package/lib/module/version.js +2 -0
- package/lib/typescript/core/adapters.d.ts +138 -0
- package/lib/typescript/core/beacons.d.ts +32 -0
- package/lib/typescript/core/cache.d.ts +53 -0
- package/lib/typescript/core/click.d.ts +8 -0
- package/lib/typescript/core/client.d.ts +58 -0
- package/lib/typescript/core/config.d.ts +24 -0
- package/lib/typescript/core/http.d.ts +34 -0
- package/lib/typescript/core/index.d.ts +8 -0
- package/lib/typescript/core/outbox.d.ts +56 -0
- package/lib/typescript/core/tracker.d.ts +95 -0
- package/lib/typescript/core/types.d.ts +170 -0
- package/lib/typescript/index.d.ts +3 -0
- package/lib/typescript/interstitial.d.ts +2 -0
- package/lib/typescript/react/EthiclyInterstitial.d.ts +39 -0
- package/lib/typescript/react/useEthiclyInterstitial.d.ts +20 -0
- package/lib/typescript/version.d.ts +2 -0
- package/package.json +101 -4
- package/src/core/adapters.ts +337 -0
- package/src/core/beacons.ts +81 -0
- package/src/core/cache.ts +216 -0
- package/src/core/click.ts +16 -0
- package/src/core/client.ts +547 -0
- package/src/core/config.ts +111 -0
- package/src/core/http.ts +115 -0
- package/src/core/index.ts +14 -0
- package/src/core/outbox.ts +193 -0
- package/src/core/tracker.ts +371 -0
- package/src/core/types.ts +180 -0
- package/src/index.ts +5 -0
- package/src/interstitial.ts +4 -0
- package/src/react/EthiclyInterstitial.tsx +386 -0
- package/src/react/useEthiclyInterstitial.ts +74 -0
- package/src/version.ts +2 -0
|
@@ -0,0 +1,286 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
+
exports.EthiclyInterstitial = EthiclyInterstitial;
|
|
27
|
+
const jsx_runtime_1 = require("react/jsx-runtime");
|
|
28
|
+
// Optional ready-made fullscreen screen. Needs expo-video (Expo SDK 52+).
|
|
29
|
+
// Layout follows a typical in-app video player: 9:16 video, a skip control that counts
|
|
30
|
+
// down then reads "Skip Ad", a white call-to-action button, an optional
|
|
31
|
+
// "Remove ads" slot and a small "Sponsored by Ethicly.ch" line under it.
|
|
32
|
+
// Video starts with sound unless interstitial.audio is 'none' or 'optional'.
|
|
33
|
+
// The controls keep clear of the notch, status bar and home indicator: see
|
|
34
|
+
// useSafeInsets below.
|
|
35
|
+
const react_1 = __importStar(require("react"));
|
|
36
|
+
const react_native_1 = require("react-native");
|
|
37
|
+
const expo_video_1 = require("expo-video");
|
|
38
|
+
const TICK_MS = 250;
|
|
39
|
+
/** Space between the safe area and the controls. */
|
|
40
|
+
const TOP_GAP = 12;
|
|
41
|
+
const BOTTOM_GAP = 10;
|
|
42
|
+
const SIDE_GAP_TOP = 16;
|
|
43
|
+
const SIDE_GAP_BOTTOM = 20;
|
|
44
|
+
/**
|
|
45
|
+
* The 0.1 layout (controls 56 from the top, 44 from the bottom) is a floor:
|
|
46
|
+
* insets only ever move the controls inward, so a phone without a notch or a
|
|
47
|
+
* tall status bar looks exactly as before.
|
|
48
|
+
*/
|
|
49
|
+
const MIN_TOP = 56;
|
|
50
|
+
const MIN_BOTTOM = 44;
|
|
51
|
+
/**
|
|
52
|
+
* react-native-safe-area-context when the app has it, else null. Metro treats
|
|
53
|
+
* a require inside try/catch as an optional dependency (the React Native and
|
|
54
|
+
* Expo Metro configs set allowOptionalDependencies), so an app without the
|
|
55
|
+
* package still builds.
|
|
56
|
+
*/
|
|
57
|
+
function loadSafeArea() {
|
|
58
|
+
try {
|
|
59
|
+
// @ts-ignore: the build compiles without Node's types, where require is undeclared.
|
|
60
|
+
return typeof require === 'function' ? require('react-native-safe-area-context') : null;
|
|
61
|
+
}
|
|
62
|
+
catch {
|
|
63
|
+
return null;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
const safeArea = loadSafeArea();
|
|
67
|
+
const noInsets = react_1.default.createContext(null);
|
|
68
|
+
// Read with useContext, which returns null without a SafeAreaProvider instead
|
|
69
|
+
// of throwing like useSafeAreaInsets(). Fixed at load, so hook order never changes.
|
|
70
|
+
const InsetsContext = safeArea && safeArea.SafeAreaInsetsContext && typeof safeArea.SafeAreaInsetsContext === 'object'
|
|
71
|
+
? safeArea.SafeAreaInsetsContext
|
|
72
|
+
: noInsets;
|
|
73
|
+
function validInsets(v) {
|
|
74
|
+
const i = v;
|
|
75
|
+
if (!i || typeof i !== 'object')
|
|
76
|
+
return null;
|
|
77
|
+
const n = (x) => (typeof x === 'number' && Number.isFinite(x) && x >= 0 ? x : 0);
|
|
78
|
+
if (typeof i.top !== 'number' && typeof i.bottom !== 'number')
|
|
79
|
+
return null;
|
|
80
|
+
return { top: n(i.top), bottom: n(i.bottom), left: n(i.left), right: n(i.right) };
|
|
81
|
+
}
|
|
82
|
+
/** The `insets` prop, else the provider's insets, else the window's first metrics, else the status bar. */
|
|
83
|
+
function useSafeInsets(prop) {
|
|
84
|
+
var _a, _b, _c, _d, _e;
|
|
85
|
+
const fromProvider = (0, react_1.useContext)(InsetsContext);
|
|
86
|
+
return (_d = (_b = (_a = validInsets(prop)) !== null && _a !== void 0 ? _a : validInsets(fromProvider)) !== null && _b !== void 0 ? _b : validInsets((_c = safeArea === null || safeArea === void 0 ? void 0 : safeArea.initialWindowMetrics) === null || _c === void 0 ? void 0 : _c.insets)) !== null && _d !== void 0 ? _d : { top: react_native_1.Platform.OS === 'android' ? (_e = react_native_1.StatusBar.currentHeight) !== null && _e !== void 0 ? _e : 0 : 0, bottom: 0, left: 0, right: 0 };
|
|
87
|
+
}
|
|
88
|
+
function EthiclyInterstitial(props) {
|
|
89
|
+
// Android back: a Modal receives it through onRequestClose, an inline screen
|
|
90
|
+
// through BackHandler. Either way it closes the ad only once Skip is allowed,
|
|
91
|
+
// and is swallowed before that.
|
|
92
|
+
const backRef = (0, react_1.useRef)(null);
|
|
93
|
+
const inline = props.modal === false;
|
|
94
|
+
(0, react_1.useEffect)(() => {
|
|
95
|
+
if (!inline)
|
|
96
|
+
return undefined;
|
|
97
|
+
const sub = react_native_1.BackHandler.addEventListener('hardwareBackPress', () => {
|
|
98
|
+
var _a;
|
|
99
|
+
(_a = backRef.current) === null || _a === void 0 ? void 0 : _a.call(backRef);
|
|
100
|
+
return true;
|
|
101
|
+
});
|
|
102
|
+
return () => sub.remove();
|
|
103
|
+
}, [inline]);
|
|
104
|
+
const body = (0, jsx_runtime_1.jsx)(InterstitialBody, { ...props, backRef: backRef });
|
|
105
|
+
if (inline)
|
|
106
|
+
return body;
|
|
107
|
+
return ((0, jsx_runtime_1.jsx)(react_native_1.Modal, { visible: true, animationType: "fade", presentationStyle: "fullScreen", statusBarTranslucent: true, onRequestClose: () => { var _a; return (_a = backRef.current) === null || _a === void 0 ? void 0 : _a.call(backRef); }, children: body }));
|
|
108
|
+
}
|
|
109
|
+
function useAppActive() {
|
|
110
|
+
const [active, setActive] = (0, react_1.useState)(react_native_1.AppState.currentState === 'active');
|
|
111
|
+
(0, react_1.useEffect)(() => {
|
|
112
|
+
const sub = react_native_1.AppState.addEventListener('change', (s) => setActive(s === 'active'));
|
|
113
|
+
return () => sub.remove();
|
|
114
|
+
}, []);
|
|
115
|
+
return active;
|
|
116
|
+
}
|
|
117
|
+
function InterstitialBody({ ad, tracker, onClose, onRemoveAds, removeAdsLabel, renderBelowButton, skipStyle, skipTextStyle, insets, backRef, }) {
|
|
118
|
+
const safe = useSafeInsets(insets);
|
|
119
|
+
const isVideo = ad.media.kind === 'video' && !!ad.media.videoUri;
|
|
120
|
+
const appActive = useAppActive();
|
|
121
|
+
const activeRef = (0, react_1.useRef)(appActive);
|
|
122
|
+
activeRef.current = appActive;
|
|
123
|
+
const [positionMs, setPositionMs] = (0, react_1.useState)(0);
|
|
124
|
+
const [ended, setEnded] = (0, react_1.useState)(false);
|
|
125
|
+
const [failed, setFailed] = (0, react_1.useState)(false);
|
|
126
|
+
const [muted, setMuted] = (0, react_1.useState)(ad.audio !== 'on');
|
|
127
|
+
/** Wall-clock time on screen with the app in the foreground. */
|
|
128
|
+
const [onScreenMs, setOnScreenMs] = (0, react_1.useState)(0);
|
|
129
|
+
const [clicked, setClicked] = (0, react_1.useState)(false);
|
|
130
|
+
const positionRef = (0, react_1.useRef)(0);
|
|
131
|
+
const closedRef = (0, react_1.useRef)(false);
|
|
132
|
+
const player = (0, expo_video_1.useVideoPlayer)(isVideo ? ad.media.videoUri : null, (p) => {
|
|
133
|
+
p.loop = false;
|
|
134
|
+
p.muted = ad.audio !== 'on';
|
|
135
|
+
p.timeUpdateEventInterval = TICK_MS / 1000;
|
|
136
|
+
p.play();
|
|
137
|
+
});
|
|
138
|
+
const onCloseRef = (0, react_1.useRef)(onClose);
|
|
139
|
+
onCloseRef.current = onClose;
|
|
140
|
+
// The impression goes out the moment the ad is on screen. shown() refuses an
|
|
141
|
+
// ad past its token lifetime (nothing is recorded), so the screen closes
|
|
142
|
+
// itself instead of playing an ad nobody is paid for.
|
|
143
|
+
(0, react_1.useEffect)(() => {
|
|
144
|
+
const expired = tracker.isExpired();
|
|
145
|
+
void tracker.shown();
|
|
146
|
+
if (expired && !closedRef.current) {
|
|
147
|
+
closedRef.current = true;
|
|
148
|
+
void tracker.closed();
|
|
149
|
+
onCloseRef.current();
|
|
150
|
+
}
|
|
151
|
+
}, [tracker]);
|
|
152
|
+
// Video: playback position drives viewability, quartiles and the skip countdown.
|
|
153
|
+
(0, react_1.useEffect)(() => {
|
|
154
|
+
if (!isVideo)
|
|
155
|
+
return undefined;
|
|
156
|
+
const subs = [
|
|
157
|
+
player.addListener('timeUpdate', ({ currentTime }) => {
|
|
158
|
+
const ms = Math.max(0, currentTime * 1000);
|
|
159
|
+
positionRef.current = ms;
|
|
160
|
+
setPositionMs(ms);
|
|
161
|
+
const durationMs = player.duration > 0 ? player.duration * 1000 : undefined;
|
|
162
|
+
tracker.progress(ms, { visible: activeRef.current && player.playing, durationMs });
|
|
163
|
+
}),
|
|
164
|
+
player.addListener('playToEnd', () => {
|
|
165
|
+
setEnded(true);
|
|
166
|
+
tracker.complete();
|
|
167
|
+
}),
|
|
168
|
+
player.addListener('statusChange', ({ status }) => {
|
|
169
|
+
if (status === 'error') {
|
|
170
|
+
setFailed(true);
|
|
171
|
+
tracker.error();
|
|
172
|
+
}
|
|
173
|
+
}),
|
|
174
|
+
];
|
|
175
|
+
return () => subs.forEach((s) => s.remove());
|
|
176
|
+
}, [isVideo, player, tracker]);
|
|
177
|
+
// Time on screen while the app is in the foreground. For a still image it is
|
|
178
|
+
// the playback position; for a video it is the skip countdown's fallback, so
|
|
179
|
+
// Skip still appears after skipAfterMs if the video stalls or never starts.
|
|
180
|
+
(0, react_1.useEffect)(() => {
|
|
181
|
+
let shownMs = 0;
|
|
182
|
+
const id = setInterval(() => {
|
|
183
|
+
if (activeRef.current)
|
|
184
|
+
shownMs += TICK_MS;
|
|
185
|
+
setOnScreenMs(shownMs);
|
|
186
|
+
if (isVideo)
|
|
187
|
+
return;
|
|
188
|
+
positionRef.current = shownMs;
|
|
189
|
+
setPositionMs(shownMs);
|
|
190
|
+
tracker.progress(shownMs, { visible: activeRef.current });
|
|
191
|
+
}, TICK_MS);
|
|
192
|
+
return () => clearInterval(id);
|
|
193
|
+
}, [isVideo, tracker]);
|
|
194
|
+
(0, react_1.useEffect)(() => {
|
|
195
|
+
if (!isVideo)
|
|
196
|
+
return;
|
|
197
|
+
if (appActive && !ended)
|
|
198
|
+
player.play();
|
|
199
|
+
else
|
|
200
|
+
player.pause();
|
|
201
|
+
}, [appActive, ended, isVideo, player]);
|
|
202
|
+
// Leaving the foreground ends continuous visibility right away, without
|
|
203
|
+
// waiting for the next player tick (which may never come while paused).
|
|
204
|
+
(0, react_1.useEffect)(() => {
|
|
205
|
+
if (appActive)
|
|
206
|
+
return;
|
|
207
|
+
const durationMs = isVideo && player.duration > 0 ? player.duration * 1000 : undefined;
|
|
208
|
+
tracker.progress(positionRef.current, { visible: false, durationMs });
|
|
209
|
+
}, [appActive, isVideo, player, tracker]);
|
|
210
|
+
// The tracker is closed only from close() below (or by the hook's dismiss()),
|
|
211
|
+
// never from an effect cleanup: React StrictMode runs every cleanup once on
|
|
212
|
+
// mount, and a closed tracker drops /viewable and the video events.
|
|
213
|
+
const close = (0, react_1.useCallback)(() => {
|
|
214
|
+
if (closedRef.current)
|
|
215
|
+
return;
|
|
216
|
+
closedRef.current = true;
|
|
217
|
+
if (isVideo && !ended && !failed)
|
|
218
|
+
tracker.skipped(positionRef.current);
|
|
219
|
+
void tracker.closed();
|
|
220
|
+
onClose();
|
|
221
|
+
}, [ended, failed, isVideo, onClose, tracker]);
|
|
222
|
+
const onCta = (0, react_1.useCallback)(() => {
|
|
223
|
+
if (isVideo)
|
|
224
|
+
player.pause();
|
|
225
|
+
// Once the user has visited the advertiser, they can always close the ad
|
|
226
|
+
// when they come back.
|
|
227
|
+
setClicked(true);
|
|
228
|
+
void tracker.clicked();
|
|
229
|
+
}, [isVideo, player, tracker]);
|
|
230
|
+
const toggleSound = (0, react_1.useCallback)(() => {
|
|
231
|
+
setMuted((m) => {
|
|
232
|
+
player.muted = !m;
|
|
233
|
+
return !m;
|
|
234
|
+
});
|
|
235
|
+
}, [player]);
|
|
236
|
+
const remainingS = Math.ceil(Math.max(0, ad.skipAfterMs - Math.max(positionMs, onScreenMs)) / 1000);
|
|
237
|
+
const canSkip = remainingS === 0 || ended || failed || clicked;
|
|
238
|
+
backRef.current = canSkip ? close : null;
|
|
239
|
+
return ((0, jsx_runtime_1.jsxs)(react_native_1.View, { style: styles.root, children: [isVideo ? ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [ad.media.posterUri ? (0, jsx_runtime_1.jsx)(react_native_1.Image, { source: { uri: ad.media.posterUri }, style: react_native_1.StyleSheet.absoluteFill, resizeMode: "cover" }) : null, (0, jsx_runtime_1.jsx)(expo_video_1.VideoView, { player: player, style: react_native_1.StyleSheet.absoluteFill, contentFit: "cover", nativeControls: false, allowsFullscreen: false, allowsPictureInPicture: false })] })) : ((0, jsx_runtime_1.jsx)(react_native_1.Image, { source: { uri: ad.media.uri }, style: react_native_1.StyleSheet.absoluteFill, resizeMode: "cover" })), (0, jsx_runtime_1.jsxs)(react_native_1.View, { style: [styles.topRow, { top: Math.max(safe.top + TOP_GAP, MIN_TOP), left: safe.left + SIDE_GAP_TOP, right: safe.right + SIDE_GAP_TOP }], pointerEvents: "box-none", children: [(0, jsx_runtime_1.jsxs)(react_native_1.View, { style: styles.leftGroup, children: [(0, jsx_runtime_1.jsx)(react_native_1.Text, { style: styles.adLabel, accessibilityRole: "text", children: "Ad" }), isVideo && ad.audio !== 'none' ? ((0, jsx_runtime_1.jsx)(react_native_1.Pressable, { onPress: toggleSound, style: styles.soundButton, accessibilityRole: "button", accessibilityLabel: muted ? 'Turn sound on' : 'Mute', hitSlop: 8, children: (0, jsx_runtime_1.jsx)(react_native_1.Text, { style: styles.soundText, children: muted ? 'Sound off' : 'Sound on' }) })) : null] }), (0, jsx_runtime_1.jsx)(react_native_1.Pressable, { onPress: canSkip ? close : undefined, disabled: !canSkip, style: [styles.skip, skipStyle], accessibilityRole: "button", accessibilityLabel: canSkip ? 'Skip ad' : `Skip in ${remainingS} seconds`, hitSlop: 8, children: canSkip ? ((0, jsx_runtime_1.jsxs)(react_native_1.View, { style: styles.skipRow, children: [(0, jsx_runtime_1.jsx)(react_native_1.Text, { style: [styles.skipText, skipTextStyle], children: "Skip Ad" }), (0, jsx_runtime_1.jsx)(SkipIcon, {})] })) : ((0, jsx_runtime_1.jsx)(react_native_1.Text, { style: [styles.skipText, styles.skipTextWaiting, skipTextStyle], children: `Skip in ${remainingS}` })) })] }), (0, jsx_runtime_1.jsxs)(react_native_1.View, { style: [styles.bottom, {
|
|
240
|
+
bottom: Math.max(safe.bottom + BOTTOM_GAP, MIN_BOTTOM), left: safe.left + SIDE_GAP_BOTTOM, right: safe.right + SIDE_GAP_BOTTOM,
|
|
241
|
+
}], pointerEvents: "box-none", children: [(0, jsx_runtime_1.jsx)(react_native_1.Pressable, { onPress: onCta, style: ({ pressed }) => [styles.cta, pressed && styles.ctaPressed], accessibilityRole: "link", children: (0, jsx_runtime_1.jsx)(react_native_1.Text, { style: styles.ctaText, numberOfLines: 1, children: ad.ctaLabel || 'Learn more' }) }), renderBelowButton ? renderBelowButton() : onRemoveAds ? ((0, jsx_runtime_1.jsx)(react_native_1.Pressable, { onPress: onRemoveAds, accessibilityRole: "button", hitSlop: 8, children: (0, jsx_runtime_1.jsx)(react_native_1.Text, { style: styles.removeAds, children: removeAdsLabel || 'Remove ads' }) })) : null, (0, jsx_runtime_1.jsx)(react_native_1.Text, { style: styles.sponsor, children: "Sponsored by Ethicly.ch" })] })] }));
|
|
242
|
+
}
|
|
243
|
+
/** Skip-to-end mark (a play triangle against a bar), drawn so it never depends on a font glyph. */
|
|
244
|
+
function SkipIcon() {
|
|
245
|
+
return ((0, jsx_runtime_1.jsxs)(react_native_1.View, { style: styles.skipIcon, accessibilityElementsHidden: true, importantForAccessibility: "no-hide-descendants", children: [(0, jsx_runtime_1.jsx)(react_native_1.View, { style: styles.skipTriangle }), (0, jsx_runtime_1.jsx)(react_native_1.View, { style: styles.skipBar })] }));
|
|
246
|
+
}
|
|
247
|
+
const styles = react_native_1.StyleSheet.create({
|
|
248
|
+
root: { flex: 1, backgroundColor: '#000' },
|
|
249
|
+
topRow: {
|
|
250
|
+
position: 'absolute',
|
|
251
|
+
flexDirection: 'row', justifyContent: 'space-between', alignItems: 'center',
|
|
252
|
+
},
|
|
253
|
+
leftGroup: { flexDirection: 'row', alignItems: 'center' },
|
|
254
|
+
adLabel: {
|
|
255
|
+
color: '#fff', fontSize: 11, fontWeight: '600', letterSpacing: 0.5,
|
|
256
|
+
backgroundColor: 'rgba(0,0,0,0.45)', paddingHorizontal: 6, paddingVertical: 2, borderRadius: 4, overflow: 'hidden',
|
|
257
|
+
},
|
|
258
|
+
soundButton: { marginLeft: 8, backgroundColor: 'rgba(0,0,0,0.45)', paddingHorizontal: 8, paddingVertical: 4, borderRadius: 6 },
|
|
259
|
+
soundText: { color: '#fff', fontSize: 12, fontWeight: '500' },
|
|
260
|
+
// Solid black, no blur or translucency. The faint hairline keeps the edge
|
|
261
|
+
// visible over a dark frame.
|
|
262
|
+
skip: {
|
|
263
|
+
backgroundColor: '#000', paddingHorizontal: 18, height: 40, borderRadius: 20,
|
|
264
|
+
justifyContent: 'center', alignItems: 'center',
|
|
265
|
+
borderWidth: react_native_1.StyleSheet.hairlineWidth, borderColor: 'rgba(255,255,255,0.22)',
|
|
266
|
+
},
|
|
267
|
+
skipRow: { flexDirection: 'row', alignItems: 'center' },
|
|
268
|
+
skipText: { color: '#fff', fontSize: 15, fontWeight: '500', letterSpacing: 0.2 },
|
|
269
|
+
skipTextWaiting: { opacity: 0.75 },
|
|
270
|
+
skipIcon: { flexDirection: 'row', alignItems: 'center', marginLeft: 10 },
|
|
271
|
+
skipTriangle: {
|
|
272
|
+
width: 0, height: 0,
|
|
273
|
+
borderTopWidth: 6, borderBottomWidth: 6, borderLeftWidth: 10,
|
|
274
|
+
borderTopColor: 'transparent', borderBottomColor: 'transparent', borderLeftColor: '#fff',
|
|
275
|
+
},
|
|
276
|
+
skipBar: { width: 2, height: 12, backgroundColor: '#fff', marginLeft: 2, borderRadius: 1 },
|
|
277
|
+
bottom: { position: 'absolute', alignItems: 'center' },
|
|
278
|
+
cta: {
|
|
279
|
+
alignSelf: 'stretch', backgroundColor: '#fff', borderRadius: 12, paddingVertical: 15,
|
|
280
|
+
alignItems: 'center', justifyContent: 'center',
|
|
281
|
+
},
|
|
282
|
+
ctaPressed: { opacity: 0.85 },
|
|
283
|
+
ctaText: { color: '#000', fontSize: 17, fontWeight: '600' },
|
|
284
|
+
removeAds: { color: '#fff', fontSize: 14, marginTop: 14, textDecorationLine: 'underline', opacity: 0.9 },
|
|
285
|
+
sponsor: { color: '#fff', fontSize: 12, marginTop: 10, opacity: 0.6 },
|
|
286
|
+
});
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// React hook over the headless core. No React Native or Expo imports, so it
|
|
3
|
+
// works with any player the host app already has.
|
|
4
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
5
|
+
exports.useEthiclyInterstitial = useEthiclyInterstitial;
|
|
6
|
+
const react_1 = require("react");
|
|
7
|
+
function useEthiclyInterstitial(ethicly, options = {}) {
|
|
8
|
+
var _a, _b;
|
|
9
|
+
const preload = options.preload !== false;
|
|
10
|
+
const [current, setCurrent] = (0, react_1.useState)(null);
|
|
11
|
+
const [reason, setReason] = (0, react_1.useState)(null);
|
|
12
|
+
const [loading, setLoading] = (0, react_1.useState)(false);
|
|
13
|
+
const mounted = (0, react_1.useRef)(true);
|
|
14
|
+
const currentRef = (0, react_1.useRef)(current);
|
|
15
|
+
currentRef.current = current;
|
|
16
|
+
(0, react_1.useEffect)(() => {
|
|
17
|
+
mounted.current = true;
|
|
18
|
+
if (preload)
|
|
19
|
+
void ethicly.preload();
|
|
20
|
+
return () => {
|
|
21
|
+
var _a;
|
|
22
|
+
mounted.current = false;
|
|
23
|
+
void ((_a = currentRef.current) === null || _a === void 0 ? void 0 : _a.tracker.closed());
|
|
24
|
+
};
|
|
25
|
+
}, [ethicly, preload]);
|
|
26
|
+
const show = (0, react_1.useCallback)(async () => {
|
|
27
|
+
setLoading(true);
|
|
28
|
+
try {
|
|
29
|
+
const r = await ethicly.loadInterstitial();
|
|
30
|
+
if (!mounted.current)
|
|
31
|
+
return false;
|
|
32
|
+
if (r.ad) {
|
|
33
|
+
setCurrent({ ad: r.ad, tracker: r.tracker });
|
|
34
|
+
setReason(null);
|
|
35
|
+
return true;
|
|
36
|
+
}
|
|
37
|
+
setReason(r.reason);
|
|
38
|
+
return false;
|
|
39
|
+
}
|
|
40
|
+
finally {
|
|
41
|
+
if (mounted.current)
|
|
42
|
+
setLoading(false);
|
|
43
|
+
}
|
|
44
|
+
}, [ethicly]);
|
|
45
|
+
const dismiss = (0, react_1.useCallback)(() => {
|
|
46
|
+
var _a;
|
|
47
|
+
void ((_a = currentRef.current) === null || _a === void 0 ? void 0 : _a.tracker.closed());
|
|
48
|
+
setCurrent(null);
|
|
49
|
+
if (preload)
|
|
50
|
+
void ethicly.preload();
|
|
51
|
+
}, [ethicly, preload]);
|
|
52
|
+
const canShow = (0, react_1.useCallback)(() => ethicly.canShow(), [ethicly]);
|
|
53
|
+
return { ad: (_a = current === null || current === void 0 ? void 0 : current.ad) !== null && _a !== void 0 ? _a : null, tracker: (_b = current === null || current === void 0 ? void 0 : current.tracker) !== null && _b !== void 0 ? _b : null, reason, loading, show, dismiss, canShow };
|
|
54
|
+
}
|
|
@@ -0,0 +1,276 @@
|
|
|
1
|
+
// Default in-memory adapters and helpers that wrap the usual Expo modules.
|
|
2
|
+
// Nothing here imports a native module: the app passes the module in.
|
|
3
|
+
/** In-memory key-value store. Lost when the process ends. */
|
|
4
|
+
export function createMemoryStorage() {
|
|
5
|
+
const data = new Map();
|
|
6
|
+
return {
|
|
7
|
+
data,
|
|
8
|
+
async getItem(k) { return data.has(k) ? data.get(k) : null; },
|
|
9
|
+
async setItem(k, v) { data.set(k, v); },
|
|
10
|
+
async removeItem(k) { data.delete(k); },
|
|
11
|
+
};
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* In-memory "cache" that downloads nothing: the uri it returns is the remote
|
|
15
|
+
* URL, so the player streams. The default when no fileCache is given.
|
|
16
|
+
*/
|
|
17
|
+
export function createMemoryFileCache() {
|
|
18
|
+
const files = new Set();
|
|
19
|
+
return {
|
|
20
|
+
async download(url) { files.add(url); return { uri: url, bytes: 0 }; },
|
|
21
|
+
async exists(uri) { return files.has(uri); },
|
|
22
|
+
async delete(uri) { files.delete(uri); },
|
|
23
|
+
async size() { return 0; },
|
|
24
|
+
async list() { return Array.from(files); },
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Creative cache on disk via expo-file-system. Pass the module as your app
|
|
29
|
+
* imports it, on any Expo SDK from 50; the adapter works out which API it has:
|
|
30
|
+
* import * as FileSystem from 'expo-file-system'; // or 'expo-file-system/legacy'
|
|
31
|
+
* createEthicly({ ..., fileCache: createExpoFileCache(FileSystem) })
|
|
32
|
+
*/
|
|
33
|
+
export const DEFAULT_CACHE_SUBDIRECTORY = 'ethicly-ads/';
|
|
34
|
+
/**
|
|
35
|
+
* The cache sweep deletes every file in this directory that the SDK's index
|
|
36
|
+
* does not list, so it must be a folder of its own: one plain name directly
|
|
37
|
+
* under the app's cache directory. An empty, nested or traversing name ('',
|
|
38
|
+
* '/', '..', 'a/b') would point the sweep at the host app's files, so anything
|
|
39
|
+
* else falls back to the default rather than risk deleting them.
|
|
40
|
+
*/
|
|
41
|
+
export function safeCacheSubdirectory(subdirectory) {
|
|
42
|
+
if (typeof subdirectory !== 'string')
|
|
43
|
+
return DEFAULT_CACHE_SUBDIRECTORY;
|
|
44
|
+
const name = subdirectory.trim().replace(/\/+$/, '');
|
|
45
|
+
if (!/^[A-Za-z0-9][A-Za-z0-9._-]{0,63}$/.test(name) || name === '.' || name.includes('..')) {
|
|
46
|
+
return DEFAULT_CACHE_SUBDIRECTORY;
|
|
47
|
+
}
|
|
48
|
+
return `${name}/`;
|
|
49
|
+
}
|
|
50
|
+
function isModernFs(fs) {
|
|
51
|
+
const m = fs;
|
|
52
|
+
return !!m && typeof m.File === 'function' && typeof m.Directory === 'function'
|
|
53
|
+
&& typeof m.File.downloadFileAsync === 'function' && !!m.Paths;
|
|
54
|
+
}
|
|
55
|
+
function isLegacyFs(fs) {
|
|
56
|
+
const l = fs;
|
|
57
|
+
return !!l && typeof l.cacheDirectory === 'string' && !!l.cacheDirectory
|
|
58
|
+
&& typeof l.downloadAsync === 'function' && typeof l.getInfoAsync === 'function'
|
|
59
|
+
&& typeof l.deleteAsync === 'function' && typeof l.makeDirectoryAsync === 'function';
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* A file cache over either expo-file-system API: the object API of SDK 54+
|
|
63
|
+
* (`File`, `Directory`, `Paths`), else the legacy functions (`getInfoAsync`,
|
|
64
|
+
* `downloadAsync`, ...). Anything else gets the streaming cache, which writes
|
|
65
|
+
* nothing to disk, and one warning through `logger` (default `console`).
|
|
66
|
+
*/
|
|
67
|
+
export function createExpoFileCache(fs, subdirectory = DEFAULT_CACHE_SUBDIRECTORY, logger) {
|
|
68
|
+
const sub = safeCacheSubdirectory(subdirectory);
|
|
69
|
+
if (isModernFs(fs))
|
|
70
|
+
return modernFileCache(fs, sub);
|
|
71
|
+
if (isLegacyFs(fs))
|
|
72
|
+
return legacyFileCache(fs, sub);
|
|
73
|
+
safeLogger(logger || consoleLogger()).warn('createExpoFileCache: not an expo-file-system module (no File/Directory/Paths, no cacheDirectory); creatives will stream');
|
|
74
|
+
return createMemoryFileCache();
|
|
75
|
+
}
|
|
76
|
+
function consoleLogger() {
|
|
77
|
+
const c = globalThis.console;
|
|
78
|
+
return { warn: (m, ...rest) => { var _a; return (_a = c === null || c === void 0 ? void 0 : c.warn) === null || _a === void 0 ? void 0 : _a.call(c, m, ...rest); }, error: (m, ...rest) => { var _a; return (_a = c === null || c === void 0 ? void 0 : c.error) === null || _a === void 0 ? void 0 : _a.call(c, m, ...rest); } };
|
|
79
|
+
}
|
|
80
|
+
function legacyFileCache(fs, sub) {
|
|
81
|
+
const dir = `${fs.cacheDirectory}${sub}`;
|
|
82
|
+
let made = null;
|
|
83
|
+
const sizeOf = async (uri) => {
|
|
84
|
+
const info = await fs.getInfoAsync(uri, { size: true });
|
|
85
|
+
return info.exists && typeof info.size === 'number' ? info.size : 0;
|
|
86
|
+
};
|
|
87
|
+
return {
|
|
88
|
+
async download(url, fileName, opts) {
|
|
89
|
+
if (!made)
|
|
90
|
+
made = fs.makeDirectoryAsync(dir, { intermediates: true }).catch(() => undefined);
|
|
91
|
+
await made;
|
|
92
|
+
const target = dir + fileName;
|
|
93
|
+
const max = opts === null || opts === void 0 ? void 0 : opts.maxBytes;
|
|
94
|
+
let res;
|
|
95
|
+
let tooBig = false;
|
|
96
|
+
if (max !== undefined && fs.createDownloadResumable) {
|
|
97
|
+
// Stop as soon as the server announces, or we have written, more than the budget.
|
|
98
|
+
const dl = fs.createDownloadResumable(url, target, {}, (p) => {
|
|
99
|
+
if (tooBig)
|
|
100
|
+
return;
|
|
101
|
+
if (p.totalBytesExpectedToWrite > max || p.totalBytesWritten > max) {
|
|
102
|
+
tooBig = true;
|
|
103
|
+
void (dl.cancelAsync ? dl.cancelAsync() : dl.pauseAsync()).catch(() => undefined);
|
|
104
|
+
}
|
|
105
|
+
});
|
|
106
|
+
res = await dl.downloadAsync().catch(() => undefined);
|
|
107
|
+
}
|
|
108
|
+
else {
|
|
109
|
+
res = await fs.downloadAsync(url, target);
|
|
110
|
+
}
|
|
111
|
+
if (tooBig || !res || res.status < 200 || res.status >= 300) {
|
|
112
|
+
await fs.deleteAsync(target, { idempotent: true }).catch(() => undefined);
|
|
113
|
+
throw new Error(tooBig ? 'creative over the cache budget' : `download failed (${res ? res.status : 'cancelled'})`);
|
|
114
|
+
}
|
|
115
|
+
return { uri: res.uri, bytes: await sizeOf(res.uri) };
|
|
116
|
+
},
|
|
117
|
+
async exists(uri) { return (await fs.getInfoAsync(uri)).exists; },
|
|
118
|
+
async delete(uri) { await fs.deleteAsync(uri, { idempotent: true }); },
|
|
119
|
+
size: sizeOf,
|
|
120
|
+
uriFor: (fileName) => dir + fileName,
|
|
121
|
+
async list() {
|
|
122
|
+
if (!fs.readDirectoryAsync)
|
|
123
|
+
return [];
|
|
124
|
+
const names = await fs.readDirectoryAsync(dir).catch(() => []);
|
|
125
|
+
return names.map((n) => dir + n);
|
|
126
|
+
},
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* The same cache over the object API. Its calls are synchronous and throw on
|
|
131
|
+
* failure; every adapter method is async, so a throw becomes a rejection and
|
|
132
|
+
* never reaches the host. Files are addressed by uri strings built from the
|
|
133
|
+
* cache directory's uri, so the index and the sweep compare like with like.
|
|
134
|
+
*/
|
|
135
|
+
function modernFileCache(fs, sub) {
|
|
136
|
+
let dirUri = null;
|
|
137
|
+
const dir = () => {
|
|
138
|
+
if (dirUri === null) {
|
|
139
|
+
const cache = fs.Paths.cache.uri;
|
|
140
|
+
dirUri = `${cache.endsWith('/') ? cache : `${cache}/`}${sub}`;
|
|
141
|
+
}
|
|
142
|
+
return dirUri;
|
|
143
|
+
};
|
|
144
|
+
const file = (uri) => new fs.File(uri);
|
|
145
|
+
const remove = (uri) => {
|
|
146
|
+
const f = file(uri);
|
|
147
|
+
if (f.exists)
|
|
148
|
+
f.delete();
|
|
149
|
+
};
|
|
150
|
+
const sizeOf = async (uri) => {
|
|
151
|
+
const f = file(uri);
|
|
152
|
+
return f.exists && typeof f.size === 'number' && f.size > 0 ? f.size : 0;
|
|
153
|
+
};
|
|
154
|
+
let made = false;
|
|
155
|
+
return {
|
|
156
|
+
async download(url, fileName, opts) {
|
|
157
|
+
if (!made) {
|
|
158
|
+
const d = new fs.Directory(dir());
|
|
159
|
+
if (!d.exists)
|
|
160
|
+
d.create({ intermediates: true, idempotent: true });
|
|
161
|
+
made = true;
|
|
162
|
+
}
|
|
163
|
+
const target = dir() + fileName;
|
|
164
|
+
const max = opts === null || opts === void 0 ? void 0 : opts.maxBytes;
|
|
165
|
+
let tooBig = false;
|
|
166
|
+
try {
|
|
167
|
+
remove(target);
|
|
168
|
+
const createTask = fs.File.createDownloadTask;
|
|
169
|
+
if (max !== undefined && typeof createTask === 'function') {
|
|
170
|
+
// Stop as soon as the server announces, or we have written, more than the budget.
|
|
171
|
+
const task = createTask.call(fs.File, url, file(target), {
|
|
172
|
+
onProgress: (p) => {
|
|
173
|
+
if (tooBig || !(p.totalBytes > max || p.bytesWritten > max))
|
|
174
|
+
return;
|
|
175
|
+
tooBig = true;
|
|
176
|
+
try {
|
|
177
|
+
task.cancel();
|
|
178
|
+
}
|
|
179
|
+
catch { /* already settled */ }
|
|
180
|
+
},
|
|
181
|
+
});
|
|
182
|
+
await task.downloadAsync();
|
|
183
|
+
}
|
|
184
|
+
else {
|
|
185
|
+
await fs.File.downloadFileAsync(url, file(target), { idempotent: true });
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
catch (e) {
|
|
189
|
+
try {
|
|
190
|
+
remove(target);
|
|
191
|
+
}
|
|
192
|
+
catch { /* nothing to remove */ }
|
|
193
|
+
throw tooBig ? new Error('creative over the cache budget') : e;
|
|
194
|
+
}
|
|
195
|
+
// Without a progress callback the whole file lands first: check it now
|
|
196
|
+
// and delete it if it is over the budget.
|
|
197
|
+
const bytes = await sizeOf(target);
|
|
198
|
+
if (tooBig || (max !== undefined && bytes > max)) {
|
|
199
|
+
try {
|
|
200
|
+
remove(target);
|
|
201
|
+
}
|
|
202
|
+
catch { /* ignore */ }
|
|
203
|
+
throw new Error('creative over the cache budget');
|
|
204
|
+
}
|
|
205
|
+
return { uri: target, bytes };
|
|
206
|
+
},
|
|
207
|
+
async exists(uri) { return file(uri).exists; },
|
|
208
|
+
async delete(uri) { remove(uri); },
|
|
209
|
+
size: sizeOf,
|
|
210
|
+
uriFor: (fileName) => dir() + fileName,
|
|
211
|
+
async list() {
|
|
212
|
+
const d = new fs.Directory(dir());
|
|
213
|
+
if (!d.exists)
|
|
214
|
+
return [];
|
|
215
|
+
// Files only: the SDK never creates folders here, so it never deletes one.
|
|
216
|
+
return d.list()
|
|
217
|
+
.filter((e) => e instanceof fs.File)
|
|
218
|
+
.map((e) => e.name || e.uri.replace(/\/+$/, '').split('/').pop() || '')
|
|
219
|
+
.filter((n) => n && n !== '.' && n !== '..' && !n.includes('/'))
|
|
220
|
+
.map((n) => dir() + n);
|
|
221
|
+
},
|
|
222
|
+
};
|
|
223
|
+
}
|
|
224
|
+
/**
|
|
225
|
+
* True only in a React Native release build (`__DEV__ === false`). Where
|
|
226
|
+
* `__DEV__` is not defined at all the code is not running from a Metro bundle
|
|
227
|
+
* (a Node script such as scripts/live-test.mjs, or tests), which is never a
|
|
228
|
+
* shipped app, so that counts as not release.
|
|
229
|
+
*/
|
|
230
|
+
export function isReleaseBuild() {
|
|
231
|
+
return typeof __DEV__ !== 'undefined' && __DEV__ === false;
|
|
232
|
+
}
|
|
233
|
+
/**
|
|
234
|
+
* React Native's AppState, or null outside React Native. The core takes no
|
|
235
|
+
* hard dependency on react-native: a require inside try/catch is an optional
|
|
236
|
+
* dependency for Metro (the React Native and Expo Metro configs both set
|
|
237
|
+
* allowOptionalDependencies), and in Node it simply fails and returns null.
|
|
238
|
+
*/
|
|
239
|
+
export function defaultAppState() {
|
|
240
|
+
try {
|
|
241
|
+
// @ts-ignore: the build compiles without Node's types, where require is undeclared.
|
|
242
|
+
const rn = typeof require === 'function' ? require('react-native') : null;
|
|
243
|
+
const s = rn && rn.AppState;
|
|
244
|
+
return s && typeof s.addEventListener === 'function' ? s : null;
|
|
245
|
+
}
|
|
246
|
+
catch {
|
|
247
|
+
return null;
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
/** Wrap a logger so a throwing logger can never reach the host app. */
|
|
251
|
+
export function safeLogger(logger) {
|
|
252
|
+
const call = (level) => (message, ...rest) => {
|
|
253
|
+
var _a;
|
|
254
|
+
try {
|
|
255
|
+
(_a = logger === null || logger === void 0 ? void 0 : logger[level]) === null || _a === void 0 ? void 0 : _a.call(logger, `[ethicly] ${message}`, ...rest);
|
|
256
|
+
}
|
|
257
|
+
catch { /* never throw */ }
|
|
258
|
+
};
|
|
259
|
+
return { debug: call('debug'), info: call('info'), warn: call('warn'), error: call('error') };
|
|
260
|
+
}
|
|
261
|
+
/** UUID v4. Uses the platform RNG when there is one (Hermes has none by default). */
|
|
262
|
+
export function uuidv4() {
|
|
263
|
+
const c = globalThis.crypto;
|
|
264
|
+
if (c && typeof c.randomUUID === 'function')
|
|
265
|
+
return c.randomUUID();
|
|
266
|
+
const b = new Uint8Array(16);
|
|
267
|
+
if (c && typeof c.getRandomValues === 'function')
|
|
268
|
+
c.getRandomValues(b);
|
|
269
|
+
else
|
|
270
|
+
for (let i = 0; i < 16; i++)
|
|
271
|
+
b[i] = Math.floor(Math.random() * 256);
|
|
272
|
+
b[6] = (b[6] & 0x0f) | 0x40;
|
|
273
|
+
b[8] = (b[8] & 0x3f) | 0x80;
|
|
274
|
+
const h = Array.from(b, (x) => x.toString(16).padStart(2, '0')).join('');
|
|
275
|
+
return `${h.slice(0, 8)}-${h.slice(8, 12)}-${h.slice(12, 16)}-${h.slice(16, 20)}-${h.slice(20)}`;
|
|
276
|
+
}
|