Fix arranging songs in PlayQueue

This commit is contained in:
Deluan
2021-07-03 20:35:21 -04:00
parent ace5c905eb
commit fa8b4d40b4
4 changed files with 36 additions and 33 deletions
+5 -3
View File
@@ -68,10 +68,12 @@ export const playTracks = (data, ids, selectedId) => {
} }
} }
export const syncQueue = (id, data) => ({ export const syncQueue = (audioInfo, audioLists) => ({
type: PLAYER_SYNC_QUEUE, type: PLAYER_SYNC_QUEUE,
id, data: {
data, audioInfo,
audioLists,
},
}) })
export const clearQueue = () => ({ export const clearQueue = () => ({
+1 -2
View File
@@ -82,8 +82,7 @@ const Player = () => {
}, [playerState, defaultOptions]) }, [playerState, defaultOptions])
const onAudioListsChange = useCallback( const onAudioListsChange = useCallback(
(currentPlayIndex, audioLists) => (_, audioLists, audioInfo) => dispatch(syncQueue(audioInfo, audioLists)),
dispatch(syncQueue(currentPlayIndex, audioLists)),
[dispatch] [dispatch]
) )
+26 -27
View File
@@ -17,6 +17,7 @@ const initialState = {
current: {}, current: {},
clear: false, clear: false,
volume: 1, volume: 1,
savedPlayIndex: 0,
} }
const mapToAudioLists = (item) => { const mapToAudioLists = (item) => {
@@ -58,15 +59,6 @@ const reducePlayTracks = (state, { data, id }) => {
} }
} }
const reduceSyncQueue = (state, { data }) => {
const current = data.length > 0 ? state.current : {}
return {
...state,
current,
queue: data,
}
}
const reduceSetTrack = (state, { data }) => { const reduceSetTrack = (state, { data }) => {
return { return {
...state, ...state,
@@ -103,13 +95,9 @@ const reducePlayNext = (state, { data }) => {
}) })
} }
const playIndex = state.queue.findIndex((item) => item.uuid === current.uuid)
return { return {
...state, ...state,
queue: newQueue, queue: newQueue,
// TODO: This is a workaround for a bug in the player that resets the playIndex to 0 when the current playing
// song is not the first one. It is still not great, as it resets the current playing song
playIndex,
clear: true, clear: true,
} }
} }
@@ -121,21 +109,32 @@ const reduceSetVolume = (state, { data: { volume } }) => {
} }
} }
const reduceCurrent = (state, { data }) => { const reduceSyncQueue = (state, { data: { audioInfo, audioLists } }) => {
const current = data.ended const current = audioLists.length > 0 ? audioInfo : {}
? {} const savedPlayIndex = audioLists.findIndex(
: { (item) => item.uuid === current.uuid
idx: data.idx, )
trackId: data.trackId,
paused: data.paused,
uuid: data.uuid,
song: data.song,
}
const playIndex = state.queue.findIndex((item) => item.uuid === current.uuid)
return { return {
...state, ...state,
current, current,
playIndex: playIndex > -1 ? playIndex : undefined, savedPlayIndex,
queue: audioLists,
clear: false,
playIndex: undefined,
}
}
const reduceCurrent = (state, { data }) => {
const current = data.ended ? {} : data
const savedPlayIndex = state.queue.findIndex(
(item) => item.uuid === current.uuid
)
return {
...state,
current,
playIndex: undefined,
savedPlayIndex,
volume: data.volume, volume: data.volume,
} }
} }
@@ -153,10 +152,10 @@ export const playerReducer = (previousState = initialState, payload) => {
return reduceAddTracks(previousState, payload) return reduceAddTracks(previousState, payload)
case PLAYER_PLAY_NEXT: case PLAYER_PLAY_NEXT:
return reducePlayNext(previousState, payload) return reducePlayNext(previousState, payload)
case PLAYER_SYNC_QUEUE:
return reduceSyncQueue(previousState, payload)
case PLAYER_SET_VOLUME: case PLAYER_SET_VOLUME:
return reduceSetVolume(previousState, payload) return reduceSetVolume(previousState, payload)
case PLAYER_SYNC_QUEUE:
return reduceSyncQueue(previousState, payload)
case PLAYER_CURRENT: case PLAYER_CURRENT:
return reduceCurrent(previousState, payload) return reduceCurrent(previousState, payload)
default: default:
+4 -1
View File
@@ -37,6 +37,9 @@ const createAdminStore = ({
compose compose
const persistedState = loadState() const persistedState = loadState()
if (persistedState?.player?.savedPlayIndex) {
persistedState.player.playIndex = persistedState.player.savedPlayIndex
}
const store = createStore( const store = createStore(
resettableAppReducer, resettableAppReducer,
persistedState, persistedState,
@@ -48,7 +51,7 @@ const createAdminStore = ({
const state = store.getState() const state = store.getState()
saveState({ saveState({
theme: state.theme, theme: state.theme,
player: pick(state.player, ['queue', 'volume', 'playIndex']), player: pick(state.player, ['queue', 'volume', 'savedPlayIndex']),
albumView: state.albumView, albumView: state.albumView,
settings: state.settings, settings: state.settings,
}) })