Factorized some more code

This commit is contained in:
emeric
2021-05-20 17:39:06 +02:00
parent 7cc8d22d5d
commit 0af6342b34
5 changed files with 62 additions and 51 deletions
+2 -3
View File
@@ -8,14 +8,13 @@
<message id="Lms.PlayQueue.template.radio-btn"><i class="fa fa-fw fa-rss"></i></message> <message id="Lms.PlayQueue.template.radio-btn"><i class="fa fa-fw fa-rss"></i></message>
<message id="Lms.PlayQueue.template"> <message id="Lms.PlayQueue.template">
<div class="row">
<div class="col-lg-8">
<div class="Lms-header"> <div class="Lms-header">
${clear-btn class="Lms-playqueue-btn Lms-btn"}${shuffle-btn class="Lms-playqueue-btn Lms-btn"}${repeat-btn class="Lms-playqueue-btn Lms-btn"}${radio-btn class="Lms-playqueue-btn Lms-btn"} ${clear-btn class="Lms-playqueue-btn Lms-btn"}${shuffle-btn class="Lms-playqueue-btn Lms-btn"}${repeat-btn class="Lms-playqueue-btn Lms-btn"}${radio-btn class="Lms-playqueue-btn Lms-btn"}
<h4><small>${nb-tracks}</small></h4> <h4><small>${nb-tracks}</small></h4>
</div> </div>
<div class="row">
<div class="col-lg-8">
${entries} ${entries}
${loading-indicator class="Lms-horizontal-center Lms-loading-indicator"}
</div> </div>
</div> </div>
</message> </message>
+17 -38
View File
@@ -33,7 +33,7 @@
#include "utils/Service.hpp" #include "utils/Service.hpp"
#include "utils/String.hpp" #include "utils/String.hpp"
#include "common/LoadingIndicator.hpp" #include "common/InfiniteScrollingContainer.hpp"
#include "resource/CoverResource.hpp" #include "resource/CoverResource.hpp"
#include "resource/DownloadResource.hpp" #include "resource/DownloadResource.hpp"
#include "LmsApplication.hpp" #include "LmsApplication.hpp"
@@ -67,8 +67,12 @@ PlayQueue::PlayQueue()
clearTracks(); clearTracks();
}); });
_entriesContainer = bindNew<Wt::WContainerWidget>("entries"); _entriesContainer = bindNew<InfiniteScrollingContainer>("entries");
hideLoadingIndicator(); _entriesContainer->onRequestElements.connect([this]
{
addSome();
updateCurrentTrack(true);
});
Wt::WText* shuffleBtn = bindNew<Wt::WText>("shuffle-btn", Wt::WString::tr("Lms.PlayQueue.template.shuffle-btn"), Wt::TextFormat::XHTML); Wt::WText* shuffleBtn = bindNew<Wt::WText>("shuffle-btn", Wt::WString::tr("Lms.PlayQueue.template.shuffle-btn"), Wt::TextFormat::XHTML);
setToolTip(*shuffleBtn, Wt::WString::tr("Lms.PlayQueue.shuffle")); setToolTip(*shuffleBtn, Wt::WString::tr("Lms.PlayQueue.shuffle"));
@@ -184,27 +188,6 @@ PlayQueue::updateRadioBtn()
_radioBtn->toggleStyleClass("text-muted", !_radioMode); _radioBtn->toggleStyleClass("text-muted", !_radioMode);
} }
void
PlayQueue::displayLoadingIndicator()
{
_loadingIndicator = bindWidget<Wt::WTemplate>("loading-indicator", createLoadingIndicator());
_loadingIndicator->scrollVisibilityChanged().connect([this](bool visible)
{
if (!visible)
return;
addSome();
updateCurrentTrack(true);
});
}
void
PlayQueue::hideLoadingIndicator()
{
_loadingIndicator = nullptr;
bindEmpty("loading-indicator");
}
Database::TrackList::pointer Database::TrackList::pointer
PlayQueue::getTrackList() const PlayQueue::getTrackList() const
{ {
@@ -226,7 +209,6 @@ PlayQueue::clearTracks()
getTrackList().modify()->clear(); getTrackList().modify()->clear();
} }
hideLoadingIndicator();
_entriesContainer->clear(); _entriesContainer->clear();
updateInfo(); updateInfo();
} }
@@ -322,10 +304,10 @@ PlayQueue::updateInfo()
void void
PlayQueue::updateCurrentTrack(bool selected) PlayQueue::updateCurrentTrack(bool selected)
{ {
if (!_trackPos || *_trackPos >= static_cast<std::size_t>(_entriesContainer->count())) if (!_trackPos || *_trackPos >= static_cast<std::size_t>(_entriesContainer->getCount()))
return; return;
Wt::WTemplate* entry {static_cast<Wt::WTemplate*>(_entriesContainer->widget(*_trackPos))}; Wt::WTemplate* entry {static_cast<Wt::WTemplate*>(_entriesContainer->getWidget(*_trackPos))};
if (entry) if (entry)
entry->bindString("is-selected", selected ? "Lms-playqueue-selected" : ""); entry->bindString("is-selected", selected ? "Lms-playqueue-selected" : "");
} }
@@ -410,14 +392,11 @@ PlayQueue::addSome()
auto tracklist = getTrackList(); auto tracklist = getTrackList();
auto tracklistEntries = tracklist->getEntries(_entriesContainer->count(), 50); auto tracklistEntries = tracklist->getEntries(_entriesContainer->getCount(), 50);
for (const Database::TrackListEntry::pointer& tracklistEntry : tracklistEntries) for (const Database::TrackListEntry::pointer& tracklistEntry : tracklistEntries)
addEntry(tracklistEntry); addEntry(tracklistEntry);
if (static_cast<std::size_t>(_entriesContainer->count()) < tracklist->getCount()) _entriesContainer->setHasMore(_entriesContainer->getCount() < tracklist->getCount());
displayLoadingIndicator();
else
hideLoadingIndicator();
} }
void void
@@ -474,9 +453,9 @@ PlayQueue::addEntry(const Database::TrackListEntry::pointer& tracklistEntry)
Wt::WText* playBtn {entry->bindNew<Wt::WText>("play-btn", Wt::WString::tr("Lms.PlayQueue.template.play-btn"), Wt::TextFormat::XHTML)}; Wt::WText* playBtn {entry->bindNew<Wt::WText>("play-btn", Wt::WString::tr("Lms.PlayQueue.template.play-btn"), Wt::TextFormat::XHTML)};
playBtn->clicked().connect([=] playBtn->clicked().connect([=]
{ {
auto pos = _entriesContainer->indexOf(entry); const std::optional<std::size_t> pos {_entriesContainer->getIndexOf(*entry)};
if (pos >= 0) if (pos)
loadTrack(pos, true); loadTrack(*pos, true);
}); });
Wt::WText* delBtn {entry->bindNew<Wt::WText>("del-btn", Wt::WString::tr("Lms.PlayQueue.template.delete-btn"), Wt::TextFormat::XHTML)}; Wt::WText* delBtn {entry->bindNew<Wt::WText>("del-btn", Wt::WString::tr("Lms.PlayQueue.template.delete-btn"), Wt::TextFormat::XHTML)};
@@ -492,12 +471,12 @@ PlayQueue::addEntry(const Database::TrackListEntry::pointer& tracklistEntry)
if (_trackPos) if (_trackPos)
{ {
auto pos {_entriesContainer->indexOf(entry)}; const std::optional<std::size_t> pos {_entriesContainer->getIndexOf(*entry)};
if (pos > 0 && *_trackPos >= static_cast<std::size_t>(pos)) if (pos && *_trackPos >= *pos)
(*_trackPos)--; (*_trackPos)--;
} }
_entriesContainer->removeWidget(entry); _entriesContainer->remove(*entry);
updateInfo(); updateInfo();
}); });
+7 -6
View File
@@ -28,11 +28,13 @@
#include "database/Types.hpp" #include "database/Types.hpp"
#include "PlayQueueAction.hpp" #include "PlayQueueAction.hpp"
namespace Similarity { namespace Similarity
{
class Finder; class Finder;
} }
namespace Database { namespace Database
{
class Track; class Track;
class TrackList; class TrackList;
class TrackListEntry; class TrackListEntry;
@@ -40,6 +42,8 @@ namespace Database {
namespace UserInterface { namespace UserInterface {
class InfiniteScrollingContainer;
class PlayQueue : public Wt::WTemplate class PlayQueue : public Wt::WTemplate
{ {
public: public:
@@ -72,8 +76,6 @@ class PlayQueue : public Wt::WTemplate
void updateCurrentTrack(bool selected); void updateCurrentTrack(bool selected);
void updateRepeatBtn(); void updateRepeatBtn();
void updateRadioBtn(); void updateRadioBtn();
void displayLoadingIndicator();
void hideLoadingIndicator();
void loadTrack(std::size_t pos, bool play); void loadTrack(std::size_t pos, bool play);
void stop(); void stop();
@@ -88,8 +90,7 @@ class PlayQueue : public Wt::WTemplate
bool _radioMode {}; bool _radioMode {};
bool _mediaPlayerSettingsLoaded {}; bool _mediaPlayerSettingsLoaded {};
Database::IdType _tracklistId {}; Database::IdType _tracklistId {};
Wt::WContainerWidget* _entriesContainer {}; InfiniteScrollingContainer* _entriesContainer {};
Wt::WTemplate* _loadingIndicator {};
Wt::WText* _nbTracks {}; Wt::WText* _nbTracks {};
Wt::WText* _repeatBtn {}; Wt::WText* _repeatBtn {};
Wt::WText* _radioBtn {}; Wt::WText* _radioBtn {};
@@ -59,6 +59,25 @@ namespace UserInterface
hideLoadingIndicator(); hideLoadingIndicator();
} }
void
InfiniteScrollingContainer::remove(Wt::WWidget& widget)
{
_elements->removeWidget(&widget);
}
Wt::WWidget*
InfiniteScrollingContainer::getWidget(std::size_t pos) const
{
return _elements->widget(pos);
}
std::optional<std::size_t>
InfiniteScrollingContainer::getIndexOf(Wt::WWidget& widget) const
{
return _elements->indexOf(&widget);
}
void void
InfiniteScrollingContainer::displayLoadingIndicator() InfiniteScrollingContainer::displayLoadingIndicator()
{ {
@@ -79,5 +98,4 @@ namespace UserInterface
bindEmpty("loading-indicator"); bindEmpty("loading-indicator");
} }
} }
@@ -19,6 +19,9 @@
#pragma once #pragma once
#include <optional>
#include <utility>
#include <Wt/WContainerWidget.h> #include <Wt/WContainerWidget.h>
#include <Wt/WSignal.h> #include <Wt/WSignal.h>
#include <Wt/WString.h> #include <Wt/WString.h>
@@ -36,6 +39,17 @@ namespace UserInterface
std::size_t getCount(); std::size_t getCount();
void add(std::unique_ptr<Wt::WWidget> result); void add(std::unique_ptr<Wt::WWidget> result);
template<typename T, typename... Args>
T* addNew(Args&&... args)
{
return _elements->addNew<T>(std::forward<Args>(args)...);
}
void remove(Wt::WWidget& widget);
Wt::WWidget* getWidget(std::size_t pos) const;
std::optional<std::size_t> getIndexOf(Wt::WWidget& widget) const;
void setHasMore(bool hasMore); void setHasMore(bool hasMore);
Wt::Signal<> onRequestElements; Wt::Signal<> onRequestElements;