fixed bug in SetShuffle

This commit is contained in:
Siiiiinth
2026-02-09 19:12:19 +01:00
parent d077bfb953
commit d463971ce8
2 changed files with 34 additions and 17 deletions
+31 -17
View File
@@ -342,40 +342,54 @@ func (p *playbackEngine) GetLoopMode() LoopMode {
return p.loopMode return p.loopMode
} }
func (p *playbackEngine) GetNowPlayingIdxFrom(items []mediaprovider.MediaItem) int { func (p *playbackEngine) GetTrackIdxByIdFrom(items []mediaprovider.MediaItem, id string) int {
newNowPlayingIdx := -1 foundIdx := -1
if p.nowPlayingIdx >= 0 && len(items) > p.nowPlayingIdx { for i, tr := range items {
nowPlayingID := p.getPlayQueueItemAt(p.nowPlayingIdx).Metadata().ID if tr.Metadata().ID == id {
for i, tr := range items { foundIdx = i
if tr.Metadata().ID == nowPlayingID { break
newNowPlayingIdx = i
break
}
} }
} }
return newNowPlayingIdx return foundIdx
} }
func (p *playbackEngine) SetShuffle(shuffle bool) { func (p *playbackEngine) SetShuffle(shuffle bool) {
for _, cb := range p.onShuffleChange {
cb(shuffle)
}
if p.shuffle == shuffle { if p.shuffle == shuffle {
return return
} }
for _, cb := range p.onShuffleChange {
cb(shuffle)
}
p.shuffle = shuffle
// guard against changing shuffle with an empty queue
if p.getPlayQueue() == nil || len(p.getPlayQueue()) == 0 {
return
}
newNowPlayingIdx := 0 newNowPlayingIdx := 0
if shuffle { if shuffle {
shuffledQueue := deepCopyMediaItemSlice(p.playQueue) shuffledQueue := deepCopyMediaItemSlice(p.playQueue)
rand.Shuffle(len(shuffledQueue), func(i, j int) { rand.Shuffle(len(shuffledQueue), func(i, j int) {
shuffledQueue[i], shuffledQueue[j] = shuffledQueue[j], shuffledQueue[i] shuffledQueue[i], shuffledQueue[j] = shuffledQueue[j], shuffledQueue[i]
}) })
p.setShuffledPlayQueue(sharedutil.ReorderItems(shuffledQueue, []int{p.GetNowPlayingIdxFrom(shuffledQueue)}, 0)) if p.nowPlayingIdx >= 0 && len(p.getPlayQueue()) > p.nowPlayingIdx {
nowPlayingID := p.getPlayQueue()[p.nowPlayingIdx].Metadata().ID
p.setShuffledPlayQueue(sharedutil.ReorderItems(shuffledQueue, []int{p.GetTrackIdxByIdFrom(shuffledQueue, nowPlayingID)}, 0))
} else {
return
}
} else { } else {
newNowPlayingIdx = p.GetNowPlayingIdxFrom(p.playQueue) if p.nowPlayingIdx >= 0 && len(p.getShuffledPlayQueue()) > p.nowPlayingIdx {
} nowPlayingID := p.getShuffledPlayQueue()[p.nowPlayingIdx].Metadata().ID
newNowPlayingIdx = p.GetTrackIdxByIdFrom(p.playQueue, nowPlayingID)
} else {
return
}
p.shuffle = shuffle }
if p.nowPlayingIdx >= 0 && newNowPlayingIdx == -1 { if p.nowPlayingIdx >= 0 && newNowPlayingIdx == -1 {
return return
+3
View File
@@ -112,6 +112,9 @@ func TracksToIDs(tracks []*mediaprovider.Track) []string {
// Reorder items and return a new track slice. // Reorder items and return a new track slice.
// idxToMove must contain only valid indexes into tracks, and no repeats // idxToMove must contain only valid indexes into tracks, and no repeats
func ReorderItems[T any](items []T, idxToMove []int, insertIdx int) []T { func ReorderItems[T any](items []T, idxToMove []int, insertIdx int) []T {
if len(items) < 2 {
return items
}
idxToMoveSet := ToSet(idxToMove) idxToMoveSet := ToSet(idxToMove)
newItems := make([]T, 0, len(items)) newItems := make([]T, 0, len(items))