Added a playque shuffle

This commit is contained in:
emeric
2018-06-08 17:11:59 +02:00
parent 49f92ba0f9
commit ed008533b2
4 changed files with 46 additions and 2 deletions
+5
View File
@@ -6,11 +6,16 @@
<i class="fa fa-times-circle"></i> <i class="fa fa-times-circle"></i>
</message> </message>
<message id="Lms.PlayQueue.shuffle">
<i class="fa fa-random"></i>
</message>
<message id="Lms.PlayQueue.template"> <message id="Lms.PlayQueue.template">
<div class="page-header"> <div class="page-header">
<h2>${tr:Lms.PlayQueue.playqueue}</h2> <h2>${tr:Lms.PlayQueue.playqueue}</h2>
<small>${nb-tracks}</small> <small>${nb-tracks}</small>
${clear-btn} ${clear-btn}
${shuffle-btn}
${radio-mode} ${radio-mode}
</div> </div>
${entries} ${entries}
+24 -1
View File
@@ -19,6 +19,7 @@
#include "Playlist.hpp" #include "Playlist.hpp"
#include <cassert> #include <cassert>
#include <random>
#include "Cluster.hpp" #include "Cluster.hpp"
#include "User.hpp" #include "User.hpp"
@@ -115,6 +116,13 @@ Playlist::getEntries(int offset, int size, bool& moreResults) const
return res; return res;
} }
std::vector<Wt::Dbo::ptr<PlaylistEntry>>
Playlist::getAllEntries() const
{
bool moreResults;
return getEntries(-1, -1, moreResults);
}
Wt::Dbo::ptr<PlaylistEntry> Wt::Dbo::ptr<PlaylistEntry>
Playlist::getEntry(std::size_t pos) const Playlist::getEntry(std::size_t pos) const
{ {
@@ -171,8 +179,23 @@ Playlist::getTrackIds() const
.where("p.id = ?").bind(self()->id()); .where("p.id = ?").bind(self()->id());
return std::vector<IdType>(res.begin(), res.end()); 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 } // namespace Database
+4 -1
View File
@@ -53,11 +53,14 @@ class Playlist : public Wt::Dbo::Dbo<Playlist>
// Modifiers // Modifiers
void clear() { _entries.clear(); } void clear() { _entries.clear(); }
void shuffle();
// Get tracks, ordered by position // Get tracks, ordered by position
std::size_t getCount() const; std::size_t getCount() const;
Wt::Dbo::ptr<PlaylistEntry> getEntry(std::size_t pos) 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>> getEntries(int offset, int size, bool& moreResults) const;
std::vector<Wt::Dbo::ptr<PlaylistEntry>> getAllEntries() const;
std::vector<IdType> getTrackIds() const; std::vector<IdType> getTrackIds() const;
// Get clusters, order by occurence // Get clusters, order by occurence
@@ -95,7 +98,7 @@ class PlaylistEntry
static pointer getById(Wt::Dbo::Session& session, IdType id); static pointer getById(Wt::Dbo::Session& session, IdType id);
// Create utility // 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 // Accessors
Wt::Dbo::ptr<Track> getTrack() const { return _track; } Wt::Dbo::ptr<Track> getTrack() const { return _track; }
+13
View File
@@ -50,6 +50,7 @@ PlayQueue::PlayQueue()
_showMore->addFunction("tr", &Wt::WTemplate::Functions::tr); _showMore->addFunction("tr", &Wt::WTemplate::Functions::tr);
_showMore->setHidden(true); _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")); _radioMode = bindNew<Wt::WCheckBox>("radio-mode", Wt::WString::tr("Lms.PlayQueue.radio-mode"));
_nbTracks = bindNew<Wt::WText>("nb-tracks"); _nbTracks = bindNew<Wt::WText>("nb-tracks");
@@ -78,6 +79,18 @@ PlayQueue::PlayQueue()
updateInfo(); 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([=] _showMore->clicked().connect(std::bind([=]
{ {
addSome(); addSome();