mirror of
https://github.com/amnezia-vpn/amnezia-client.git
synced 2026-06-22 02:01:08 +07:00
chore: simplify benefits
This commit is contained in:
@@ -434,8 +434,7 @@ ApiConfigsController::ApiConfigsController(const QSharedPointer<ServersModel> &s
|
|||||||
connect(m_apiServicesModel.data(), &ApiServicesModel::serviceSelectionChanged, this, [this]() {
|
connect(m_apiServicesModel.data(), &ApiServicesModel::serviceSelectionChanged, this, [this]() {
|
||||||
const ApiServicesModel::ApiServicesData serviceData = m_apiServicesModel->selectedServiceData();
|
const ApiServicesModel::ApiServicesData serviceData = m_apiServicesModel->selectedServiceData();
|
||||||
m_subscriptionPlansModel->updateModel(serviceData.subscriptionPlansJson);
|
m_subscriptionPlansModel->updateModel(serviceData.subscriptionPlansJson);
|
||||||
m_benefitsModel->updateModel(serviceData.benefits, serviceData.serviceInfo.region, serviceData.serviceInfo.speed,
|
m_benefitsModel->updateModel(serviceData.benefits);
|
||||||
serviceData.serviceInfo.price, serviceData.supportInfo);
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -5,8 +5,6 @@
|
|||||||
#include <QJsonObject>
|
#include <QJsonObject>
|
||||||
#include <QJsonValue>
|
#include <QJsonValue>
|
||||||
|
|
||||||
#include "core/api/apiDefs.h"
|
|
||||||
|
|
||||||
namespace
|
namespace
|
||||||
{
|
{
|
||||||
namespace configKey
|
namespace configKey
|
||||||
@@ -14,12 +12,7 @@ namespace configKey
|
|||||||
constexpr char title[] = "title";
|
constexpr char title[] = "title";
|
||||||
constexpr char body[] = "body";
|
constexpr char body[] = "body";
|
||||||
constexpr char icon[] = "icon";
|
constexpr char icon[] = "icon";
|
||||||
constexpr char injectKey[] = "inject_key";
|
|
||||||
constexpr char accent[] = "accent";
|
constexpr char accent[] = "accent";
|
||||||
|
|
||||||
constexpr char region[] = "region";
|
|
||||||
constexpr char speed[] = "speed";
|
|
||||||
constexpr char price[] = "price";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
QString gatewayIconKeyToUrl(const QString &iconKey)
|
QString gatewayIconKeyToUrl(const QString &iconKey)
|
||||||
@@ -86,8 +79,7 @@ QHash<int, QByteArray> ApiBenefitsModel::roleNames() const
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
void ApiBenefitsModel::updateModel(const QJsonArray &benefits, const QString ®ion, const QString &speed,
|
void ApiBenefitsModel::updateModel(const QJsonArray &benefits)
|
||||||
const QString &price, const QJsonObject &supportInfo)
|
|
||||||
{
|
{
|
||||||
beginResetModel();
|
beginResetModel();
|
||||||
m_serviceBenefits.clear();
|
m_serviceBenefits.clear();
|
||||||
@@ -99,14 +91,6 @@ void ApiBenefitsModel::updateModel(const QJsonArray &benefits, const QString &re
|
|||||||
QString title = benefitObject.value(configKey::title).toString();
|
QString title = benefitObject.value(configKey::title).toString();
|
||||||
QString body = benefitObject.value(configKey::body).toString();
|
QString body = benefitObject.value(configKey::body).toString();
|
||||||
const QString iconKey = benefitObject.value(configKey::icon).toString();
|
const QString iconKey = benefitObject.value(configKey::icon).toString();
|
||||||
const QString injectKey = benefitObject.value(configKey::injectKey).toString();
|
|
||||||
if (body.contains(QLatin1String("%1")) && !injectKey.isEmpty()) {
|
|
||||||
QString injected = benefitInjectValue(injectKey, region, speed, price, supportInfo);
|
|
||||||
if (injected.isEmpty()) {
|
|
||||||
injected = QStringLiteral("—");
|
|
||||||
}
|
|
||||||
body = body.arg(injected);
|
|
||||||
}
|
|
||||||
if (title.isEmpty() && body.isEmpty()) {
|
if (title.isEmpty() && body.isEmpty()) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@@ -126,41 +110,3 @@ void ApiBenefitsModel::clear()
|
|||||||
m_serviceBenefits.clear();
|
m_serviceBenefits.clear();
|
||||||
endResetModel();
|
endResetModel();
|
||||||
}
|
}
|
||||||
|
|
||||||
QString ApiBenefitsModel::formatPriceForBenefit(const QString &rawPrice) const
|
|
||||||
{
|
|
||||||
if (rawPrice == QStringLiteral("free")) {
|
|
||||||
return tr("Free");
|
|
||||||
}
|
|
||||||
#if defined(Q_OS_IOS) || defined(MACOS_NE)
|
|
||||||
return tr("%1 $").arg(rawPrice);
|
|
||||||
#else
|
|
||||||
return tr("%1 $/month").arg(rawPrice);
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
QString ApiBenefitsModel::benefitInjectValue(const QString &injectKey, const QString ®ion, const QString &speed,
|
|
||||||
const QString &price, const QJsonObject &supportInfo) const
|
|
||||||
{
|
|
||||||
if (injectKey == QLatin1String(configKey::region)) {
|
|
||||||
return region.isEmpty() ? QStringLiteral("—") : region;
|
|
||||||
}
|
|
||||||
if (injectKey == QLatin1String(configKey::speed)) {
|
|
||||||
return speed.isEmpty() ? QStringLiteral("—") : speed;
|
|
||||||
}
|
|
||||||
if (injectKey == QLatin1String(configKey::price)) {
|
|
||||||
return formatPriceForBenefit(price);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (injectKey == apiDefs::key::telegram) {
|
|
||||||
const QString handle = supportInfo.value(apiDefs::key::telegram).toString().trimmed();
|
|
||||||
if (handle.isEmpty()) {
|
|
||||||
return QStringLiteral("—");
|
|
||||||
}
|
|
||||||
if (handle.startsWith(QLatin1Char('@'))) {
|
|
||||||
return handle;
|
|
||||||
}
|
|
||||||
return QLatin1Char('@') + handle;
|
|
||||||
}
|
|
||||||
return QString();
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -3,7 +3,6 @@
|
|||||||
|
|
||||||
#include <QAbstractListModel>
|
#include <QAbstractListModel>
|
||||||
#include <QJsonArray>
|
#include <QJsonArray>
|
||||||
#include <QJsonObject>
|
|
||||||
#include <QString>
|
#include <QString>
|
||||||
#include <QVector>
|
#include <QVector>
|
||||||
|
|
||||||
@@ -26,8 +25,7 @@ public:
|
|||||||
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
|
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
|
||||||
QHash<int, QByteArray> roleNames() const override;
|
QHash<int, QByteArray> roleNames() const override;
|
||||||
|
|
||||||
void updateModel(const QJsonArray &benefits, const QString ®ion, const QString &speed, const QString &price,
|
void updateModel(const QJsonArray &benefits);
|
||||||
const QJsonObject &supportInfo);
|
|
||||||
void clear();
|
void clear();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
@@ -40,10 +38,6 @@ private:
|
|||||||
};
|
};
|
||||||
|
|
||||||
QVector<ServiceBenefitItem> m_serviceBenefits;
|
QVector<ServiceBenefitItem> m_serviceBenefits;
|
||||||
|
|
||||||
QString formatPriceForBenefit(const QString &rawPrice) const;
|
|
||||||
QString benefitInjectValue(const QString &injectKey, const QString ®ion, const QString &speed, const QString &price,
|
|
||||||
const QJsonObject &supportInfo) const;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -21,14 +21,9 @@ namespace
|
|||||||
constexpr char serviceProtocol[] = "service_protocol";
|
constexpr char serviceProtocol[] = "service_protocol";
|
||||||
constexpr char serviceDescription[] = "service_description";
|
constexpr char serviceDescription[] = "service_description";
|
||||||
|
|
||||||
constexpr char name[] = "name";
|
|
||||||
constexpr char price[] = "price";
|
|
||||||
constexpr char speed[] = "speed";
|
|
||||||
constexpr char timelimit[] = "timelimit";
|
|
||||||
constexpr char region[] = "region";
|
|
||||||
|
|
||||||
constexpr char description[] = "description";
|
constexpr char description[] = "description";
|
||||||
constexpr char cardDescription[] = "card_description";
|
constexpr char cardDescription[] = "card_description";
|
||||||
|
constexpr char serviceName[] = "service_name";
|
||||||
|
|
||||||
constexpr char availableCountries[] = "available_countries";
|
constexpr char availableCountries[] = "available_countries";
|
||||||
|
|
||||||
@@ -74,9 +69,8 @@ QVariant ApiServicesModel::data(const QModelIndex &index, int role) const
|
|||||||
return apiServiceData.serviceInfo.name;
|
return apiServiceData.serviceInfo.name;
|
||||||
}
|
}
|
||||||
case CardDescriptionRole: {
|
case CardDescriptionRole: {
|
||||||
auto speed = apiServiceData.serviceInfo.speed;
|
|
||||||
if (serviceType == serviceType::amneziaPremium) {
|
if (serviceType == serviceType::amneziaPremium) {
|
||||||
return apiServiceData.serviceInfo.cardDescription.arg(speed);
|
return apiServiceData.serviceInfo.cardDescription;
|
||||||
} else if (serviceType == serviceType::amneziaFree) {
|
} else if (serviceType == serviceType::amneziaFree) {
|
||||||
QString description = apiServiceData.serviceInfo.cardDescription;
|
QString description = apiServiceData.serviceInfo.cardDescription;
|
||||||
if (!isServiceAvailable) {
|
if (!isServiceAvailable) {
|
||||||
@@ -97,32 +91,8 @@ QVariant ApiServicesModel::data(const QModelIndex &index, int role) const
|
|||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
case SpeedRole: {
|
|
||||||
return tr("%1 MBit/s").arg(apiServiceData.serviceInfo.speed);
|
|
||||||
}
|
|
||||||
case TimeLimitRole: {
|
|
||||||
auto timeLimit = apiServiceData.serviceInfo.timeLimit;
|
|
||||||
if (timeLimit == "0") {
|
|
||||||
return "";
|
|
||||||
}
|
|
||||||
return tr("%1 days").arg(timeLimit);
|
|
||||||
}
|
|
||||||
case RegionRole: {
|
|
||||||
return apiServiceData.serviceInfo.region;
|
|
||||||
}
|
|
||||||
case PriceRole: {
|
case PriceRole: {
|
||||||
if (serviceType == serviceType::amneziaPremium && !apiServiceData.minPriceLabel.isEmpty()) {
|
return apiServiceData.minPriceLabel;
|
||||||
return apiServiceData.minPriceLabel;
|
|
||||||
}
|
|
||||||
auto price = apiServiceData.serviceInfo.price;
|
|
||||||
if (price == "free") {
|
|
||||||
return tr("Free");
|
|
||||||
}
|
|
||||||
#if defined(Q_OS_IOS) || defined(MACOS_NE)
|
|
||||||
return tr("%1 $").arg(price);
|
|
||||||
#else
|
|
||||||
return tr("%1 $/month").arg(price);
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
case EndDateRole: {
|
case EndDateRole: {
|
||||||
return QDateTime::fromString(apiServiceData.subscription.endDate, Qt::ISODate).toLocalTime().toString("d MMM yyyy");
|
return QDateTime::fromString(apiServiceData.subscription.endDate, Qt::ISODate).toLocalTime().toString("d MMM yyyy");
|
||||||
@@ -263,9 +233,6 @@ QHash<int, QByteArray> ApiServicesModel::roleNames() const
|
|||||||
roles[CardDescriptionRole] = "cardDescription";
|
roles[CardDescriptionRole] = "cardDescription";
|
||||||
roles[ServiceDescriptionRole] = "serviceDescription";
|
roles[ServiceDescriptionRole] = "serviceDescription";
|
||||||
roles[IsServiceAvailableRole] = "isServiceAvailable";
|
roles[IsServiceAvailableRole] = "isServiceAvailable";
|
||||||
roles[SpeedRole] = "speed";
|
|
||||||
roles[TimeLimitRole] = "timeLimit";
|
|
||||||
roles[RegionRole] = "region";
|
|
||||||
roles[PriceRole] = "price";
|
roles[PriceRole] = "price";
|
||||||
roles[EndDateRole] = "endDate";
|
roles[EndDateRole] = "endDate";
|
||||||
roles[TermsOfUseUrlRole] = "termsOfUseUrl";
|
roles[TermsOfUseUrlRole] = "termsOfUseUrl";
|
||||||
@@ -287,11 +254,7 @@ ApiServicesModel::ApiServicesData ApiServicesModel::getApiServicesData(const QJs
|
|||||||
auto subscriptionObject = data.value(apiDefs::key::subscription).toObject();
|
auto subscriptionObject = data.value(apiDefs::key::subscription).toObject();
|
||||||
|
|
||||||
ApiServicesData serviceData;
|
ApiServicesData serviceData;
|
||||||
serviceData.serviceInfo.name = serviceInfo.value(configKey::name).toString();
|
serviceData.serviceInfo.name = serviceDescription.value(configKey::serviceName).toString();
|
||||||
serviceData.serviceInfo.price = serviceInfo.value(configKey::price).toString();
|
|
||||||
serviceData.serviceInfo.region = serviceInfo.value(configKey::region).toString();
|
|
||||||
serviceData.serviceInfo.speed = serviceInfo.value(configKey::speed).toString();
|
|
||||||
serviceData.serviceInfo.timeLimit = serviceInfo.value(configKey::timelimit).toString();
|
|
||||||
|
|
||||||
serviceData.serviceInfo.cardDescription = serviceDescription.value(configKey::cardDescription).toString();
|
serviceData.serviceInfo.cardDescription = serviceDescription.value(configKey::cardDescription).toString();
|
||||||
serviceData.serviceInfo.description = serviceDescription.value(configKey::description).toString();
|
serviceData.serviceInfo.description = serviceDescription.value(configKey::description).toString();
|
||||||
@@ -301,9 +264,7 @@ ApiServicesModel::ApiServicesData ApiServicesModel::getApiServicesData(const QJs
|
|||||||
serviceData.subscriptionPlansJson = serviceDescription.value(configKey::subscriptionPlans).toArray();
|
serviceData.subscriptionPlansJson = serviceDescription.value(configKey::subscriptionPlans).toArray();
|
||||||
serviceData.benefits = serviceDescription.value(configKey::benefits).toArray();
|
serviceData.benefits = serviceDescription.value(configKey::benefits).toArray();
|
||||||
|
|
||||||
if (serviceType == serviceType::amneziaPremium) {
|
serviceData.minPriceLabel = serviceDescription.value(configKey::minPriceLabel).toString().trimmed();
|
||||||
serviceData.minPriceLabel = serviceDescription.value(configKey::minPriceLabel).toString().trimmed();
|
|
||||||
}
|
|
||||||
|
|
||||||
serviceData.supportInfo = data.value(apiDefs::key::supportInfo).toObject();
|
serviceData.supportInfo = data.value(apiDefs::key::supportInfo).toObject();
|
||||||
|
|
||||||
|
|||||||
@@ -14,10 +14,6 @@ public:
|
|||||||
struct ServiceInfo
|
struct ServiceInfo
|
||||||
{
|
{
|
||||||
QString name;
|
QString name;
|
||||||
QString speed;
|
|
||||||
QString timeLimit;
|
|
||||||
QString region;
|
|
||||||
QString price;
|
|
||||||
|
|
||||||
QString description;
|
QString description;
|
||||||
QString cardDescription;
|
QString cardDescription;
|
||||||
@@ -58,9 +54,6 @@ public:
|
|||||||
CardDescriptionRole,
|
CardDescriptionRole,
|
||||||
ServiceDescriptionRole,
|
ServiceDescriptionRole,
|
||||||
IsServiceAvailableRole,
|
IsServiceAvailableRole,
|
||||||
SpeedRole,
|
|
||||||
TimeLimitRole,
|
|
||||||
RegionRole,
|
|
||||||
PriceRole,
|
PriceRole,
|
||||||
EndDateRole,
|
EndDateRole,
|
||||||
TermsOfUseUrlRole,
|
TermsOfUseUrlRole,
|
||||||
|
|||||||
Reference in New Issue
Block a user