After toggling shuffle, track down the new playback index and prepare the next track.

This commit is contained in:
Casey Langen 2016-06-25 18:36:27 -07:00
parent f574d11ca7
commit 01a41531c6
3 changed files with 30 additions and 2 deletions

View File

@ -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());
}

View File

@ -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);

View File

@ -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) {