From 7264cdc364a9ba17362a72ea0e9d8e0abd39cee5 Mon Sep 17 00:00:00 2001 From: emeric Date: Thu, 13 Sep 2018 13:21:20 +0200 Subject: [PATCH] Centralized the play/stop events from playqueue --- src/ui/LmsApplication.cpp | 65 +++++++++++------------------ src/ui/LmsApplication.hpp | 17 ++++++-- src/ui/MediaPlayer.cpp | 3 ++ src/ui/MediaPlayer.hpp | 5 +-- src/ui/PlayHistoryView.cpp | 18 ++++---- src/ui/PlayHistoryView.hpp | 2 - src/ui/PlayQueueView.cpp | 2 +- src/ui/PlayQueueView.hpp | 2 +- src/ui/explore/ArtistsInfoView.cpp | 10 +++++ src/ui/explore/Explore.cpp | 18 -------- src/ui/explore/Explore.hpp | 6 --- src/ui/explore/ReleasesInfoView.cpp | 10 +++++ src/ui/explore/ReleasesInfoView.hpp | 2 +- src/ui/explore/TracksInfoView.cpp | 10 +++++ 14 files changed, 85 insertions(+), 85 deletions(-) diff --git a/src/ui/LmsApplication.cpp b/src/ui/LmsApplication.cpp index 78e53c7f..d116fca9 100644 --- a/src/ui/LmsApplication.cpp +++ b/src/ui/LmsApplication.cpp @@ -145,7 +145,7 @@ LmsApplication::finalize() getApplicationGroup().postOthers([info] { - LmsApp->getGroupEvents().appClosed(info); + LmsApp->getEvents().appClosed(info); }); getApplicationGroup().leave(); @@ -304,7 +304,7 @@ LmsApplication::handleAuthEvent() getApplicationGroup().postOthers([info] { - LmsApp->getGroupEvents().appOpen(info); + LmsApp->getEvents().appOpen(info); }); createHome(); @@ -407,7 +407,7 @@ LmsApplication::createHome() Explore* explore = mainStack->addNew(); PlayQueue* playqueue = mainStack->addNew(); - PlayHistory* playhistory = mainStack->addNew(); + mainStack->addNew(); mainStack->addNew(); // Admin stuff @@ -446,54 +446,44 @@ LmsApplication::createHome() playqueue->playNext(); }); - // Events from the PlayQueue - playqueue->loadTrack.connect([=](Database::IdType trackId, bool play) + playqueue->loadTrack.connect([=] (Database::IdType trackId, bool play) { - playhistory->addTrack(trackId); + _events.trackLoaded(trackId, play); }); - playqueue->loadTrack.connect([=](Database::IdType trackId, bool play) + playqueue->trackUnload.connect([=] { - explore->handleTrackPlayed(trackId); + _events.trackUnloaded(); }); - playqueue->loadTrack.connect(player, &MediaPlayer::loadTrack); - - playqueue->playbackStop.connect(player, &MediaPlayer::stop); - // Events from MediaScanner std::string sessionId = LmsApp->sessionId(); _scanner.scanComplete().connect([=] (Scanner::MediaScanner::Stats stats) { + // Runs from media scanner context Wt::WServer::instance()->post(sessionId, [=] { - bool changes = false; - - if (_isAdmin) - { - notifyMsg(MsgType::Info, Wt::WString::tr("Lms.Admin.Database.scan-complete") - .arg(static_cast(stats.nbFiles())) - .arg(static_cast(stats.additions)) - .arg(static_cast(stats.updates)) - .arg(static_cast(stats.deletions)) - .arg(static_cast(stats.nbDuplicates())) - .arg(static_cast(stats.nbErrors()))); - changes = true; - } - - if (stats.nbChanges() > 0) - { - explore->handleDbChanged(); - changes = true; - } - - if (changes) - triggerUpdate(); + _events.dbScanned.emit(stats); + triggerUpdate(); }); }); + _events.dbScanned.connect([=] (Scanner::MediaScanner::Stats stats) + { + if (_isAdmin) + { + notifyMsg(MsgType::Info, Wt::WString::tr("Lms.Admin.Database.scan-complete") + .arg(static_cast(stats.nbFiles())) + .arg(static_cast(stats.additions)) + .arg(static_cast(stats.updates)) + .arg(static_cast(stats.deletions)) + .arg(static_cast(stats.nbDuplicates())) + .arg(static_cast(stats.nbErrors()))); + } + }); + // Events from Application group - _groupEvents.appOpen.connect([=] (LmsApplicationInfo info) + _events.appOpen.connect([=] (LmsApplicationInfo info) { // Only one active session by user if (!LmsApp->getUser()->isDemo()) @@ -503,11 +493,6 @@ LmsApplication::createHome() } }); - _groupEvents.appClosed.connect([=] (LmsApplicationInfo info) - { - ; - }); - internalPathChanged().connect(std::bind([=] { handlePathChange(mainStack, _isAdmin); diff --git a/src/ui/LmsApplication.hpp b/src/ui/LmsApplication.hpp index 5bb3eb31..156eaffd 100644 --- a/src/ui/LmsApplication.hpp +++ b/src/ui/LmsApplication.hpp @@ -38,12 +38,23 @@ namespace UserInterface { class TranscodeResource; class ImageResource; -struct GroupEvents +// Events that can be listen to anywhere in the application +struct Events { + // Events relative to group Wt::Signal appOpen; Wt::Signal appClosed; + + // A track is being loaded + Wt::Signal trackLoaded; + // Unload current track + Wt::Signal<> trackUnloaded; + + // A database scan is complete + Wt::Signal dbScanned; }; +// Used to classify the message sent to the user enum class MsgType { Success, @@ -73,7 +84,7 @@ class LmsApplication : public Wt::WApplication Scanner::MediaScanner& getMediaScanner() { return _scanner; } - GroupEvents& getGroupEvents() { return _groupEvents; } + Events& getEvents() { return _events; } // Utils void goHome(); @@ -105,7 +116,7 @@ class LmsApplication : public Wt::WApplication Wt::Signal<> _preQuit; Database::Handler _db; LmsApplicationGroupContainer& _appGroups; - GroupEvents _groupEvents; + Events _events; Wt::WString _userIdentity; Auth* _auth; Scanner::MediaScanner& _scanner; diff --git a/src/ui/MediaPlayer.cpp b/src/ui/MediaPlayer.cpp index 63c9e13b..6afa3f8c 100644 --- a/src/ui/MediaPlayer.cpp +++ b/src/ui/MediaPlayer.cpp @@ -48,6 +48,9 @@ MediaPlayer::MediaPlayer() _release->setTextFormat(Wt::TextFormat::Plain); wApp->doJavaScript("LMS.mediaplayer.init(" + jsRef() + ")"); + + LmsApp->getEvents().trackLoaded.connect(this, &MediaPlayer::loadTrack); + LmsApp->getEvents().trackUnloaded.connect(this, &MediaPlayer::stop); } void diff --git a/src/ui/MediaPlayer.hpp b/src/ui/MediaPlayer.hpp index 02125d15..538d64b1 100644 --- a/src/ui/MediaPlayer.hpp +++ b/src/ui/MediaPlayer.hpp @@ -33,15 +33,14 @@ class MediaPlayer : public Wt::WTemplate public: MediaPlayer(); - void stop(); - void loadTrack(Database::IdType trackId, bool play); - // Signals Wt::JSignal<> playbackEnded; Wt::JSignal<> playPrevious; Wt::JSignal<> playNext; private: + void stop(); + void loadTrack(Database::IdType trackId, bool play); Wt::WText* _title; Wt::WAnchor* _release; diff --git a/src/ui/PlayHistoryView.cpp b/src/ui/PlayHistoryView.cpp index ecdee58b..ac44f95a 100644 --- a/src/ui/PlayHistoryView.cpp +++ b/src/ui/PlayHistoryView.cpp @@ -74,19 +74,17 @@ PlayHistory::PlayHistory() addSome(); }); + LmsApp->getEvents().trackLoaded.connect([=](Database::IdType trackId, bool /* play */) + { + Wt::Dbo::Transaction transaction (LmsApp->getDboSession()); + + auto trackEntry = LmsApp->getUser()->getPlayedTrackList().modify()->add(trackId); + _entriesContainer->insertWidget(0, createEntry(trackEntry->getTrack())); + }); + addSome(); } -void -PlayHistory::addTrack(Database::IdType trackId) -{ - Wt::Dbo::Transaction transaction (LmsApp->getDboSession()); - - auto trackEntry = LmsApp->getUser()->getPlayedTrackList().modify()->add(trackId); - _entriesContainer->insertWidget(0, createEntry(trackEntry->getTrack())); -} - - void PlayHistory::addSome() { diff --git a/src/ui/PlayHistoryView.hpp b/src/ui/PlayHistoryView.hpp index 7bd5c88e..36cf73c2 100644 --- a/src/ui/PlayHistoryView.hpp +++ b/src/ui/PlayHistoryView.hpp @@ -32,8 +32,6 @@ class PlayHistory : public Wt::WTemplate public: PlayHistory(); - void addTrack(Database::IdType trackId); - private: void addSome(); diff --git a/src/ui/PlayQueueView.cpp b/src/ui/PlayQueueView.cpp index a37b318d..6011c2ed 100644 --- a/src/ui/PlayQueueView.cpp +++ b/src/ui/PlayQueueView.cpp @@ -135,7 +135,7 @@ PlayQueue::stop() { updateCurrentTrack(false); _trackPos.reset(); - playbackStop.emit(); + trackUnload.emit(); } void diff --git a/src/ui/PlayQueueView.hpp b/src/ui/PlayQueueView.hpp index dd08ca43..596486c8 100644 --- a/src/ui/PlayQueueView.hpp +++ b/src/ui/PlayQueueView.hpp @@ -51,7 +51,7 @@ class PlayQueue : public Wt::WTemplate Wt::Signal loadTrack; // Signal emitted when play has to be stopped - Wt::Signal<> playbackStop; + Wt::Signal<> trackUnload; private: Database::TrackList::pointer getTrackList(); diff --git a/src/ui/explore/ArtistsInfoView.cpp b/src/ui/explore/ArtistsInfoView.cpp index ec389a3b..d64efadf 100644 --- a/src/ui/explore/ArtistsInfoView.cpp +++ b/src/ui/explore/ArtistsInfoView.cpp @@ -57,6 +57,16 @@ ArtistsInfo::ArtistsInfo() _mostPlayedContainer = bindNew("most-played"); _recentlyAddedContainer = bindNew("recently-added"); + LmsApp->getEvents().dbScanned.connect([=] + { + refreshRecentlyAdded(); + }); + + LmsApp->getEvents().trackLoaded.connect([=] + { + refreshMostPlayed(); + }); + refreshMostPlayed(); refreshRecentlyAdded(); } diff --git a/src/ui/explore/Explore.cpp b/src/ui/explore/Explore.cpp index b77320d2..7adbe212 100644 --- a/src/ui/explore/Explore.cpp +++ b/src/ui/explore/Explore.cpp @@ -152,32 +152,14 @@ Explore::Explore() Wt::WStackedWidget* infoStack = bindNew("info"); auto artistsInfo = std::make_unique(); - auto artistsInfoRaw = artistsInfo.get(); infoStack->addWidget(std::move(artistsInfo)); auto releasesInfo = std::make_unique(); - auto releasesInfoRaw = releasesInfo.get(); infoStack->addWidget(std::move(releasesInfo)); auto tracksInfo = std::make_unique(); - auto tracksInfoRaw = tracksInfo.get(); infoStack->addWidget(std::move(tracksInfo)); - _dbChanged.connect([=] - { - artistsInfoRaw->refreshRecentlyAdded(); - releasesInfoRaw->refreshRecentlyAdded(); - tracksInfoRaw->refreshRecentlyAdded(); - }); - - _trackPlayed.connect([=] - { - artistsInfoRaw->refreshMostPlayed(); - releasesInfoRaw->refreshMostPlayed(); - tracksInfoRaw->refreshMostPlayed(); - }); - - wApp->internalPathChanged().connect(std::bind([=] { handleContentsPathChange(contentsStack); diff --git a/src/ui/explore/Explore.hpp b/src/ui/explore/Explore.hpp index e00c8bc7..ea538876 100644 --- a/src/ui/explore/Explore.hpp +++ b/src/ui/explore/Explore.hpp @@ -36,9 +36,6 @@ class Explore : public Wt::WTemplate Wt::Signal> tracksAdd; Wt::Signal> tracksPlay; - void handleDbChanged() { _dbChanged.emit(); }; - void handleTrackPlayed(Database::IdType trackId) { _trackPlayed.emit(); } - private: void handleArtistAdd(Database::IdType id); @@ -51,9 +48,6 @@ class Explore : public Wt::WTemplate void handleTracksPlay(std::vector tracks); Filters* _filters; - - Wt::Signal<> _dbChanged; - Wt::Signal<> _trackPlayed; }; } // namespace UserInterface diff --git a/src/ui/explore/ReleasesInfoView.cpp b/src/ui/explore/ReleasesInfoView.cpp index f30f0dea..994f2fe5 100644 --- a/src/ui/explore/ReleasesInfoView.cpp +++ b/src/ui/explore/ReleasesInfoView.cpp @@ -77,6 +77,16 @@ ReleasesInfo::ReleasesInfo() _mostPlayedContainer = bindNew("most-played"); _recentlyAddedContainer = bindNew("recently-added"); + LmsApp->getEvents().dbScanned.connect([=] + { + refreshRecentlyAdded(); + }); + + LmsApp->getEvents().trackLoaded.connect([=] + { + refreshMostPlayed(); + }); + refreshRecentlyAdded(); refreshMostPlayed(); } diff --git a/src/ui/explore/ReleasesInfoView.hpp b/src/ui/explore/ReleasesInfoView.hpp index fe402421..a74edf84 100644 --- a/src/ui/explore/ReleasesInfoView.hpp +++ b/src/ui/explore/ReleasesInfoView.hpp @@ -29,10 +29,10 @@ class ReleasesInfo : public Wt::WTemplate public: ReleasesInfo(); + private: void refreshRecentlyAdded(); void refreshMostPlayed(); - private: Wt::WContainerWidget* _mostPlayedContainer; Wt::WContainerWidget* _recentlyAddedContainer; }; diff --git a/src/ui/explore/TracksInfoView.cpp b/src/ui/explore/TracksInfoView.cpp index ed2c9b18..509bf186 100644 --- a/src/ui/explore/TracksInfoView.cpp +++ b/src/ui/explore/TracksInfoView.cpp @@ -57,6 +57,16 @@ TracksInfo::TracksInfo() _mostPlayedContainer = bindNew("most-played"); _recentlyAddedContainer = bindNew("recently-added"); + LmsApp->getEvents().dbScanned.connect([=] + { + refreshRecentlyAdded(); + }); + + LmsApp->getEvents().trackLoaded.connect([=] + { + refreshMostPlayed(); + }); + refreshMostPlayed(); refreshRecentlyAdded(); }