fix: pack sorting and other search parameters

This commit is contained in:
flow 2022-05-15 20:45:27 -03:00
parent 7194bb1b81
commit 66ce5a4a2d
No known key found for this signature in database
GPG Key ID: 8D0F221F0A59F469
3 changed files with 23 additions and 12 deletions

View File

@ -105,11 +105,16 @@ void ModpackListModel::performPaginatedSearch()
{ {
// TODO: Move to standalone API // TODO: Move to standalone API
NetJob* netJob = new NetJob("Modrinth::SearchModpack", APPLICATION->network()); NetJob* netJob = new NetJob("Modrinth::SearchModpack", APPLICATION->network());
auto searchAllUrl = QString( auto searchAllUrl = QString(BuildConfig.MODRINTH_STAGING_URL +
"%1/search?" "/search?"
"offset=%1&"
"limit=20&"
"query=%2&" "query=%2&"
"index=%3&"
"facets=[[\"project_type:modpack\"]]") "facets=[[\"project_type:modpack\"]]")
.arg(BuildConfig.MODRINTH_STAGING_URL, currentSearchTerm); .arg(nextSearchOffset)
.arg(currentSearchTerm)
.arg(currentSort);
netJob->addNetAction(Net::Download::makeByteArray(QUrl(searchAllUrl), &m_all_response)); netJob->addNetAction(Net::Download::makeByteArray(QUrl(searchAllUrl), &m_all_response));
@ -148,14 +153,21 @@ void ModpackListModel::refresh()
performPaginatedSearch(); performPaginatedSearch();
} }
static std::array<QString, 5> sorts {"relevance", "downloads", "follows", "newest", "updated"};
void ModpackListModel::searchWithTerm(const QString& term, const int sort) void ModpackListModel::searchWithTerm(const QString& term, const int sort)
{ {
if (currentSearchTerm == term && currentSearchTerm.isNull() == term.isNull() && currentSort == sort) { if(sort > 5 || sort < 0)
return;
auto sort_str = sorts.at(sort);
if (currentSearchTerm == term && currentSearchTerm.isNull() == term.isNull() && currentSort == sort_str) {
return; return;
} }
currentSearchTerm = term; currentSearchTerm = term;
currentSort = sort; currentSort = sort_str;
refresh(); refresh();
} }
@ -255,7 +267,7 @@ void ModpackListModel::searchRequestFailed(QString reason)
{ {
if (!jobPtr->first()->m_reply) { if (!jobPtr->first()->m_reply) {
// Network error // Network error
QMessageBox::critical(nullptr, tr("Error"), tr("A network error occurred. Could not load mods.")); QMessageBox::critical(nullptr, tr("Error"), tr("A network error occurred. Could not load modpacks."));
} else if (jobPtr->first()->m_reply && jobPtr->first()->m_reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt() == 409) { } else if (jobPtr->first()->m_reply && jobPtr->first()->m_reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt() == 409) {
// 409 Gone, notify user to update // 409 Gone, notify user to update
QMessageBox::critical(nullptr, tr("Error"), QMessageBox::critical(nullptr, tr("Error"),

View File

@ -104,7 +104,7 @@ class ModpackListModel : public QAbstractListModel {
QStringList m_loadingLogos; QStringList m_loadingLogos;
QString currentSearchTerm; QString currentSearchTerm;
int currentSort = 0; QString currentSort;
int nextSearchOffset = 0; int nextSearchOffset = 0;
enum SearchState { None, CanPossiblyFetchMore, ResetRequested, Finished } searchState = None; enum SearchState { None, CanPossiblyFetchMore, ResetRequested, Finished } searchState = None;

View File

@ -61,12 +61,11 @@ ModrinthPage::ModrinthPage(NewInstanceDialog* dialog, QWidget* parent) : QWidget
ui->versionSelectionBox->view()->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded); ui->versionSelectionBox->view()->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
ui->versionSelectionBox->view()->parentWidget()->setMaximumHeight(300); ui->versionSelectionBox->view()->parentWidget()->setMaximumHeight(300);
ui->sortByBox->addItem(tr("Sort by Featured")); ui->sortByBox->addItem(tr("Sort by Relevance"));
ui->sortByBox->addItem(tr("Sort by Popularity"));
ui->sortByBox->addItem(tr("Sort by Last Updated"));
ui->sortByBox->addItem(tr("Sort by Name"));
ui->sortByBox->addItem(tr("Sort by Author"));
ui->sortByBox->addItem(tr("Sort by Total Downloads")); ui->sortByBox->addItem(tr("Sort by Total Downloads"));
ui->sortByBox->addItem(tr("Sort by Follows"));
ui->sortByBox->addItem(tr("Sort by Newest"));
ui->sortByBox->addItem(tr("Sort by Last Updated"));
connect(ui->sortByBox, SIGNAL(currentIndexChanged(int)), this, SLOT(triggerSearch())); connect(ui->sortByBox, SIGNAL(currentIndexChanged(int)), this, SLOT(triggerSearch()));
connect(ui->packView->selectionModel(), &QItemSelectionModel::currentChanged, this, &ModrinthPage::onSelectionChanged); connect(ui->packView->selectionModel(), &QItemSelectionModel::currentChanged, this, &ModrinthPage::onSelectionChanged);