@simba-dev/react-native-media-player 1.13.8 → 1.14.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.
@@ -1,128 +0,0 @@
1
- import React from 'react';
2
- import { PlayerRoot } from '../components/PlayerRoot';
3
- import { useLaunchParams } from './useLaunchParams';
4
- import { useLaunchPlayback } from './useLaunchPlayback';
5
-
6
- /**
7
- * V14 Phase 59: the activity-branch wrapper.
8
- *
9
- * The native `PlayerActivity` is launched with playback params
10
- * (`{uri, title, type, startPositionMs}`) by any `openPlayer(...)` call.
11
- * The activity's React tree should render the player surface +
12
- * controls when those params are present, and the regular app
13
- * navigator when they're not.
14
- *
15
- * Before V14, this branching lived in the consumer's `App.tsx`:
16
- *
17
- * ```tsx
18
- * const launchParams = useLaunchParams();
19
- * if (launchParams) {
20
- * return <PlayerRoot />;
21
- * }
22
- * return <YourNavigator />;
23
- * ```
24
- *
25
- * `<SimbaPlayerRoot>` absorbs both the hook call and the switch.
26
- * Consumers just nest their navigator as children:
27
- *
28
- * @example
29
- * ```tsx
30
- * <SimbaPlayer getResumePosition={...}>
31
- * <SimbaPlayerRoot>
32
- * <YourNavigator />
33
- * </SimbaPlayerRoot>
34
- * </SimbaPlayer>
35
- * ```
36
- *
37
- * The activity branch is opaque: the consumer cannot customize what
38
- * is rendered when `launchParams` is non-null. The module owns the
39
- * player surface + controls. If a consumer needs a different
40
- * activity surface (rare), they should use `PlayerRoot` directly
41
- * with `useLaunchParams()`.
42
- *
43
- * **Why this exists:** V14's junior-dev-level integration goal is
44
- * that any consumer's `App.tsx` looks the same. The launch-params
45
- * branch is the second-largest source of glue after the resume
46
- * lookup and the deep-link handler — Phase 59 deletes it from the
47
- * consumer's `App.tsx`.
48
- */
49
- export interface SimbaPlayerRootProps {
50
- /**
51
- * The regular app content (typically a navigator). Rendered when
52
- * the activity was launched WITHOUT playback params — i.e., the
53
- * user just opened the app from the launcher or the home screen
54
- * icon, with no recent `openPlayer(...)` handoff queued.
55
- */
56
- children: React.ReactNode;
57
-
58
- /**
59
- * `1.8.0` — let the consumer own the player UI.
60
- *
61
- * `false` (the default, and every pre-1.8.0 behaviour) mounts
62
- * `<PlayerRoot />` — the module's built-in surface + controls —
63
- * whenever launch params are present. `children` renders only when
64
- * there is nothing to play.
65
- *
66
- * `true` splits the two concerns that `PlayerRoot` bundles
67
- * together: the module keeps the **load lifecycle**
68
- * (`useLaunchPlayback`, so the payload is still loaded exactly once)
69
- * while the consumer keeps the **UI** — `children` renders
70
- * unconditionally, and the consumer is responsible for showing it
71
- * only while something is playing.
72
- *
73
- * ## Why a consumer needs this
74
- *
75
- * `<PlayerRoot>` returns the module's default controls *instead of*
76
- * `children`, so any custom chrome passed as children is discarded
77
- * exactly when playback starts — which makes a bespoke player screen
78
- * unreachable. `headless` is the supported way to keep the load
79
- * guarantee without surrendering the UI.
80
- *
81
- * @example
82
- * ```tsx
83
- * <SimbaPlayerRoot headless>
84
- * <MyNavigator />
85
- * </SimbaPlayerRoot>
86
- * ```
87
- */
88
- headless?: boolean;
89
- }
90
-
91
- /**
92
- * The activity-branch wrapper. Mounts `<PlayerRoot />` when the
93
- * activity was launched with playback params, otherwise renders
94
- * `children`. See `SimbaPlayerRootProps` for usage.
95
- *
96
- * V22.0.0 / 1.5.8 (D-035 fix #5 — `loadFile(null)` race): this
97
- * component is the **single owner** of the one-shot `useLaunchParams()`
98
- * queue. The hook reads `lastLaunchParams` once; a second consumer
99
- * (PlayerRoot calling its own `useLaunchParams()`) would see `null`.
100
- * We pass the resolved `launchParams` down to `<PlayerRoot>` as a
101
- * prop, so the player root never re-reads the queue. Direct
102
- * consumers that mount `<PlayerRoot>` without `<SimbaPlayerRoot>`
103
- * still get the hook fallback (PlayerRoot's `launchParams?` prop is
104
- * optional).
105
- */
106
- export function SimbaPlayerRoot({
107
- children,
108
- headless = false,
109
- }: SimbaPlayerRootProps): React.ReactElement {
110
- const launchParams = useLaunchParams();
111
-
112
- // `1.8.0` headless mode: the module keeps the load guarantee and
113
- // hands the UI back to the consumer.
114
- //
115
- // Gated on `headless` on purpose. In the default (non-headless)
116
- // path `<PlayerRoot>` runs the SAME hook, and two hook instances
117
- // holding the same payload are two independent effects — the media
118
- // would be loaded twice per launch, which is the exact regression
119
- // this extraction was meant to end. So exactly one owner runs it:
120
- // `<PlayerRoot>` normally, this component only in headless mode.
121
- useLaunchPlayback(headless ? launchParams : null);
122
-
123
- if (launchParams && !headless) {
124
- return <PlayerRoot launchParams={launchParams} />;
125
- }
126
-
127
- return <>{children}</>;
128
- }
@@ -1,61 +0,0 @@
1
- import { getMpvPlayerModule } from '../bridge/MpvPlayerModule';
2
-
3
- /**
4
- * Synchronous, idempotent "is this React tree hosted by the player
5
- * activity?" predicate.
6
- *
7
- * ## Why this exists (V19 W6.0)
8
- *
9
- * A consumer that wants to render its OWN chrome over the native
10
- * video surface needs to know whether it is running in
11
- * `PlayerActivity` (the surface exists) or `MainActivity` (it does
12
- * not). Before this hook, the only public way to ask was
13
- * `useLaunchParams()`, which is the wrong tool:
14
- *
15
- * 1. **It is one-shot.** The native side keeps launch params as a
16
- * single-shot queue; the first React consumer reads them and
17
- * every later consumer sees `null`. A chrome component asking
18
- * "am I the player activity?" would silently steal the payload
19
- * from `<PlayerRoot />` and break the default player UI.
20
- * 2. **It is async.** `getLaunchParams()` crosses the bridge as a
21
- * `Promise`, so the answer arrives one render late — a consumer
22
- * using it as a mount gate flashes its own absence for a frame.
23
- * 3. **It conflates two questions.** "Are there launch params?" is
24
- * not "am I the player host?" — a player activity with an empty
25
- * queue answers `null` to the first and `true` to the second.
26
- *
27
- * This hook answers the second question directly, and that is the
28
- * one a chrome compositor actually needs.
29
- *
30
- * ## Why it is not a subscription
31
- *
32
- * `isCurrentActivityPlayer()` is a plain boolean the native side
33
- * flips in `PlayerActivity.onCreate` / `onDestroy`. Each activity
34
- * mounts its OWN React tree, so the value is fixed for the lifetime
35
- * of any given tree and is already correct on its first render. A
36
- * `useSyncExternalStore` subscription would be pure overhead: there
37
- * is no intermediate state to observe, because the transition tears
38
- * the tree down rather than re-rendering it.
39
- *
40
- * Consumers that genuinely need to react to the flip *without* a
41
- * teardown (e.g. handling PiP) should subscribe to the `onPip*`
42
- * player events instead.
43
- *
44
- * ## Compatibility
45
- *
46
- * If the bridge predates this method the hook reports `false` — the
47
- * conservative answer. `false` means "no player surface here", which
48
- * is what a jest/Storybook/web host wants; a consumer that mis-detects
49
- * would render a chrome overlay over a non-existent surface, so
50
- * failing closed is the safe direction.
51
- */
52
- export function useIsPlayerActivity(): boolean {
53
- const bridge = getMpvPlayerModule();
54
- try {
55
- return bridge.isCurrentActivityPlayer();
56
- } catch {
57
- // Pre-1.7.0 bridge (method absent) or a non-RN host. Fail closed
58
- // — see the compatibility note above.
59
- return false;
60
- }
61
- }
@@ -1,126 +0,0 @@
1
- import { useEffect, useState } from 'react';
2
- import { getMpvPlayerModule, type LaunchParams } from '../bridge/MpvPlayerModule';
3
-
4
- /**
5
- * V13 Phase 54: read the activity's launch params (one-shot).
6
- *
7
- * The native `PlayerActivity` is launched with a payload
8
- * `{uri, title, type, startPositionMs}` from the most recent
9
- * `openPlayer(...)` call (see `LaunchParams` in
10
- * `MpvPlayerModule.ts`). The Kotlin side keeps the params as a
11
- * single-shot queue — the first reader consumes them, subsequent
12
- * calls return `null`.
13
- *
14
- * In the JS layer, this hook is what `PlayerActivity`'s React
15
- * tree calls to fetch the params (typically in the activity's
16
- * top-level component, e.g. `AppContent`). After reading, the
17
- * hook exposes the params as React state so the consumer can:
18
- *
19
- * 1. Conditionally render `<PlayerRoot />` (the activity's
20
- * player surface + controls) when params are present.
21
- * 2. Render the regular navigator when no params (e.g. when
22
- * launched from the home screen icon, no recent playback
23
- * was queued).
24
- *
25
- * The hook is **idempotent at the React level** — calling it
26
- * from multiple components reads the same native one-shot
27
- * queue. The first mount wins; subsequent mounts see `null`.
28
- * (This matches the Kotlin-side behavior; the alternative is a
29
- * shared React state, but that requires hoisting the hook to
30
- * a common ancestor.)
31
- *
32
- * V22.0.0 / 1.5.10 (D-034) activity-aware guard:
33
- *
34
- * The previous implementation consumed `lastLaunchParams` from
35
- * the FIRST React tree to mount. With MainActivity and
36
- * PlayerActivity both mounting the same `App` component (both
37
- * wrap in `<SimbaPlayerRoot>`), cold-start races meant stale
38
- * launchParams from a prior `openPlayer` call would get
39
- * consumed by MainActivity's React tree, and `<PlayerRoot />`
40
- * would get rendered over the Home screen.
41
- *
42
- * The fix here: before calling `getLaunchParams()` we ask the
43
- * native module `isCurrentActivityPlayer()` — a synchronous
44
- * boolean set to `true` by `PlayerActivity.onCreate` and reset
45
- * to `false` in `PlayerActivity.onDestroy`. When false (we are
46
- * in MainActivity, a Share Sheet host, or no React host yet),
47
- * we return `null` *without* calling `getLaunchParams()`, so
48
- * the single-shot queue stays intact for the eventual
49
- * PlayerActivity mount to consume.
50
- *
51
- * @example
52
- * ```tsx
53
- * function AppContent() {
54
- * const launchParams = useLaunchParams();
55
- * if (launchParams) {
56
- * return <PlayerRoot />;
57
- * }
58
- * return <RootNavigator />;
59
- * }
60
- * ```
61
- *
62
- * For consumers that just want the data without the React
63
- * subscription (e.g. for analytics), use
64
- * `getMpvPlayerModule().getLaunchParams()` directly. Note that
65
- * a direct call consumes the one-shot queue the same way the
66
- * hook does — so don't call it twice from `MainActivity`'s
67
- * tree (it would steal the params from `PlayerActivity`).
68
- */
69
- export function useLaunchParams(): LaunchParams | null {
70
- const [params, setParams] = useState<LaunchParams | null>(null);
71
-
72
- useEffect(() => {
73
- const bridge = getMpvPlayerModule();
74
- // V22.0.0 / 1.5.10 (D-034): refuse to consume the queue
75
- // unless we are the player host. see hook comment for why.
76
- try {
77
- const isPlayer = bridge.isCurrentActivityPlayer();
78
- if (!isPlayer) {
79
- setParams(null);
80
- return;
81
- }
82
- } catch {
83
- // Bridge didn't expose the guard (pre-1.5.10 lib) — fall
84
- // through to the legacy one-shot-queue read so older
85
- // versions of the lib keep working.
86
- }
87
- // V22.0.0 / 1.5.8 (D-035 fix #6): the runtime's
88
- // `getLaunchParams()` is an async bridge method (Kotlin
89
- // `@ReactMethod` without `isBlockingSynchronousMethod = true`),
90
- // so it returns a JS Promise<LaunchParams | null>. The previous
91
- // implementation called `setParams(next)` directly with the
92
- // Promise object, which stored a Promise in React state. That
93
- // had two observable symptoms:
94
- //
95
- // - `if (launchParams != null)` was true (a Promise is
96
- // truthy), so consumers went down the "we have params"
97
- // branch — `<SimbaPlayerRoot>` rendered `<PlayerRoot />`,
98
- // but with a Promise as the payload.
99
- // - `launchParams.uri` was undefined on that Promise, so the
100
- // `bridge.loadFile(launchParams.uri)` call passed `undefined`
101
- // to Kotlin's `@NonNull String path`, which threw
102
- // `IllegalArgumentException` at the bridge boundary — a
103
- // RedBox on every tap-to-card.
104
- //
105
- // Wrap in `Promise.resolve` so the call site stays correct
106
- // whether the bridge returns a Promise (the runtime case) or
107
- // a plain value (the TS interface case, or older versions of
108
- // the lib that surface the value synchronously through a
109
- // legacy `NativeModules` shim).
110
- let cancelled = false;
111
- Promise.resolve(bridge.getLaunchParams())
112
- .then((next) => {
113
- if (cancelled) return;
114
- setParams(next ?? null);
115
- })
116
- .catch(() => {
117
- if (cancelled) return;
118
- setParams(null);
119
- });
120
- return () => {
121
- cancelled = true;
122
- };
123
- }, []);
124
-
125
- return params;
126
- }