@simba-dev/react-native-media-player 1.2.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.
@@ -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
+ }));
@@ -1,21 +0,0 @@
1
- import React from 'react';
2
- import { PlayerResumeContext } from './useOpenWithResume';
3
- import type { PlayerResumeProviderProps } from './useOpenWithResume';
4
-
5
- /**
6
- * JSX shim for `<PlayerResumeProvider>`. The context itself lives in
7
- * `useOpenWithResume.ts` (a `.ts` file); the JSX-using component
8
- * lives here (a `.tsx` file) so TypeScript's legacy JSX transform
9
- * (`jsx: "react-native"` in the project's tsconfig) can parse the
10
- * JSX without a `.ts`-file-with-JSX parse error.
11
- */
12
- export function PlayerResumeProvider({
13
- lookup,
14
- children,
15
- }: PlayerResumeProviderProps): React.ReactElement {
16
- return (
17
- <PlayerResumeContext.Provider value={lookup}>
18
- {children}
19
- </PlayerResumeContext.Provider>
20
- );
21
- }