Updated musikbox to take advantage of LocalLibrary's ability to dispatch

results on a specified MessageQueue to reduce gymnastics required to
update category and track list views.
This commit is contained in:
casey langen 2017-01-08 21:16:14 -08:00
parent 5af9b778f5
commit c71f5a5f10
5 changed files with 23 additions and 55 deletions

View File

@ -110,6 +110,7 @@ int main(int argc, char* argv[])
musik::core::prefs::components::Settings);
LibraryPtr library = LibraryFactory::Libraries().at(0);
library->SetMessageQueue(Window::MessageQueue());
MasterTransport transport;
PlaybackService playback(Window::MessageQueue(), library, transport);

View File

@ -54,8 +54,6 @@ using namespace musik::box;
using namespace musik::glue;
using namespace cursespp;
#define WINDOW_MESSAGE_QUERY_COMPLETED 1002
CategoryListView::CategoryListView(
musik::core::audio::PlaybackService& playback,
LibraryPtr library,
@ -84,8 +82,6 @@ void CategoryListView::RequeryWithField(
const std::string& filter,
const DBID selectAfterQuery)
{
std::unique_lock<std::mutex> lock(this->queryMutex);
if (this->activeQuery) {
this->activeQuery->Cancel();
}
@ -162,42 +158,20 @@ bool CategoryListView::KeyPress(const std::string& key) {
}
void CategoryListView::OnQueryCompleted(musik::core::IQueryPtr query) {
std::unique_lock<std::mutex> lock(this->queryMutex);
auto active = this->activeQuery;
if (query == active) {
int selectIndex = -1;
int selectedIndex = -1;
if (this->selectAfterQuery != 0) {
selectIndex = active->GetIndexOf(this->selectAfterQuery);
selectedIndex = active->GetIndexOf(this->selectAfterQuery);
}
this->PostMessage(
WINDOW_MESSAGE_QUERY_COMPLETED,
selectIndex,
query->GetId());
}
}
void CategoryListView::ProcessMessage(IMessage &message) {
if (message.Type() == WINDOW_MESSAGE_QUERY_COMPLETED) {
std::unique_lock<std::mutex> lock(this->queryMutex);
/* UserData2 contains the ID of the query that dispatched this message.
it's possible another query started after the message was sent, and we
only want to react to the most recent result set. */
DBID queryId = static_cast<DBID>(message.UserData2());
if (this->activeQuery &&
this->activeQuery->GetId() == queryId &&
this->activeQuery->GetId() == query->GetId() &&
this->activeQuery->GetStatus() == IQuery::Finished)
{
this->metadata = activeQuery->GetResult();
activeQuery.reset();
/* UserData1 will be the index of the item we should select. if
the value is -1, we won't go out of our way to select anything. */
int selectedIndex = static_cast<int>(message.UserData1());
if (selectedIndex >= 0) {
this->SetSelectedIndex(selectedIndex);

View File

@ -78,8 +78,6 @@ namespace musik {
void Reset();
virtual void ProcessMessage(musik::core::runtime::IMessage &message);
DBID GetSelectedId();
std::string GetFieldName();
void SetFieldName(const std::string& fieldName);
@ -109,7 +107,6 @@ namespace musik {
musik::core::audio::PlaybackService& playback;
Adapter *adapter;
std::mutex queryMutex;
std::shared_ptr<musik::glue::CategoryListQuery> activeQuery;
musik::core::LibraryPtr library;

View File

@ -48,7 +48,6 @@
#include <boost/format.hpp>
#define WINDOW_MESSAGE_QUERY_COMPLETED 1002
#define WINDOW_MESSAGE_SCROLL_TO_PLAYING 1003
using namespace musik::core;
@ -105,7 +104,23 @@ void TrackListView::Requery(std::shared_ptr<TrackListQueryBase> query) {
void TrackListView::OnQueryCompleted(IQueryPtr query) {
if (query == this->query) {
this->PostMessage(WINDOW_MESSAGE_QUERY_COMPLETED);
if (this->query->GetStatus() == IQuery::Finished) {
this->metadata = this->query->GetResult();
this->headers = this->query->GetHeaders();
/* if the query was functionally the same as the last query, don't
mess with the selected index */
if (this->lastQueryHash != this->query->GetQueryHash()) {
this->SetSelectedIndex(0);
this->ScrollToTop();
}
this->lastQueryHash = this->query->GetQueryHash();
this->query.reset();
this->OnAdapterChanged(); /* internal handling */
this->Requeried(); /* for external handlers */
}
}
}
@ -140,26 +155,7 @@ void TrackListView::ScrollToPlaying() {
}
void TrackListView::ProcessMessage(IMessage &message) {
if (message.Type() == WINDOW_MESSAGE_QUERY_COMPLETED) {
if (this->query && this->query->GetStatus() == IQuery::Finished) {
this->metadata = this->query->GetResult();
this->headers = this->query->GetHeaders();
/* if the query was functionally the same as the last query, don't
mess with the selected index */
if (this->lastQueryHash != query->GetQueryHash()) {
this->SetSelectedIndex(0);
this->ScrollToTop();
}
this->lastQueryHash = this->query->GetQueryHash();
this->query.reset();
this->OnAdapterChanged(); /* internal handling */
this->Requeried(); /* for external handlers */
}
}
else if (message.Type() == WINDOW_MESSAGE_SCROLL_TO_PLAYING) {
if (message.Type() == WINDOW_MESSAGE_SCROLL_TO_PLAYING) {
this->ScrollToPlaying();
}
}

View File

@ -286,7 +286,7 @@ TransportWindow::TransportWindow(musik::core::audio::PlaybackService& playback)
this->playback.TrackChanged.connect(this, &TransportWindow::OnPlaybackServiceTrackChanged);
this->playback.ModeChanged.connect(this, &TransportWindow::OnPlaybackModeChanged);
this->playback.Shuffled.connect(this, &TransportWindow::OnPlaybackShuffled);
this->transport.VolumeChanged.connect(this, &TransportWindow::OnTransportVolumeChanged);
this->playback.VolumeChanged.connect(this, &TransportWindow::OnTransportVolumeChanged);
this->transport.TimeChanged.connect(this, &TransportWindow::OnTransportTimeChanged);
this->paused = false;
this->lastTime = DEFAULT_TIME;