Files
amnezia-client/client/ui/models/api/apiSubscriptionPlansModel.cpp
vkamn 78f504e35c 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>
2026-04-08 12:21:12 +08:00

132 lines
4.1 KiB
C++

#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;
}