mirror of
https://github.com/The-Powder-Toy/The-Powder-Toy.git
synced 2025-09-01 04:01:56 +02:00
Add day/week/month/year selector in the search.
This commit is contained in:
@@ -14,4 +14,13 @@ namespace http
|
|||||||
sortByVotes,
|
sortByVotes,
|
||||||
sortByDate,
|
sortByDate,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
enum Period
|
||||||
|
{
|
||||||
|
allSaves,
|
||||||
|
todaySaves,
|
||||||
|
weekSaves,
|
||||||
|
monthSaves,
|
||||||
|
yearSaves,
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
@@ -1,3 +1,4 @@
|
|||||||
|
#include <ctime>
|
||||||
#include "SearchSavesRequest.h"
|
#include "SearchSavesRequest.h"
|
||||||
#include "Config.h"
|
#include "Config.h"
|
||||||
#include "client/Client.h"
|
#include "client/Client.h"
|
||||||
@@ -6,7 +7,7 @@
|
|||||||
|
|
||||||
namespace http
|
namespace http
|
||||||
{
|
{
|
||||||
static ByteString Url(int start, int count, ByteString query, Sort sort, Category category)
|
static ByteString Url(int start, int count, ByteString query, Period period, Sort sort, Category category)
|
||||||
{
|
{
|
||||||
ByteStringBuilder builder;
|
ByteStringBuilder builder;
|
||||||
builder << SCHEME << SERVER << "/Browse.json?Start=" << start << "&Count=" << count;
|
builder << SCHEME << SERVER << "/Browse.json?Start=" << start << "&Count=" << count;
|
||||||
@@ -17,6 +18,38 @@ namespace http
|
|||||||
}
|
}
|
||||||
query += str;
|
query += str;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
time_t currentTime = time(NULL);
|
||||||
|
|
||||||
|
if(period)
|
||||||
|
{
|
||||||
|
switch (period)
|
||||||
|
{
|
||||||
|
case todaySaves:
|
||||||
|
currentTime -= 60*60*24; // One day
|
||||||
|
break;
|
||||||
|
case weekSaves:
|
||||||
|
currentTime -= 60*60*24*7; // One week
|
||||||
|
break;
|
||||||
|
case monthSaves:
|
||||||
|
currentTime -= 60*60*24*31; // One month
|
||||||
|
break;
|
||||||
|
case yearSaves:
|
||||||
|
currentTime -= 60*60*24*365; // One year
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct tm currentTimeData = *localtime(¤tTime);
|
||||||
|
ByteStringBuilder afterQuery;
|
||||||
|
|
||||||
|
afterQuery << "after:" << currentTimeData.tm_year+1900 << "-" <<
|
||||||
|
(currentTimeData.tm_mon < 9 ? "0" : "") << currentTimeData.tm_mon+1 << "-" <<
|
||||||
|
(currentTimeData.tm_mday < 10 ? "0" : "") << currentTimeData.tm_mday;
|
||||||
|
appendToQuery(afterQuery.Build());
|
||||||
|
}
|
||||||
|
|
||||||
switch (sort)
|
switch (sort)
|
||||||
{
|
{
|
||||||
case sortByDate:
|
case sortByDate:
|
||||||
@@ -48,7 +81,7 @@ namespace http
|
|||||||
return builder.Build();
|
return builder.Build();
|
||||||
}
|
}
|
||||||
|
|
||||||
SearchSavesRequest::SearchSavesRequest(int start, int count, ByteString query, Sort sort, Category category) : APIRequest(Url(start, count, query, sort, category), authUse, false)
|
SearchSavesRequest::SearchSavesRequest(int start, int count, ByteString query, Period period, Sort sort, Category category) : APIRequest(Url(start, count, query, period, sort, category), authUse, false)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -8,7 +8,7 @@ namespace http
|
|||||||
class SearchSavesRequest : public APIRequest
|
class SearchSavesRequest : public APIRequest
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
SearchSavesRequest(int start, int count, ByteString query, Sort sort, Category category);
|
SearchSavesRequest(int start, int count, ByteString query, Period period, Sort sort, Category category);
|
||||||
|
|
||||||
std::pair<int, std::vector<std::unique_ptr<SaveInfo>>> Finish();
|
std::pair<int, std::vector<std::unique_ptr<SaveInfo>>> Finish();
|
||||||
};
|
};
|
||||||
|
@@ -136,6 +136,32 @@ void SearchController::SetPageRelative(int offset)
|
|||||||
searchModel->UpdateSaveList(page, searchModel->GetLastQuery());
|
searchModel->UpdateSaveList(page, searchModel->GetLastQuery());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void SearchController::ChangePeriod(int period)
|
||||||
|
{
|
||||||
|
switch(period)
|
||||||
|
{
|
||||||
|
case 0:
|
||||||
|
searchModel->SetPeriod(http::allSaves);
|
||||||
|
break;
|
||||||
|
case 1:
|
||||||
|
searchModel->SetPeriod(http::todaySaves);
|
||||||
|
break;
|
||||||
|
case 2:
|
||||||
|
searchModel->SetPeriod(http::weekSaves);
|
||||||
|
break;
|
||||||
|
case 3:
|
||||||
|
searchModel->SetPeriod(http::monthSaves);
|
||||||
|
break;
|
||||||
|
case 4:
|
||||||
|
searchModel->SetPeriod(http::yearSaves);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
searchModel->SetPeriod(http::allSaves);
|
||||||
|
}
|
||||||
|
|
||||||
|
searchModel->UpdateSaveList(1, searchModel->GetLastQuery());
|
||||||
|
}
|
||||||
|
|
||||||
void SearchController::ChangeSort()
|
void SearchController::ChangeSort()
|
||||||
{
|
{
|
||||||
if(searchModel->GetSort() == http::sortByDate)
|
if(searchModel->GetSort() == http::sortByDate)
|
||||||
|
@@ -37,6 +37,7 @@ public:
|
|||||||
void Refresh();
|
void Refresh();
|
||||||
void SetPage(int page);
|
void SetPage(int page);
|
||||||
void SetPageRelative(int offset);
|
void SetPageRelative(int offset);
|
||||||
|
void ChangePeriod(int period);
|
||||||
void ChangeSort();
|
void ChangeSort();
|
||||||
void ShowOwn(bool show);
|
void ShowOwn(bool show);
|
||||||
void ShowFavourite(bool show);
|
void ShowFavourite(bool show);
|
||||||
|
@@ -11,6 +11,7 @@
|
|||||||
#include <cmath>
|
#include <cmath>
|
||||||
|
|
||||||
SearchModel::SearchModel():
|
SearchModel::SearchModel():
|
||||||
|
currentPeriod(http::allSaves),
|
||||||
currentSort(http::sortByVotes),
|
currentSort(http::sortByVotes),
|
||||||
currentPage(1),
|
currentPage(1),
|
||||||
resultCount(0),
|
resultCount(0),
|
||||||
@@ -30,11 +31,11 @@ bool SearchModel::GetShowTags()
|
|||||||
return showTags;
|
return showTags;
|
||||||
}
|
}
|
||||||
|
|
||||||
void SearchModel::BeginSearchSaves(int start, int count, String query, http::Sort sort, http::Category category)
|
void SearchModel::BeginSearchSaves(int start, int count, String query, http::Period period, http::Sort sort, http::Category category)
|
||||||
{
|
{
|
||||||
lastError = "";
|
lastError = "";
|
||||||
resultCount = 0;
|
resultCount = 0;
|
||||||
searchSaves = std::make_unique<http::SearchSavesRequest>(start, count, query.ToUtf8(), sort, category);
|
searchSaves = std::make_unique<http::SearchSavesRequest>(start, count, query.ToUtf8(), period, sort, category);
|
||||||
searchSaves->Start();
|
searchSaves->Start();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -95,7 +96,7 @@ bool SearchModel::UpdateSaveList(int pageNumber, String query)
|
|||||||
//resultCount = 0;
|
//resultCount = 0;
|
||||||
currentPage = pageNumber;
|
currentPage = pageNumber;
|
||||||
|
|
||||||
if(pageNumber == 1 && !showOwn && !showFavourite && currentSort == http::sortByVotes && query == "")
|
if(pageNumber == 1 && !showOwn && !showFavourite && currentPeriod == http::allSaves && currentSort == http::sortByVotes && query == "")
|
||||||
SetShowTags(true);
|
SetShowTags(true);
|
||||||
else
|
else
|
||||||
SetShowTags(false);
|
SetShowTags(false);
|
||||||
@@ -120,7 +121,7 @@ bool SearchModel::UpdateSaveList(int pageNumber, String query)
|
|||||||
{
|
{
|
||||||
category = http::categoryMyOwn;
|
category = http::categoryMyOwn;
|
||||||
}
|
}
|
||||||
BeginSearchSaves((currentPage-1)*20, 20, lastQuery, currentSort, category);
|
BeginSearchSaves((currentPage-1)*20, 20, lastQuery, currentPeriod, currentSort, category);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
@@ -178,6 +179,7 @@ void SearchModel::AddObserver(SearchView * observer)
|
|||||||
observers.push_back(observer);
|
observers.push_back(observer);
|
||||||
observer->NotifySaveListChanged(this);
|
observer->NotifySaveListChanged(this);
|
||||||
observer->NotifyPageChanged(this);
|
observer->NotifyPageChanged(this);
|
||||||
|
observer->NotifyPeriodChanged(this);
|
||||||
observer->NotifySortChanged(this);
|
observer->NotifySortChanged(this);
|
||||||
observer->NotifyShowOwnChanged(this);
|
observer->NotifyShowOwnChanged(this);
|
||||||
observer->NotifyTagListChanged(this);
|
observer->NotifyTagListChanged(this);
|
||||||
@@ -258,6 +260,15 @@ void SearchModel::notifyPageChanged()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void SearchModel::notifyPeriodChanged()
|
||||||
|
{
|
||||||
|
for (size_t i = 0; i < observers.size(); i++)
|
||||||
|
{
|
||||||
|
SearchView* cObserver = observers[i];
|
||||||
|
cObserver->NotifyPeriodChanged(this);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void SearchModel::notifySortChanged()
|
void SearchModel::notifySortChanged()
|
||||||
{
|
{
|
||||||
for (size_t i = 0; i < observers.size(); i++)
|
for (size_t i = 0; i < observers.size(); i++)
|
||||||
@@ -296,7 +307,7 @@ void SearchModel::notifySelectedChanged()
|
|||||||
|
|
||||||
int SearchModel::GetPageCount()
|
int SearchModel::GetPageCount()
|
||||||
{
|
{
|
||||||
if (!showOwn && !showFavourite && currentSort == http::sortByVotes && lastQuery == "")
|
if (!showOwn && !showFavourite && currentPeriod == http::allSaves && currentSort == http::sortByVotes && lastQuery == "")
|
||||||
return std::max(1, (int)(ceil(resultCount/20.0f))+1); //add one for front page (front page saves are repeated twice)
|
return std::max(1, (int)(ceil(resultCount/20.0f))+1); //add one for front page (front page saves are repeated twice)
|
||||||
else
|
else
|
||||||
return std::max(1, (int)(ceil(resultCount/20.0f)));
|
return std::max(1, (int)(ceil(resultCount/20.0f)));
|
||||||
|
@@ -18,7 +18,7 @@ class SearchModel
|
|||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
std::unique_ptr<http::SearchSavesRequest> searchSaves;
|
std::unique_ptr<http::SearchSavesRequest> searchSaves;
|
||||||
void BeginSearchSaves(int start, int count, String query, http::Sort sort, http::Category category);
|
void BeginSearchSaves(int start, int count, String query, http::Period period, http::Sort sort, http::Category category);
|
||||||
std::vector<std::unique_ptr<SaveInfo>> EndSearchSaves();
|
std::vector<std::unique_ptr<SaveInfo>> EndSearchSaves();
|
||||||
|
|
||||||
void BeginGetTags(int start, int count, String query);
|
void BeginGetTags(int start, int count, String query);
|
||||||
@@ -26,6 +26,7 @@ private:
|
|||||||
std::unique_ptr<http::SearchTagsRequest> getTags;
|
std::unique_ptr<http::SearchTagsRequest> getTags;
|
||||||
|
|
||||||
std::unique_ptr<SaveInfo> loadedSave;
|
std::unique_ptr<SaveInfo> loadedSave;
|
||||||
|
http::Period currentPeriod;
|
||||||
http::Sort currentSort;
|
http::Sort currentSort;
|
||||||
String lastQuery;
|
String lastQuery;
|
||||||
String lastError;
|
String lastError;
|
||||||
@@ -42,6 +43,7 @@ private:
|
|||||||
void notifyTagListChanged();
|
void notifyTagListChanged();
|
||||||
void notifySelectedChanged();
|
void notifySelectedChanged();
|
||||||
void notifyPageChanged();
|
void notifyPageChanged();
|
||||||
|
void notifyPeriodChanged();
|
||||||
void notifySortChanged();
|
void notifySortChanged();
|
||||||
void notifyShowOwnChanged();
|
void notifyShowOwnChanged();
|
||||||
void notifyShowFavouriteChanged();
|
void notifyShowFavouriteChanged();
|
||||||
@@ -61,6 +63,8 @@ public:
|
|||||||
int GetPageCount();
|
int GetPageCount();
|
||||||
int GetPageNum() { return currentPage; }
|
int GetPageNum() { return currentPage; }
|
||||||
String GetLastQuery() { return lastQuery; }
|
String GetLastQuery() { return lastQuery; }
|
||||||
|
void SetPeriod(http::Period period) { if(!searchSaves) { currentPeriod = period; } notifyPeriodChanged(); }
|
||||||
|
http::Period GetPeriod() { return currentPeriod; }
|
||||||
void SetSort(http::Sort sort) { if(!searchSaves) { currentSort = sort; } notifySortChanged(); }
|
void SetSort(http::Sort sort) { if(!searchSaves) { currentSort = sort; } notifySortChanged(); }
|
||||||
http::Sort GetSort() { return currentSort; }
|
http::Sort GetSort() { return currentSort; }
|
||||||
void SetShowOwn(bool show) { if(!searchSaves) { if(show!=showOwn) { showOwn = show; } } notifyShowOwnChanged(); }
|
void SetShowOwn(bool show) { if(!searchSaves) { if(show!=showOwn) { showOwn = show; } } notifyShowOwnChanged(); }
|
||||||
|
@@ -9,6 +9,7 @@
|
|||||||
#include "gui/interface/RichLabel.h"
|
#include "gui/interface/RichLabel.h"
|
||||||
#include "gui/interface/Textbox.h"
|
#include "gui/interface/Textbox.h"
|
||||||
#include "gui/interface/Spinner.h"
|
#include "gui/interface/Spinner.h"
|
||||||
|
#include "gui/interface/DropDown.h"
|
||||||
#include "PowderToySDL.h"
|
#include "PowderToySDL.h"
|
||||||
#include "graphics/Graphics.h"
|
#include "graphics/Graphics.h"
|
||||||
#include "SimulationConfig.h"
|
#include "SimulationConfig.h"
|
||||||
@@ -43,7 +44,7 @@ SearchView::SearchView():
|
|||||||
AddComponent(pageCountLabel);
|
AddComponent(pageCountLabel);
|
||||||
AddComponent(pageTextbox);
|
AddComponent(pageTextbox);
|
||||||
|
|
||||||
searchField = new ui::Textbox(ui::Point(60, 10), ui::Point(WINDOWW-238, 17), "", "[search]");
|
searchField = new ui::Textbox(ui::Point(60, 10), ui::Point(WINDOWW-283, 17), "", "[search]");
|
||||||
searchField->Appearance.icon = IconSearch;
|
searchField->Appearance.icon = IconSearch;
|
||||||
searchField->Appearance.HorizontalAlign = ui::Appearance::AlignLeft;
|
searchField->Appearance.HorizontalAlign = ui::Appearance::AlignLeft;
|
||||||
searchField->Appearance.VerticalAlign = ui::Appearance::AlignMiddle;
|
searchField->Appearance.VerticalAlign = ui::Appearance::AlignMiddle;
|
||||||
@@ -51,6 +52,17 @@ SearchView::SearchView():
|
|||||||
searchField->SetLimit(100);
|
searchField->SetLimit(100);
|
||||||
FocusComponent(searchField);
|
FocusComponent(searchField);
|
||||||
|
|
||||||
|
dateRange = new ui::DropDown(ui::Point(WINDOWW-185, 10), ui::Point(36, 17));
|
||||||
|
dateRange->SetActionCallback({ [this] { c->ChangePeriod(dateRange->GetOption().second); } });
|
||||||
|
dateRange->AddOption({"All", 0});
|
||||||
|
dateRange->AddOption({"Day", 1});
|
||||||
|
dateRange->AddOption({"Week", 2});
|
||||||
|
dateRange->AddOption({"Month", 3});
|
||||||
|
dateRange->AddOption({"Year", 4});
|
||||||
|
dateRange->Appearance.HorizontalAlign = ui::Appearance::AlignCentre;
|
||||||
|
dateRange->Appearance.VerticalAlign = ui::Appearance::AlignMiddle;
|
||||||
|
AddComponent(dateRange);
|
||||||
|
|
||||||
sortButton = new ui::Button(ui::Point(WINDOWW-140, 10), ui::Point(61, 17), "Sort");
|
sortButton = new ui::Button(ui::Point(WINDOWW-140, 10), ui::Point(61, 17), "Sort");
|
||||||
sortButton->SetIcon(IconVoteSort);
|
sortButton->SetIcon(IconVoteSort);
|
||||||
sortButton->SetTogglable(true);
|
sortButton->SetTogglable(true);
|
||||||
@@ -202,6 +214,11 @@ void SearchView::Search(String query)
|
|||||||
c->DoSearch(query, true);
|
c->DoSearch(query, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void SearchView::NotifyPeriodChanged(SearchModel * sender)
|
||||||
|
{
|
||||||
|
dateRange->SetOption(sender->GetPeriod());
|
||||||
|
}
|
||||||
|
|
||||||
void SearchView::NotifySortChanged(SearchModel * sender)
|
void SearchView::NotifySortChanged(SearchModel * sender)
|
||||||
{
|
{
|
||||||
if(sender->GetSort() == http::sortByVotes)
|
if(sender->GetSort() == http::sortByVotes)
|
||||||
|
@@ -11,6 +11,7 @@ namespace ui
|
|||||||
class Label;
|
class Label;
|
||||||
class Spinner;
|
class Spinner;
|
||||||
class Textbox;
|
class Textbox;
|
||||||
|
class DropDown;
|
||||||
}
|
}
|
||||||
|
|
||||||
class SearchModel;
|
class SearchModel;
|
||||||
@@ -32,6 +33,7 @@ private:
|
|||||||
ui::Label * pageCountLabel;
|
ui::Label * pageCountLabel;
|
||||||
ui::Label * tagsLabel;
|
ui::Label * tagsLabel;
|
||||||
ui::RichLabel * motdLabel = nullptr;
|
ui::RichLabel * motdLabel = nullptr;
|
||||||
|
ui::DropDown * dateRange;
|
||||||
ui::Button * sortButton;
|
ui::Button * sortButton;
|
||||||
ui::Button * ownButton;
|
ui::Button * ownButton;
|
||||||
ui::Spinner * loadingSpinner;
|
ui::Spinner * loadingSpinner;
|
||||||
@@ -52,6 +54,7 @@ public:
|
|||||||
void NotifySaveListChanged(SearchModel * sender);
|
void NotifySaveListChanged(SearchModel * sender);
|
||||||
void NotifySelectedChanged(SearchModel * sender);
|
void NotifySelectedChanged(SearchModel * sender);
|
||||||
void NotifyPageChanged(SearchModel * sender);
|
void NotifyPageChanged(SearchModel * sender);
|
||||||
|
void NotifyPeriodChanged(SearchModel * sender);
|
||||||
void NotifySortChanged(SearchModel * sender);
|
void NotifySortChanged(SearchModel * sender);
|
||||||
void NotifyShowOwnChanged(SearchModel * sender);
|
void NotifyShowOwnChanged(SearchModel * sender);
|
||||||
void NotifyShowFavouriteChanged(SearchModel * sender);
|
void NotifyShowFavouriteChanged(SearchModel * sender);
|
||||||
|
Reference in New Issue
Block a user