- Fixed a bug that could let partial results get displayed in

CategoryListView
- Allow queries to be marked as cancelable so they won't be processed.
This commit is contained in:
casey 2016-05-27 22:59:19 -07:00
parent 306103c0e8
commit dd9b0818f1
6 changed files with 32 additions and 12 deletions

View File

@ -22,6 +22,7 @@ namespace musik { namespace core {
Running = 2,
Failed = 3,
Finished = 4,
Canceled = 5
} Status;
virtual ~IQuery() { }

View File

@ -48,7 +48,8 @@ static boost::atomic<int> nextId(0);
QueryBase::QueryBase()
: status(0)
, options(0)
, queryId(0) {
, queryId(0)
, cancel(false) {
this->queryId = nextId++;
}
@ -62,7 +63,11 @@ std::string QueryBase::Name() {
bool QueryBase::Run(db::Connection &db) {
this->SetStatus(Running);
try {
if (OnRun(db)) {
if (this->IsCanceled()) {
this->SetStatus(Canceled);
return true;
}
else if (OnRun(db)) {
this->SetStatus(Finished);
return true;
}

View File

@ -57,6 +57,8 @@ namespace musik { namespace core { namespace query {
virtual int GetStatus();
virtual int GetId();
virtual int GetOptions();
virtual void Cancel() { this->cancel = true; }
virtual bool IsCanceled() { return cancel; }
protected:
void SetStatus(int status);
@ -69,6 +71,7 @@ namespace musik { namespace core { namespace query {
unsigned int status;
unsigned int queryId;
unsigned int options;
volatile bool cancel;
boost::mutex stateMutex;
};

View File

@ -10,7 +10,13 @@
using namespace musik::core::library::constants;
#define CATEGORY_WIDTH 25
#define TRANSPORT_HEIGHT 2
#ifdef WIN32
#define TRANSPORT_HEIGHT 3
#else
#define TRANSPORT_HEIGHT 2
#endif
#define DEFAULT_CATEGORY constants::Track::ALBUM_ID
using namespace musik::core;

View File

@ -15,7 +15,7 @@ using namespace musik::core::db;
using namespace musik::core::library::constants;
using namespace musik::box;
#define reset(x) x.reset(new std::vector<std::shared_ptr<Result> >);
#define RESET_RESULT(x) x.reset(new std::vector<std::shared_ptr<Result> >);
static const std::string ALBUM_QUERY =
"SELECT DISTINCT albums.id, albums.name "
@ -47,7 +47,7 @@ static void initFieldToQueryMap() {
CategoryListViewQuery::CategoryListViewQuery(const std::string& trackField) {
this->trackField = trackField;
reset(result);
RESET_RESULT(result);
{
boost::mutex::scoped_lock lock(QUERY_MAP_MUTEX);
@ -71,7 +71,7 @@ CategoryListViewQuery::ResultList CategoryListViewQuery::GetResult() {
}
bool CategoryListViewQuery::OnRun(Connection& db) {
reset(result);
RESET_RESULT(result);
std::string query = FIELD_TO_QUERY_MAP[this->trackField];
Statement stmt(query.c_str(), db);

View File

@ -34,6 +34,10 @@ CategoryListView::~CategoryListView() {
}
void CategoryListView::Requery() {
if (this->activeQuery) {
this->activeQuery->Cancel();
}
this->activeQuery.reset(new CategoryListViewQuery(this->fieldName));
this->library->Enqueue(activeQuery);
}
@ -56,7 +60,6 @@ void CategoryListView::SetFieldName(const std::string& fieldName) {
if (this->metadata) {
this->metadata.reset();
//this->OnAdapterChanged();
}
this->Requery();
@ -71,10 +74,12 @@ void CategoryListView::OnQueryCompleted(QueryPtr query) {
void CategoryListView::ProcessMessage(IMessage &message) {
if (message.Type() == WINDOW_MESSAGE_QUERY_COMPLETED) {
this->metadata = activeQuery->GetResult();
activeQuery.reset();
this->OnAdapterChanged();
this->OnInvalidated();
if (this->activeQuery && this->activeQuery->GetStatus() == IQuery::Finished) {
this->metadata = activeQuery->GetResult();
activeQuery.reset();
this->OnAdapterChanged();
this->OnInvalidated();
}
}
}