mirror of
https://github.com/amnezia-vpn/amnezia-client.git
synced 2026-06-22 02:01:08 +07:00
feat: new services description (#2412)
* feat: iap for apple now use storekit2 * fix: fixed error 101 on connection event * feat: enhance StoreKit2Helper to handle entitlements and improve restore service from App Store functionality * chore: add isInAppPurchase and isTestPurchase in primary config * refactor: use end_date from primary config for renew ui * fix: hide renew button for free * fix: hide renew button for appstore purchases * feat: add new premium info page * feat: add new free info page * chore: minor fixes * refactor: move plan and benefits into separate models * fix: fixed expired status when configs without an end date * feat: add trial api support * chore: add api message parsing for 422 error * feat: move privacy policy and term of use to gateway * feat: add iap support for new premium info page * chore: minor fixes * chore: minor fix * chore: minor fixes * feat: additional parsing for storekit subscription plans * chore: minor codestyle fixes * chore: simplify benefits * chore: hide extend buttons on external premium * feat: add trial error processing * fix: remove wrong check from tiral handler * chore: cleanup --------- Co-authored-by: spectrum <yyy@amnezia.org>
This commit is contained in:
@@ -57,6 +57,11 @@ QVariant ApiAccountInfoModel::data(const QModelIndex &index, int role) const
|
||||
|| m_accountInfoData.configType == apiDefs::ConfigType::ExternalPremium
|
||||
|| m_accountInfoData.configType == apiDefs::ConfigType::ExternalTrial;
|
||||
}
|
||||
case IsSubscriptionRenewalAvailableRole: {
|
||||
return m_accountInfoData.configType == apiDefs::ConfigType::AmneziaPremiumV2
|
||||
|| m_accountInfoData.configType == apiDefs::ConfigType::AmneziaTrialV2
|
||||
|| m_accountInfoData.configType == apiDefs::ConfigType::ExternalTrial;
|
||||
}
|
||||
case HasExpiredWorkerRole: {
|
||||
for (int i = 0; i < m_issuedConfigsInfo.size(); i++) {
|
||||
QJsonObject issuedConfigObject = m_issuedConfigsInfo.at(i).toObject();
|
||||
@@ -77,16 +82,31 @@ QVariant ApiAccountInfoModel::data(const QModelIndex &index, int role) const
|
||||
return false;
|
||||
}
|
||||
case IsSubscriptionExpiredRole: {
|
||||
if (m_accountInfoData.configType == apiDefs::ConfigType::AmneziaFreeV3) return false;
|
||||
if (m_accountInfoData.subscriptionEndDate.isEmpty()) return false;
|
||||
if (m_accountInfoData.configType == apiDefs::ConfigType::AmneziaFreeV3) {
|
||||
return false;
|
||||
}
|
||||
if (m_accountInfoData.isInAppPurchase) {
|
||||
return false;
|
||||
}
|
||||
if (m_accountInfoData.subscriptionEndDate.isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
return apiUtils::isSubscriptionExpired(m_accountInfoData.subscriptionEndDate);
|
||||
}
|
||||
case IsSubscriptionExpiringSoonRole: {
|
||||
if (m_accountInfoData.configType == apiDefs::ConfigType::AmneziaFreeV3) return false;
|
||||
if (m_accountInfoData.subscriptionEndDate.isEmpty()) return false;
|
||||
if (apiUtils::isSubscriptionExpired(m_accountInfoData.subscriptionEndDate)) return false;
|
||||
QDateTime endDate = QDateTime::fromString(m_accountInfoData.subscriptionEndDate, Qt::ISODateWithMs);
|
||||
return endDate <= QDateTime::currentDateTimeUtc().addDays(10);
|
||||
if (m_accountInfoData.configType == apiDefs::ConfigType::AmneziaFreeV3) {
|
||||
return false;
|
||||
}
|
||||
if (m_accountInfoData.isInAppPurchase) {
|
||||
return false;
|
||||
}
|
||||
if (m_accountInfoData.subscriptionEndDate.isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
return apiUtils::isSubscriptionExpiringSoon(m_accountInfoData.subscriptionEndDate);
|
||||
}
|
||||
case IsInAppPurchaseRole: {
|
||||
return m_accountInfoData.isInAppPurchase;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -108,6 +128,9 @@ void ApiAccountInfoModel::updateModel(const QJsonObject &accountInfoObject, cons
|
||||
|
||||
accountInfoData.configType = apiUtils::getConfigType(serverConfig);
|
||||
|
||||
const QJsonObject apiConfig = serverConfig.value(apiDefs::key::apiConfig).toObject();
|
||||
accountInfoData.isInAppPurchase = apiConfig.value(apiDefs::key::isInAppPurchase).toBool(false);
|
||||
|
||||
accountInfoData.subscriptionDescription = accountInfoObject.value(apiDefs::key::subscriptionDescription).toString();
|
||||
|
||||
for (const auto &protocol : accountInfoObject.value(apiDefs::key::supportedProtocols).toArray()) {
|
||||
@@ -177,10 +200,12 @@ QHash<int, QByteArray> ApiAccountInfoModel::roleNames() const
|
||||
roles[ConnectedDevicesRole] = "connectedDevices";
|
||||
roles[ServiceDescriptionRole] = "serviceDescription";
|
||||
roles[IsComponentVisibleRole] = "isComponentVisible";
|
||||
roles[IsSubscriptionRenewalAvailableRole] = "isSubscriptionRenewalAvailable";
|
||||
roles[HasExpiredWorkerRole] = "hasExpiredWorker";
|
||||
roles[IsProtocolSelectionSupportedRole] = "isProtocolSelectionSupported";
|
||||
roles[IsSubscriptionExpiredRole] = "isSubscriptionExpired";
|
||||
roles[IsSubscriptionExpiringSoonRole] = "isSubscriptionExpiringSoon";
|
||||
roles[IsInAppPurchaseRole] = "isInAppPurchase";
|
||||
|
||||
return roles;
|
||||
}
|
||||
|
||||
@@ -18,10 +18,12 @@ public:
|
||||
ServiceDescriptionRole,
|
||||
EndDateRole,
|
||||
IsComponentVisibleRole,
|
||||
IsSubscriptionRenewalAvailableRole,
|
||||
HasExpiredWorkerRole,
|
||||
IsProtocolSelectionSupportedRole,
|
||||
IsSubscriptionExpiredRole,
|
||||
IsSubscriptionExpiringSoonRole
|
||||
IsSubscriptionExpiringSoonRole,
|
||||
IsInAppPurchaseRole
|
||||
};
|
||||
|
||||
explicit ApiAccountInfoModel(QObject *parent = nullptr);
|
||||
@@ -57,6 +59,8 @@ private:
|
||||
QStringList supportedProtocols;
|
||||
|
||||
QString subscriptionDescription;
|
||||
|
||||
bool isInAppPurchase = false;
|
||||
};
|
||||
|
||||
AccountInfoData m_accountInfoData;
|
||||
|
||||
@@ -0,0 +1,112 @@
|
||||
#include "apiBenefitsModel.h"
|
||||
|
||||
#include <QHash>
|
||||
#include <utility>
|
||||
#include <QJsonObject>
|
||||
#include <QJsonValue>
|
||||
|
||||
namespace
|
||||
{
|
||||
namespace configKey
|
||||
{
|
||||
constexpr char title[] = "title";
|
||||
constexpr char body[] = "body";
|
||||
constexpr char icon[] = "icon";
|
||||
constexpr char accent[] = "accent";
|
||||
}
|
||||
|
||||
QString gatewayIconKeyToUrl(const QString &iconKey)
|
||||
{
|
||||
if (iconKey.startsWith(QLatin1String("qrc:"))) {
|
||||
return iconKey;
|
||||
}
|
||||
static const QHash<QString, QString> map = {
|
||||
{ QStringLiteral("globe-2"), QStringLiteral("qrc:/images/controls/globe-2.svg") },
|
||||
{ QStringLiteral("smartphone"), QStringLiteral("qrc:/images/controls/smartphone.svg") },
|
||||
{ QStringLiteral("gauge"), QStringLiteral("qrc:/images/controls/gauge.svg") },
|
||||
{ QStringLiteral("infinity"), QStringLiteral("qrc:/images/controls/infinity.svg") },
|
||||
{ QStringLiteral("tag"), QStringLiteral("qrc:/images/controls/tag.svg") },
|
||||
{ QStringLiteral("history"), QStringLiteral("qrc:/images/controls/history.svg") },
|
||||
{ QStringLiteral("info"), QStringLiteral("qrc:/images/controls/info.svg") },
|
||||
{ QStringLiteral("app"), QStringLiteral("qrc:/images/controls/app.svg") },
|
||||
{ QStringLiteral("download"), QStringLiteral("qrc:/images/controls/download.svg") },
|
||||
{ QStringLiteral("help-circle"), QStringLiteral("qrc:/images/controls/help-circle.svg") },
|
||||
};
|
||||
return map.value(iconKey, QStringLiteral("qrc:/images/controls/info.svg"));
|
||||
}
|
||||
}
|
||||
|
||||
ApiBenefitsModel::ApiBenefitsModel(QObject *parent)
|
||||
: QAbstractListModel(parent)
|
||||
{
|
||||
}
|
||||
|
||||
int ApiBenefitsModel::rowCount(const QModelIndex &parent) const
|
||||
{
|
||||
if (parent.isValid()) {
|
||||
return 0;
|
||||
}
|
||||
return m_serviceBenefits.size();
|
||||
}
|
||||
|
||||
QVariant ApiBenefitsModel::data(const QModelIndex &index, int role) const
|
||||
{
|
||||
if (!index.isValid() || index.row() < 0 || index.row() >= m_serviceBenefits.size()) {
|
||||
return {};
|
||||
}
|
||||
const ServiceBenefitItem &item = m_serviceBenefits.at(index.row());
|
||||
switch (role) {
|
||||
case IconRole:
|
||||
return item.icon;
|
||||
case TitleRole:
|
||||
return item.title;
|
||||
case BodyRole:
|
||||
return item.body;
|
||||
case AccentRole:
|
||||
return item.accent;
|
||||
default:
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
||||
QHash<int, QByteArray> ApiBenefitsModel::roleNames() const
|
||||
{
|
||||
return {
|
||||
{ IconRole, "icon" },
|
||||
{ TitleRole, "title" },
|
||||
{ BodyRole, "body" },
|
||||
{ AccentRole, "accent" },
|
||||
};
|
||||
}
|
||||
|
||||
void ApiBenefitsModel::updateModel(const QJsonArray &benefits)
|
||||
{
|
||||
beginResetModel();
|
||||
m_serviceBenefits.clear();
|
||||
for (const QJsonValue &benefitValue : benefits) {
|
||||
if (!benefitValue.isObject()) {
|
||||
continue;
|
||||
}
|
||||
const QJsonObject benefitObject = benefitValue.toObject();
|
||||
QString title = benefitObject.value(configKey::title).toString();
|
||||
QString body = benefitObject.value(configKey::body).toString();
|
||||
const QString iconKey = benefitObject.value(configKey::icon).toString();
|
||||
if (title.isEmpty() && body.isEmpty()) {
|
||||
continue;
|
||||
}
|
||||
ServiceBenefitItem item;
|
||||
item.icon = gatewayIconKeyToUrl(iconKey);
|
||||
item.title = std::move(title);
|
||||
item.body = std::move(body);
|
||||
item.accent = benefitObject.value(configKey::accent).toBool();
|
||||
m_serviceBenefits.append(std::move(item));
|
||||
}
|
||||
endResetModel();
|
||||
}
|
||||
|
||||
void ApiBenefitsModel::clear()
|
||||
{
|
||||
beginResetModel();
|
||||
m_serviceBenefits.clear();
|
||||
endResetModel();
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
#ifndef APIBENEFITSMODEL_H
|
||||
#define APIBENEFITSMODEL_H
|
||||
|
||||
#include <QAbstractListModel>
|
||||
#include <QJsonArray>
|
||||
#include <QString>
|
||||
#include <QVector>
|
||||
|
||||
class ApiBenefitsModel : public QAbstractListModel
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
enum Roles {
|
||||
IconRole = Qt::UserRole + 1,
|
||||
TitleRole,
|
||||
BodyRole,
|
||||
AccentRole
|
||||
};
|
||||
Q_ENUM(Roles)
|
||||
|
||||
explicit ApiBenefitsModel(QObject *parent = nullptr);
|
||||
|
||||
int rowCount(const QModelIndex &parent = QModelIndex()) const override;
|
||||
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
|
||||
QHash<int, QByteArray> roleNames() const override;
|
||||
|
||||
void updateModel(const QJsonArray &benefits);
|
||||
void clear();
|
||||
|
||||
private:
|
||||
struct ServiceBenefitItem
|
||||
{
|
||||
QString icon;
|
||||
QString title;
|
||||
QString body;
|
||||
bool accent = false;
|
||||
};
|
||||
|
||||
QVector<ServiceBenefitItem> m_serviceBenefits;
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -1,7 +1,11 @@
|
||||
#include "apiServicesModel.h"
|
||||
|
||||
#include <QDateTime>
|
||||
#include <QHash>
|
||||
#include <QJsonArray>
|
||||
#include <QJsonObject>
|
||||
|
||||
#include "core/api/apiDefs.h"
|
||||
#include "logger.h"
|
||||
|
||||
namespace
|
||||
@@ -17,15 +21,9 @@ namespace
|
||||
constexpr char serviceProtocol[] = "service_protocol";
|
||||
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 cardDescription[] = "card_description";
|
||||
constexpr char features[] = "features";
|
||||
constexpr char serviceName[] = "service_name";
|
||||
|
||||
constexpr char availableCountries[] = "available_countries";
|
||||
|
||||
@@ -33,19 +31,21 @@ namespace
|
||||
|
||||
constexpr char isAvailable[] = "is_available";
|
||||
|
||||
constexpr char subscription[] = "subscription";
|
||||
constexpr char endDate[] = "end_date";
|
||||
constexpr char subscriptionPlans[] = "subscription_plans";
|
||||
constexpr char minPriceLabel[] = "min_price_label";
|
||||
constexpr char benefits[] = "benefits";
|
||||
}
|
||||
|
||||
namespace serviceType
|
||||
{
|
||||
constexpr char amneziaFree[] = "amnezia-free";
|
||||
constexpr char amneziaPremium[] = "amnezia-premium";
|
||||
constexpr char amneziaTrial[] = "amnezia-trial";
|
||||
}
|
||||
}
|
||||
|
||||
ApiServicesModel::ApiServicesModel(QObject *parent) : QAbstractListModel(parent)
|
||||
ApiServicesModel::ApiServicesModel(QObject *parent)
|
||||
: QAbstractListModel(parent)
|
||||
, m_selectedServiceIndex(0)
|
||||
{
|
||||
}
|
||||
|
||||
@@ -69,9 +69,8 @@ QVariant ApiServicesModel::data(const QModelIndex &index, int role) const
|
||||
return apiServiceData.serviceInfo.name;
|
||||
}
|
||||
case CardDescriptionRole: {
|
||||
auto speed = apiServiceData.serviceInfo.speed;
|
||||
if (serviceType == serviceType::amneziaPremium || serviceType == serviceType::amneziaTrial) {
|
||||
return apiServiceData.serviceInfo.cardDescription.arg(speed);
|
||||
if (serviceType == serviceType::amneziaPremium) {
|
||||
return apiServiceData.serviceInfo.cardDescription;
|
||||
} else if (serviceType == serviceType::amneziaFree) {
|
||||
QString description = apiServiceData.serviceInfo.cardDescription;
|
||||
if (!isServiceAvailable) {
|
||||
@@ -92,44 +91,29 @@ QVariant ApiServicesModel::data(const QModelIndex &index, int role) const
|
||||
}
|
||||
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 FeaturesRole: {
|
||||
return apiServiceData.serviceInfo.features;
|
||||
}
|
||||
case PriceRole: {
|
||||
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
|
||||
return apiServiceData.minPriceLabel;
|
||||
}
|
||||
case EndDateRole: {
|
||||
return QDateTime::fromString(apiServiceData.subscription.endDate, Qt::ISODate).toLocalTime().toString("d MMM yyyy");
|
||||
}
|
||||
case TermsOfUseUrlRole: {
|
||||
return apiServiceData.serviceInfo.termsOfUseUrl;
|
||||
}
|
||||
case PrivacyPolicyUrlRole: {
|
||||
return apiServiceData.serviceInfo.privacyPolicyUrl;
|
||||
}
|
||||
case ShowRecommendedRole: {
|
||||
return serviceType == serviceType::amneziaPremium;
|
||||
}
|
||||
case OrderRole: {
|
||||
if (serviceType == serviceType::amneziaPremium) {
|
||||
return 0;
|
||||
} else if (serviceType == serviceType::amneziaTrial) {
|
||||
return 1;
|
||||
} else if (serviceType == serviceType::amneziaFree) {
|
||||
return 2;
|
||||
}
|
||||
if (serviceType == serviceType::amneziaFree) {
|
||||
return 1;
|
||||
}
|
||||
return QVariant();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -155,12 +139,27 @@ void ApiServicesModel::updateModel(const QJsonObject &data)
|
||||
}
|
||||
}
|
||||
|
||||
if (!m_services.isEmpty() && m_selectedServiceIndex >= m_services.size()) {
|
||||
m_selectedServiceIndex = 0;
|
||||
}
|
||||
|
||||
endResetModel();
|
||||
|
||||
emit serviceSelectionChanged();
|
||||
}
|
||||
|
||||
void ApiServicesModel::setServiceIndex(const int index)
|
||||
{
|
||||
m_selectedServiceIndex = index;
|
||||
emit serviceSelectionChanged();
|
||||
}
|
||||
|
||||
ApiServicesModel::ApiServicesData ApiServicesModel::selectedServiceData() const
|
||||
{
|
||||
if (m_services.isEmpty() || m_selectedServiceIndex < 0 || m_selectedServiceIndex >= m_services.size()) {
|
||||
return {};
|
||||
}
|
||||
return m_services.at(m_selectedServiceIndex);
|
||||
}
|
||||
|
||||
QJsonObject ApiServicesModel::getSelectedServiceInfo()
|
||||
@@ -217,6 +216,16 @@ QVariant ApiServicesModel::getSelectedServiceData(const QString roleString)
|
||||
return {};
|
||||
}
|
||||
|
||||
int ApiServicesModel::serviceIndexForType(const QString &type) const
|
||||
{
|
||||
for (int serviceIndex = 0; serviceIndex < m_services.size(); ++serviceIndex) {
|
||||
if (m_services.at(serviceIndex).type == type) {
|
||||
return serviceIndex;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
QHash<int, QByteArray> ApiServicesModel::roleNames() const
|
||||
{
|
||||
QHash<int, QByteArray> roles;
|
||||
@@ -224,12 +233,11 @@ QHash<int, QByteArray> ApiServicesModel::roleNames() const
|
||||
roles[CardDescriptionRole] = "cardDescription";
|
||||
roles[ServiceDescriptionRole] = "serviceDescription";
|
||||
roles[IsServiceAvailableRole] = "isServiceAvailable";
|
||||
roles[SpeedRole] = "speed";
|
||||
roles[TimeLimitRole] = "timeLimit";
|
||||
roles[RegionRole] = "region";
|
||||
roles[FeaturesRole] = "features";
|
||||
roles[PriceRole] = "price";
|
||||
roles[EndDateRole] = "endDate";
|
||||
roles[TermsOfUseUrlRole] = "termsOfUseUrl";
|
||||
roles[PrivacyPolicyUrlRole] = "privacyPolicyUrl";
|
||||
roles[ShowRecommendedRole] = "showRecommended";
|
||||
roles[OrderRole] = "order";
|
||||
|
||||
return roles;
|
||||
@@ -243,18 +251,22 @@ ApiServicesModel::ApiServicesData ApiServicesModel::getApiServicesData(const QJs
|
||||
auto availableCountries = data.value(configKey::availableCountries).toArray();
|
||||
auto serviceDescription = data.value(configKey::serviceDescription).toObject();
|
||||
|
||||
auto subscriptionObject = data.value(configKey::subscription).toObject();
|
||||
auto subscriptionObject = data.value(apiDefs::key::subscription).toObject();
|
||||
|
||||
ApiServicesData serviceData;
|
||||
serviceData.serviceInfo.name = serviceInfo.value(configKey::name).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.name = serviceDescription.value(configKey::serviceName).toString();
|
||||
|
||||
serviceData.serviceInfo.cardDescription = serviceDescription.value(configKey::cardDescription).toString();
|
||||
serviceData.serviceInfo.description = serviceDescription.value(configKey::description).toString();
|
||||
serviceData.serviceInfo.features = serviceDescription.value(configKey::features).toString();
|
||||
serviceData.serviceInfo.termsOfUseUrl = serviceDescription.value(apiDefs::key::termsOfUseUrl).toString();
|
||||
serviceData.serviceInfo.privacyPolicyUrl = serviceDescription.value(apiDefs::key::privacyPolicyUrl).toString();
|
||||
|
||||
serviceData.subscriptionPlansJson = serviceDescription.value(configKey::subscriptionPlans).toArray();
|
||||
serviceData.benefits = serviceDescription.value(configKey::benefits).toArray();
|
||||
|
||||
serviceData.minPriceLabel = serviceDescription.value(configKey::minPriceLabel).toString().trimmed();
|
||||
|
||||
serviceData.supportInfo = data.value(apiDefs::key::supportInfo).toObject();
|
||||
|
||||
serviceData.type = serviceType;
|
||||
serviceData.protocol = serviceProtocol;
|
||||
@@ -270,7 +282,7 @@ ApiServicesModel::ApiServicesData ApiServicesModel::getApiServicesData(const QJs
|
||||
serviceData.serviceInfo.object = serviceInfo;
|
||||
serviceData.availableCountries = availableCountries;
|
||||
|
||||
serviceData.subscription.endDate = subscriptionObject.value(configKey::endDate).toString();
|
||||
serviceData.subscription.endDate = subscriptionObject.value(apiDefs::key::endDate).toString();
|
||||
|
||||
return serviceData;
|
||||
}
|
||||
|
||||
@@ -4,65 +4,23 @@
|
||||
#include <QAbstractListModel>
|
||||
#include <QJsonArray>
|
||||
#include <QJsonObject>
|
||||
#include <QVector>
|
||||
|
||||
class ApiServicesModel : public QAbstractListModel
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
enum Roles {
|
||||
NameRole = Qt::UserRole + 1,
|
||||
CardDescriptionRole,
|
||||
ServiceDescriptionRole,
|
||||
IsServiceAvailableRole,
|
||||
SpeedRole,
|
||||
TimeLimitRole,
|
||||
RegionRole,
|
||||
FeaturesRole,
|
||||
PriceRole,
|
||||
EndDateRole,
|
||||
OrderRole
|
||||
};
|
||||
|
||||
explicit ApiServicesModel(QObject *parent = nullptr);
|
||||
|
||||
int rowCount(const QModelIndex &parent = QModelIndex()) const override;
|
||||
|
||||
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
|
||||
|
||||
public slots:
|
||||
void updateModel(const QJsonObject &data);
|
||||
|
||||
void setServiceIndex(const int index);
|
||||
|
||||
QJsonObject getSelectedServiceInfo();
|
||||
QString getSelectedServiceType();
|
||||
QString getSelectedServiceProtocol();
|
||||
QString getSelectedServiceName();
|
||||
QJsonArray getSelectedServiceCountries();
|
||||
|
||||
QString getCountryCode();
|
||||
|
||||
QString getStoreEndpoint();
|
||||
|
||||
QVariant getSelectedServiceData(const QString roleString);
|
||||
|
||||
protected:
|
||||
QHash<int, QByteArray> roleNames() const override;
|
||||
|
||||
private:
|
||||
struct ServiceInfo
|
||||
{
|
||||
QString name;
|
||||
QString speed;
|
||||
QString timeLimit;
|
||||
QString region;
|
||||
QString price;
|
||||
|
||||
QString description;
|
||||
QString features;
|
||||
QString cardDescription;
|
||||
|
||||
QString termsOfUseUrl;
|
||||
QString privacyPolicyUrl;
|
||||
|
||||
QJsonObject object;
|
||||
};
|
||||
|
||||
@@ -80,11 +38,64 @@ private:
|
||||
QString storeEndpoint;
|
||||
|
||||
ServiceInfo serviceInfo;
|
||||
QJsonObject supportInfo;
|
||||
Subscription subscription;
|
||||
|
||||
QJsonArray availableCountries;
|
||||
|
||||
QJsonArray subscriptionPlansJson;
|
||||
QJsonArray benefits;
|
||||
|
||||
QString minPriceLabel;
|
||||
};
|
||||
|
||||
enum Roles {
|
||||
NameRole = Qt::UserRole + 1,
|
||||
CardDescriptionRole,
|
||||
ServiceDescriptionRole,
|
||||
IsServiceAvailableRole,
|
||||
PriceRole,
|
||||
EndDateRole,
|
||||
TermsOfUseUrlRole,
|
||||
PrivacyPolicyUrlRole,
|
||||
ShowRecommendedRole,
|
||||
OrderRole
|
||||
};
|
||||
|
||||
explicit ApiServicesModel(QObject *parent = nullptr);
|
||||
|
||||
int rowCount(const QModelIndex &parent = QModelIndex()) const override;
|
||||
|
||||
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
|
||||
|
||||
ApiServicesData selectedServiceData() const;
|
||||
|
||||
public slots:
|
||||
void updateModel(const QJsonObject &data);
|
||||
|
||||
void setServiceIndex(const int index);
|
||||
|
||||
QJsonObject getSelectedServiceInfo();
|
||||
QString getSelectedServiceType();
|
||||
QString getSelectedServiceProtocol();
|
||||
QString getSelectedServiceName();
|
||||
QJsonArray getSelectedServiceCountries();
|
||||
|
||||
QString getCountryCode();
|
||||
|
||||
QString getStoreEndpoint();
|
||||
|
||||
QVariant getSelectedServiceData(const QString roleString);
|
||||
|
||||
Q_INVOKABLE int serviceIndexForType(const QString &type) const;
|
||||
|
||||
signals:
|
||||
void serviceSelectionChanged();
|
||||
|
||||
protected:
|
||||
QHash<int, QByteArray> roleNames() const override;
|
||||
|
||||
private:
|
||||
ApiServicesData getApiServicesData(const QJsonObject &data);
|
||||
|
||||
QString m_countryCode;
|
||||
@@ -93,4 +104,4 @@ private:
|
||||
int m_selectedServiceIndex;
|
||||
};
|
||||
|
||||
#endif // APISERVICESMODEL_H
|
||||
#endif
|
||||
|
||||
@@ -0,0 +1,131 @@
|
||||
#include "apiSubscriptionPlansModel.h"
|
||||
|
||||
#include <QJsonObject>
|
||||
#include <QJsonValue>
|
||||
#include <QModelIndex>
|
||||
#include <utility>
|
||||
|
||||
namespace
|
||||
{
|
||||
namespace configKey
|
||||
{
|
||||
constexpr char billingPeriod[] = "billing_period";
|
||||
constexpr char priceLabel[] = "price_label";
|
||||
constexpr char subtitle[] = "subtitle";
|
||||
constexpr char recommended[] = "recommended";
|
||||
constexpr char checkoutUrl[] = "checkout_url";
|
||||
constexpr char isTrial[] = "is_trial";
|
||||
constexpr char serviceProtocol[] = "service_protocol";
|
||||
constexpr char storeProductId[] = "store_product_id";
|
||||
}
|
||||
}
|
||||
|
||||
ApiSubscriptionPlansModel::ApiSubscriptionPlansModel(QObject *parent)
|
||||
: QAbstractListModel(parent)
|
||||
{
|
||||
}
|
||||
|
||||
int ApiSubscriptionPlansModel::rowCount(const QModelIndex &parent) const
|
||||
{
|
||||
if (parent.isValid()) {
|
||||
return 0;
|
||||
}
|
||||
return m_subscriptionPlans.size();
|
||||
}
|
||||
|
||||
QVariant ApiSubscriptionPlansModel::data(const QModelIndex &index, int role) const
|
||||
{
|
||||
if (!index.isValid() || index.row() < 0 || index.row() >= m_subscriptionPlans.size()) {
|
||||
return {};
|
||||
}
|
||||
const SubscriptionPlanItem &plan = m_subscriptionPlans.at(index.row());
|
||||
switch (role) {
|
||||
case BillingPeriodRole:
|
||||
return plan.billingPeriod;
|
||||
case PriceLabelRole:
|
||||
return plan.priceLabel;
|
||||
case SubtitleRole:
|
||||
return plan.subtitle;
|
||||
case RecommendedRole:
|
||||
return plan.recommended;
|
||||
case CheckoutUrlRole:
|
||||
return plan.checkoutUrl;
|
||||
case IsTrialRole:
|
||||
return plan.isTrial;
|
||||
case ServiceProtocolRole:
|
||||
return plan.serviceProtocol;
|
||||
case StoreProductIdRole:
|
||||
return plan.storeProductId;
|
||||
default:
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
||||
QHash<int, QByteArray> ApiSubscriptionPlansModel::roleNames() const
|
||||
{
|
||||
return {
|
||||
{ BillingPeriodRole, "billingPeriod" },
|
||||
{ PriceLabelRole, "priceLabel" },
|
||||
{ SubtitleRole, "subtitle" },
|
||||
{ RecommendedRole, "recommended" },
|
||||
{ CheckoutUrlRole, "checkoutUrl" },
|
||||
{ IsTrialRole, "isTrial" },
|
||||
{ ServiceProtocolRole, "serviceProtocol" },
|
||||
{ StoreProductIdRole, "storeProductId" },
|
||||
};
|
||||
}
|
||||
|
||||
void ApiSubscriptionPlansModel::updateModel(const QJsonArray &arr)
|
||||
{
|
||||
beginResetModel();
|
||||
m_subscriptionPlans.clear();
|
||||
m_subscriptionPlans.reserve(arr.size());
|
||||
for (const QJsonValue &planValue : arr) {
|
||||
if (!planValue.isObject()) {
|
||||
continue;
|
||||
}
|
||||
const QJsonObject planObject = planValue.toObject();
|
||||
SubscriptionPlanItem subscriptionPlan;
|
||||
subscriptionPlan.billingPeriod = planObject.value(configKey::billingPeriod).toString();
|
||||
subscriptionPlan.priceLabel = planObject.value(configKey::priceLabel).toString();
|
||||
subscriptionPlan.subtitle = planObject.value(configKey::subtitle).toString();
|
||||
subscriptionPlan.recommended = planObject.value(configKey::recommended).toBool();
|
||||
subscriptionPlan.checkoutUrl = planObject.value(configKey::checkoutUrl).toString();
|
||||
subscriptionPlan.isTrial = planObject.value(configKey::isTrial).toBool();
|
||||
subscriptionPlan.serviceProtocol = planObject.value(configKey::serviceProtocol).toString();
|
||||
subscriptionPlan.storeProductId = planObject.value(configKey::storeProductId).toString();
|
||||
m_subscriptionPlans.append(std::move(subscriptionPlan));
|
||||
}
|
||||
endResetModel();
|
||||
}
|
||||
|
||||
void ApiSubscriptionPlansModel::clear()
|
||||
{
|
||||
beginResetModel();
|
||||
m_subscriptionPlans.clear();
|
||||
endResetModel();
|
||||
}
|
||||
|
||||
QVariantMap ApiSubscriptionPlansModel::planAt(int row) const
|
||||
{
|
||||
if (row < 0 || row >= m_subscriptionPlans.size()) {
|
||||
return {};
|
||||
}
|
||||
const QModelIndex modelIndex = index(row, 0);
|
||||
QVariantMap planMap;
|
||||
const QHash<int, QByteArray> roles = roleNames();
|
||||
for (auto roleIt = roles.cbegin(); roleIt != roles.cend(); ++roleIt) {
|
||||
planMap.insert(QString::fromUtf8(roleIt.value()), data(modelIndex, roleIt.key()));
|
||||
}
|
||||
return planMap;
|
||||
}
|
||||
|
||||
int ApiSubscriptionPlansModel::recommendedRowIndex() const
|
||||
{
|
||||
for (int planIndex = 0; planIndex < m_subscriptionPlans.size(); ++planIndex) {
|
||||
if (m_subscriptionPlans.at(planIndex).recommended) {
|
||||
return planIndex;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
#ifndef APISUBSCRIPTIONPLANSMODEL_H
|
||||
#define APISUBSCRIPTIONPLANSMODEL_H
|
||||
|
||||
#include <QAbstractListModel>
|
||||
#include <QJsonArray>
|
||||
#include <QVector>
|
||||
|
||||
class ApiSubscriptionPlansModel : public QAbstractListModel
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
enum Roles {
|
||||
BillingPeriodRole = Qt::UserRole + 1,
|
||||
PriceLabelRole,
|
||||
SubtitleRole,
|
||||
RecommendedRole,
|
||||
CheckoutUrlRole,
|
||||
IsTrialRole,
|
||||
ServiceProtocolRole,
|
||||
StoreProductIdRole
|
||||
};
|
||||
Q_ENUM(Roles)
|
||||
|
||||
explicit ApiSubscriptionPlansModel(QObject *parent = nullptr);
|
||||
|
||||
int rowCount(const QModelIndex &parent = QModelIndex()) const override;
|
||||
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
|
||||
QHash<int, QByteArray> roleNames() const override;
|
||||
|
||||
void updateModel(const QJsonArray &arr);
|
||||
void clear();
|
||||
|
||||
Q_INVOKABLE QVariantMap planAt(int row) const;
|
||||
Q_INVOKABLE int recommendedRowIndex() const;
|
||||
|
||||
private:
|
||||
struct SubscriptionPlanItem
|
||||
{
|
||||
QString billingPeriod;
|
||||
QString priceLabel;
|
||||
QString subtitle;
|
||||
bool recommended = false;
|
||||
QString checkoutUrl;
|
||||
bool isTrial = false;
|
||||
QString serviceProtocol;
|
||||
QString storeProductId;
|
||||
};
|
||||
|
||||
QVector<SubscriptionPlanItem> m_subscriptionPlans;
|
||||
};
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user