refactor vuex actions

This commit is contained in:
Thomas Amland 2020-10-02 20:07:53 +02:00
parent e64ad522af
commit f3bdbbd44d
5 changed files with 24 additions and 20 deletions

View File

@ -3,7 +3,7 @@
<template #button-content> <template #button-content>
<Icon icon="three-dots-vertical" /> <Icon icon="three-dots-vertical" />
</template> </template>
<b-dropdown-item-button @click="playNext()"> <b-dropdown-item-button @click="setNextInQueue()">
Play next Play next
</b-dropdown-item-button> </b-dropdown-item-button>
<b-dropdown-item-button @click="addToQueue()"> <b-dropdown-item-button @click="addToQueue()">
@ -36,8 +36,8 @@
} }
this.starred = !this.starred this.starred = !this.starred
}, },
playNext() { setNextInQueue() {
return this.$store.dispatch('player/playNext', this.track) return this.$store.dispatch('player/setNextInQueue', this.track)
}, },
addToQueue() { addToQueue() {
return this.$store.dispatch('player/addToQueue', this.track) return this.$store.dispatch('player/addToQueue', this.track)

View File

@ -120,9 +120,9 @@
if ((this.tracks as any)[index].id === this.playingTrackId) { if ((this.tracks as any)[index].id === this.playingTrackId) {
return this.$store.dispatch('player/playPause') return this.$store.dispatch('player/playPause')
} }
return this.$store.dispatch('player/playQueue', { return this.$store.dispatch('player/playTrackList', {
index, index,
queue: this.tracks, tracks: this.tracks,
}) })
}, },
dragstart(id: string, event: any) { dragstart(id: string, event: any) {

View File

@ -60,9 +60,9 @@
methods: { methods: {
play() { play() {
if (this.album?.tracks) { if (this.album?.tracks) {
return this.$store.dispatch('player/playQueue', { return this.$store.dispatch('player/playTrackList', {
index: 0, index: 0,
queue: this.album.tracks, tracks: this.album.tracks,
}) })
} }
}, },

View File

@ -59,7 +59,10 @@
if (this.items[index].id === this.playingTrackId) { if (this.items[index].id === this.playingTrackId) {
return this.$store.dispatch('player/playPause') return this.$store.dispatch('player/playPause')
} }
return this.$store.dispatch('player/playQueue', { index, queue: this.items }) return this.$store.dispatch('player/playTrackList', {
index,
tracks: this.items
})
}, },
} }
}) })

View File

@ -87,20 +87,26 @@ export const playerModule: Module<State, any> = {
}, },
actions: { actions: {
async playQueue({ commit }, { queue, index }) { async playTrackList({ commit }, { queue, index }) {
commit('setQueue', [...queue]) commit('setQueue', [...queue])
commit('setQueueIndex', index) commit('setQueueIndex', index)
commit('setPlaying') commit('setPlaying')
await audio.play() await audio.play()
}, },
async play({ commit }) { async resume({ commit }) {
commit('setPlaying') commit('setPlaying')
await audio.play() await audio.play()
}, },
pause({ commit }) { async pause({ commit }) {
audio.pause() audio.pause()
commit('setPaused') commit('setPaused')
}, },
async playPause({ state, dispatch }) {
if (state.isPlaying) {
return dispatch('pause')
}
return dispatch('resume')
},
async next({ commit, state }) { async next({ commit, state }) {
commit('setQueueIndex', state.queueIndex + 1) commit('setQueueIndex', state.queueIndex + 1)
commit('setPlaying') commit('setPlaying')
@ -111,23 +117,18 @@ export const playerModule: Module<State, any> = {
commit('setPlaying') commit('setPlaying')
await audio.play() await audio.play()
}, },
playPause({ state, dispatch }) {
if (state.isPlaying) {
return dispatch('pause')
}
return dispatch('play')
},
seek({ commit, state }, value) { seek({ commit, state }, value) {
if (isFinite(state.duration)) { if (isFinite(state.duration)) {
commit('setPosition', state.duration * value) commit('setPosition', state.duration * value)
} }
}, },
playNext({ commit }, track) {
commit('setNextInQueue', track)
}, },
addToQueue({ commit }, track) { addToQueue({ commit }, track) {
commit('addToQueue', track) commit('addToQueue', track)
}, },
setNextInQueue({ commit }, track) {
commit('setNextInQueue', track)
},
}, },
getters: { getters: {
@ -172,7 +173,7 @@ export function setupAudio(store: Store<any>) {
if (mediaSession) { if (mediaSession) {
mediaSession.setActionHandler('play', () => { mediaSession.setActionHandler('play', () => {
store.dispatch('player/play') store.dispatch('player/resume')
}) })
mediaSession.setActionHandler('pause', () => { mediaSession.setActionHandler('pause', () => {
store.dispatch('player/pause') store.dispatch('player/pause')