Refactor updateCurrentSong into 3 different functions and now it's more clear

This commit is contained in:
tramhao 2021-04-07 11:52:13 +08:00
parent d5ee151c34
commit a30e18c787
3 changed files with 99 additions and 67 deletions

View File

@ -270,7 +270,7 @@ func (p *Playlist) deleteSong(audioFile *AudioFile) (err error) {
// Here we remove the song from queue
gomu.queue.updateQueuePath()
gomu.queue.updateCurrentSong(audioFile, nil, true)
gomu.queue.updateCurrentSongDelete(audioFile)
}
})
@ -804,14 +804,21 @@ func (p *Playlist) paste() error {
defaultTimedPopup(" Success ", p.yankFile.name+"\n has been pasted to\n"+newPathDir)
// keep queue references updated
p.refresh()
gomu.queue.updateQueuePath()
newAudio := oldAudio
newAudio.path = newPathFull
err = gomu.queue.updateCurrentSong(oldAudio, newAudio, false)
if err != nil {
return tracerr.Wrap(err)
p.refresh()
gomu.queue.updateQueuePath()
if p.yankFile.isAudioFile {
err = gomu.queue.updateCurrentSongName(oldAudio, newAudio)
if err != nil {
return tracerr.Wrap(err)
}
} else {
err = gomu.queue.updateCurrentSongPath(oldAudio, newAudio)
if err != nil {
return tracerr.Wrap(err)
}
}
p.yankFile = nil
@ -851,12 +858,16 @@ func (p *Playlist) refreshAfterRename(node *AudioFile, newName string) error {
if err != nil {
return tracerr.Wrap(err)
}
err = gomu.queue.updateCurrentSongName(node, newNode)
if err != nil {
return tracerr.Wrap(err)
}
} else {
gomu.queue.updateQueuePath()
}
err := gomu.queue.updateCurrentSong(node, newNode, false)
if err != nil {
return tracerr.Wrap(err)
err := gomu.queue.updateCurrentSongPath(node, newNode)
if err != nil {
return tracerr.Wrap(err)
}
}
return nil

View File

@ -141,12 +141,13 @@ func confirmDeleteAllPopup(selPlaylist *tview.TreeNode) (err error) {
gomu.popups.pop()
if confirmationText == "DELETE" {
err = gomu.playlist.deletePlaylist(selPlaylist.GetReference().(*AudioFile))
audioFile := selPlaylist.GetReference().(*AudioFile)
err = gomu.playlist.deletePlaylist(audioFile)
if err != nil {
errorPopup(err)
}
gomu.queue.updateQueuePath()
gomu.queue.updateCurrentSong(selPlaylist.GetReference().(*AudioFile), nil, true)
gomu.queue.updateCurrentSongDelete(audioFile)
}
case tcell.KeyEscape:

128
queue.go
View File

@ -500,7 +500,6 @@ func (q *Queue) updateQueuePath() {
audioFile, err := gomu.playlist.findAudioFile(v)
if err != nil {
logError(err)
continue
}
q.enqueue(audioFile)
@ -509,8 +508,8 @@ func (q *Queue) updateQueuePath() {
q.updateTitle()
}
// update current playing song info to reflect the changes during rename and paste
func (q *Queue) updateCurrentSong(oldAudio *AudioFile, newAudio *AudioFile, isDelete bool) error {
// update current playing song name to reflect the changes during rename and paste
func (q *Queue) updateCurrentSongName(oldAudio *AudioFile, newAudio *AudioFile) error {
if !gomu.player.IsRunning() && !gomu.player.IsPaused() {
return nil
@ -520,61 +519,11 @@ func (q *Queue) updateCurrentSong(oldAudio *AudioFile, newAudio *AudioFile, isDe
position := gomu.playingBar.getProgress()
paused := gomu.player.IsPaused()
if !oldAudio.isAudioFile {
// Here we check the situation when currentsong is under oldAudio folder
// if strings.Contains(currentSong.Path(), oldAudio.path) || strings.Contains(currentSong.Path(), oldAudio.name) {
if strings.Contains(currentSong.Path(), oldAudio.path) {
if isDelete {
tmpLoop := q.isLoop
q.isLoop = false
gomu.player.Skip()
if paused {
gomu.player.TogglePause()
}
q.isLoop = tmpLoop
q.updateTitle()
return nil
} else {
// Here is the handling of folder rename and paste
currentSong, err := gomu.playlist.findAudioFile(sha1Hex(getName(currentSong.Name())))
if err != nil {
return tracerr.Wrap(err)
}
gomu.queue.pushFront(currentSong)
tmpLoop := q.isLoop
q.isLoop = false
gomu.player.Skip()
gomu.player.Seek(position)
if paused {
gomu.player.TogglePause()
}
q.isLoop = tmpLoop
q.updateTitle()
return nil
}
}
}
if oldAudio.name != currentSong.Name() {
return nil
}
// if newAudio is empty, we simply skip current song
if newAudio == nil {
tmpLoop := q.isLoop
q.isLoop = false
gomu.player.Skip()
if paused {
gomu.player.TogglePause()
}
q.isLoop = tmpLoop
return nil
}
// if newAudio is not empty, we insert it in the first of queue, then play it
// we insert it in the first of queue, then play it
gomu.queue.pushFront(newAudio)
tmpLoop := q.isLoop
q.isLoop = false
@ -588,3 +537,74 @@ func (q *Queue) updateCurrentSong(oldAudio *AudioFile, newAudio *AudioFile, isDe
return nil
}
// update current playing song path to reflect the changes during rename and paste
func (q *Queue) updateCurrentSongPath(oldAudio *AudioFile, newAudio *AudioFile) error {
if !gomu.player.IsRunning() && !gomu.player.IsPaused() {
return nil
}
currentSong := gomu.player.GetCurrentSong()
position := gomu.playingBar.getProgress()
paused := gomu.player.IsPaused()
// Here we check the situation when currentsong is under oldAudio folder
if !strings.Contains(currentSong.Path(), oldAudio.path) {
return nil
}
// Here is the handling of folder rename and paste
currentSongAudioFile, err := gomu.playlist.findAudioFile(sha1Hex(getName(currentSong.Name())))
if err != nil {
return tracerr.Wrap(err)
}
gomu.queue.pushFront(currentSongAudioFile)
tmpLoop := q.isLoop
q.isLoop = false
gomu.player.Skip()
gomu.player.Seek(position)
if paused {
gomu.player.TogglePause()
}
q.isLoop = tmpLoop
q.updateTitle()
return nil
}
// update current playing song simply delete it
func (q *Queue) updateCurrentSongDelete(oldAudio *AudioFile) {
if !gomu.player.IsRunning() && !gomu.player.IsPaused() {
return
}
currentSong := gomu.player.GetCurrentSong()
paused := gomu.player.IsPaused()
var delete bool
if oldAudio.isAudioFile {
if oldAudio.name == currentSong.Name() {
delete = true
}
} else {
if strings.Contains(currentSong.Path(), oldAudio.path) {
delete = true
}
}
if !delete {
return
}
tmpLoop := q.isLoop
q.isLoop = false
gomu.player.Skip()
if paused {
gomu.player.TogglePause()
}
q.isLoop = tmpLoop
q.updateTitle()
}