Added a playque shuffle
This commit is contained in:
@@ -6,11 +6,16 @@
|
||||
<i class="fa fa-times-circle"></i>
|
||||
</message>
|
||||
|
||||
<message id="Lms.PlayQueue.shuffle">
|
||||
<i class="fa fa-random"></i>
|
||||
</message>
|
||||
|
||||
<message id="Lms.PlayQueue.template">
|
||||
<div class="page-header">
|
||||
<h2>${tr:Lms.PlayQueue.playqueue}</h2>
|
||||
<small>${nb-tracks}</small>
|
||||
${clear-btn}
|
||||
${shuffle-btn}
|
||||
${radio-mode}
|
||||
</div>
|
||||
${entries}
|
||||
|
||||
@@ -19,6 +19,7 @@
|
||||
#include "Playlist.hpp"
|
||||
|
||||
#include <cassert>
|
||||
#include <random>
|
||||
|
||||
#include "Cluster.hpp"
|
||||
#include "User.hpp"
|
||||
@@ -115,6 +116,13 @@ Playlist::getEntries(int offset, int size, bool& moreResults) const
|
||||
return res;
|
||||
}
|
||||
|
||||
std::vector<Wt::Dbo::ptr<PlaylistEntry>>
|
||||
Playlist::getAllEntries() const
|
||||
{
|
||||
bool moreResults;
|
||||
return getEntries(-1, -1, moreResults);
|
||||
}
|
||||
|
||||
Wt::Dbo::ptr<PlaylistEntry>
|
||||
Playlist::getEntry(std::size_t pos) const
|
||||
{
|
||||
@@ -171,8 +179,23 @@ Playlist::getTrackIds() const
|
||||
.where("p.id = ?").bind(self()->id());
|
||||
|
||||
return std::vector<IdType>(res.begin(), res.end());
|
||||
|
||||
}
|
||||
|
||||
void
|
||||
Playlist::shuffle()
|
||||
{
|
||||
assert(session());
|
||||
|
||||
auto entries = getAllEntries();
|
||||
|
||||
auto now = std::chrono::system_clock::now();
|
||||
std::mt19937 randGenerator(std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch()).count());
|
||||
|
||||
std::shuffle(entries.begin(), entries.end(), randGenerator);
|
||||
|
||||
clear();
|
||||
for (auto entry : entries)
|
||||
PlaylistEntry::create(*session(), entry->getTrack(), self());
|
||||
}
|
||||
|
||||
} // namespace Database
|
||||
|
||||
@@ -53,11 +53,14 @@ class Playlist : public Wt::Dbo::Dbo<Playlist>
|
||||
|
||||
// Modifiers
|
||||
void clear() { _entries.clear(); }
|
||||
void shuffle();
|
||||
|
||||
// Get tracks, ordered by position
|
||||
std::size_t getCount() const;
|
||||
Wt::Dbo::ptr<PlaylistEntry> getEntry(std::size_t pos) const;
|
||||
std::vector<Wt::Dbo::ptr<PlaylistEntry>> getEntries(int offset, int size, bool& moreResults) const;
|
||||
std::vector<Wt::Dbo::ptr<PlaylistEntry>> getAllEntries() const;
|
||||
|
||||
std::vector<IdType> getTrackIds() const;
|
||||
|
||||
// Get clusters, order by occurence
|
||||
@@ -95,7 +98,7 @@ class PlaylistEntry
|
||||
static pointer getById(Wt::Dbo::Session& session, IdType id);
|
||||
|
||||
// Create utility
|
||||
static pointer create(Wt::Dbo::Session& session, Wt::Dbo::ptr<Track> track, Wt::Dbo::ptr<Playlist> playlist);
|
||||
static pointer create(Wt::Dbo::Session& session, Wt::Dbo::ptr<Track> track, Wt::Dbo::ptr<Playlist> playlist);
|
||||
|
||||
// Accessors
|
||||
Wt::Dbo::ptr<Track> getTrack() const { return _track; }
|
||||
|
||||
@@ -50,6 +50,7 @@ PlayQueue::PlayQueue()
|
||||
_showMore->addFunction("tr", &Wt::WTemplate::Functions::tr);
|
||||
_showMore->setHidden(true);
|
||||
|
||||
Wt::WText* shuffleBtn = bindNew<Wt::WText>("shuffle-btn", Wt::WString::tr("Lms.PlayQueue.shuffle"), Wt::TextFormat::XHTML);
|
||||
_radioMode = bindNew<Wt::WCheckBox>("radio-mode", Wt::WString::tr("Lms.PlayQueue.radio-mode"));
|
||||
|
||||
_nbTracks = bindNew<Wt::WText>("nb-tracks");
|
||||
@@ -78,6 +79,18 @@ PlayQueue::PlayQueue()
|
||||
updateInfo();
|
||||
}));
|
||||
|
||||
shuffleBtn->clicked().connect([=]
|
||||
{
|
||||
{
|
||||
Wt::Dbo::Transaction transaction(LmsApp->getDboSession());
|
||||
|
||||
auto playlist = Database::Playlist::get(LmsApp->getDboSession(), currentPlayQueueName, LmsApp->getCurrentUser());
|
||||
playlist.modify()->shuffle();
|
||||
}
|
||||
_entriesContainer->clear();
|
||||
addSome();
|
||||
});
|
||||
|
||||
_showMore->clicked().connect(std::bind([=]
|
||||
{
|
||||
addSome();
|
||||
|
||||
Reference in New Issue
Block a user