From 01a41531c65de1091e22b5eb08d54d287675e8f5 Mon Sep 17 00:00:00 2001 From: Casey Langen Date: Sat, 25 Jun 2016 18:36:27 -0700 Subject: [PATCH] After toggling shuffle, track down the new playback index and prepare the next track. --- src/musikbox/app/model/TrackList.cpp | 9 +++++++++ src/musikbox/app/model/TrackList.h | 2 ++ src/musikbox/app/service/PlaybackService.cpp | 21 ++++++++++++++++++-- 3 files changed, 30 insertions(+), 2 deletions(-) diff --git a/src/musikbox/app/model/TrackList.cpp b/src/musikbox/app/model/TrackList.cpp index 23c038e39..1d3639185 100755 --- a/src/musikbox/app/model/TrackList.cpp +++ b/src/musikbox/app/model/TrackList.cpp @@ -106,6 +106,10 @@ TrackPtr TrackList::Get(size_t index) { return query->Result(); } +DBID TrackList::GetId(size_t index) { + return this->ids.at(index); +} + void TrackList::CopyFrom(TrackList& from) { this->Clear(); @@ -115,6 +119,11 @@ void TrackList::CopyFrom(TrackList& from) { std::back_inserter(this->ids)); } +int TrackList::IndexOf(DBID id) { + auto it = std::find(this->ids.begin(), this->ids.end(), id); + return (it == this->ids.end()) ? -1 : it - this->ids.begin(); +} + void TrackList::Shuffle() { std::random_shuffle(this->ids.begin(), this->ids.end()); } diff --git a/src/musikbox/app/model/TrackList.h b/src/musikbox/app/model/TrackList.h index e615bd7f0..67c0102f2 100755 --- a/src/musikbox/app/model/TrackList.h +++ b/src/musikbox/app/model/TrackList.h @@ -50,6 +50,8 @@ namespace musik { size_t Count(); void Add(const DBID& id); musik::core::TrackPtr Get(size_t index); + DBID GetId(size_t index); + int IndexOf(DBID id); void ClearCache(); void Clear(); void Swap(TrackList& list); diff --git a/src/musikbox/app/service/PlaybackService.cpp b/src/musikbox/app/service/PlaybackService.cpp index 6c69c5718..eaad4b241 100755 --- a/src/musikbox/app/service/PlaybackService.cpp +++ b/src/musikbox/app/service/PlaybackService.cpp @@ -133,16 +133,33 @@ void PlaybackService::SetRepeatMode(RepeatMode mode) { void PlaybackService::ToggleShuffle() { boost::recursive_mutex::scoped_lock lock(this->playlistMutex); - if (this->unshuffled.Count() > 0) { + + /* remember the ID of the playing track -- we're going to need to look + it up after the shuffle */ + DBID id = -1; + if (this->index < this->playlist.Count()) { + id = this->playlist.GetId(this->index); + } + + if (this->unshuffled.Count() > 0) { /* shuffled -> unshuffled */ this->playlist.Clear(); this->playlist.Swap(this->unshuffled); this->Shuffled(false); } - else { + else { /* unshuffled -> shuffle */ this->unshuffled.CopyFrom(this->playlist); this->playlist.Shuffle(); this->Shuffled(true); } + + /* find the new playback index and prefetch the next track */ + if (id != -1) { + int index = this->playlist.IndexOf(id); + if (index != -1) { + this->index = index; + POST(this, MESSAGE_PREPARE_NEXT_TRACK, 0, 0); + } + } } void PlaybackService::ProcessMessage(IMessage &message) {