@ethicly/react-native 0.0.0-stage → 0.2.1
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 +133 -0
- package/LICENSE +21 -0
- package/README.md +428 -2
- package/core/package.json +6 -0
- package/interstitial/package.json +6 -0
- package/lib/commonjs/core/adapters.js +326 -0
- package/lib/commonjs/core/beacons.js +70 -0
- package/lib/commonjs/core/cache.js +261 -0
- package/lib/commonjs/core/click.js +29 -0
- package/lib/commonjs/core/client.js +585 -0
- package/lib/commonjs/core/config.js +128 -0
- package/lib/commonjs/core/http.js +115 -0
- package/lib/commonjs/core/index.js +21 -0
- package/lib/commonjs/core/outbox.js +192 -0
- package/lib/commonjs/core/tracker.js +379 -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 +319 -0
- package/lib/commonjs/react/useEthiclyInterstitial.js +54 -0
- package/lib/commonjs/version.js +5 -0
- package/lib/module/core/adapters.js +315 -0
- package/lib/module/core/beacons.js +64 -0
- package/lib/module/core/cache.js +257 -0
- package/lib/module/core/click.js +24 -0
- package/lib/module/core/client.js +581 -0
- package/lib/module/core/config.js +118 -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 +375 -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 +292 -0
- package/lib/module/react/useEthiclyInterstitial.js +51 -0
- package/lib/module/version.js +2 -0
- package/lib/typescript/core/adapters.d.ts +139 -0
- package/lib/typescript/core/beacons.d.ts +32 -0
- package/lib/typescript/core/cache.d.ts +71 -0
- package/lib/typescript/core/click.d.ts +13 -0
- package/lib/typescript/core/client.d.ts +78 -0
- package/lib/typescript/core/config.d.ts +33 -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 +175 -0
- package/lib/typescript/index.d.ts +3 -0
- package/lib/typescript/interstitial.d.ts +2 -0
- package/lib/typescript/react/EthiclyInterstitial.d.ts +51 -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 +365 -0
- package/src/core/beacons.ts +81 -0
- package/src/core/cache.ts +261 -0
- package/src/core/click.ts +26 -0
- package/src/core/client.ts +665 -0
- package/src/core/config.ts +124 -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 +376 -0
- package/src/core/types.ts +185 -0
- package/src/index.ts +5 -0
- package/src/interstitial.ts +4 -0
- package/src/react/EthiclyInterstitial.tsx +432 -0
- package/src/react/useEthiclyInterstitial.ts +74 -0
- package/src/version.ts +2 -0
|
@@ -0,0 +1,432 @@
|
|
|
1
|
+
// Optional ready-made fullscreen screen. Needs expo-video (Expo SDK 52+).
|
|
2
|
+
// Layout follows a typical in-app video player: 9:16 video, a skip control that counts
|
|
3
|
+
// down then reads "Skip Ad", a white call-to-action button, an optional
|
|
4
|
+
// "Remove ads" slot and a small "Sponsored by Ethicly.ch" line under it.
|
|
5
|
+
// Video starts with sound unless the effective audio (the `audio` prop, else
|
|
6
|
+
// ad.audio: createEthicly's `audio` option, else /config) is 'none' or 'optional'.
|
|
7
|
+
// The controls keep clear of the notch, status bar and home indicator: see
|
|
8
|
+
// useSafeInsets below.
|
|
9
|
+
|
|
10
|
+
import React, { useCallback, useContext, useEffect, useRef, useState } from 'react';
|
|
11
|
+
import {
|
|
12
|
+
AppState, BackHandler, Image, Modal, Platform, Pressable, StatusBar, StyleSheet, Text, View,
|
|
13
|
+
type AppStateStatus, type StyleProp, type TextStyle, type ViewStyle,
|
|
14
|
+
} from 'react-native';
|
|
15
|
+
import { useVideoPlayer, VideoView } from 'expo-video';
|
|
16
|
+
import type { AudioPolicy, EthiclyAd, Tracker } from '../core/types';
|
|
17
|
+
|
|
18
|
+
/** Safe-area insets in points, as react-native-safe-area-context reports them. */
|
|
19
|
+
export interface EthiclyInsets { top: number; bottom: number; left: number; right: number }
|
|
20
|
+
|
|
21
|
+
export interface EthiclyInterstitialProps {
|
|
22
|
+
ad: EthiclyAd;
|
|
23
|
+
tracker: Tracker;
|
|
24
|
+
/** Called after the ad is skipped or closed. Hide the screen here. */
|
|
25
|
+
onClose(): void;
|
|
26
|
+
/** Shows a "Remove ads" link under the button that calls this. */
|
|
27
|
+
onRemoveAds?(): void;
|
|
28
|
+
removeAdsLabel?: string;
|
|
29
|
+
/** Replaces the "Remove ads" link with anything the app wants. */
|
|
30
|
+
renderBelowButton?(): React.ReactNode;
|
|
31
|
+
/** Default true: wraps the screen in a fullscreen Modal. */
|
|
32
|
+
modal?: boolean;
|
|
33
|
+
/**
|
|
34
|
+
* Overrides for the skip control, merged over the default: a solid black
|
|
35
|
+
* pill reading "Skip in N", then "Skip Ad" with a skip-to-end icon.
|
|
36
|
+
*/
|
|
37
|
+
skipStyle?: StyleProp<ViewStyle>;
|
|
38
|
+
/** Overrides for the skip label (default white, "Skip in N" then "Skip Ad"). */
|
|
39
|
+
skipTextStyle?: StyleProp<TextStyle>;
|
|
40
|
+
/**
|
|
41
|
+
* Safe-area insets, for example `useSafeAreaInsets()`. Optional: without
|
|
42
|
+
* them the screen reads react-native-safe-area-context itself when the app
|
|
43
|
+
* has it, else the status bar height on Android. The controls never sit
|
|
44
|
+
* higher or lower than in 0.1 (56 from the top, 44 from the bottom); insets
|
|
45
|
+
* only move them inward.
|
|
46
|
+
*/
|
|
47
|
+
insets?: EthiclyInsets;
|
|
48
|
+
/**
|
|
49
|
+
* Sound for this screen, over `ad.audio` (createEthicly's `audio` option,
|
|
50
|
+
* else the server's interstitial.audio, else 'on'). 'on' plays with sound,
|
|
51
|
+
* 'optional' starts muted with a sound toggle, 'none' plays muted with no toggle.
|
|
52
|
+
* With expo-video on iPhone, sound plays even with the silent switch on and
|
|
53
|
+
* pauses the user's own audio.
|
|
54
|
+
*/
|
|
55
|
+
audio?: AudioPolicy;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** The sound a screen plays with: the prop, else ad.audio, else 'on'. */
|
|
59
|
+
export function effectiveAudio(prop: AudioPolicy | undefined, ad: { audio?: AudioPolicy }): AudioPolicy {
|
|
60
|
+
for (const v of [prop, ad.audio]) if (v === 'on' || v === 'optional' || v === 'none') return v;
|
|
61
|
+
return 'on';
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
const TICK_MS = 250;
|
|
65
|
+
|
|
66
|
+
/** Space between the safe area and the controls. */
|
|
67
|
+
const TOP_GAP = 12;
|
|
68
|
+
const BOTTOM_GAP = 10;
|
|
69
|
+
const SIDE_GAP_TOP = 16;
|
|
70
|
+
const SIDE_GAP_BOTTOM = 20;
|
|
71
|
+
/**
|
|
72
|
+
* The 0.1 layout (controls 56 from the top, 44 from the bottom) is a floor:
|
|
73
|
+
* insets only ever move the controls inward, so a phone without a notch or a
|
|
74
|
+
* tall status bar looks exactly as before.
|
|
75
|
+
*/
|
|
76
|
+
const MIN_TOP = 56;
|
|
77
|
+
const MIN_BOTTOM = 44;
|
|
78
|
+
|
|
79
|
+
interface SafeAreaModule {
|
|
80
|
+
SafeAreaInsetsContext?: React.Context<EthiclyInsets | null>;
|
|
81
|
+
initialWindowMetrics?: { insets?: EthiclyInsets } | null;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* react-native-safe-area-context when the app has it, else null. Metro treats
|
|
86
|
+
* a require inside try/catch as an optional dependency (the React Native and
|
|
87
|
+
* Expo Metro configs set allowOptionalDependencies), so an app without the
|
|
88
|
+
* package still builds.
|
|
89
|
+
*/
|
|
90
|
+
function loadSafeArea(): SafeAreaModule | null {
|
|
91
|
+
try {
|
|
92
|
+
// @ts-ignore: the build compiles without Node's types, where require is undeclared.
|
|
93
|
+
return typeof require === 'function' ? (require('react-native-safe-area-context') as SafeAreaModule) : null;
|
|
94
|
+
} catch {
|
|
95
|
+
return null;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
const safeArea = loadSafeArea();
|
|
100
|
+
const noInsets = React.createContext<EthiclyInsets | null>(null);
|
|
101
|
+
// Read with useContext, which returns null without a SafeAreaProvider instead
|
|
102
|
+
// of throwing like useSafeAreaInsets(). Fixed at load, so hook order never changes.
|
|
103
|
+
const InsetsContext: React.Context<EthiclyInsets | null> =
|
|
104
|
+
safeArea && safeArea.SafeAreaInsetsContext && typeof safeArea.SafeAreaInsetsContext === 'object'
|
|
105
|
+
? safeArea.SafeAreaInsetsContext
|
|
106
|
+
: noInsets;
|
|
107
|
+
|
|
108
|
+
function validInsets(v: unknown): EthiclyInsets | null {
|
|
109
|
+
const i = v as Partial<EthiclyInsets> | null | undefined;
|
|
110
|
+
if (!i || typeof i !== 'object') return null;
|
|
111
|
+
const n = (x: unknown) => (typeof x === 'number' && Number.isFinite(x) && x >= 0 ? x : 0);
|
|
112
|
+
if (typeof i.top !== 'number' && typeof i.bottom !== 'number') return null;
|
|
113
|
+
return { top: n(i.top), bottom: n(i.bottom), left: n(i.left), right: n(i.right) };
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/** The `insets` prop, else the provider's insets, else the window's first metrics, else the status bar. */
|
|
117
|
+
function useSafeInsets(prop: EthiclyInsets | undefined): EthiclyInsets {
|
|
118
|
+
const fromProvider = useContext(InsetsContext);
|
|
119
|
+
return validInsets(prop)
|
|
120
|
+
?? validInsets(fromProvider)
|
|
121
|
+
?? validInsets(safeArea?.initialWindowMetrics?.insets)
|
|
122
|
+
?? { top: Platform.OS === 'android' ? StatusBar.currentHeight ?? 0 : 0, bottom: 0, left: 0, right: 0 };
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
export function EthiclyInterstitial(props: EthiclyInterstitialProps): React.ReactElement {
|
|
126
|
+
// Android back: a Modal receives it through onRequestClose, an inline screen
|
|
127
|
+
// through BackHandler. Either way it closes the ad only once Skip is allowed,
|
|
128
|
+
// and is swallowed before that.
|
|
129
|
+
const backRef = useRef<(() => void) | null>(null);
|
|
130
|
+
const inline = props.modal === false;
|
|
131
|
+
useEffect(() => {
|
|
132
|
+
if (!inline) return undefined;
|
|
133
|
+
const sub = BackHandler.addEventListener('hardwareBackPress', () => {
|
|
134
|
+
backRef.current?.();
|
|
135
|
+
return true;
|
|
136
|
+
});
|
|
137
|
+
return () => sub.remove();
|
|
138
|
+
}, [inline]);
|
|
139
|
+
|
|
140
|
+
const body = <InterstitialBody {...props} backRef={backRef} />;
|
|
141
|
+
if (inline) return body;
|
|
142
|
+
return (
|
|
143
|
+
<Modal visible animationType="fade" presentationStyle="fullScreen" statusBarTranslucent onRequestClose={() => backRef.current?.()}>
|
|
144
|
+
{body}
|
|
145
|
+
</Modal>
|
|
146
|
+
);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
function useAppActive(): boolean {
|
|
150
|
+
const [active, setActive] = useState(AppState.currentState === 'active');
|
|
151
|
+
useEffect(() => {
|
|
152
|
+
const sub = AppState.addEventListener('change', (s: AppStateStatus) => setActive(s === 'active'));
|
|
153
|
+
return () => sub.remove();
|
|
154
|
+
}, []);
|
|
155
|
+
return active;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
interface BodyProps extends EthiclyInterstitialProps {
|
|
159
|
+
backRef: React.MutableRefObject<(() => void) | null>;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
function InterstitialBody({
|
|
163
|
+
ad, tracker, onClose, onRemoveAds, removeAdsLabel, renderBelowButton, skipStyle, skipTextStyle, insets, backRef, audio: audioProp,
|
|
164
|
+
}: BodyProps) {
|
|
165
|
+
const audio = effectiveAudio(audioProp, ad);
|
|
166
|
+
const safe = useSafeInsets(insets);
|
|
167
|
+
const isVideo = ad.media.kind === 'video' && !!ad.media.videoUri;
|
|
168
|
+
const appActive = useAppActive();
|
|
169
|
+
const activeRef = useRef(appActive);
|
|
170
|
+
activeRef.current = appActive;
|
|
171
|
+
|
|
172
|
+
const [positionMs, setPositionMs] = useState(0);
|
|
173
|
+
const [ended, setEnded] = useState(false);
|
|
174
|
+
const [failed, setFailed] = useState(false);
|
|
175
|
+
const [muted, setMuted] = useState(audio !== 'on');
|
|
176
|
+
/**
|
|
177
|
+
* A still image counts as visible only once it has loaded: an image that
|
|
178
|
+
* never arrives is never billed as viewable.
|
|
179
|
+
*/
|
|
180
|
+
const imageLoadedRef = useRef(false);
|
|
181
|
+
/** Wall-clock time on screen with the app in the foreground. */
|
|
182
|
+
const [onScreenMs, setOnScreenMs] = useState(0);
|
|
183
|
+
const [clicked, setClicked] = useState(false);
|
|
184
|
+
const positionRef = useRef(0);
|
|
185
|
+
const closedRef = useRef(false);
|
|
186
|
+
const failedRef = useRef(false);
|
|
187
|
+
|
|
188
|
+
const player = useVideoPlayer(isVideo ? ad.media.videoUri : null, (p) => {
|
|
189
|
+
p.loop = false;
|
|
190
|
+
p.muted = audio !== 'on';
|
|
191
|
+
p.timeUpdateEventInterval = TICK_MS / 1000;
|
|
192
|
+
p.play();
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
const onCloseRef = useRef(onClose);
|
|
196
|
+
onCloseRef.current = onClose;
|
|
197
|
+
|
|
198
|
+
// The impression goes out the moment the ad is on screen. shown() refuses an
|
|
199
|
+
// ad past its token lifetime (nothing is recorded), so the screen closes
|
|
200
|
+
// itself instead of playing an ad nobody is paid for.
|
|
201
|
+
useEffect(() => {
|
|
202
|
+
const expired = tracker.isExpired();
|
|
203
|
+
void tracker.shown();
|
|
204
|
+
if (expired && !closedRef.current) {
|
|
205
|
+
closedRef.current = true;
|
|
206
|
+
void tracker.closed();
|
|
207
|
+
onCloseRef.current();
|
|
208
|
+
}
|
|
209
|
+
}, [tracker]);
|
|
210
|
+
|
|
211
|
+
// Video: playback position drives viewability, quartiles and the skip countdown.
|
|
212
|
+
useEffect(() => {
|
|
213
|
+
if (!isVideo) return undefined;
|
|
214
|
+
const subs = [
|
|
215
|
+
player.addListener('timeUpdate', ({ currentTime }) => {
|
|
216
|
+
const ms = Math.max(0, currentTime * 1000);
|
|
217
|
+
positionRef.current = ms;
|
|
218
|
+
setPositionMs(ms);
|
|
219
|
+
const durationMs = player.duration > 0 ? player.duration * 1000 : undefined;
|
|
220
|
+
tracker.progress(ms, { visible: activeRef.current && player.playing, durationMs });
|
|
221
|
+
}),
|
|
222
|
+
player.addListener('playToEnd', () => {
|
|
223
|
+
setEnded(true);
|
|
224
|
+
tracker.complete();
|
|
225
|
+
}),
|
|
226
|
+
player.addListener('statusChange', ({ status }) => {
|
|
227
|
+
if (status === 'error' && !failedRef.current) {
|
|
228
|
+
failedRef.current = true;
|
|
229
|
+
setFailed(true);
|
|
230
|
+
tracker.error();
|
|
231
|
+
}
|
|
232
|
+
}),
|
|
233
|
+
];
|
|
234
|
+
return () => subs.forEach((s) => s.remove());
|
|
235
|
+
}, [isVideo, player, tracker]);
|
|
236
|
+
|
|
237
|
+
// Time on screen while the app is in the foreground: the skip countdown, so
|
|
238
|
+
// Skip appears after skipAfterMs even if the video stalls or the image never
|
|
239
|
+
// loads. For a still image, the time since it loaded is the playback position.
|
|
240
|
+
useEffect(() => {
|
|
241
|
+
let shownMs = 0;
|
|
242
|
+
let loadedMs = 0;
|
|
243
|
+
const id = setInterval(() => {
|
|
244
|
+
if (activeRef.current) shownMs += TICK_MS;
|
|
245
|
+
setOnScreenMs(shownMs);
|
|
246
|
+
if (isVideo || !imageLoadedRef.current || failedRef.current) return;
|
|
247
|
+
if (activeRef.current) loadedMs += TICK_MS;
|
|
248
|
+
positionRef.current = loadedMs;
|
|
249
|
+
setPositionMs(loadedMs);
|
|
250
|
+
tracker.progress(loadedMs, { visible: activeRef.current });
|
|
251
|
+
}, TICK_MS);
|
|
252
|
+
return () => clearInterval(id);
|
|
253
|
+
}, [isVideo, tracker]);
|
|
254
|
+
|
|
255
|
+
const onImageLoad = useCallback(() => {
|
|
256
|
+
imageLoadedRef.current = true;
|
|
257
|
+
}, []);
|
|
258
|
+
|
|
259
|
+
const onImageError = useCallback(() => {
|
|
260
|
+
if (failedRef.current) return;
|
|
261
|
+
failedRef.current = true;
|
|
262
|
+
setFailed(true);
|
|
263
|
+
tracker.error();
|
|
264
|
+
}, [tracker]);
|
|
265
|
+
|
|
266
|
+
useEffect(() => {
|
|
267
|
+
if (!isVideo) return;
|
|
268
|
+
if (appActive && !ended) player.play();
|
|
269
|
+
else player.pause();
|
|
270
|
+
}, [appActive, ended, isVideo, player]);
|
|
271
|
+
|
|
272
|
+
// Leaving the foreground ends continuous visibility right away, without
|
|
273
|
+
// waiting for the next player tick (which may never come while paused).
|
|
274
|
+
useEffect(() => {
|
|
275
|
+
if (appActive) return;
|
|
276
|
+
if (!isVideo && (!imageLoadedRef.current || failedRef.current)) return;
|
|
277
|
+
const durationMs = isVideo && player.duration > 0 ? player.duration * 1000 : undefined;
|
|
278
|
+
tracker.progress(positionRef.current, { visible: false, durationMs });
|
|
279
|
+
}, [appActive, isVideo, player, tracker]);
|
|
280
|
+
|
|
281
|
+
// The tracker is closed only from close() below (or by the hook's dismiss()),
|
|
282
|
+
// never from an effect cleanup: React StrictMode runs every cleanup once on
|
|
283
|
+
// mount, and a closed tracker drops /viewable and the video events.
|
|
284
|
+
|
|
285
|
+
const close = useCallback(() => {
|
|
286
|
+
if (closedRef.current) return;
|
|
287
|
+
closedRef.current = true;
|
|
288
|
+
if (isVideo && !ended && !failed) tracker.skipped(positionRef.current);
|
|
289
|
+
void tracker.closed();
|
|
290
|
+
onClose();
|
|
291
|
+
}, [ended, failed, isVideo, onClose, tracker]);
|
|
292
|
+
|
|
293
|
+
const onCta = useCallback(() => {
|
|
294
|
+
if (isVideo) player.pause();
|
|
295
|
+
// Once the user has visited the advertiser, they can always close the ad
|
|
296
|
+
// when they come back.
|
|
297
|
+
setClicked(true);
|
|
298
|
+
void tracker.clicked();
|
|
299
|
+
}, [isVideo, player, tracker]);
|
|
300
|
+
|
|
301
|
+
const toggleSound = useCallback(() => {
|
|
302
|
+
setMuted((m) => {
|
|
303
|
+
player.muted = !m;
|
|
304
|
+
return !m;
|
|
305
|
+
});
|
|
306
|
+
}, [player]);
|
|
307
|
+
|
|
308
|
+
const remainingS = Math.ceil(Math.max(0, ad.skipAfterMs - Math.max(positionMs, onScreenMs)) / 1000);
|
|
309
|
+
const canSkip = remainingS === 0 || ended || failed || clicked;
|
|
310
|
+
backRef.current = canSkip ? close : null;
|
|
311
|
+
|
|
312
|
+
return (
|
|
313
|
+
<View style={styles.root}>
|
|
314
|
+
{isVideo ? (
|
|
315
|
+
<>
|
|
316
|
+
{ad.media.posterUri ? <Image source={{ uri: ad.media.posterUri }} style={StyleSheet.absoluteFill} resizeMode="cover" /> : null}
|
|
317
|
+
{/* No native controls, so no fullscreen button: no fullscreen prop is needed (allowsFullscreen is deprecated). */}
|
|
318
|
+
<VideoView player={player} style={StyleSheet.absoluteFill} contentFit="cover" nativeControls={false} allowsPictureInPicture={false} />
|
|
319
|
+
</>
|
|
320
|
+
) : (
|
|
321
|
+
<Image
|
|
322
|
+
source={{ uri: ad.media.uri }}
|
|
323
|
+
style={StyleSheet.absoluteFill}
|
|
324
|
+
resizeMode="cover"
|
|
325
|
+
onLoad={onImageLoad}
|
|
326
|
+
onError={onImageError}
|
|
327
|
+
/>
|
|
328
|
+
)}
|
|
329
|
+
|
|
330
|
+
<View
|
|
331
|
+
style={[styles.topRow, { top: Math.max(safe.top + TOP_GAP, MIN_TOP), left: safe.left + SIDE_GAP_TOP, right: safe.right + SIDE_GAP_TOP }]}
|
|
332
|
+
pointerEvents="box-none"
|
|
333
|
+
>
|
|
334
|
+
<View style={styles.leftGroup}>
|
|
335
|
+
<Text style={styles.adLabel} accessibilityRole="text">Ad</Text>
|
|
336
|
+
{ad.test ? <Text style={[styles.adLabel, styles.testLabel]} accessibilityRole="text">Test ad</Text> : null}
|
|
337
|
+
{isVideo && audio !== 'none' ? (
|
|
338
|
+
<Pressable onPress={toggleSound} style={styles.soundButton} accessibilityRole="button" accessibilityLabel={muted ? 'Turn sound on' : 'Mute'} hitSlop={8}>
|
|
339
|
+
<Text style={styles.soundText}>{muted ? 'Sound off' : 'Sound on'}</Text>
|
|
340
|
+
</Pressable>
|
|
341
|
+
) : null}
|
|
342
|
+
</View>
|
|
343
|
+
<Pressable
|
|
344
|
+
onPress={canSkip ? close : undefined}
|
|
345
|
+
disabled={!canSkip}
|
|
346
|
+
style={[styles.skip, skipStyle]}
|
|
347
|
+
accessibilityRole="button"
|
|
348
|
+
accessibilityLabel={canSkip ? 'Skip ad' : `Skip in ${remainingS} seconds`}
|
|
349
|
+
hitSlop={8}
|
|
350
|
+
>
|
|
351
|
+
{canSkip ? (
|
|
352
|
+
<View style={styles.skipRow}>
|
|
353
|
+
<Text style={[styles.skipText, skipTextStyle]}>Skip Ad</Text>
|
|
354
|
+
<SkipIcon />
|
|
355
|
+
</View>
|
|
356
|
+
) : (
|
|
357
|
+
<Text style={[styles.skipText, styles.skipTextWaiting, skipTextStyle]}>{`Skip in ${remainingS}`}</Text>
|
|
358
|
+
)}
|
|
359
|
+
</Pressable>
|
|
360
|
+
</View>
|
|
361
|
+
|
|
362
|
+
<View
|
|
363
|
+
style={[styles.bottom, {
|
|
364
|
+
bottom: Math.max(safe.bottom + BOTTOM_GAP, MIN_BOTTOM), left: safe.left + SIDE_GAP_BOTTOM, right: safe.right + SIDE_GAP_BOTTOM,
|
|
365
|
+
}]}
|
|
366
|
+
pointerEvents="box-none"
|
|
367
|
+
>
|
|
368
|
+
<Pressable onPress={onCta} style={({ pressed }) => [styles.cta, pressed && styles.ctaPressed]} accessibilityRole="link">
|
|
369
|
+
<Text style={styles.ctaText} numberOfLines={1}>{ad.ctaLabel || 'Learn more'}</Text>
|
|
370
|
+
</Pressable>
|
|
371
|
+
{renderBelowButton ? renderBelowButton() : onRemoveAds ? (
|
|
372
|
+
<Pressable onPress={onRemoveAds} accessibilityRole="button" hitSlop={8}>
|
|
373
|
+
<Text style={styles.removeAds}>{removeAdsLabel || 'Remove ads'}</Text>
|
|
374
|
+
</Pressable>
|
|
375
|
+
) : null}
|
|
376
|
+
<Text style={styles.sponsor}>Sponsored by Ethicly.ch</Text>
|
|
377
|
+
</View>
|
|
378
|
+
</View>
|
|
379
|
+
);
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
/** Skip-to-end mark (a play triangle against a bar), drawn so it never depends on a font glyph. */
|
|
383
|
+
function SkipIcon() {
|
|
384
|
+
return (
|
|
385
|
+
<View style={styles.skipIcon} accessibilityElementsHidden importantForAccessibility="no-hide-descendants">
|
|
386
|
+
<View style={styles.skipTriangle} />
|
|
387
|
+
<View style={styles.skipBar} />
|
|
388
|
+
</View>
|
|
389
|
+
);
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
const styles = StyleSheet.create({
|
|
393
|
+
root: { flex: 1, backgroundColor: '#000' },
|
|
394
|
+
topRow: {
|
|
395
|
+
position: 'absolute',
|
|
396
|
+
flexDirection: 'row', justifyContent: 'space-between', alignItems: 'center',
|
|
397
|
+
},
|
|
398
|
+
leftGroup: { flexDirection: 'row', alignItems: 'center' },
|
|
399
|
+
adLabel: {
|
|
400
|
+
color: '#fff', fontSize: 11, fontWeight: '600', letterSpacing: 0.5,
|
|
401
|
+
backgroundColor: 'rgba(0,0,0,0.45)', paddingHorizontal: 6, paddingVertical: 2, borderRadius: 4, overflow: 'hidden',
|
|
402
|
+
},
|
|
403
|
+
testLabel: { marginLeft: 8, backgroundColor: 'rgba(196,120,0,0.85)' },
|
|
404
|
+
soundButton: { marginLeft: 8, backgroundColor: 'rgba(0,0,0,0.45)', paddingHorizontal: 8, paddingVertical: 4, borderRadius: 6 },
|
|
405
|
+
soundText: { color: '#fff', fontSize: 12, fontWeight: '500' },
|
|
406
|
+
// Solid black, no blur or translucency. The faint hairline keeps the edge
|
|
407
|
+
// visible over a dark frame.
|
|
408
|
+
skip: {
|
|
409
|
+
backgroundColor: '#000', paddingHorizontal: 18, height: 40, borderRadius: 20,
|
|
410
|
+
justifyContent: 'center', alignItems: 'center',
|
|
411
|
+
borderWidth: StyleSheet.hairlineWidth, borderColor: 'rgba(255,255,255,0.22)',
|
|
412
|
+
},
|
|
413
|
+
skipRow: { flexDirection: 'row', alignItems: 'center' },
|
|
414
|
+
skipText: { color: '#fff', fontSize: 15, fontWeight: '500', letterSpacing: 0.2 },
|
|
415
|
+
skipTextWaiting: { opacity: 0.75 },
|
|
416
|
+
skipIcon: { flexDirection: 'row', alignItems: 'center', marginLeft: 10 },
|
|
417
|
+
skipTriangle: {
|
|
418
|
+
width: 0, height: 0,
|
|
419
|
+
borderTopWidth: 6, borderBottomWidth: 6, borderLeftWidth: 10,
|
|
420
|
+
borderTopColor: 'transparent', borderBottomColor: 'transparent', borderLeftColor: '#fff',
|
|
421
|
+
},
|
|
422
|
+
skipBar: { width: 2, height: 12, backgroundColor: '#fff', marginLeft: 2, borderRadius: 1 },
|
|
423
|
+
bottom: { position: 'absolute', alignItems: 'center' },
|
|
424
|
+
cta: {
|
|
425
|
+
alignSelf: 'stretch', backgroundColor: '#fff', borderRadius: 12, paddingVertical: 15,
|
|
426
|
+
alignItems: 'center', justifyContent: 'center',
|
|
427
|
+
},
|
|
428
|
+
ctaPressed: { opacity: 0.85 },
|
|
429
|
+
ctaText: { color: '#000', fontSize: 17, fontWeight: '600' },
|
|
430
|
+
removeAds: { color: '#fff', fontSize: 14, marginTop: 14, textDecorationLine: 'underline', opacity: 0.9 },
|
|
431
|
+
sponsor: { color: '#fff', fontSize: 12, marginTop: 10, opacity: 0.6 },
|
|
432
|
+
});
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
// React hook over the headless core. No React Native or Expo imports, so it
|
|
2
|
+
// works with any player the host app already has.
|
|
3
|
+
|
|
4
|
+
import { useCallback, useEffect, useRef, useState } from 'react';
|
|
5
|
+
import type { Ethicly } from '../core/client';
|
|
6
|
+
import type { EthiclyAd, NoFillReason, Tracker } from '../core/types';
|
|
7
|
+
|
|
8
|
+
export interface UseEthiclyInterstitialOptions {
|
|
9
|
+
/** Preload on mount and after each dismiss. Default true. */
|
|
10
|
+
preload?: boolean;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export interface EthiclyInterstitialState {
|
|
14
|
+
/** The ad to render, set by a successful show(). */
|
|
15
|
+
ad: EthiclyAd | null;
|
|
16
|
+
tracker: Tracker | null;
|
|
17
|
+
/** Why the last show() found no ad: show your house ad, except for 'ads_disabled' (a paid user). */
|
|
18
|
+
reason: NoFillReason | null;
|
|
19
|
+
loading: boolean;
|
|
20
|
+
/** Load the ad to show now. Resolves true when `ad` is set, false on no fill. */
|
|
21
|
+
show(): Promise<boolean>;
|
|
22
|
+
/** Close the current ad: flushes queued events and preloads the next one. */
|
|
23
|
+
dismiss(): void;
|
|
24
|
+
canShow(): boolean;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export function useEthiclyInterstitial(
|
|
28
|
+
ethicly: Ethicly,
|
|
29
|
+
options: UseEthiclyInterstitialOptions = {},
|
|
30
|
+
): EthiclyInterstitialState {
|
|
31
|
+
const preload = options.preload !== false;
|
|
32
|
+
const [current, setCurrent] = useState<{ ad: EthiclyAd; tracker: Tracker } | null>(null);
|
|
33
|
+
const [reason, setReason] = useState<NoFillReason | null>(null);
|
|
34
|
+
const [loading, setLoading] = useState(false);
|
|
35
|
+
const mounted = useRef(true);
|
|
36
|
+
const currentRef = useRef(current);
|
|
37
|
+
currentRef.current = current;
|
|
38
|
+
|
|
39
|
+
useEffect(() => {
|
|
40
|
+
mounted.current = true;
|
|
41
|
+
if (preload) void ethicly.preload();
|
|
42
|
+
return () => {
|
|
43
|
+
mounted.current = false;
|
|
44
|
+
void currentRef.current?.tracker.closed();
|
|
45
|
+
};
|
|
46
|
+
}, [ethicly, preload]);
|
|
47
|
+
|
|
48
|
+
const show = useCallback(async (): Promise<boolean> => {
|
|
49
|
+
setLoading(true);
|
|
50
|
+
try {
|
|
51
|
+
const r = await ethicly.loadInterstitial();
|
|
52
|
+
if (!mounted.current) return false;
|
|
53
|
+
if (r.ad) {
|
|
54
|
+
setCurrent({ ad: r.ad, tracker: r.tracker });
|
|
55
|
+
setReason(null);
|
|
56
|
+
return true;
|
|
57
|
+
}
|
|
58
|
+
setReason(r.reason);
|
|
59
|
+
return false;
|
|
60
|
+
} finally {
|
|
61
|
+
if (mounted.current) setLoading(false);
|
|
62
|
+
}
|
|
63
|
+
}, [ethicly]);
|
|
64
|
+
|
|
65
|
+
const dismiss = useCallback(() => {
|
|
66
|
+
void currentRef.current?.tracker.closed();
|
|
67
|
+
setCurrent(null);
|
|
68
|
+
if (preload) void ethicly.preload();
|
|
69
|
+
}, [ethicly, preload]);
|
|
70
|
+
|
|
71
|
+
const canShow = useCallback(() => ethicly.canShow(), [ethicly]);
|
|
72
|
+
|
|
73
|
+
return { ad: current?.ad ?? null, tracker: current?.tracker ?? null, reason, loading, show, dismiss, canShow };
|
|
74
|
+
}
|
package/src/version.ts
ADDED