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">
<div class="row">
<div class="col-lg-8">
<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"}
<h4><small>${nb-tracks}</small></h4>
</div>
<div class="row">
<div class="col-lg-8">
${entries}
${loading-indicator class="Lms-horizontal-center Lms-loading-indicator"}
</div>
</div>
</message>
+17 -38
View File
@@ -33,7 +33,7 @@
#include "utils/Service.hpp"
#include "utils/String.hpp"
#include "common/LoadingIndicator.hpp"
#include "common/InfiniteScrollingContainer.hpp"
#include "resource/CoverResource.hpp"
#include "resource/DownloadResource.hpp"
#include "LmsApplication.hpp"
@@ -67,8 +67,12 @@ PlayQueue::PlayQueue()
clearTracks();
});
_entriesContainer = bindNew<Wt::WContainerWidget>("entries");
hideLoadingIndicator();
_entriesContainer = bindNew<InfiniteScrollingContainer>("entries");
_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);
setToolTip(*shuffleBtn, Wt::WString::tr("Lms.PlayQueue.shuffle"));
@@ -184,27 +188,6 @@ PlayQueue::updateRadioBtn()
_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
PlayQueue::getTrackList() const
{
@@ -226,7 +209,6 @@ PlayQueue::clearTracks()
getTrackList().modify()->clear();
}
hideLoadingIndicator();
_entriesContainer->clear();
updateInfo();
}
@@ -322,10 +304,10 @@ PlayQueue::updateInfo()
void
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;
Wt::WTemplate* entry {static_cast<Wt::WTemplate*>(_entriesContainer->widget(*_trackPos))};
Wt::WTemplate* entry {static_cast<Wt::WTemplate*>(_entriesContainer->getWidget(*_trackPos))};
if (entry)
entry->bindString("is-selected", selected ? "Lms-playqueue-selected" : "");
}
@@ -410,14 +392,11 @@ PlayQueue::addSome()
auto tracklist = getTrackList();
auto tracklistEntries = tracklist->getEntries(_entriesContainer->count(), 50);
auto tracklistEntries = tracklist->getEntries(_entriesContainer->getCount(), 50);
for (const Database::TrackListEntry::pointer& tracklistEntry : tracklistEntries)
addEntry(tracklistEntry);
if (static_cast<std::size_t>(_entriesContainer->count()) < tracklist->getCount())
displayLoadingIndicator();
else
hideLoadingIndicator();
_entriesContainer->setHasMore(_entriesContainer->getCount() < tracklist->getCount());
}
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)};
playBtn->clicked().connect([=]
{
auto pos = _entriesContainer->indexOf(entry);
if (pos >= 0)
loadTrack(pos, true);
const std::optional<std::size_t> pos {_entriesContainer->getIndexOf(*entry)};
if (pos)
loadTrack(*pos, true);
});
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)
{
auto pos {_entriesContainer->indexOf(entry)};
if (pos > 0 && *_trackPos >= static_cast<std::size_t>(pos))
const std::optional<std::size_t> pos {_entriesContainer->getIndexOf(*entry)};
if (pos && *_trackPos >= *pos)
(*_trackPos)--;
}
_entriesContainer->removeWidget(entry);
_entriesContainer->remove(*entry);
updateInfo();
});
+7 -6
View File
@@ -28,11 +28,13 @@
#include "database/Types.hpp"
#include "PlayQueueAction.hpp"
namespace Similarity {
namespace Similarity
{
class Finder;
}
namespace Database {
namespace Database
{
class Track;
class TrackList;
class TrackListEntry;
@@ -40,6 +42,8 @@ namespace Database {
namespace UserInterface {
class InfiniteScrollingContainer;
class PlayQueue : public Wt::WTemplate
{
public:
@@ -72,8 +76,6 @@ class PlayQueue : public Wt::WTemplate
void updateCurrentTrack(bool selected);
void updateRepeatBtn();
void updateRadioBtn();
void displayLoadingIndicator();
void hideLoadingIndicator();
void loadTrack(std::size_t pos, bool play);
void stop();
@@ -88,8 +90,7 @@ class PlayQueue : public Wt::WTemplate
bool _radioMode {};
bool _mediaPlayerSettingsLoaded {};
Database::IdType _tracklistId {};
Wt::WContainerWidget* _entriesContainer {};
Wt::WTemplate* _loadingIndicator {};
InfiniteScrollingContainer* _entriesContainer {};
Wt::WText* _nbTracks {};
Wt::WText* _repeatBtn {};
Wt::WText* _radioBtn {};
@@ -59,6 +59,25 @@ namespace UserInterface
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
InfiniteScrollingContainer::displayLoadingIndicator()
{
@@ -79,5 +98,4 @@ namespace UserInterface
bindEmpty("loading-indicator");
}
}
@@ -19,6 +19,9 @@
#pragma once
#include <optional>
#include <utility>
#include <Wt/WContainerWidget.h>
#include <Wt/WSignal.h>
#include <Wt/WString.h>
@@ -36,6 +39,17 @@ namespace UserInterface
std::size_t getCount();
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);
Wt::Signal<> onRequestElements;