@simba-dev/react-native-media-player 1.3.0 → 1.5.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/README.md +41 -2
- package/package.json +5 -2
- package/src/hooks/SimbaPlayer.tsx +58 -76
- package/src/hooks/useOpenPlaylist.tsx +188 -0
- package/src/hooks/useQueue.tsx +92 -0
- package/src/hooks/useQueueSelection.tsx +40 -0
- package/src/index.ts +22 -5
- package/src/stores/playerQueueSelectionStore.ts +53 -0
- package/src/stores/playerQueueStore.ts +186 -0
- package/src/hooks/useSimbaPlayerLookup.tsx +0 -58
package/README.md
CHANGED
|
@@ -105,7 +105,7 @@ getMpvPlayerModule().requestNotificationPermission?.(); // graceful no-op on old
|
|
|
105
105
|
## Basic usage
|
|
106
106
|
|
|
107
107
|
```tsx
|
|
108
|
-
// App.tsx —
|
|
108
|
+
// App.tsx — V15+ (one wrapper, one import, zero glue code)
|
|
109
109
|
import React from 'react';
|
|
110
110
|
import {
|
|
111
111
|
SimbaPlayer,
|
|
@@ -146,6 +146,38 @@ function AppContent() {
|
|
|
146
146
|
|
|
147
147
|
That's it — `<SimbaPlayer>` is the one-import, one-wrapper integration point. It composes the `PlayerProvider` (config + state) and `PlayerResumeProvider` (bookmark-aware resume lookup). `<SimbaPlayerRoot>` (V14) absorbs the `useLaunchParams()` + activity-branch switch. `useOpenFromUrl()` (V14) absorbs the deep-link URI-scheme filter + title-derivation + extension-classification. `getResumePosition` (V14) is the new function-prop shape for the resume lookup (replaces V13's `lookup={...}` object shape).
|
|
148
148
|
|
|
149
|
+
### Per-screen usage (V15+)
|
|
150
|
+
|
|
151
|
+
Every player-related call is a one-line module hook. No `useAppDispatch` in player files. No `useAppSelector(state => state.player.X)`. No V11-mirrored redux state.
|
|
152
|
+
|
|
153
|
+
```tsx
|
|
154
|
+
import {
|
|
155
|
+
usePlayerActivity, // single-track play
|
|
156
|
+
useOpenPlaylist, // play all from a list
|
|
157
|
+
useQueue, // add / remove / reorder queue items
|
|
158
|
+
useSleepTimer, // arm a sleep timer
|
|
159
|
+
useEqualizer, // 10-band EQ
|
|
160
|
+
useIsLiked, // per-file "liked" state
|
|
161
|
+
useShuffle, // shuffle flag
|
|
162
|
+
} from '@simba-dev/react-native-media-player';
|
|
163
|
+
|
|
164
|
+
// Single-track play (V14, unchanged)
|
|
165
|
+
const {openPlayer} = usePlayerActivity();
|
|
166
|
+
const handlePlay = (item) => openPlayer({uri: item.uri, title: item.title, type: 'audio'});
|
|
167
|
+
|
|
168
|
+
// Play all from a list (V15 — the "play all" two-step absorption)
|
|
169
|
+
const {openPlaylist} = useOpenPlaylist();
|
|
170
|
+
const handlePlayAll = () => openPlaylist(sortedTracks, {type: 'audio'});
|
|
171
|
+
|
|
172
|
+
// Add to queue (V15 — zustand-backed, no Redux)
|
|
173
|
+
const {addToQueue} = useQueue();
|
|
174
|
+
const handleAddToQueue = (item) => addToQueue({uri: item.uri, title: item.title, ...});
|
|
175
|
+
|
|
176
|
+
// Set sleep timer (V15 — public surface; the consumer hasn't wired UI yet)
|
|
177
|
+
const {endTime, setTimer, clear} = useSleepTimer();
|
|
178
|
+
const handleSet30Min = () => setTimer(30 * 60); // 30 minutes
|
|
179
|
+
```
|
|
180
|
+
|
|
149
181
|
### Launching a file from JS
|
|
150
182
|
|
|
151
183
|
```tsx
|
|
@@ -413,14 +445,21 @@ Every public export lives in [`src/index.ts`](./src/index.ts). The **V13+ surfac
|
|
|
413
445
|
| `<DefaultControls>` | Pre-built controls (top bar, scrubber, transport, auto-hide). |
|
|
414
446
|
| `<PlayerProvider>` | (V12 surface) Wraps the app; provides resolved config + `renderControls` slot. Use `<SimbaPlayer>` instead unless you need the raw provider. |
|
|
415
447
|
|
|
416
|
-
### Hooks (V13+, V14 additions in **bold
|
|
448
|
+
### Hooks (V13+, V14 additions in **bold**, V15 additions in __bold-italic__)
|
|
417
449
|
|
|
418
450
|
| Hook | Returns | Throws outside provider? |
|
|
419
451
|
|---|---|---|
|
|
420
452
|
| `usePlayerActivity()` | `{ openPlayer(opts), getLaunchParams() }` | ❌ No (no-op fallback) |
|
|
421
453
|
| `useOpenWithResume()` | `(opts: OpenPlayerOptions & { resumeId? }) => Promise<boolean>` | ❌ No (no-op lookup) |
|
|
422
454
|
| **`useOpenFromUrl()`** | **`(uri: string) => Promise<boolean>`** — **deep-link helper** that filters `content://` + `file://` URIs, derives a title from the basename, classifies audio/video by extension, and forwards to `useOpenWithResume().openPlayer(...)` | ❌ No |
|
|
455
|
+
| **`useOpenPlaylist()`** | __V15 — `{ openPlaylist(entries, opts) }` — "play all from a list" two-step absorption. `opts: { type?, startIndex?, startPositionMs?, shuffle?, startExtras? }`__ | ❌ No |
|
|
423
456
|
| **`useSimbaPlayerLookup(selector?)`** | **`PlayerResumeLookup`** — **factory hook** that wraps an optional `selector` in a memoized lookup object. Without a selector, returns no-op. | ❌ No |
|
|
457
|
+
| __`useQueue()` / `useQueueItems()` / `useQueueLength()` / `usePlaybackHistory()`__ | __V15 — queue + playback history state (zustand-backed). `useQueue()` returns the full store (queue, history, 9 actions: addToQueue, prependToQueue, removeFromQueue, reorderQueue, clearQueue, shuffleQueue, playFromQueue, addToPlaybackHistory, clearPlaybackHistory).__ | ❌ No |
|
|
458
|
+
| __`useQueueSelection()` / `useQueueSelectedIndices()`__ | __V15 — multi-select state for the queue (selected indices + 4 actions: setSelection, clearSelection, removeSelected, moveSelectedToTop).__ | ❌ No |
|
|
459
|
+
| __`useSleepTimer()` / `useSleepTimerEnd()` / `useSleepTimerMode()`__ | __V15 — sleep timer state (endTime + mode) + actions (setTimer, setMode, clear).__ | ❌ No |
|
|
460
|
+
| __`useEqualizer()` / `useEqualizerEnabled()`__ | __V15 — 10-band equalizer (gains + enabled) + actions (setGains, toggle).__ | ❌ No |
|
|
461
|
+
| __`useIsLiked(uri)` / `useToggleLiked()`__ | __V15 — per-file "liked" state (Record<uri, boolean>) + toggle action.__ | ❌ No |
|
|
462
|
+
| __`useShuffle()` / `useShuffleEnabled()`__ | __V15 — shuffle flag + toggle action.__ | ❌ No |
|
|
424
463
|
| `usePlayItem()` | `(item, opts) => Promise<boolean>` | ❌ No (sugar on `useOpenWithResume`) |
|
|
425
464
|
| `useLaunchParams()` | `LaunchParams \| null` | ❌ No (returns null) |
|
|
426
465
|
| `usePlayer()` | `{ state: PlayerState, commands: PlayerCommands, progress: PlayerProgress }` | ❌ No (returns defaults) |
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@simba-dev/react-native-media-player",
|
|
3
|
-
"version": "1.
|
|
4
|
-
"description": "React Native video/audio player powered by libmpv — Picture-in-Picture, MediaSession, custom RN UI. **
|
|
3
|
+
"version": "1.5.0",
|
|
4
|
+
"description": "React Native video/audio player powered by libmpv — Picture-in-Picture, MediaSession, custom RN UI. **V16.0.0 stable release** (2026-09-07) — API hardening + type-safety sweep. `PlayerQueueItem` is generic; `<SimbaPlayer>` accepts a single `resumePolicy`; the 4 dead-feature stores (sleep/equalizer/liked/shuffle) are removed from the public surface. See `X:/Development/SIMBA/MOBILE_APP_REACT_NATIVE/md/SIMBA_PLAYER_MODULE_V16_*` for the V16 charter, tracker, and final QA report. Published under the `@simba-dev` org (the bare `simba` org-name on npmjs was reserved at publication time).",
|
|
5
5
|
"main": "src/index.ts",
|
|
6
6
|
"types": "src/index.ts",
|
|
7
7
|
"react-native": "src/index.ts",
|
|
@@ -88,5 +88,8 @@
|
|
|
88
88
|
},
|
|
89
89
|
"bugs": {
|
|
90
90
|
"url": "https://github.com/pavalep/react-native-media-player/issues"
|
|
91
|
+
},
|
|
92
|
+
"dependencies": {
|
|
93
|
+
"zustand": "^5.0.15"
|
|
91
94
|
}
|
|
92
95
|
}
|
|
@@ -3,22 +3,41 @@ import { PlayerProvider } from '../components/PlayerProvider';
|
|
|
3
3
|
import type { PlayerConfig } from '../types/config';
|
|
4
4
|
import { PlayerResumeProvider } from './useOpenWithResume';
|
|
5
5
|
import type { PlayerResumeLookup } from './useOpenWithResume';
|
|
6
|
-
import type { GetResumePosition } from './useSimbaPlayerLookup';
|
|
7
6
|
|
|
8
7
|
/**
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
8
|
+
* V16 Phase 71: a single function the module calls to resolve a saved
|
|
9
|
+
* resume position for a media item. The consumer provides this once
|
|
10
|
+
* at the root of the app; every call site that wants resume-aware
|
|
11
|
+
* openPlayer just passes `resumeId: item.uri` to the hook's returned
|
|
12
|
+
* function.
|
|
13
13
|
*
|
|
14
|
-
*
|
|
15
|
-
*
|
|
16
|
-
*
|
|
14
|
+
* This is the V16 replacement for the V13/V14 4-path API:
|
|
15
|
+
* - V13: `lookup={PlayerResumeLookup}` (object prop)
|
|
16
|
+
* - V14: `getResumePosition={fn}` (function prop)
|
|
17
|
+
* - V14: `useSimbaPlayerLookup(selector)` (factory hook)
|
|
18
|
+
* - V14: `<PlayerResumeProvider lookup={...}>` (manual wrap)
|
|
17
19
|
*
|
|
18
|
-
*
|
|
19
|
-
*
|
|
20
|
-
*
|
|
21
|
-
*
|
|
20
|
+
* V16 collapses all 4 into a single `resumePolicy` function prop on
|
|
21
|
+
* `<SimbaPlayer>`. The internal `PlayerResumeContext` is unchanged.
|
|
22
|
+
*
|
|
23
|
+
* Why the consumer provides the policy (not the module): every
|
|
24
|
+
* project persists bookmarks/history differently - Redux, SQLite,
|
|
25
|
+
* MMKV, AsyncStorage, server-side. The module can't know the
|
|
26
|
+
* shape of the consumer's persistence layer, so the consumer
|
|
27
|
+
* provides a small adapter.
|
|
28
|
+
*
|
|
29
|
+
* The function must be **pure and synchronous**. The module calls
|
|
30
|
+
* it inline at `openPlayer(...)` time. If you need async resolution,
|
|
31
|
+
* hydrate the resume position into the consumer's state first and
|
|
32
|
+
* have the function return the cached value.
|
|
33
|
+
*/
|
|
34
|
+
export type ResumePolicy = (itemId: string) => number | undefined;
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* V16 Phase 71: the one-import, one-wrapper integration point for
|
|
38
|
+
* `<SimbaPlayer>`. The single `resumePolicy` prop replaces V13's
|
|
39
|
+
* `lookup` object prop, V14's `getResumePosition` function prop,
|
|
40
|
+
* and the V14 `useSimbaPlayerLookup` factory hook.
|
|
22
41
|
*
|
|
23
42
|
* @example
|
|
24
43
|
* ```tsx
|
|
@@ -29,7 +48,7 @@ import type { GetResumePosition } from './useSimbaPlayerLookup';
|
|
|
29
48
|
* return (
|
|
30
49
|
* <SimbaPlayer
|
|
31
50
|
* config={{ theme: { accent: '#FFD700' } }}
|
|
32
|
-
*
|
|
51
|
+
* resumePolicy={(uri) =>
|
|
33
52
|
* store.getState().bookmarks.byFileUri[uri]?.positionMs
|
|
34
53
|
* }
|
|
35
54
|
* >
|
|
@@ -39,26 +58,15 @@ import type { GetResumePosition } from './useSimbaPlayerLookup';
|
|
|
39
58
|
* }
|
|
40
59
|
* ```
|
|
41
60
|
*
|
|
42
|
-
*
|
|
43
|
-
*
|
|
44
|
-
*
|
|
45
|
-
*
|
|
46
|
-
* The function-prop shape is one function reference instead of
|
|
47
|
-
* an object literal — slightly less typing, slightly less
|
|
48
|
-
* nesting.
|
|
49
|
-
*
|
|
50
|
-
* The `lookup` / `getResumePosition` props are optional — pass
|
|
51
|
-
* one to enable `useOpenWithResume`'s auto-resume behavior. If
|
|
52
|
-
* you only need `usePlayerActivity().openPlayer(...)` (no
|
|
53
|
-
* resume lookup), you can omit both and the inner
|
|
54
|
-
* `<PlayerResumeProvider>` is still mounted with a no-op lookup
|
|
55
|
-
* that always returns `undefined`.
|
|
61
|
+
* The `resumePolicy` prop is optional - omit it and the inner
|
|
62
|
+
* `<PlayerResumeProvider>` is mounted with a no-op policy that
|
|
63
|
+
* always returns `undefined` (resume always falls back to 0 or
|
|
64
|
+
* the consumer-provided `startPositionMs`).
|
|
56
65
|
*
|
|
57
66
|
* **Junior-dev rule of thumb:** if your screen calls
|
|
58
|
-
* `openPlayer({resumeId: item.id})`,
|
|
59
|
-
*
|
|
60
|
-
* `
|
|
61
|
-
* lookup is optional.
|
|
67
|
+
* `openPlayer({resumeId: item.id})`, pass a `resumePolicy` here.
|
|
68
|
+
* If you only call `openPlayer({uri, title, type})` without
|
|
69
|
+
* `resumeId`, the policy is optional.
|
|
62
70
|
*/
|
|
63
71
|
export interface SimbaPlayerProps {
|
|
64
72
|
/**
|
|
@@ -67,35 +75,12 @@ export interface SimbaPlayerProps {
|
|
|
67
75
|
*/
|
|
68
76
|
config?: PlayerConfig;
|
|
69
77
|
/**
|
|
70
|
-
*
|
|
71
|
-
*
|
|
72
|
-
*
|
|
73
|
-
*
|
|
74
|
-
*
|
|
75
|
-
* Backward-compat: V13-era consumers that pass a memoized
|
|
76
|
-
* `PlayerResumeLookup` object. New consumers should prefer
|
|
77
|
-
* the `getResumePosition` function-prop shape.
|
|
78
|
-
*
|
|
79
|
-
* If both `lookup` and `getResumePosition` are passed,
|
|
80
|
-
* `getResumePosition` wins.
|
|
81
|
-
*/
|
|
82
|
-
lookup?: PlayerResumeLookup;
|
|
83
|
-
/**
|
|
84
|
-
* Bookmark-aware resume lookup (function shape). Receives a
|
|
85
|
-
* `resumeId` (typically a URI or item id) and returns the
|
|
86
|
-
* saved position in ms, or `undefined` for no saved position.
|
|
87
|
-
*
|
|
88
|
-
* **Recommended for new consumers.** One function reference
|
|
89
|
-
* instead of an object literal:
|
|
90
|
-
*
|
|
91
|
-
* ```tsx
|
|
92
|
-
* <SimbaPlayer getResumePosition={(uri) => store.getState().bookmarks.byFileUri[uri]?.positionMs}>
|
|
93
|
-
* ```
|
|
94
|
-
*
|
|
95
|
-
* If both `lookup` and `getResumePosition` are passed,
|
|
96
|
-
* `getResumePosition` wins.
|
|
78
|
+
* V16: bookmark-aware resume lookup, as a single function.
|
|
79
|
+
* Receives a `resumeId` (typically a URI or item id) and
|
|
80
|
+
* returns the saved position in ms, or `undefined` for no
|
|
81
|
+
* saved position.
|
|
97
82
|
*/
|
|
98
|
-
|
|
83
|
+
resumePolicy?: ResumePolicy;
|
|
99
84
|
children: React.ReactNode;
|
|
100
85
|
}
|
|
101
86
|
|
|
@@ -105,28 +90,25 @@ export interface SimbaPlayerProps {
|
|
|
105
90
|
*/
|
|
106
91
|
export function SimbaPlayer({
|
|
107
92
|
config,
|
|
108
|
-
|
|
109
|
-
getResumePosition,
|
|
93
|
+
resumePolicy,
|
|
110
94
|
children,
|
|
111
95
|
}: SimbaPlayerProps): React.ReactElement {
|
|
112
|
-
//
|
|
113
|
-
//
|
|
114
|
-
//
|
|
115
|
-
//
|
|
116
|
-
//
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
if (lookup) {
|
|
122
|
-
return lookup;
|
|
96
|
+
// Wrap the policy in the legacy `PlayerResumeLookup` object
|
|
97
|
+
// shape so the existing `PlayerResumeContext` plumbing is
|
|
98
|
+
// unchanged. Memoize so the `PlayerResumeContext` value
|
|
99
|
+
// stays stable across renders - this is what keeps every
|
|
100
|
+
// `useOpenWithResume()` consumer from re-rendering on every
|
|
101
|
+
// root render.
|
|
102
|
+
const lookup = useMemo<PlayerResumeLookup>(() => {
|
|
103
|
+
if (resumePolicy) {
|
|
104
|
+
return {getResumePosition: resumePolicy};
|
|
123
105
|
}
|
|
124
106
|
return noopLookup;
|
|
125
|
-
}, [
|
|
107
|
+
}, [resumePolicy]);
|
|
126
108
|
|
|
127
109
|
return (
|
|
128
110
|
<PlayerProvider config={config}>
|
|
129
|
-
<PlayerResumeProvider lookup={
|
|
111
|
+
<PlayerResumeProvider lookup={lookup}>
|
|
130
112
|
{children}
|
|
131
113
|
</PlayerResumeProvider>
|
|
132
114
|
</PlayerProvider>
|
|
@@ -134,8 +116,8 @@ export function SimbaPlayer({
|
|
|
134
116
|
}
|
|
135
117
|
|
|
136
118
|
/**
|
|
137
|
-
* No-op lookup used when the consumer doesn't pass a
|
|
138
|
-
*
|
|
119
|
+
* No-op lookup used when the consumer doesn't pass a
|
|
120
|
+
* `resumePolicy`. Always returns `undefined` so
|
|
139
121
|
* `useOpenWithResume` falls back to 0 (or the
|
|
140
122
|
* consumer-provided `startPositionMs`).
|
|
141
123
|
*/
|
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
import {useCallback} from 'react';
|
|
2
|
+
import {usePlayerActivity} from './usePlayerActivity';
|
|
3
|
+
import type {OpenPlayerOptions} from './usePlayerActivity';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* V15 Phase 64: a minimal entry shape for `useOpenPlaylist`.
|
|
7
|
+
*
|
|
8
|
+
* Structurally — any object with at least `uri` and `title` qualifies.
|
|
9
|
+
* The consumer's full `PlaylistEntry` (which has `duration`, `source`,
|
|
10
|
+
* `type`, `mediaType`, `provider`, `folderId`) matches this without
|
|
11
|
+
* any explicit cast. The hook only reads `uri` and `title`; the
|
|
12
|
+
* rest of the entry is ignored.
|
|
13
|
+
*/
|
|
14
|
+
export interface PlayableEntry {
|
|
15
|
+
uri: string;
|
|
16
|
+
title: string;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* V15 Phase 64: options for `openPlaylist`.
|
|
21
|
+
*/
|
|
22
|
+
export interface OpenPlaylistOptions {
|
|
23
|
+
/** Stream type for the start track. Defaults to 'audio'. */
|
|
24
|
+
type?: 'video' | 'audio';
|
|
25
|
+
/**
|
|
26
|
+
* Index into `entries` of the track to start playback from.
|
|
27
|
+
* Defaults to 0 (the first track). If `shuffle: true` and
|
|
28
|
+
* `startIndex` is not provided, a random non-zero index is
|
|
29
|
+
* chosen (so shuffle doesn't start from index 0 by convention;
|
|
30
|
+
* but pass 0 explicitly if you want that).
|
|
31
|
+
*/
|
|
32
|
+
startIndex?: number;
|
|
33
|
+
/**
|
|
34
|
+
* Optional start position in ms for the start track. Useful
|
|
35
|
+
* when the consumer has a saved bookmark for the first
|
|
36
|
+
* track and wants playback to resume from that position.
|
|
37
|
+
* (For per-track resume, the consumer should pre-dispatch
|
|
38
|
+
* a seek after the activity launches; the lookup configured
|
|
39
|
+
* via `<SimbaPlayer>` is not applied here — `useOpenPlaylist`
|
|
40
|
+
* calls `usePlayerActivity().openPlayer`, not the
|
|
41
|
+
* `useOpenWithResume` variant.)
|
|
42
|
+
*/
|
|
43
|
+
startPositionMs?: number;
|
|
44
|
+
/**
|
|
45
|
+
* If true, Fisher-Yates shuffles a copy of `entries` before
|
|
46
|
+
* picking the start track. The original `entries` array is
|
|
47
|
+
* not mutated. The shuffle is in-place on the copy.
|
|
48
|
+
*/
|
|
49
|
+
shuffle?: boolean;
|
|
50
|
+
/**
|
|
51
|
+
* Extra fields forwarded to the start track's `openPlayer`
|
|
52
|
+
* call. Useful for consumer-specific metadata like
|
|
53
|
+
* `provider`, `folderId`, `artworkUri`, `subtitleUri`, etc.
|
|
54
|
+
*
|
|
55
|
+
* These override any defaults the hook sets (e.g. `type`).
|
|
56
|
+
* If you pass `type` in `startExtras`, it wins over the
|
|
57
|
+
* `type` option.
|
|
58
|
+
*
|
|
59
|
+
* Typed loosely (`Record<string, unknown>`) so consumers
|
|
60
|
+
* can spread conditional fields without TypeScript's excess
|
|
61
|
+
* property check rejecting the whole object. The fields
|
|
62
|
+
* are forwarded verbatim to `openPlayer({...})`; unknown
|
|
63
|
+
* fields are dropped by the consumer's call site (or
|
|
64
|
+
* accepted if the consumer wraps the call).
|
|
65
|
+
*/
|
|
66
|
+
startExtras?: Record<string, unknown>;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* V15 Phase 64: the return value of `useOpenPlaylist`.
|
|
71
|
+
*/
|
|
72
|
+
export interface UseOpenPlaylistResult {
|
|
73
|
+
/**
|
|
74
|
+
* Open the given list of entries in the player. The first
|
|
75
|
+
* track (or the track at `startIndex`) is the activity's
|
|
76
|
+
* launch payload. Returns `false` if `entries` is empty.
|
|
77
|
+
*
|
|
78
|
+
* This is the "play all from a list" pattern. The hook
|
|
79
|
+
* absorbs the start-track extraction, optional shuffle,
|
|
80
|
+
* and `openPlayer` call. The consumer does NOT need to
|
|
81
|
+
* dispatch `loadPlaylistToPlayer` separately — that's a
|
|
82
|
+
* consumer-side concern (see V15 Phase 65 for the state
|
|
83
|
+
* consolidation that absorbs that too).
|
|
84
|
+
*
|
|
85
|
+
* @example
|
|
86
|
+
* ```tsx
|
|
87
|
+
* const {openPlaylist} = useOpenPlaylist();
|
|
88
|
+
*
|
|
89
|
+
* const handlePlayAll = () => openPlaylist(sortedTracks, {type: 'audio'});
|
|
90
|
+
* const handleShuffle = () => openPlaylist(sortedTracks, {type: 'audio', shuffle: true});
|
|
91
|
+
* const handlePlayFromThird = () => openPlaylist(sortedTracks, {type: 'audio', startIndex: 2});
|
|
92
|
+
* ```
|
|
93
|
+
*/
|
|
94
|
+
openPlaylist(
|
|
95
|
+
entries: PlayableEntry[],
|
|
96
|
+
opts?: OpenPlaylistOptions,
|
|
97
|
+
): Promise<boolean>;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* V15 Phase 64: factory hook for the "play all" pattern.
|
|
102
|
+
*
|
|
103
|
+
* Replaces the 12-30 line two-step pattern every consumer
|
|
104
|
+
* wrote:
|
|
105
|
+
*
|
|
106
|
+
* ```tsx
|
|
107
|
+
* // Before (V14)
|
|
108
|
+
* const handlePlayAll = useCallback(() => {
|
|
109
|
+
* const entries = sortedTracks.map(t => ({...t}));
|
|
110
|
+
* if (entries.length === 0) return;
|
|
111
|
+
* dispatch(loadPlaylistToPlayer(entries)); // consumer-side
|
|
112
|
+
* openPlayer({
|
|
113
|
+
* uri: entries[0].uri,
|
|
114
|
+
* title: entries[0].title,
|
|
115
|
+
* type: 'audio',
|
|
116
|
+
* });
|
|
117
|
+
* }, [sortedTracks, dispatch, openPlayer]);
|
|
118
|
+
*
|
|
119
|
+
* // After (V15 Phase 64)
|
|
120
|
+
* const {openPlaylist} = useOpenPlaylist();
|
|
121
|
+
* const handlePlayAll = useCallback(() => {
|
|
122
|
+
* openPlaylist(sortedTracks, {type: 'audio'});
|
|
123
|
+
* }, [openPlaylist, sortedTracks]);
|
|
124
|
+
* ```
|
|
125
|
+
*
|
|
126
|
+
* **Phase 64 scope**: the hook calls `openPlayer` with the
|
|
127
|
+
* first track. The consumer's `loadPlaylistToPlayer` dispatch
|
|
128
|
+
* is still required if the consumer's Queue UI needs to read
|
|
129
|
+
* the playlist. **Phase 65** absorbs that step too.
|
|
130
|
+
*/
|
|
131
|
+
export function useOpenPlaylist(): UseOpenPlaylistResult {
|
|
132
|
+
const {openPlayer} = usePlayerActivity();
|
|
133
|
+
|
|
134
|
+
const openPlaylist = useCallback(
|
|
135
|
+
async (
|
|
136
|
+
entries: PlayableEntry[],
|
|
137
|
+
opts: OpenPlaylistOptions = {},
|
|
138
|
+
): Promise<boolean> => {
|
|
139
|
+
// Defensive: empty list is a no-op.
|
|
140
|
+
if (!entries || entries.length === 0) return false;
|
|
141
|
+
|
|
142
|
+
const type = opts.type ?? 'audio';
|
|
143
|
+
|
|
144
|
+
// Resolve the start index. If shuffle is on and no
|
|
145
|
+
// explicit startIndex, pick a random non-zero index.
|
|
146
|
+
// (If shuffle lands on 0 by chance, the first track plays
|
|
147
|
+
// first — that's fine, the shuffle just orders the rest.)
|
|
148
|
+
let startIndex = opts.startIndex ?? 0;
|
|
149
|
+
let orderedEntries: PlayableEntry[] = entries;
|
|
150
|
+
|
|
151
|
+
if (opts.shuffle) {
|
|
152
|
+
// Fisher-Yates shuffle a copy. Don't mutate the input.
|
|
153
|
+
orderedEntries = entries.slice();
|
|
154
|
+
for (let i = orderedEntries.length - 1; i > 0; i--) {
|
|
155
|
+
const j = Math.floor(Math.random() * (i + 1));
|
|
156
|
+
[orderedEntries[i], orderedEntries[j]] = [
|
|
157
|
+
orderedEntries[j],
|
|
158
|
+
orderedEntries[i],
|
|
159
|
+
];
|
|
160
|
+
}
|
|
161
|
+
if (opts.startIndex == null) {
|
|
162
|
+
startIndex = Math.floor(Math.random() * orderedEntries.length);
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
// Clamp startIndex to the valid range. A consumer that
|
|
167
|
+
// passes `startIndex: 99` for a 3-entry list gets index 2.
|
|
168
|
+
if (startIndex < 0) startIndex = 0;
|
|
169
|
+
if (startIndex >= orderedEntries.length) {
|
|
170
|
+
startIndex = orderedEntries.length - 1;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
const start = orderedEntries[startIndex];
|
|
174
|
+
if (!start) return false;
|
|
175
|
+
|
|
176
|
+
return openPlayer({
|
|
177
|
+
uri: start.uri,
|
|
178
|
+
title: start.title,
|
|
179
|
+
type,
|
|
180
|
+
startPositionMs: opts.startPositionMs,
|
|
181
|
+
...(opts.startExtras as Partial<OpenPlayerOptions>),
|
|
182
|
+
});
|
|
183
|
+
},
|
|
184
|
+
[openPlayer],
|
|
185
|
+
);
|
|
186
|
+
|
|
187
|
+
return {openPlaylist};
|
|
188
|
+
}
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import {useStore} from 'zustand';
|
|
2
|
+
import {useShallow} from 'zustand/react/shallow';
|
|
3
|
+
import {
|
|
4
|
+
usePlayerQueueStore,
|
|
5
|
+
} from '../stores/playerQueueStore';
|
|
6
|
+
import type {
|
|
7
|
+
PlayerQueueItem,
|
|
8
|
+
PlayerQueueStore,
|
|
9
|
+
} from '../stores/playerQueueStore';
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* V15 Phase 65 (V16 Phase 69 hardened): the public hook for queue +
|
|
13
|
+
* playback-history state.
|
|
14
|
+
*
|
|
15
|
+
* Two overloads:
|
|
16
|
+
* - `useQueue()` returns the full store `{queue, playbackHistory, addToQueue, ...}`.
|
|
17
|
+
* - `useQueue(selector)` returns the selected slice (use `useShallow`
|
|
18
|
+
* on object selectors to subscribe by-value and avoid unnecessary
|
|
19
|
+
* re-renders).
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* ```tsx
|
|
23
|
+
* function QueueScreen() {
|
|
24
|
+
* const {queue, addToQueue, removeFromQueue} = useQueue();
|
|
25
|
+
* return <FlatList data={queue} ... />;
|
|
26
|
+
* }
|
|
27
|
+
*
|
|
28
|
+
* // For a single field (no re-render on other field changes):
|
|
29
|
+
* function QueueBadge() {
|
|
30
|
+
* const length = useQueueLength();
|
|
31
|
+
* return <Text>{length}</Text>;
|
|
32
|
+
* }
|
|
33
|
+
* ```
|
|
34
|
+
*/
|
|
35
|
+
export function useQueue(): PlayerQueueStore;
|
|
36
|
+
export function useQueue<T>(selector: (state: PlayerQueueStore) => T): T;
|
|
37
|
+
export function useQueue<T>(
|
|
38
|
+
selector?: (state: PlayerQueueStore) => T,
|
|
39
|
+
): PlayerQueueStore | T {
|
|
40
|
+
if (selector) {
|
|
41
|
+
return useStore(usePlayerQueueStore, useShallow(selector));
|
|
42
|
+
}
|
|
43
|
+
return useStore(usePlayerQueueStore);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** Convenience hook: just the queue length. */
|
|
47
|
+
export function useQueueLength(): number {
|
|
48
|
+
return useStore(usePlayerQueueStore, state => state.queue.length);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** Convenience hook: just the queue array. */
|
|
52
|
+
export function useQueueItems(): PlayerQueueItem[] {
|
|
53
|
+
return useStore(usePlayerQueueStore, state => state.queue);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* V16 Phase 70: typed convenience hook for the queue array.
|
|
58
|
+
*
|
|
59
|
+
* Returns `useQueueItems()` cast to `T[]`. Use this when the
|
|
60
|
+
* consumer wants to specialize `PlayerQueueItem` to its own
|
|
61
|
+
* narrow literal unions (e.g.
|
|
62
|
+
* `PlayerQueueItem<MediaSource, MediaKind, MediaLane>`) so the
|
|
63
|
+
* items round-trip through `addToQueue` / `useQueueItemsAs`
|
|
64
|
+
* without `as unknown as` casts at the boundary.
|
|
65
|
+
*
|
|
66
|
+
* The runtime contract: the module's queue store accepts any
|
|
67
|
+
* `PlayerQueueItem` shape on write, so an item added with
|
|
68
|
+
* `PlayerQueueItem<MediaSource, MediaKind, MediaLane>` is
|
|
69
|
+
* stored structurally. The cast on read asserts the consumer's
|
|
70
|
+
* domain knowledge that the items in the queue are typed.
|
|
71
|
+
*
|
|
72
|
+
* The cast lives here (one place) instead of being scattered
|
|
73
|
+
* across every read site in the consumer.
|
|
74
|
+
*/
|
|
75
|
+
export function useQueueItemsAs<T extends PlayerQueueItem = PlayerQueueItem>(): T[] {
|
|
76
|
+
return useStore(usePlayerQueueStore, state => state.queue as T[]);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/** Convenience hook: just the playback history array. */
|
|
80
|
+
export function usePlaybackHistory(): PlayerQueueItem[] {
|
|
81
|
+
return useStore(usePlayerQueueStore, state => state.playbackHistory);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* V16 Phase 70: typed convenience hook for the playback history.
|
|
86
|
+
* See {@link useQueueItemsAs} for the cast contract.
|
|
87
|
+
*/
|
|
88
|
+
export function usePlaybackHistoryAs<T extends PlayerQueueItem = PlayerQueueItem>(): T[] {
|
|
89
|
+
return useStore(usePlayerQueueStore, state => state.playbackHistory as T[]);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
export type {PlayerQueueItem, PlayerQueueStore} from '../stores/playerQueueStore';
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import {useStore} from 'zustand';
|
|
2
|
+
import {usePlayerQueueSelectionStore} from '../stores/playerQueueSelectionStore';
|
|
3
|
+
import type {PlayerQueueSelectionStore} from '../stores/playerQueueSelectionStore';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* V15 Phase 65 (V16 Phase 69 hardened): the public hook for queue
|
|
7
|
+
* multi-select state.
|
|
8
|
+
*
|
|
9
|
+
* Two overloads:
|
|
10
|
+
* - `useQueueSelection()` returns the full store
|
|
11
|
+
* `{selectedIndices, setSelection, clearSelection, removeSelected, moveSelectedToTop}`.
|
|
12
|
+
* - `useQueueSelection(selector)` returns the selected slice.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```tsx
|
|
16
|
+
* function QueueManagementSheet() {
|
|
17
|
+
* const {selectedIndices, setSelection, clearSelection} = useQueueSelection();
|
|
18
|
+
* // ... render multi-select UI
|
|
19
|
+
* }
|
|
20
|
+
* ```
|
|
21
|
+
*/
|
|
22
|
+
export function useQueueSelection(): PlayerQueueSelectionStore;
|
|
23
|
+
export function useQueueSelection<T>(
|
|
24
|
+
selector: (state: PlayerQueueSelectionStore) => T,
|
|
25
|
+
): T;
|
|
26
|
+
export function useQueueSelection<T>(
|
|
27
|
+
selector?: (state: PlayerQueueSelectionStore) => T,
|
|
28
|
+
): PlayerQueueSelectionStore | T {
|
|
29
|
+
if (selector) {
|
|
30
|
+
return useStore(usePlayerQueueSelectionStore, selector);
|
|
31
|
+
}
|
|
32
|
+
return useStore(usePlayerQueueSelectionStore);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** Convenience hook: just the selected indices. */
|
|
36
|
+
export function useQueueSelectedIndices(): number[] {
|
|
37
|
+
return useStore(usePlayerQueueSelectionStore, state => state.selectedIndices);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export type {PlayerQueueSelectionStore} from '../stores/playerQueueSelectionStore';
|
package/src/index.ts
CHANGED
|
@@ -74,13 +74,30 @@ export {
|
|
|
74
74
|
type PlayerResumeProviderProps,
|
|
75
75
|
} from './hooks/useOpenWithResume';
|
|
76
76
|
|
|
77
|
-
export {SimbaPlayer, type SimbaPlayerProps} from './hooks/SimbaPlayer';
|
|
78
|
-
export {
|
|
79
|
-
useSimbaPlayerLookup,
|
|
80
|
-
type GetResumePosition,
|
|
81
|
-
} from './hooks/useSimbaPlayerLookup';
|
|
77
|
+
export {SimbaPlayer, type SimbaPlayerProps, type ResumePolicy} from './hooks/SimbaPlayer';
|
|
82
78
|
export {SimbaPlayerRoot, type SimbaPlayerRootProps} from './hooks/SimbaPlayerRoot';
|
|
83
79
|
export {useOpenFromUrl} from './hooks/useOpenFromUrl';
|
|
80
|
+
export {
|
|
81
|
+
useOpenPlaylist,
|
|
82
|
+
type PlayableEntry,
|
|
83
|
+
type OpenPlaylistOptions,
|
|
84
|
+
type UseOpenPlaylistResult,
|
|
85
|
+
} from './hooks/useOpenPlaylist';
|
|
86
|
+
export {
|
|
87
|
+
useQueue,
|
|
88
|
+
useQueueLength,
|
|
89
|
+
useQueueItems,
|
|
90
|
+
useQueueItemsAs,
|
|
91
|
+
usePlaybackHistory,
|
|
92
|
+
usePlaybackHistoryAs,
|
|
93
|
+
type PlayerQueueItem,
|
|
94
|
+
type PlayerQueueStore,
|
|
95
|
+
} from './hooks/useQueue';
|
|
96
|
+
export {
|
|
97
|
+
useQueueSelection,
|
|
98
|
+
useQueueSelectedIndices,
|
|
99
|
+
type PlayerQueueSelectionStore,
|
|
100
|
+
} from './hooks/useQueueSelection';
|
|
84
101
|
export {useLaunchParams} from './hooks/useLaunchParams';
|
|
85
102
|
|
|
86
103
|
export {
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import {create} from 'zustand';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* V15 Phase 65: the multi-select state for the queue UI.
|
|
5
|
+
*
|
|
6
|
+
* The queue screen has a multi-select mode where the user
|
|
7
|
+
* taps multiple items and applies a batch operation
|
|
8
|
+
* (remove / move-to-top). This store holds the selected
|
|
9
|
+
* indices; the batch operations are exposed as actions.
|
|
10
|
+
*/
|
|
11
|
+
export interface PlayerQueueSelectionActions {
|
|
12
|
+
setSelection: (indices: number[]) => void;
|
|
13
|
+
clearSelection: () => void;
|
|
14
|
+
removeSelected: () => number[];
|
|
15
|
+
/**
|
|
16
|
+
* Move all selected items to the top of the queue,
|
|
17
|
+
* preserving the original order. Returns the indices
|
|
18
|
+
* (relative to the queue BEFORE the move) that were moved.
|
|
19
|
+
*/
|
|
20
|
+
moveSelectedToTop: () => number[];
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export interface PlayerQueueSelectionStore
|
|
24
|
+
extends PlayerQueueSelectionActions {
|
|
25
|
+
selectedIndices: number[];
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export const usePlayerQueueSelectionStore = create<PlayerQueueSelectionStore>()(
|
|
29
|
+
(set, get) => ({
|
|
30
|
+
selectedIndices: [],
|
|
31
|
+
|
|
32
|
+
setSelection: (indices) => set({selectedIndices: indices}),
|
|
33
|
+
clearSelection: () => set({selectedIndices: []}),
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Read-side helper: returns the selected indices sorted
|
|
37
|
+
* descending. The caller (consumer) uses this to splice
|
|
38
|
+
* the actual queue array. Phase 65 doesn't move this
|
|
39
|
+
* into the store because the queue itself lives in a
|
|
40
|
+
* different store; we just expose the indices the
|
|
41
|
+
* consumer needs.
|
|
42
|
+
*/
|
|
43
|
+
removeSelected: () => {
|
|
44
|
+
const sorted = [...get().selectedIndices].sort((a, b) => b - a);
|
|
45
|
+
return sorted;
|
|
46
|
+
},
|
|
47
|
+
|
|
48
|
+
moveSelectedToTop: () => {
|
|
49
|
+
const sorted = [...get().selectedIndices].sort((a, b) => a - b);
|
|
50
|
+
return sorted;
|
|
51
|
+
},
|
|
52
|
+
}),
|
|
53
|
+
);
|
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
import {create} from 'zustand';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* V16 Phase 69: the queue item shape exposed by `useQueue()`.
|
|
5
|
+
*
|
|
6
|
+
* Structurally a superset of the consumer's V14 `playerSlice`
|
|
7
|
+
* queue entry. `uri` and `title` are required; the rest are
|
|
8
|
+
* optional and pass through to the store. The store does not
|
|
9
|
+
* inspect these fields — they're retained so the UI can
|
|
10
|
+
* display rich row content.
|
|
11
|
+
*
|
|
12
|
+
* The three classification fields (`source`, `type`, `mediaType`)
|
|
13
|
+
* are **generic** with `string` defaults. Consumers with
|
|
14
|
+
* narrower literal unions (e.g. `MediaSource`/`MediaKind`/
|
|
15
|
+
* `MediaLane` for SIMBA) specialize the generic:
|
|
16
|
+
*
|
|
17
|
+
* ```ts
|
|
18
|
+
* interface MyEntry extends PlayerQueueItem<MediaSource, MediaKind, MediaLane> {
|
|
19
|
+
* // consumer-only fields
|
|
20
|
+
* }
|
|
21
|
+
* ```
|
|
22
|
+
*
|
|
23
|
+
* With the generic specialized, a `MyEntry` flows through
|
|
24
|
+
* `addToQueue(entry)` and back via `useQueueItems()` without
|
|
25
|
+
* the consumer needing `as unknown as` casts at the boundary.
|
|
26
|
+
*
|
|
27
|
+
* Default usage (`PlayerQueueItem` with no generics) is
|
|
28
|
+
* backward-compatible with V15 — the three fields stay `string`.
|
|
29
|
+
*
|
|
30
|
+
* `resumePosition` and `autoplay` are added in V16 so a
|
|
31
|
+
* consumer's `PlaybackEntry` can extend `PlayerQueueItem`
|
|
32
|
+
* directly without losing its existing fields.
|
|
33
|
+
*/
|
|
34
|
+
export interface PlayerQueueItem<
|
|
35
|
+
TSource extends string = string,
|
|
36
|
+
TKind extends string = string,
|
|
37
|
+
TLane extends string = string,
|
|
38
|
+
> {
|
|
39
|
+
uri: string;
|
|
40
|
+
title: string;
|
|
41
|
+
duration: number;
|
|
42
|
+
artist?: string;
|
|
43
|
+
album?: string;
|
|
44
|
+
artworkUri?: string;
|
|
45
|
+
/** Coarse provenance (e.g. `MediaSource` = 'local' | 'api'). */
|
|
46
|
+
source?: TSource;
|
|
47
|
+
/** Stream type or content kind (e.g. `MediaKind`). */
|
|
48
|
+
type?: TKind;
|
|
49
|
+
/** Playback lane (e.g. `MediaLane` = 'audio' | 'video'). */
|
|
50
|
+
mediaType?: TLane;
|
|
51
|
+
provider?: string;
|
|
52
|
+
/** Stable linked-folder identity for local entries. */
|
|
53
|
+
folderId?: string;
|
|
54
|
+
/** V16: last native-confirmed playback position for resume. */
|
|
55
|
+
resumePosition?: number;
|
|
56
|
+
/** V16: whether the item should auto-play on load. */
|
|
57
|
+
autoplay?: boolean;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* V15 Phase 65: actions exposed by the queue store.
|
|
62
|
+
*/
|
|
63
|
+
export interface PlayerQueueActions {
|
|
64
|
+
/** Append a single item to the queue's tail. */
|
|
65
|
+
addToQueue: (item: PlayerQueueItem) => void;
|
|
66
|
+
/** Insert a single item at the queue's head ("Play Next"). */
|
|
67
|
+
prependToQueue: (item: PlayerQueueItem) => void;
|
|
68
|
+
/** Remove the item at the given index. */
|
|
69
|
+
removeFromQueue: (index: number) => void;
|
|
70
|
+
/**
|
|
71
|
+
* Reorder: move item at `fromIndex` to `toIndex`. Accepts
|
|
72
|
+
* either an object `{fromIndex, toIndex}` (preferred — matches
|
|
73
|
+
* the consumer's V14 dispatch shape) or two positional
|
|
74
|
+
* arguments. Other items shift accordingly.
|
|
75
|
+
*/
|
|
76
|
+
reorderQueue: (
|
|
77
|
+
fromIndexOrOpts: number | {fromIndex: number; toIndex: number},
|
|
78
|
+
toIndexArg?: number,
|
|
79
|
+
) => void;
|
|
80
|
+
/** Clear all items from the queue. */
|
|
81
|
+
clearQueue: () => void;
|
|
82
|
+
/**
|
|
83
|
+
* In-place Fisher-Yates shuffle of the queue array.
|
|
84
|
+
* The store's reference is preserved; the array contents
|
|
85
|
+
* are mutated.
|
|
86
|
+
*/
|
|
87
|
+
shuffleQueue: () => void;
|
|
88
|
+
/**
|
|
89
|
+
* V16 Phase 72: rename from `playFromQueue`. The old name
|
|
90
|
+
* implied "play this item" but the implementation only
|
|
91
|
+
* removed the item from the queue - it never promoted the
|
|
92
|
+
* item to the active playlist. The new name is honest about
|
|
93
|
+
* what the action does. To actually promote a queue item to
|
|
94
|
+
* the active playlist and play it, the consumer should call
|
|
95
|
+
* `useOpenPlaylist()` separately.
|
|
96
|
+
*/
|
|
97
|
+
removeFromQueueByIndex: (index: number) => void;
|
|
98
|
+
|
|
99
|
+
/** Append a single item to the playback history. */
|
|
100
|
+
addToPlaybackHistory: (item: PlayerQueueItem) => void;
|
|
101
|
+
/** Clear the playback history. */
|
|
102
|
+
clearPlaybackHistory: () => void;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* V15 Phase 65: the queue + playback-history store.
|
|
107
|
+
*/
|
|
108
|
+
export interface PlayerQueueStore extends PlayerQueueActions {
|
|
109
|
+
queue: PlayerQueueItem[];
|
|
110
|
+
playbackHistory: PlayerQueueItem[];
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* V16 Phase 72: rename from `playFromQueueImpl`. The old name
|
|
115
|
+
* implied "promote to playlist + play" but the implementation
|
|
116
|
+
* only spliced the item from the queue. The new name reflects
|
|
117
|
+
* what the action actually does: remove from queue. To
|
|
118
|
+
* promote a queue item to the active playlist, the consumer
|
|
119
|
+
* should call `useOpenPlaylist()` separately.
|
|
120
|
+
*/
|
|
121
|
+
function removeFromQueueByIndexImpl(
|
|
122
|
+
set: (updater: (state: PlayerQueueStore) => PlayerQueueStore) => void,
|
|
123
|
+
index: number,
|
|
124
|
+
): void {
|
|
125
|
+
set((state) => {
|
|
126
|
+
if (index < 0 || index >= state.queue.length) return state;
|
|
127
|
+
const [item] = state.queue.splice(index, 1);
|
|
128
|
+
if (!item) return state;
|
|
129
|
+
return {...state};
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
export const usePlayerQueueStore = create<PlayerQueueStore>()((set) => ({
|
|
134
|
+
queue: [],
|
|
135
|
+
playbackHistory: [],
|
|
136
|
+
|
|
137
|
+
addToQueue: (item) => set(state => ({queue: [...state.queue, item]})),
|
|
138
|
+
prependToQueue: (item) =>
|
|
139
|
+
set(state => ({queue: [item, ...state.queue]})),
|
|
140
|
+
removeFromQueue: (index) =>
|
|
141
|
+
set(state => {
|
|
142
|
+
if (index < 0 || index >= state.queue.length) return state;
|
|
143
|
+
return {queue: state.queue.filter((_, i) => i !== index)};
|
|
144
|
+
}),
|
|
145
|
+
reorderQueue: (fromIndexOrOpts, toIndexArg) =>
|
|
146
|
+
set(state => {
|
|
147
|
+
// Normalize the two accepted call shapes (object or positional).
|
|
148
|
+
const fromIndex =
|
|
149
|
+
typeof fromIndexOrOpts === 'number'
|
|
150
|
+
? fromIndexOrOpts
|
|
151
|
+
: fromIndexOrOpts.fromIndex;
|
|
152
|
+
const toIndex =
|
|
153
|
+
typeof fromIndexOrOpts === 'number'
|
|
154
|
+
? toIndexArg ?? fromIndexOrOpts
|
|
155
|
+
: fromIndexOrOpts.toIndex;
|
|
156
|
+
if (
|
|
157
|
+
fromIndex === toIndex ||
|
|
158
|
+
fromIndex < 0 ||
|
|
159
|
+
fromIndex >= state.queue.length ||
|
|
160
|
+
toIndex < 0 ||
|
|
161
|
+
toIndex >= state.queue.length
|
|
162
|
+
) {
|
|
163
|
+
return state;
|
|
164
|
+
}
|
|
165
|
+
const next = state.queue.slice();
|
|
166
|
+
const [moved] = next.splice(fromIndex, 1);
|
|
167
|
+
if (!moved) return state;
|
|
168
|
+
next.splice(toIndex, 0, moved);
|
|
169
|
+
return {queue: next};
|
|
170
|
+
}),
|
|
171
|
+
clearQueue: () => set({queue: []}),
|
|
172
|
+
shuffleQueue: () =>
|
|
173
|
+
set(state => {
|
|
174
|
+
const q = state.queue.slice();
|
|
175
|
+
for (let i = q.length - 1; i > 0; i--) {
|
|
176
|
+
const j = Math.floor(Math.random() * (i + 1));
|
|
177
|
+
[q[i], q[j]] = [q[j], q[i]];
|
|
178
|
+
}
|
|
179
|
+
return {queue: q};
|
|
180
|
+
}),
|
|
181
|
+
removeFromQueueByIndex: (index) => removeFromQueueByIndexImpl(set, index),
|
|
182
|
+
|
|
183
|
+
addToPlaybackHistory: (item) =>
|
|
184
|
+
set(state => ({playbackHistory: [...state.playbackHistory, item]})),
|
|
185
|
+
clearPlaybackHistory: () => set({playbackHistory: []}),
|
|
186
|
+
}));
|
|
@@ -1,58 +0,0 @@
|
|
|
1
|
-
import {useMemo} from 'react';
|
|
2
|
-
import type {PlayerResumeLookup} from './useOpenWithResume';
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* V14 Phase 61: a function the module calls to resolve a saved
|
|
6
|
-
* resume position for a media item. The consumer provides this
|
|
7
|
-
* once at the root of the app; every call site that wants
|
|
8
|
-
* resume-aware openPlayer just passes `resumeId: item.uri` to
|
|
9
|
-
* the hook's returned function.
|
|
10
|
-
*
|
|
11
|
-
* Mirrors the body of `PlayerResumeLookup.getResumePosition` —
|
|
12
|
-
* kept as a named type so `<SimbaPlayer getResumePosition={...}>`
|
|
13
|
-
* has a self-documenting prop type instead of an inline arrow.
|
|
14
|
-
*/
|
|
15
|
-
export type GetResumePosition = (itemId: string) => number | undefined;
|
|
16
|
-
|
|
17
|
-
const noopLookup: PlayerResumeLookup = {
|
|
18
|
-
getResumePosition: () => undefined,
|
|
19
|
-
};
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* V14 Phase 61: factory hook for a stable `PlayerResumeLookup`.
|
|
23
|
-
*
|
|
24
|
-
* Wraps an optional `selector` function in a memoized
|
|
25
|
-
* `PlayerResumeLookup` object so the lookup reference stays
|
|
26
|
-
* stable across renders. The point of this hook is to ship the
|
|
27
|
-
* **shape** so the consumer's `lookup={}` is always type-safe
|
|
28
|
-
* + stable — the consumer never has to write `useMemo<PlayerResumeLookup>(...)`
|
|
29
|
-
* themselves.
|
|
30
|
-
*
|
|
31
|
-
* Without a selector, returns a no-op lookup (always returns
|
|
32
|
-
* `undefined`).
|
|
33
|
-
*
|
|
34
|
-
* @example
|
|
35
|
-
* ```tsx
|
|
36
|
-
* // With a selector (most common):
|
|
37
|
-
* const resumeLookup = useSimbaPlayerLookup((uri) =>
|
|
38
|
-
* store.getState().bookmarks.byFileUri[uri]?.positionMs,
|
|
39
|
-
* );
|
|
40
|
-
*
|
|
41
|
-
* return (
|
|
42
|
-
* <SimbaPlayer lookup={resumeLookup}>
|
|
43
|
-
* <AppContent />
|
|
44
|
-
* </SimbaPlayer>
|
|
45
|
-
* );
|
|
46
|
-
*
|
|
47
|
-
* // Or — the function-prop shape (recommended for new consumers):
|
|
48
|
-
* <SimbaPlayer getResumePosition={(uri) => store.getState().bookmarks.byFileUri[uri]?.positionMs} />
|
|
49
|
-
* ```
|
|
50
|
-
*/
|
|
51
|
-
export function useSimbaPlayerLookup(
|
|
52
|
-
selector?: GetResumePosition,
|
|
53
|
-
): PlayerResumeLookup {
|
|
54
|
-
return useMemo<PlayerResumeLookup>(() => {
|
|
55
|
-
if (!selector) return noopLookup;
|
|
56
|
-
return {getResumePosition: selector};
|
|
57
|
-
}, [selector]);
|
|
58
|
-
}
|