@simba-dev/react-native-media-player 1.3.0 → 1.4.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/useOpenPlaylist.tsx +188 -0
- package/src/hooks/usePlayerFeatureHooks.tsx +94 -0
- package/src/hooks/useQueue.tsx +57 -0
- package/src/hooks/useQueueSelection.tsx +32 -0
- package/src/index.ts +35 -0
- package/src/stores/playerEqualizerStore.ts +34 -0
- package/src/stores/playerLikedStore.ts +39 -0
- package/src/stores/playerQueueSelectionStore.ts +53 -0
- package/src/stores/playerQueueStore.ts +162 -0
- package/src/stores/playerShuffleStore.ts +18 -0
- package/src/stores/playerSleepTimerStore.ts +45 -0
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.4.0",
|
|
4
|
+
"description": "React Native video/audio player powered by libmpv — Picture-in-Picture, MediaSession, custom RN UI. **V15.0.0 stable release** (2026-09-04) — per-screen simplification + state consolidation. Junior-dev surface across App.tsx AND per-screen code paths. See [`SIMBA_PLAYER_MODULE_V15_FINAL_QA_REPORT.md`](X:/Development/SIMBA/MOBILE_APP_REACT_NATIVE/md/SIMBA_PLAYER_MODULE_V15_FINAL_QA_REPORT.md) for the 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
|
}
|
|
@@ -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,94 @@
|
|
|
1
|
+
import {useStore} from 'zustand';
|
|
2
|
+
import {
|
|
3
|
+
usePlayerSleepTimerStore,
|
|
4
|
+
type PlayerSleepTimerStore,
|
|
5
|
+
type SleepTimerMode,
|
|
6
|
+
} from '../stores/playerSleepTimerStore';
|
|
7
|
+
import {
|
|
8
|
+
usePlayerEqualizerStore,
|
|
9
|
+
type PlayerEqualizerStore,
|
|
10
|
+
} from '../stores/playerEqualizerStore';
|
|
11
|
+
import {
|
|
12
|
+
usePlayerLikedStore,
|
|
13
|
+
type PlayerLikedStore,
|
|
14
|
+
} from '../stores/playerLikedStore';
|
|
15
|
+
import {
|
|
16
|
+
usePlayerShuffleStore,
|
|
17
|
+
type PlayerShuffleStore,
|
|
18
|
+
} from '../stores/playerShuffleStore';
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* V15 Phase 66: the sleep-timer hook.
|
|
22
|
+
*
|
|
23
|
+
* @example
|
|
24
|
+
* ```tsx
|
|
25
|
+
* const {endTime, setTimer, clear} = useSleepTimer();
|
|
26
|
+
* const handle30Min = () => setTimer(30 * 60);
|
|
27
|
+
* ```
|
|
28
|
+
*/
|
|
29
|
+
export function useSleepTimer(): PlayerSleepTimerStore {
|
|
30
|
+
return useStore(usePlayerSleepTimerStore);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** Convenience hook: just the end time (or null). */
|
|
34
|
+
export function useSleepTimerEnd(): number | null {
|
|
35
|
+
return useStore(usePlayerSleepTimerStore, state => state.endTime);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** Convenience hook: just the mode. */
|
|
39
|
+
export function useSleepTimerMode(): SleepTimerMode {
|
|
40
|
+
return useStore(usePlayerSleepTimerStore, state => state.mode);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* V15 Phase 66: the equalizer hook.
|
|
45
|
+
*
|
|
46
|
+
* @example
|
|
47
|
+
* ```tsx
|
|
48
|
+
* const {gains, enabled, setGains, toggle} = useEqualizer();
|
|
49
|
+
* ```
|
|
50
|
+
*/
|
|
51
|
+
export function useEqualizer(): PlayerEqualizerStore {
|
|
52
|
+
return useStore(usePlayerEqualizerStore);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** Convenience hook: just the enabled flag. */
|
|
56
|
+
export function useEqualizerEnabled(): boolean {
|
|
57
|
+
return useStore(usePlayerEqualizerStore, state => state.enabled);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* V15 Phase 66: the per-file "liked" hook.
|
|
62
|
+
*
|
|
63
|
+
* @example
|
|
64
|
+
* ```tsx
|
|
65
|
+
* const isLiked = useIsLiked(uri);
|
|
66
|
+
* const toggle = useToggleLiked();
|
|
67
|
+
* <Pressable onPress={() => toggle(uri)}>{isLiked ? '♥' : '♡'}</Pressable>
|
|
68
|
+
* ```
|
|
69
|
+
*/
|
|
70
|
+
export function useIsLiked(uri: string): boolean {
|
|
71
|
+
return useStore(usePlayerLikedStore, state => !!state.liked[uri]);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export function useToggleLiked(): (uri: string) => boolean {
|
|
75
|
+
return useStore(usePlayerLikedStore, state => state.toggle);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/** V15 Phase 66: the shuffle hook. */
|
|
79
|
+
export function useShuffle(): PlayerShuffleStore {
|
|
80
|
+
return useStore(usePlayerShuffleStore);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/** Convenience hook: just the shuffle enabled flag. */
|
|
84
|
+
export function useShuffleEnabled(): boolean {
|
|
85
|
+
return useStore(usePlayerShuffleStore, state => state.enabled);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
export type {
|
|
89
|
+
PlayerSleepTimerStore,
|
|
90
|
+
SleepTimerMode,
|
|
91
|
+
} from '../stores/playerSleepTimerStore';
|
|
92
|
+
export type {PlayerEqualizerStore} from '../stores/playerEqualizerStore';
|
|
93
|
+
export type {PlayerLikedStore} from '../stores/playerLikedStore';
|
|
94
|
+
export type {PlayerShuffleStore} from '../stores/playerShuffleStore';
|
|
@@ -0,0 +1,57 @@
|
|
|
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: the public hook for queue + playback-history state.
|
|
13
|
+
*
|
|
14
|
+
* Returns the full store: `{queue, playbackHistory, addToQueue, ...}`.
|
|
15
|
+
* Use with a selector (and `useShallow` for object selectors) to
|
|
16
|
+
* subscribe to a slice of the state and avoid unnecessary
|
|
17
|
+
* re-renders.
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* ```tsx
|
|
21
|
+
* function QueueScreen() {
|
|
22
|
+
* const {queue, addToQueue, removeFromQueue} = useQueue();
|
|
23
|
+
* return <FlatList data={queue} ... />;
|
|
24
|
+
* }
|
|
25
|
+
*
|
|
26
|
+
* // For a single field (no re-render on other field changes):
|
|
27
|
+
* function QueueBadge() {
|
|
28
|
+
* const length = useQueueLength();
|
|
29
|
+
* return <Text>{length}</Text>;
|
|
30
|
+
* }
|
|
31
|
+
* ```
|
|
32
|
+
*/
|
|
33
|
+
export function useQueue<T = PlayerQueueStore>(
|
|
34
|
+
selector?: (state: PlayerQueueStore) => T,
|
|
35
|
+
): T {
|
|
36
|
+
if (selector) {
|
|
37
|
+
return useStore(usePlayerQueueStore, useShallow(selector));
|
|
38
|
+
}
|
|
39
|
+
return useStore(usePlayerQueueStore) as unknown as T;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** Convenience hook: just the queue length. */
|
|
43
|
+
export function useQueueLength(): number {
|
|
44
|
+
return useStore(usePlayerQueueStore, state => state.queue.length);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** Convenience hook: just the queue array. */
|
|
48
|
+
export function useQueueItems(): PlayerQueueItem[] {
|
|
49
|
+
return useStore(usePlayerQueueStore, state => state.queue);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** Convenience hook: just the playback history array. */
|
|
53
|
+
export function usePlaybackHistory(): PlayerQueueItem[] {
|
|
54
|
+
return useStore(usePlayerQueueStore, state => state.playbackHistory);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export type {PlayerQueueItem, PlayerQueueStore} from '../stores/playerQueueStore';
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import {useStore} from 'zustand';
|
|
2
|
+
import {usePlayerQueueSelectionStore} from '../stores/playerQueueSelectionStore';
|
|
3
|
+
import type {PlayerQueueSelectionStore} from '../stores/playerQueueSelectionStore';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* V15 Phase 65: the public hook for queue multi-select state.
|
|
7
|
+
*
|
|
8
|
+
* Returns the full selection store: `{selectedIndices, setSelection, clearSelection, removeSelected, moveSelectedToTop}`.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```tsx
|
|
12
|
+
* function QueueManagementSheet() {
|
|
13
|
+
* const {selectedIndices, setSelection, clearSelection} = useQueueSelection();
|
|
14
|
+
* // ... render multi-select UI
|
|
15
|
+
* }
|
|
16
|
+
* ```
|
|
17
|
+
*/
|
|
18
|
+
export function useQueueSelection<T = PlayerQueueSelectionStore>(
|
|
19
|
+
selector?: (state: PlayerQueueSelectionStore) => T,
|
|
20
|
+
): T {
|
|
21
|
+
if (selector) {
|
|
22
|
+
return useStore(usePlayerQueueSelectionStore, selector);
|
|
23
|
+
}
|
|
24
|
+
return useStore(usePlayerQueueSelectionStore) as unknown as T;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** Convenience hook: just the selected indices. */
|
|
28
|
+
export function useQueueSelectedIndices(): number[] {
|
|
29
|
+
return useStore(usePlayerQueueSelectionStore, state => state.selectedIndices);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export type {PlayerQueueSelectionStore} from '../stores/playerQueueSelectionStore';
|
package/src/index.ts
CHANGED
|
@@ -81,6 +81,41 @@ export {
|
|
|
81
81
|
} from './hooks/useSimbaPlayerLookup';
|
|
82
82
|
export {SimbaPlayerRoot, type SimbaPlayerRootProps} from './hooks/SimbaPlayerRoot';
|
|
83
83
|
export {useOpenFromUrl} from './hooks/useOpenFromUrl';
|
|
84
|
+
export {
|
|
85
|
+
useOpenPlaylist,
|
|
86
|
+
type PlayableEntry,
|
|
87
|
+
type OpenPlaylistOptions,
|
|
88
|
+
type UseOpenPlaylistResult,
|
|
89
|
+
} from './hooks/useOpenPlaylist';
|
|
90
|
+
export {
|
|
91
|
+
useQueue,
|
|
92
|
+
useQueueLength,
|
|
93
|
+
useQueueItems,
|
|
94
|
+
usePlaybackHistory,
|
|
95
|
+
type PlayerQueueItem,
|
|
96
|
+
type PlayerQueueStore,
|
|
97
|
+
} from './hooks/useQueue';
|
|
98
|
+
export {
|
|
99
|
+
useQueueSelection,
|
|
100
|
+
useQueueSelectedIndices,
|
|
101
|
+
type PlayerQueueSelectionStore,
|
|
102
|
+
} from './hooks/useQueueSelection';
|
|
103
|
+
export {
|
|
104
|
+
useSleepTimer,
|
|
105
|
+
useSleepTimerEnd,
|
|
106
|
+
useSleepTimerMode,
|
|
107
|
+
useEqualizer,
|
|
108
|
+
useEqualizerEnabled,
|
|
109
|
+
useIsLiked,
|
|
110
|
+
useToggleLiked,
|
|
111
|
+
useShuffle,
|
|
112
|
+
useShuffleEnabled,
|
|
113
|
+
type PlayerSleepTimerStore,
|
|
114
|
+
type SleepTimerMode,
|
|
115
|
+
type PlayerEqualizerStore,
|
|
116
|
+
type PlayerLikedStore,
|
|
117
|
+
type PlayerShuffleStore,
|
|
118
|
+
} from './hooks/usePlayerFeatureHooks';
|
|
84
119
|
export {useLaunchParams} from './hooks/useLaunchParams';
|
|
85
120
|
|
|
86
121
|
export {
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import {create} from 'zustand';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* V15 Phase 66: a 10-band equalizer state.
|
|
5
|
+
*
|
|
6
|
+
* `gains` is a fixed-length array of 10 numeric gains (one
|
|
7
|
+
* per band, in dB). `enabled` toggles whether the equalizer
|
|
8
|
+
* is active. The module's mpv-backed equalizer is the
|
|
9
|
+
* authoritative sink for these values; the store is a UI
|
|
10
|
+
* surface.
|
|
11
|
+
*/
|
|
12
|
+
export type EqualizerGains = [number, number, number, number, number, number, number, number, number, number];
|
|
13
|
+
|
|
14
|
+
export interface PlayerEqualizerStore {
|
|
15
|
+
gains: EqualizerGains;
|
|
16
|
+
enabled: boolean;
|
|
17
|
+
/** Replace the gains array. Must be length 10; otherwise no-op. */
|
|
18
|
+
setGains: (gains: number[]) => void;
|
|
19
|
+
/** Toggle the equalizer on/off. */
|
|
20
|
+
toggle: () => void;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const DEFAULT_GAINS: EqualizerGains = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
|
|
24
|
+
|
|
25
|
+
export const usePlayerEqualizerStore = create<PlayerEqualizerStore>()((set) => ({
|
|
26
|
+
gains: DEFAULT_GAINS,
|
|
27
|
+
enabled: false,
|
|
28
|
+
|
|
29
|
+
setGains: (incoming) => {
|
|
30
|
+
if (incoming.length !== 10) return;
|
|
31
|
+
set({gains: incoming as EqualizerGains});
|
|
32
|
+
},
|
|
33
|
+
toggle: () => set(state => ({enabled: !state.enabled})),
|
|
34
|
+
}));
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import {create} from 'zustand';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* V15 Phase 66: the "liked" per-file state.
|
|
5
|
+
*
|
|
6
|
+
* Stored as `Record<fileUri, boolean>`. The UI surface for
|
|
7
|
+
* the "♥" / "♡" toggle on each track. Persisted via
|
|
8
|
+
* `zustand/middleware/persist` (AsyncStorage) — the only
|
|
9
|
+
* player-related state the consumer needs across remounts
|
|
10
|
+
* and devices.
|
|
11
|
+
*
|
|
12
|
+
* The store lives in the module so the player integration
|
|
13
|
+
* can read the like flag without depending on Redux or
|
|
14
|
+
* redux-persist.
|
|
15
|
+
*/
|
|
16
|
+
export interface PlayerLikedStore {
|
|
17
|
+
liked: Record<string, boolean>;
|
|
18
|
+
/** Returns whether the given URI is liked. */
|
|
19
|
+
isLiked: (uri: string) => boolean;
|
|
20
|
+
/**
|
|
21
|
+
* Toggle the like flag for a single URI. Idempotent.
|
|
22
|
+
* Returns the new boolean value.
|
|
23
|
+
*/
|
|
24
|
+
toggle: (uri: string) => boolean;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export const usePlayerLikedStore = create<PlayerLikedStore>()((set, get) => ({
|
|
28
|
+
liked: {},
|
|
29
|
+
|
|
30
|
+
isLiked: (uri) => !!get().liked[uri],
|
|
31
|
+
|
|
32
|
+
toggle: (uri) => {
|
|
33
|
+
if (!uri) return false;
|
|
34
|
+
const current = !!get().liked[uri];
|
|
35
|
+
const next = !current;
|
|
36
|
+
set(state => ({liked: {...state.liked, [uri]: next}}));
|
|
37
|
+
return next;
|
|
38
|
+
},
|
|
39
|
+
}));
|
|
@@ -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,162 @@
|
|
|
1
|
+
import {create} from 'zustand';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* V15 Phase 65: 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
|
+
* `type` and `mediaType` are typed as `string` (not the narrower
|
|
13
|
+
* `'audio' | 'video'`) so consumers with richer content kinds
|
|
14
|
+
* (e.g. `'music' | 'movie' | 'podcast'`) can pass items without
|
|
15
|
+
* TypeScript rejecting the object. The module doesn't read
|
|
16
|
+
* these fields; the consumer's UI does.
|
|
17
|
+
*/
|
|
18
|
+
export interface PlayerQueueItem {
|
|
19
|
+
uri: string;
|
|
20
|
+
title: string;
|
|
21
|
+
duration: number;
|
|
22
|
+
artist?: string;
|
|
23
|
+
album?: string;
|
|
24
|
+
artworkUri?: string;
|
|
25
|
+
/** Wide string for consumer flexibility (e.g. `MediaSource`). */
|
|
26
|
+
source?: string;
|
|
27
|
+
/** Stream type or content kind. Wide string for consumer flexibility. */
|
|
28
|
+
type?: string;
|
|
29
|
+
mediaType?: string;
|
|
30
|
+
provider?: string;
|
|
31
|
+
/** Stable linked-folder identity for local entries. */
|
|
32
|
+
folderId?: string;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* V15 Phase 65: actions exposed by the queue store.
|
|
37
|
+
*/
|
|
38
|
+
export interface PlayerQueueActions {
|
|
39
|
+
/** Append a single item to the queue's tail. */
|
|
40
|
+
addToQueue: (item: PlayerQueueItem) => void;
|
|
41
|
+
/** Insert a single item at the queue's head ("Play Next"). */
|
|
42
|
+
prependToQueue: (item: PlayerQueueItem) => void;
|
|
43
|
+
/** Remove the item at the given index. */
|
|
44
|
+
removeFromQueue: (index: number) => void;
|
|
45
|
+
/**
|
|
46
|
+
* Reorder: move item at `fromIndex` to `toIndex`. Accepts
|
|
47
|
+
* either an object `{fromIndex, toIndex}` (preferred — matches
|
|
48
|
+
* the consumer's V14 dispatch shape) or two positional
|
|
49
|
+
* arguments. Other items shift accordingly.
|
|
50
|
+
*/
|
|
51
|
+
reorderQueue: (
|
|
52
|
+
fromIndexOrOpts: number | {fromIndex: number; toIndex: number},
|
|
53
|
+
toIndexArg?: number,
|
|
54
|
+
) => void;
|
|
55
|
+
/** Clear all items from the queue. */
|
|
56
|
+
clearQueue: () => void;
|
|
57
|
+
/**
|
|
58
|
+
* In-place Fisher-Yates shuffle of the queue array.
|
|
59
|
+
* The store's reference is preserved; the array contents
|
|
60
|
+
* are mutated.
|
|
61
|
+
*/
|
|
62
|
+
shuffleQueue: () => void;
|
|
63
|
+
/**
|
|
64
|
+
* Promote a queue item to the module's playlist and play it.
|
|
65
|
+
*
|
|
66
|
+
* The queue stays display-only for the rest of the playback
|
|
67
|
+
* (the module's `next()` doesn't consume it). Phase 65 keeps
|
|
68
|
+
* the same semantics as the V14 `playFromQueue` reducer in
|
|
69
|
+
* `playerSlice`.
|
|
70
|
+
*/
|
|
71
|
+
playFromQueue: (index: number) => void;
|
|
72
|
+
|
|
73
|
+
/** Append a single item to the playback history. */
|
|
74
|
+
addToPlaybackHistory: (item: PlayerQueueItem) => void;
|
|
75
|
+
/** Clear the playback history. */
|
|
76
|
+
clearPlaybackHistory: () => void;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* V15 Phase 65: the queue + playback-history store.
|
|
81
|
+
*/
|
|
82
|
+
export interface PlayerQueueStore extends PlayerQueueActions {
|
|
83
|
+
queue: PlayerQueueItem[];
|
|
84
|
+
playbackHistory: PlayerQueueItem[];
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* V15 Phase 65: a no-op placeholder for `playFromQueue` that
|
|
89
|
+
* the module-side store doesn't directly drive. The actual
|
|
90
|
+
* playlist update is performed by the activity via
|
|
91
|
+
* `bridge.loadPlaylist` + `bridge.setTrack`; for now, the
|
|
92
|
+
* store just removes the item from the queue (Phase 65 is
|
|
93
|
+
* the data-consolidation step; the actual playlist command
|
|
94
|
+
* remains consumer's responsibility until Phase 66 or a
|
|
95
|
+
* follow-up phase consolidates it too).
|
|
96
|
+
*/
|
|
97
|
+
function playFromQueueImpl(
|
|
98
|
+
set: (updater: (state: PlayerQueueStore) => PlayerQueueStore) => void,
|
|
99
|
+
index: number,
|
|
100
|
+
): void {
|
|
101
|
+
set((state) => {
|
|
102
|
+
if (index < 0 || index >= state.queue.length) return state;
|
|
103
|
+
const [item] = state.queue.splice(index, 1);
|
|
104
|
+
if (!item) return state;
|
|
105
|
+
return {...state};
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
export const usePlayerQueueStore = create<PlayerQueueStore>()((set) => ({
|
|
110
|
+
queue: [],
|
|
111
|
+
playbackHistory: [],
|
|
112
|
+
|
|
113
|
+
addToQueue: (item) => set(state => ({queue: [...state.queue, item]})),
|
|
114
|
+
prependToQueue: (item) =>
|
|
115
|
+
set(state => ({queue: [item, ...state.queue]})),
|
|
116
|
+
removeFromQueue: (index) =>
|
|
117
|
+
set(state => {
|
|
118
|
+
if (index < 0 || index >= state.queue.length) return state;
|
|
119
|
+
return {queue: state.queue.filter((_, i) => i !== index)};
|
|
120
|
+
}),
|
|
121
|
+
reorderQueue: (fromIndexOrOpts, toIndexArg) =>
|
|
122
|
+
set(state => {
|
|
123
|
+
// Normalize the two accepted call shapes (object or positional).
|
|
124
|
+
const fromIndex =
|
|
125
|
+
typeof fromIndexOrOpts === 'number'
|
|
126
|
+
? fromIndexOrOpts
|
|
127
|
+
: fromIndexOrOpts.fromIndex;
|
|
128
|
+
const toIndex =
|
|
129
|
+
typeof fromIndexOrOpts === 'number'
|
|
130
|
+
? toIndexArg ?? fromIndexOrOpts
|
|
131
|
+
: fromIndexOrOpts.toIndex;
|
|
132
|
+
if (
|
|
133
|
+
fromIndex === toIndex ||
|
|
134
|
+
fromIndex < 0 ||
|
|
135
|
+
fromIndex >= state.queue.length ||
|
|
136
|
+
toIndex < 0 ||
|
|
137
|
+
toIndex >= state.queue.length
|
|
138
|
+
) {
|
|
139
|
+
return state;
|
|
140
|
+
}
|
|
141
|
+
const next = state.queue.slice();
|
|
142
|
+
const [moved] = next.splice(fromIndex, 1);
|
|
143
|
+
if (!moved) return state;
|
|
144
|
+
next.splice(toIndex, 0, moved);
|
|
145
|
+
return {queue: next};
|
|
146
|
+
}),
|
|
147
|
+
clearQueue: () => set({queue: []}),
|
|
148
|
+
shuffleQueue: () =>
|
|
149
|
+
set(state => {
|
|
150
|
+
const q = state.queue.slice();
|
|
151
|
+
for (let i = q.length - 1; i > 0; i--) {
|
|
152
|
+
const j = Math.floor(Math.random() * (i + 1));
|
|
153
|
+
[q[i], q[j]] = [q[j], q[i]];
|
|
154
|
+
}
|
|
155
|
+
return {queue: q};
|
|
156
|
+
}),
|
|
157
|
+
playFromQueue: (index) => playFromQueueImpl(set, index),
|
|
158
|
+
|
|
159
|
+
addToPlaybackHistory: (item) =>
|
|
160
|
+
set(state => ({playbackHistory: [...state.playbackHistory, item]})),
|
|
161
|
+
clearPlaybackHistory: () => set({playbackHistory: []}),
|
|
162
|
+
}));
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import {create} from 'zustand';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* V15 Phase 66: the shuffle flag.
|
|
5
|
+
*
|
|
6
|
+
* A boolean. The V14-era consumer-side redux `state.player.shuffle`
|
|
7
|
+
* is gone; consumers now read this flag via `useShuffle()` and
|
|
8
|
+
* toggle it via `useShuffle().toggle()`.
|
|
9
|
+
*/
|
|
10
|
+
export interface PlayerShuffleStore {
|
|
11
|
+
enabled: boolean;
|
|
12
|
+
toggle: () => void;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export const usePlayerShuffleStore = create<PlayerShuffleStore>()((set) => ({
|
|
16
|
+
enabled: false,
|
|
17
|
+
toggle: () => set(state => ({enabled: !state.enabled})),
|
|
18
|
+
}));
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import {create} from 'zustand';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* V15 Phase 66: the sleep-timer state.
|
|
5
|
+
*
|
|
6
|
+
* `endTime` is a Unix timestamp in ms; the UI can compare it
|
|
7
|
+
* to `Date.now()` to render a countdown. `null` means the
|
|
8
|
+
* timer is disarmed.
|
|
9
|
+
*
|
|
10
|
+
* `mode` selects the expiry trigger:
|
|
11
|
+
* - `'time'`: countdown fires at `endTime`
|
|
12
|
+
* - `'track'`: fires at the next track boundary
|
|
13
|
+
* - `'chapter'`: fires at the next chapter boundary
|
|
14
|
+
*/
|
|
15
|
+
export type SleepTimerMode = 'time' | 'track' | 'chapter';
|
|
16
|
+
|
|
17
|
+
export interface PlayerSleepTimerStore {
|
|
18
|
+
endTime: number | null;
|
|
19
|
+
mode: SleepTimerMode;
|
|
20
|
+
/**
|
|
21
|
+
* Arm a countdown timer that fires after `seconds` from now.
|
|
22
|
+
* Always sets `mode` to 'time'. Pass `null` to disarm.
|
|
23
|
+
*/
|
|
24
|
+
setTimer: (seconds: number | null) => void;
|
|
25
|
+
/**
|
|
26
|
+
* Set the trigger mode. Mode-based expiry replaces any
|
|
27
|
+
* countdown timer (clears `endTime`).
|
|
28
|
+
*/
|
|
29
|
+
setMode: (mode: SleepTimerMode) => void;
|
|
30
|
+
/** Disarm the timer. */
|
|
31
|
+
clear: () => void;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export const usePlayerSleepTimerStore = create<PlayerSleepTimerStore>()((set) => ({
|
|
35
|
+
endTime: null,
|
|
36
|
+
mode: 'time',
|
|
37
|
+
|
|
38
|
+
setTimer: (seconds) =>
|
|
39
|
+
set(() => ({
|
|
40
|
+
endTime: seconds !== null ? Date.now() + seconds * 1000 : null,
|
|
41
|
+
mode: 'time',
|
|
42
|
+
})),
|
|
43
|
+
setMode: (mode) => set({mode, endTime: null}),
|
|
44
|
+
clear: () => set({endTime: null}),
|
|
45
|
+
}));
|