Implement better playlist navigation logic

This commit is contained in:
Preston 2020-08-06 23:08:27 -04:00
parent 898e513974
commit f2d1f5afb9
1 changed files with 114 additions and 83 deletions

View File

@ -33,7 +33,7 @@ export default Vue.extend({
channelThumbnail: '', channelThumbnail: '',
playlistTitle: '', playlistTitle: '',
playlistItems: [], playlistItems: [],
playlistWatchedVideoList: [] randomizedPlaylistItems: []
} }
}, },
computed: { computed: {
@ -114,79 +114,79 @@ export default Vue.extend({
this.showToast({ this.showToast({
message: 'Shuffle is now enabled' message: 'Shuffle is now enabled'
}) })
this.shufflePlaylistItems()
} }
}, },
playNextVideo: function () { playNextVideo: function () {
this.showToast({
message: 'Playing Next Video'
})
const playlistInfo = { const playlistInfo = {
playlistId: this.playlistId playlistId: this.playlistId
} }
const videoIndex = this.playlistItems.findIndex((item) => { if (this.shuffleEnabled) {
return item.videoId === this.videoId const videoIndex = this.randomizedPlaylistItems.findIndex((item) => {
}) return item === this.videoId
})
const videosRemain = this.playlistWatchedVideoList.length < this.playlistItems.length if (videoIndex === this.randomizedPlaylistItems.length - 1) {
if (this.loopEnabled) {
if (this.shuffleEnabled && videosRemain) {
let runLoop = true
while (runLoop) {
const randomInt = Math.floor(Math.random() * this.playlistItems.length)
const randomVideoId = this.playlistItems[randomInt].videoId
const watchedIndex = this.playlistWatchedVideoList.findIndex((watchedVideo) => {
return watchedVideo === randomVideoId
})
if (watchedIndex === -1) {
runLoop = false
this.$router.push( this.$router.push(
{ {
path: `/watch/${randomVideoId}`, path: `/watch/${this.randomizedPlaylistItems[0]}`,
query: playlistInfo query: playlistInfo
} }
) )
break this.showToast({
message: 'Playing Next Video'
})
this.shufflePlaylistItems()
} else {
this.showToast({
message: 'The playlist has ended. Enable loop to continue playing'
})
} }
} } else {
} else if (this.shuffleEnabled && !videosRemain) { this.$router.push(
if (this.loopEnabled) { {
let runLoop = true path: `/watch/${this.randomizedPlaylistItems[videoIndex + 1]}`,
while (runLoop) { query: playlistInfo
const randomInt = Math.floor(Math.random() * this.playlistItems.length)
const randomVideoId = this.playlistItems[randomInt].videoId
if (this.videoId !== randomVideoId) {
this.playlistItems = []
runLoop = false
this.$router.push(
{
path: `/watch/${randomVideoId}`,
query: playlistInfo
}
)
break
} }
} )
this.showToast({
message: 'Playing Next Video'
})
} }
} else if (this.loopEnabled && videoIndex === this.playlistItems.length - 1) { } else {
this.$router.push( const videoIndex = this.playlistItems.findIndex((item) => {
{ return item.videoId === this.videoId
path: `/watch/${this.playlistItems[0].videoId}`, })
query: playlistInfo
if (videoIndex === this.playlistItems.length - 1) {
if (this.loopEnabled) {
this.$router.push(
{
path: `/watch/${this.playlistItems[0].videoId}`,
query: playlistInfo
}
)
this.showToast({
message: 'Playing Next Video'
})
} }
) this.showToast({
} else if (videoIndex < this.playlistItems.length - 1 && videosRemain) { message: 'The playlist has ended. Enable loop to continue playing'
this.$router.push( })
{ } else {
path: `/watch/${this.playlistItems[videoIndex + 1].videoId}`, this.$router.push(
query: playlistInfo {
} path: `/watch/${this.playlistItems[videoIndex + 1].videoId}`,
) query: playlistInfo
}
)
this.showToast({
message: 'Playing Next Video'
})
}
} }
}, },
@ -199,35 +199,46 @@ export default Vue.extend({
playlistId: this.playlistId playlistId: this.playlistId
} }
const watchedIndex = this.playlistItems.findIndex((watchedVideo) => { if (this.shuffleEnabled) {
return watchedVideo.videoId === this.videoId const videoIndex = this.randomizedPlaylistItems.findIndex((item) => {
}) return item.videoId === this.videoId
})
if (this.shuffleEnabled && this.playlistWatchedVideoList.length > 1) { if (videoIndex === 0) {
this.playlistWatchedVideoList.pop() this.$router.push(
const lastVideo = this.playlistWatchedVideoList.pop() {
this.$router.push( path: `/watch/${this.randomizedPlaylistItems[this.randomizedPlaylistItems.length - 1]}`,
{ query: playlistInfo
path: `/watch/${lastVideo}`, }
query: playlistInfo )
} } else {
) this.$router.push(
} else if (watchedIndex === 0) { {
const videoId = this.playlistItems[this.playlistItems.length - 1].videoId path: `/watch/${this.randomizedPlaylistItems[videoIndex - 1]}`,
this.$router.push( query: playlistInfo
{ }
path: `/watch/${videoId}`, )
query: playlistInfo }
}
)
} else { } else {
const videoId = this.playlistItems[watchedIndex - 1].videoId const videoIndex = this.playlistItems.findIndex((item) => {
this.$router.push( return item.videoId === this.videoId
{ })
path: `/watch/${videoId}`,
query: playlistInfo if (videoIndex === 0) {
} this.$router.push(
) {
path: `/watch/${this.playlistItems[this.randomizedPlaylistItems.length - 1].videoId}`,
query: playlistInfo
}
)
} else {
this.$router.push(
{
path: `/watch/${this.playlistItems[videoIndex - 1].videoId}`,
query: playlistInfo
}
)
}
} }
}, },
@ -303,6 +314,26 @@ export default Vue.extend({
}) })
}, },
shufflePlaylistItems: function () {
// Prevents the array from affecting the original object
const remainingItems = [].concat(this.playlistItems)
const items = []
items.push(this.videoId)
this.playlistItems.forEach((item) => {
const randomInt = Math.floor(Math.random() * remainingItems.length)
if (remainingItems[randomInt].videoId !== this.videoId) {
items.push(remainingItems[randomInt].videoId)
}
remainingItems.splice(randomInt, 1)
})
this.randomizedPlaylistItems = items
},
...mapActions([ ...mapActions([
'showToast' 'showToast'
]) ])