refactor(Mods): make provider() return a std::optional

This makes it easier to check if a mod has a provider or not, without
having to do a string comparison.

Signed-off-by: flow <flowlnlnln@gmail.com>
This commit is contained in:
flow 2022-12-29 12:39:20 -03:00
parent aa3633d2d7
commit 257970c27d
No known key found for this signature in database
GPG Key ID: 8D0F221F0A59F469
3 changed files with 18 additions and 10 deletions

View File

@ -93,10 +93,11 @@ std::pair<int, bool> Mod::compare(const Resource& other, SortType type) const
if (this_ver < other_ver)
return { -1, type == SortType::VERSION };
}
case SortType::PROVIDER:
auto compare_result = QString::compare(provider(), cast_other->provider(), Qt::CaseInsensitive);
case SortType::PROVIDER: {
auto compare_result = QString::compare(provider().value_or("Unknown"), cast_other->provider().value_or("Unknown"), Qt::CaseInsensitive);
if (compare_result != 0)
return { compare_result, type == SortType::PROVIDER };
}
}
return { 0, false };
}
@ -197,11 +198,9 @@ void Mod::finishResolvingWithDetails(ModDetails&& details)
setMetadata(std::move(metadata));
};
auto Mod::provider() const -> QString
auto Mod::provider() const -> std::optional<QString>
{
if (metadata()) {
if (metadata())
return ProviderCaps.readableName(metadata()->provider);
}
//: Unknown mod provider (i.e. not Modrinth, CurseForge, etc...)
return tr("Unknown");
return {};
}

View File

@ -39,6 +39,8 @@
#include <QFileInfo>
#include <QList>
#include <optional>
#include "Resource.h"
#include "ModDetails.h"
@ -61,7 +63,7 @@ public:
auto description() const -> QString;
auto authors() const -> QStringList;
auto status() const -> ModStatus;
auto provider() const -> QString;
auto provider() const -> std::optional<QString>;
auto metadata() -> std::shared_ptr<Metadata::ModStruct>;
auto metadata() const -> const std::shared_ptr<Metadata::ModStruct>;

View File

@ -83,8 +83,15 @@ QVariant ModFolderModel::data(const QModelIndex &index, int role) const
}
case DateColumn:
return m_resources[row]->dateTimeChanged();
case ProviderColumn:
return at(row)->provider();
case ProviderColumn: {
auto provider = at(row)->provider();
if (!provider.has_value()) {
//: Unknown mod provider (i.e. not Modrinth, CurseForge, etc...)
return tr("Unknown");
}
return provider.value();
}
default:
return QVariant();
}