extract deepCopy helper function

This commit is contained in:
Drew Weymouth
2024-01-08 17:17:12 -08:00
parent f74d1cc730
commit 7cfd176c59
+19 -24
View File
@@ -203,18 +203,14 @@ func (p *PlaybackManager) LoadTracks(tracks []*mediaprovider.Track, appendToQueu
p.nowPlayingIdx = -1 p.nowPlayingIdx = -1
p.playQueue = nil 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 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 newTracks := p.deepCopyTrackSlice(tracks)
// (tracking play count increases, favorite, and rating) without messing up if shuffle {
// other views' track models rand.Shuffle(len(newTracks), func(i, j int) { newTracks[i], newTracks[j] = newTracks[j], newTracks[i] })
tr := *tracks[i]
p.playQueue = append(p.playQueue, &tr)
} }
p.playQueue = append(p.playQueue, newTracks...)
if needToSetNext { if needToSetNext {
p.setNextTrack(p.nowPlayingIdx + 1) 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, // 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. // 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 { func (p *PlaybackManager) UpdatePlayQueue(tracks []*mediaprovider.Track) error {
newQueue := make([]*mediaprovider.Track, len(tracks)) newQueue := p.deepCopyTrackSlice(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
}
newNowPlayingIdx := -1 newNowPlayingIdx := -1
if p.nowPlayingIdx >= 0 { if p.nowPlayingIdx >= 0 {
nowPlayingID := p.playQueue[p.nowPlayingIdx].ID nowPlayingID := p.playQueue[p.nowPlayingIdx].ID
@@ -326,12 +315,7 @@ func (p *PlaybackManager) fetchAndPlayTracks(fetchFn func() ([]*mediaprovider.Tr
} }
func (p *PlaybackManager) GetPlayQueue() []*mediaprovider.Track { func (p *PlaybackManager) GetPlayQueue() []*mediaprovider.Track {
pq := make([]*mediaprovider.Track, len(p.playQueue)) return p.deepCopyTrackSlice(p.playQueue)
for i, tr := range p.playQueue {
copy := *tr
pq[i] = &copy
}
return pq
} }
// Any time the user changes the favorite status of a track elsewhere in the app, // 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) 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] = &copy
}
return newTracks
}
func (p *PlaybackManager) invokeOnSongChangeCallbacks() { func (p *PlaybackManager) invokeOnSongChangeCallbacks() {
if p.callbacksDisabled { if p.callbacksDisabled {
return return