feat+fix: cache versions and extra info in Modrinth packs
When you change a copy thinking you're changing the original data smh Signed-off-by: flow <flowlnlnln@gmail.com>
This commit is contained in:
parent
4bfc445cf8
commit
0ec4ade683
@ -104,6 +104,17 @@ auto ModpackListModel::data(const QModelIndex& index, int role) const -> QVarian
|
|||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool ModpackListModel::setData(const QModelIndex &index, const QVariant &value, int role)
|
||||||
|
{
|
||||||
|
int pos = index.row();
|
||||||
|
if (pos >= modpacks.size() || pos < 0 || !index.isValid())
|
||||||
|
return false;
|
||||||
|
|
||||||
|
modpacks[pos] = value.value<Modrinth::Modpack>();
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
void ModpackListModel::performPaginatedSearch()
|
void ModpackListModel::performPaginatedSearch()
|
||||||
{
|
{
|
||||||
// TODO: Move to standalone API
|
// TODO: Move to standalone API
|
||||||
|
@ -64,6 +64,7 @@ class ModpackListModel : public QAbstractListModel {
|
|||||||
|
|
||||||
/* Retrieve information from the model at a given index with the given role */
|
/* Retrieve information from the model at a given index with the given role */
|
||||||
auto data(const QModelIndex& index, int role) const -> QVariant override;
|
auto data(const QModelIndex& index, int role) const -> QVariant override;
|
||||||
|
bool setData(const QModelIndex &index, const QVariant &value, int role) override;
|
||||||
|
|
||||||
inline void setActiveJob(NetJob::Ptr ptr) { jobPtr = ptr; }
|
inline void setActiveJob(NetJob::Ptr ptr) { jobPtr = ptr; }
|
||||||
|
|
||||||
|
@ -101,18 +101,18 @@ bool ModrinthPage::eventFilter(QObject* watched, QEvent* event)
|
|||||||
return QObject::eventFilter(watched, event);
|
return QObject::eventFilter(watched, event);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ModrinthPage::onSelectionChanged(QModelIndex first, QModelIndex second)
|
void ModrinthPage::onSelectionChanged(QModelIndex curr, QModelIndex prev)
|
||||||
{
|
{
|
||||||
ui->versionSelectionBox->clear();
|
ui->versionSelectionBox->clear();
|
||||||
|
|
||||||
if (!first.isValid()) {
|
if (!curr.isValid()) {
|
||||||
if (isOpened) {
|
if (isOpened) {
|
||||||
dialog->setSuggestedPack();
|
dialog->setSuggestedPack();
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
current = m_model->data(first, Qt::UserRole).value<Modrinth::Modpack>();
|
current = m_model->data(curr, Qt::UserRole).value<Modrinth::Modpack>();
|
||||||
auto name = current.name;
|
auto name = current.name;
|
||||||
|
|
||||||
if (!current.extraInfoLoaded) {
|
if (!current.extraInfoLoaded) {
|
||||||
@ -125,7 +125,7 @@ void ModrinthPage::onSelectionChanged(QModelIndex first, QModelIndex second)
|
|||||||
|
|
||||||
netJob->addNetAction(Net::Download::makeByteArray(QString("%1/project/%2").arg(BuildConfig.MODRINTH_PROD_URL, id), response));
|
netJob->addNetAction(Net::Download::makeByteArray(QString("%1/project/%2").arg(BuildConfig.MODRINTH_PROD_URL, id), response));
|
||||||
|
|
||||||
QObject::connect(netJob, &NetJob::succeeded, this, [this, response, id] {
|
QObject::connect(netJob, &NetJob::succeeded, this, [this, response, id, curr] {
|
||||||
if (id != current.id) {
|
if (id != current.id) {
|
||||||
return; // wrong request?
|
return; // wrong request?
|
||||||
}
|
}
|
||||||
@ -149,6 +149,13 @@ void ModrinthPage::onSelectionChanged(QModelIndex first, QModelIndex second)
|
|||||||
}
|
}
|
||||||
|
|
||||||
updateUI();
|
updateUI();
|
||||||
|
|
||||||
|
QVariant current_updated;
|
||||||
|
current_updated.setValue(current);
|
||||||
|
|
||||||
|
if (!m_model->setData(curr, current_updated, Qt::UserRole))
|
||||||
|
qWarning() << "Failed to cache extra info for the current pack!";
|
||||||
|
|
||||||
suggestCurrent();
|
suggestCurrent();
|
||||||
});
|
});
|
||||||
QObject::connect(netJob, &NetJob::finished, this, [response, netJob] {
|
QObject::connect(netJob, &NetJob::finished, this, [response, netJob] {
|
||||||
@ -170,7 +177,7 @@ void ModrinthPage::onSelectionChanged(QModelIndex first, QModelIndex second)
|
|||||||
netJob->addNetAction(
|
netJob->addNetAction(
|
||||||
Net::Download::makeByteArray(QString("%1/project/%2/version").arg(BuildConfig.MODRINTH_PROD_URL, id), response));
|
Net::Download::makeByteArray(QString("%1/project/%2/version").arg(BuildConfig.MODRINTH_PROD_URL, id), response));
|
||||||
|
|
||||||
QObject::connect(netJob, &NetJob::succeeded, this, [this, response, id] {
|
QObject::connect(netJob, &NetJob::succeeded, this, [this, response, id, curr] {
|
||||||
if (id != current.id) {
|
if (id != current.id) {
|
||||||
return; // wrong request?
|
return; // wrong request?
|
||||||
}
|
}
|
||||||
@ -195,6 +202,12 @@ void ModrinthPage::onSelectionChanged(QModelIndex first, QModelIndex second)
|
|||||||
ui->versionSelectionBox->addItem(version.version, QVariant(version.id));
|
ui->versionSelectionBox->addItem(version.version, QVariant(version.id));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
QVariant current_updated;
|
||||||
|
current_updated.setValue(current);
|
||||||
|
|
||||||
|
if (!m_model->setData(curr, current_updated, Qt::UserRole))
|
||||||
|
qWarning() << "Failed to cache versions for the current pack!";
|
||||||
|
|
||||||
suggestCurrent();
|
suggestCurrent();
|
||||||
});
|
});
|
||||||
QObject::connect(netJob, &NetJob::finished, this, [response, netJob] {
|
QObject::connect(netJob, &NetJob::finished, this, [response, netJob] {
|
||||||
@ -224,7 +237,7 @@ void ModrinthPage::updateUI()
|
|||||||
// TODO: Implement multiple authors with links
|
// TODO: Implement multiple authors with links
|
||||||
text += "<br>" + tr(" by ") + QString("<a href=%1>%2</a>").arg(std::get<1>(current.author).toString(), std::get<0>(current.author));
|
text += "<br>" + tr(" by ") + QString("<a href=%1>%2</a>").arg(std::get<1>(current.author).toString(), std::get<0>(current.author));
|
||||||
|
|
||||||
if(current.extraInfoLoaded) {
|
if (current.extraInfoLoaded) {
|
||||||
if (!current.extra.donate.isEmpty()) {
|
if (!current.extra.donate.isEmpty()) {
|
||||||
text += "<br><br>" + tr("Donate information: ");
|
text += "<br><br>" + tr("Donate information: ");
|
||||||
auto donateToStr = [](Modrinth::DonationData& donate) -> QString {
|
auto donateToStr = [](Modrinth::DonationData& donate) -> QString {
|
||||||
|
Loading…
Reference in New Issue
Block a user