mirror of
https://github.com/austinried/subtracks.git
synced 2025-12-29 17:39:27 +01:00
switched queue ext store to zustand
This commit is contained in:
parent
661b17f232
commit
ebc31e6d05
@ -1,11 +1,12 @@
|
|||||||
import { Song } from '@app/models/music'
|
import { Song } from '@app/models/music'
|
||||||
import atomWithAsyncStorage from '@app/storage/atomWithAsyncStorage'
|
|
||||||
import PromiseQueue from '@app/util/PromiseQueue'
|
import PromiseQueue from '@app/util/PromiseQueue'
|
||||||
import equal from 'fast-deep-equal'
|
import equal from 'fast-deep-equal'
|
||||||
import { atom } from 'jotai'
|
import { atom } from 'jotai'
|
||||||
import { useAtomCallback, useAtomValue, useUpdateAtom } from 'jotai/utils'
|
import { useAtomCallback, useAtomValue, useUpdateAtom } from 'jotai/utils'
|
||||||
|
import { atomWithStore } from 'jotai/zustand'
|
||||||
import { useCallback, useEffect } from 'react'
|
import { useCallback, useEffect } from 'react'
|
||||||
import TrackPlayer, { State, Track } from 'react-native-track-player'
|
import TrackPlayer, { State, Track } from 'react-native-track-player'
|
||||||
|
import create from 'zustand'
|
||||||
import { useCoverArtUri } from './music'
|
import { useCoverArtUri } from './music'
|
||||||
|
|
||||||
type TrackExt = Track & {
|
type TrackExt = Track & {
|
||||||
@ -21,41 +22,41 @@ type Progress = {
|
|||||||
buffered: number
|
buffered: number
|
||||||
}
|
}
|
||||||
|
|
||||||
type QueueExt = {
|
type QueueStore = {
|
||||||
name?: string
|
name?: string
|
||||||
|
setName: (name?: string) => void
|
||||||
shuffleOrder?: number[]
|
shuffleOrder?: number[]
|
||||||
|
setShuffleOrder: (shuffleOrder?: number[]) => void
|
||||||
|
shuffled: () => boolean
|
||||||
|
reset: () => void
|
||||||
}
|
}
|
||||||
|
|
||||||
const queueExtAtom = atomWithAsyncStorage<QueueExt>('@queue', {})
|
const useStore = create<QueueStore>((set, get) => ({
|
||||||
|
name: undefined,
|
||||||
|
setName: (name?: string) => set({ name }),
|
||||||
|
shuffleOrder: undefined,
|
||||||
|
setShuffleOrder: (shuffleOrder?: number[]) => set({ shuffleOrder }),
|
||||||
|
shuffled: () => !!get().shuffleOrder,
|
||||||
|
reset: () => set({ name: undefined, shuffleOrder: undefined }),
|
||||||
|
}))
|
||||||
|
|
||||||
export const queueNameAtom = atom<string | undefined>(get => get(queueExtAtom).name)
|
const queueStoreAtom = atomWithStore(useStore)
|
||||||
export const queueShuffledAtom = atom<boolean>(get => get(queueExtAtom).shuffleOrder !== undefined)
|
|
||||||
|
export const queueNameAtom = atom<string | undefined, string | undefined>(
|
||||||
|
get => get(queueStoreAtom).name,
|
||||||
|
(get, set, update) => {
|
||||||
|
get(queueStoreAtom).setName(update)
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
const queueShuffleOrderAtom = atom<number[] | undefined, number[] | undefined>(
|
const queueShuffleOrderAtom = atom<number[] | undefined, number[] | undefined>(
|
||||||
get => get(queueExtAtom).shuffleOrder,
|
get => get(queueStoreAtom).shuffleOrder,
|
||||||
(get, set, update) => {
|
(get, set, update) => {
|
||||||
const queueExt = get(queueExtAtom)
|
get(queueStoreAtom).setShuffleOrder(update)
|
||||||
if (!equal(queueExt.shuffleOrder, update)) {
|
|
||||||
set(queueExtAtom, {
|
|
||||||
...queueExt,
|
|
||||||
shuffleOrder: update,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
const queueNameWriteAtom = atom<string | undefined, string | undefined>(
|
export const queueShuffledAtom = atom<boolean>(get => get(queueStoreAtom).shuffled())
|
||||||
get => get(queueExtAtom).name,
|
|
||||||
(get, set, update) => {
|
|
||||||
const queueExt = get(queueExtAtom)
|
|
||||||
if (!equal(queueExt.name, update)) {
|
|
||||||
set(queueExtAtom, {
|
|
||||||
...queueExt,
|
|
||||||
name: update,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
},
|
|
||||||
)
|
|
||||||
|
|
||||||
const playerState = atom<State>(State.None)
|
const playerState = atom<State>(State.None)
|
||||||
export const playerStateAtom = atom<State, State>(
|
export const playerStateAtom = atom<State, State>(
|
||||||
@ -223,14 +224,14 @@ export const useNext = () => {
|
|||||||
|
|
||||||
export const useReset = (enqueue = true) => {
|
export const useReset = (enqueue = true) => {
|
||||||
const setQueue = useUpdateAtom(queueAtom)
|
const setQueue = useUpdateAtom(queueAtom)
|
||||||
const setQueueExt = useUpdateAtom(queueExtAtom)
|
|
||||||
const setCurrentTrack = useUpdateAtom(currentTrackAtom)
|
const setCurrentTrack = useUpdateAtom(currentTrackAtom)
|
||||||
|
const resetQueueStore = useStore(state => state.reset)
|
||||||
|
|
||||||
const reset = async () => {
|
const reset = async () => {
|
||||||
await TrackPlayer.reset()
|
await TrackPlayer.reset()
|
||||||
setQueue([])
|
setQueue([])
|
||||||
setQueueExt({})
|
|
||||||
setCurrentTrack(undefined)
|
setCurrentTrack(undefined)
|
||||||
|
resetQueueStore()
|
||||||
}
|
}
|
||||||
|
|
||||||
return enqueue ? () => trackPlayerCommands.enqueue(reset) : reset
|
return enqueue ? () => trackPlayerCommands.enqueue(reset) : reset
|
||||||
@ -314,7 +315,7 @@ export const useSetQueue = () => {
|
|||||||
const setCurrentTrack = useUpdateAtom(currentTrackAtom)
|
const setCurrentTrack = useUpdateAtom(currentTrackAtom)
|
||||||
const setQueue = useUpdateAtom(queueAtom)
|
const setQueue = useUpdateAtom(queueAtom)
|
||||||
const setQueueShuffleOrder = useUpdateAtom(queueShuffleOrderAtom)
|
const setQueueShuffleOrder = useUpdateAtom(queueShuffleOrderAtom)
|
||||||
const setQueueName = useUpdateAtom(queueNameWriteAtom)
|
const setQueueName = useUpdateAtom(queueNameAtom)
|
||||||
const reset = useReset(false)
|
const reset = useReset(false)
|
||||||
const getQueueShuffled = useAtomCallback(useCallback(get => get(queueShuffledAtom), []))
|
const getQueueShuffled = useAtomCallback(useCallback(get => get(queueShuffledAtom), []))
|
||||||
const coverArtUri = useCoverArtUri()
|
const coverArtUri = useCoverArtUri()
|
||||||
|
|||||||
@ -35,7 +35,8 @@
|
|||||||
"react-native-track-player": "^2.0.0-rc18",
|
"react-native-track-player": "^2.0.0-rc18",
|
||||||
"react-native-vector-icons": "^8.1.0",
|
"react-native-vector-icons": "^8.1.0",
|
||||||
"uuid": "^8.3.2",
|
"uuid": "^8.3.2",
|
||||||
"xmldom": "^0.5.0"
|
"xmldom": "^0.5.0",
|
||||||
|
"zustand": "^3.5.7"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@babel/core": "^7.12.9",
|
"@babel/core": "^7.12.9",
|
||||||
|
|||||||
@ -7060,3 +7060,8 @@ yargs@^16.1.1:
|
|||||||
string-width "^4.2.0"
|
string-width "^4.2.0"
|
||||||
y18n "^5.0.5"
|
y18n "^5.0.5"
|
||||||
yargs-parser "^20.2.2"
|
yargs-parser "^20.2.2"
|
||||||
|
|
||||||
|
zustand@^3.5.7:
|
||||||
|
version "3.5.7"
|
||||||
|
resolved "https://registry.yarnpkg.com/zustand/-/zustand-3.5.7.tgz#add5e8d0ba031ce6e0ddf9cb76ef15306efb665f"
|
||||||
|
integrity sha512-DlVFXJavIHyXTOGz6dB+8QHZsPyJcGJSEBtlp2Ivmd5SwtlCnhPo3L8LB6YRfAOJC2PbqzgoD8NMjk+y+vIF0g==
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user