Skip to content

Commit

Permalink
Fix some strange issues in player
Browse files Browse the repository at this point in the history
Including:

- After playing a song in the current playlist, move the song to a different position. The player will skip to play the other song.

- An error will raise, after clear the current playlist when there is a song is playing.
  • Loading branch information
aidewoode committed Dec 4, 2023
1 parent 7a1188f commit 42c0a16
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 18 deletions.
19 changes: 10 additions & 9 deletions app/javascript/controllers/current_playlist_songs_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@ export default class extends Controller {
const shouldPlay = target.dataset.shouldPlay === 'true'
const targetIndex = this.itemTargets.indexOf(target)

if (this.player.playlist.indexOf(song.id) !== -1) { return }

this.player.playlist.insert(targetIndex, song)
this.playlist.insert(targetIndex, song)

if (shouldPlay) {
this.player.skipTo(targetIndex)
Expand All @@ -44,24 +42,27 @@ export default class extends Controller {

itemTargetDisconnected (target) {
const songId = Number(target.dataset.songId)
const targetIsDragging = target.classList.contains('is-dragging-source')

if (this.player.playlist.indexOf(songId) === -1) { return }

const deleteSongIndex = this.player.playlist.deleteSong(songId)
this.playlist.deleteSong(songId)

if (this.player.currentSong.id === songId) {
this.player.skipTo(deleteSongIndex)
if (this.player.currentSong.id === songId && !targetIsDragging) {
this.player.stop()
}
}

play = (event) => {
const { songId } = event.target.closest('[data-song-id]').dataset
const playlistIndex = this.player.playlist.indexOf(songId)
const playlistIndex = this.playlist.indexOf(songId)

this.player.skipTo(playlistIndex)
}

get player () {
return App.player
}

get playlist () {
return this.player.playlist
}
}
2 changes: 1 addition & 1 deletion app/javascript/controllers/player_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ export default class extends Controller {
#setStopStatus = () => {
this.#setPauseStatus()

if (this.player.playlist.length === 0) {
if (!this.currentSong.id) {
this.headerTarget.classList.remove('is-expanded')
}
}
Expand Down
6 changes: 2 additions & 4 deletions app/javascript/controllers/playlist_sortable_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,12 +113,10 @@ export default class extends Controller {
const destinationSongId = target.dataset.songId
const playlistId = this.element.dataset.playlistId

sourceElement.remove()

if (sourceIndex < targetIndex) {
target.insertAdjacentElement('afterend', sourceElement)
target.after(sourceElement)
} else {
target.insertAdjacentElement('beforebegin', sourceElement)
target.before(sourceElement)
}

fetchRequest(`/playlists/${playlistId}/songs/${songId}/move`, {
Expand Down
9 changes: 5 additions & 4 deletions app/javascript/player.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { dispatchEvent } from './helper'
import Playlist from './playlist'

class Player {
currentIndex = 0
currentSong = {}
isPlaying = false
playlist = new Playlist()
Expand All @@ -14,7 +13,6 @@ class Player {
dispatchEvent(document, 'player:beforePlaying')

const song = this.playlist.songs[index]
this.currentIndex = index
this.currentSong = song
this.isPlaying = true

Expand Down Expand Up @@ -52,9 +50,8 @@ class Player {
this.isPlaying = false
this.currentSong.howl && this.currentSong.howl.stop()

if (this.playlist.length === 0) {
if (!this.playlist.includes(this.currentSong.id)) {
// reset current song
this.currentIndex = 0
this.currentSong = {}
}
}
Expand Down Expand Up @@ -82,6 +79,10 @@ class Player {
seek (seconds) {
this.currentSong.howl.seek(seconds)
}

get currentIndex () {
return Math.max(this.playlist.indexOf(this.currentSong.id), 0)
}
}

export default Player
8 changes: 8 additions & 0 deletions app/javascript/playlist.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,15 @@ class Playlist {
isShuffled = false

insert (index, song) {
if (this.includes(song.id)) { return }

this.orderedSongs.splice(index, 0, song)
this.shuffledSongs.splice(randomIndex(this.shuffledSongs.length), 0, song)
}

deleteSong (songId) {
if (!this.includes(songId)) { return -1 }

const orderedSongsindex = this.#indexOfSongs(this.orderedSongs, songId)
const shuffledSongsindex = this.#indexOfSongs(this.shuffledSongs, songId)

Expand All @@ -24,6 +28,10 @@ class Playlist {
return this.#indexOfSongs(this.songs, songId)
}

includes (songId) {
return this.indexOf(songId) !== -1
}

#indexOfSongs (songs, songId) {
return songs.map((song) => song.id).indexOf(Number(songId))
}
Expand Down

0 comments on commit 42c0a16

Please sign in to comment.