From 7cfd176c59418461d27ca598eee9c717610df0bf Mon Sep 17 00:00:00 2001 From: Drew Weymouth Date: Mon, 8 Jan 2024 17:17:12 -0800 Subject: [PATCH] extract deepCopy helper function --- backend/playbackmanager.go | 43 +++++++++++++++++--------------------- 1 file changed, 19 insertions(+), 24 deletions(-) diff --git a/backend/playbackmanager.go b/backend/playbackmanager.go index 22d1438..1d0790a 100644 --- a/backend/playbackmanager.go +++ b/backend/playbackmanager.go @@ -203,18 +203,14 @@ func (p *PlaybackManager) LoadTracks(tracks []*mediaprovider.Track, appendToQueu p.nowPlayingIdx = -1 p.playQueue = nil } - nums := util.Range(len(tracks)) - if shuffle { - rand.Shuffle(len(nums), func(i, j int) { nums[i], nums[j] = nums[j], nums[i] }) - } needToSetNext := appendToQueue && len(tracks) > 0 && p.nowPlayingIdx == len(p.playQueue)-1 - for _, i := range nums { - // ensure a deep copy of the track info so that we can maintain our own state - // (tracking play count increases, favorite, and rating) without messing up - // other views' track models - tr := *tracks[i] - p.playQueue = append(p.playQueue, &tr) + + newTracks := p.deepCopyTrackSlice(tracks) + if shuffle { + rand.Shuffle(len(newTracks), func(i, j int) { newTracks[i], newTracks[j] = newTracks[j], newTracks[i] }) } + p.playQueue = append(p.playQueue, newTracks...) + if needToSetNext { p.setNextTrack(p.nowPlayingIdx + 1) } @@ -225,14 +221,7 @@ func (p *PlaybackManager) LoadTracks(tracks []*mediaprovider.Track, appendToQueu // Does not stop playback if the currently playing track is in the new queue, // but updates the now playing index to point to the first instance of the track in the new queue. func (p *PlaybackManager) UpdatePlayQueue(tracks []*mediaprovider.Track) error { - newQueue := make([]*mediaprovider.Track, len(tracks)) - for i, tr := range tracks { - // ensure a deep copy of the track info so that we can maintain our own state - // (tracking play count increases, favorite, and rating) without messing up - // other views' track models - track := *tr - newQueue[i] = &track - } + newQueue := p.deepCopyTrackSlice(tracks) newNowPlayingIdx := -1 if p.nowPlayingIdx >= 0 { nowPlayingID := p.playQueue[p.nowPlayingIdx].ID @@ -326,12 +315,7 @@ func (p *PlaybackManager) fetchAndPlayTracks(fetchFn func() ([]*mediaprovider.Tr } func (p *PlaybackManager) GetPlayQueue() []*mediaprovider.Track { - pq := make([]*mediaprovider.Track, len(p.playQueue)) - for i, tr := range p.playQueue { - copy := *tr - pq[i] = © - } - return pq + return p.deepCopyTrackSlice(p.playQueue) } // Any time the user changes the favorite status of a track elsewhere in the app, @@ -690,6 +674,17 @@ func (p *PlaybackManager) sendNowPlayingScrobble() { go p.sm.Server.TrackBeganPlayback(track.ID) } +// creates a deep copy of the track info so that we can maintain our own state +// (play count increases, favorite, and rating) without messing up other views' track models +func (p *PlaybackManager) deepCopyTrackSlice(tracks []*mediaprovider.Track) []*mediaprovider.Track { + newTracks := make([]*mediaprovider.Track, len(tracks)) + for i, tr := range tracks { + copy := *tr + newTracks[i] = © + } + return newTracks +} + func (p *PlaybackManager) invokeOnSongChangeCallbacks() { if p.callbacksDisabled { return