chore: minor fixes

This commit is contained in:
vkamn
2026-03-31 16:17:55 +08:00
parent a4b97e8764
commit 4b625fe70f
5 changed files with 30 additions and 39 deletions
@@ -46,7 +46,7 @@ namespace
constexpr char serviceDescription[] = "service_description"; constexpr char serviceDescription[] = "service_description";
constexpr char subscriptionPlans[] = "subscription_plans"; constexpr char subscriptionPlans[] = "subscription_plans";
constexpr char storeProductId[] = "store_product_id"; constexpr char storeProductId[] = "store_product_id";
constexpr char primaryRight[] = "primary_right"; constexpr char priceLabel[] = "price_label";
constexpr char apiPayload[] = "api_payload"; constexpr char apiPayload[] = "api_payload";
constexpr char keyPayload[] = "key_payload"; constexpr char keyPayload[] = "key_payload";
@@ -337,7 +337,7 @@ namespace
if (priceIt == idToDisplayPrice.cend()) { if (priceIt == idToDisplayPrice.cend()) {
continue; continue;
} }
planObject.insert(configKey::primaryRight, *priceIt); planObject.insert(configKey::priceLabel, *priceIt);
mergedPlans.append(planObject); mergedPlans.append(planObject);
} }
plans = mergedPlans; plans = mergedPlans;
@@ -2,27 +2,21 @@
#include <QJsonObject> #include <QJsonObject>
#include <QJsonValue> #include <QJsonValue>
#include <QModelIndex>
#include <utility> #include <utility>
namespace namespace
{ {
namespace configKey namespace configKey
{ {
constexpr char primaryLeft[] = "primary_left"; constexpr char billingPeriod[] = "billing_period";
constexpr char primaryRight[] = "primary_right"; constexpr char priceLabel[] = "price_label";
constexpr char subtitle[] = "subtitle"; constexpr char subtitle[] = "subtitle";
constexpr char recommended[] = "recommended"; constexpr char recommended[] = "recommended";
constexpr char checkoutUrl[] = "checkout_url"; constexpr char checkoutUrl[] = "checkout_url";
constexpr char isTrial[] = "is_trial"; constexpr char isTrial[] = "is_trial";
constexpr char serviceProtocol[] = "service_protocol"; constexpr char serviceProtocol[] = "service_protocol";
constexpr char storeProductId[] = "store_product_id"; constexpr char storeProductId[] = "store_product_id";
constexpr char primaryLeftCamel[] = "primaryLeft";
constexpr char primaryRightCamel[] = "primaryRight";
constexpr char checkoutUrlCamel[] = "checkoutUrl";
constexpr char isTrialCamel[] = "isTrial";
constexpr char serviceProtocolCamel[] = "serviceProtocol";
constexpr char storeProductIdCamel[] = "storeProductId";
} }
} }
@@ -46,10 +40,10 @@ QVariant ApiSubscriptionPlansModel::data(const QModelIndex &index, int role) con
} }
const SubscriptionPlanItem &plan = m_subscriptionPlans.at(index.row()); const SubscriptionPlanItem &plan = m_subscriptionPlans.at(index.row());
switch (role) { switch (role) {
case PrimaryLeftRole: case BillingPeriodRole:
return plan.primaryLeft; return plan.billingPeriod;
case PrimaryRightRole: case PriceLabelRole:
return plan.primaryRight; return plan.priceLabel;
case SubtitleRole: case SubtitleRole:
return plan.subtitle; return plan.subtitle;
case RecommendedRole: case RecommendedRole:
@@ -70,8 +64,8 @@ QVariant ApiSubscriptionPlansModel::data(const QModelIndex &index, int role) con
QHash<int, QByteArray> ApiSubscriptionPlansModel::roleNames() const QHash<int, QByteArray> ApiSubscriptionPlansModel::roleNames() const
{ {
return { return {
{ PrimaryLeftRole, "primaryLeft" }, { BillingPeriodRole, "billingPeriod" },
{ PrimaryRightRole, "primaryRight" }, { PriceLabelRole, "priceLabel" },
{ SubtitleRole, "subtitle" }, { SubtitleRole, "subtitle" },
{ RecommendedRole, "recommended" }, { RecommendedRole, "recommended" },
{ CheckoutUrlRole, "checkoutUrl" }, { CheckoutUrlRole, "checkoutUrl" },
@@ -92,8 +86,8 @@ void ApiSubscriptionPlansModel::updateModel(const QJsonArray &arr)
} }
const QJsonObject planObject = planValue.toObject(); const QJsonObject planObject = planValue.toObject();
SubscriptionPlanItem subscriptionPlan; SubscriptionPlanItem subscriptionPlan;
subscriptionPlan.primaryLeft = planObject.value(configKey::primaryLeft).toString(); subscriptionPlan.billingPeriod = planObject.value(configKey::billingPeriod).toString();
subscriptionPlan.primaryRight = planObject.value(configKey::primaryRight).toString(); subscriptionPlan.priceLabel = planObject.value(configKey::priceLabel).toString();
subscriptionPlan.subtitle = planObject.value(configKey::subtitle).toString(); subscriptionPlan.subtitle = planObject.value(configKey::subtitle).toString();
subscriptionPlan.recommended = planObject.value(configKey::recommended).toBool(); subscriptionPlan.recommended = planObject.value(configKey::recommended).toBool();
subscriptionPlan.checkoutUrl = planObject.value(configKey::checkoutUrl).toString(); subscriptionPlan.checkoutUrl = planObject.value(configKey::checkoutUrl).toString();
@@ -117,16 +111,13 @@ QVariantMap ApiSubscriptionPlansModel::planAt(int row) const
if (row < 0 || row >= m_subscriptionPlans.size()) { if (row < 0 || row >= m_subscriptionPlans.size()) {
return {}; return {};
} }
const SubscriptionPlanItem &plan = m_subscriptionPlans.at(row); const QModelIndex modelIndex = index(row, 0);
QVariantMap planMap; QVariantMap planMap;
planMap.insert(QLatin1String(configKey::primaryLeftCamel), plan.primaryLeft); const QHash<int, QByteArray> roles = roleNames();
planMap.insert(QLatin1String(configKey::primaryRightCamel), plan.primaryRight); planMap.reserve(roles.size());
planMap.insert(QLatin1String(configKey::subtitle), plan.subtitle); for (auto roleIt = roles.cbegin(); roleIt != roles.cend(); ++roleIt) {
planMap.insert(QLatin1String(configKey::recommended), plan.recommended); planMap.insert(QString::fromUtf8(roleIt.value()), data(modelIndex, roleIt.key()));
planMap.insert(QLatin1String(configKey::checkoutUrlCamel), plan.checkoutUrl); }
planMap.insert(QLatin1String(configKey::isTrialCamel), plan.isTrial);
planMap.insert(QLatin1String(configKey::serviceProtocolCamel), plan.serviceProtocol);
planMap.insert(QLatin1String(configKey::storeProductIdCamel), plan.storeProductId);
return planMap; return planMap;
} }
@@ -11,8 +11,8 @@ class ApiSubscriptionPlansModel : public QAbstractListModel
public: public:
enum Roles { enum Roles {
PrimaryLeftRole = Qt::UserRole + 1, BillingPeriodRole = Qt::UserRole + 1,
PrimaryRightRole, PriceLabelRole,
SubtitleRole, SubtitleRole,
RecommendedRole, RecommendedRole,
CheckoutUrlRole, CheckoutUrlRole,
@@ -37,8 +37,8 @@ public:
private: private:
struct SubscriptionPlanItem struct SubscriptionPlanItem
{ {
QString primaryLeft; QString billingPeriod;
QString primaryRight; QString priceLabel;
QString subtitle; QString subtitle;
bool recommended = false; bool recommended = false;
QString checkoutUrl; QString checkoutUrl;
@@ -9,8 +9,8 @@ Rectangle {
id: root id: root
property bool selected: false property bool selected: false
property string primaryLeft: "" property string billingPeriod: ""
property string primaryRight: "" property string priceLabel: ""
property string subtitle: "" property string subtitle: ""
property bool showRecommendedBadge: false property bool showRecommendedBadge: false
property string recommendedText: "Recommended" property string recommendedText: "Recommended"
@@ -39,7 +39,7 @@ Rectangle {
LabelTextType { LabelTextType {
Layout.fillWidth: true Layout.fillWidth: true
text: root.primaryLeft text: root.billingPeriod
color: root.selected ? AmneziaStyle.color.goldenApricot : AmneziaStyle.color.paleGray color: root.selected ? AmneziaStyle.color.goldenApricot : AmneziaStyle.color.paleGray
font.pixelSize: 17 font.pixelSize: 17
font.weight: Font.DemiBold font.weight: Font.DemiBold
@@ -47,7 +47,7 @@ Rectangle {
} }
LabelTextType { LabelTextType {
text: root.primaryRight text: root.priceLabel
color: root.selected ? AmneziaStyle.color.goldenApricot : AmneziaStyle.color.paleGray color: root.selected ? AmneziaStyle.color.goldenApricot : AmneziaStyle.color.paleGray
font.pixelSize: 17 font.pixelSize: 17
font.weight: Font.DemiBold font.weight: Font.DemiBold
@@ -84,8 +84,8 @@ PageType {
Layout.bottomMargin: index === ApiSubscriptionPlansModel.rowCount() - 1 ? 24 : 12 Layout.bottomMargin: index === ApiSubscriptionPlansModel.rowCount() - 1 ? 24 : 12
selected: root.selectedPlanIndex === index selected: root.selectedPlanIndex === index
primaryLeft: String(model.primaryLeft) billingPeriod: String(model.billingPeriod)
primaryRight: String(model.primaryRight) priceLabel: String(model.priceLabel)
subtitle: String(model.subtitle) subtitle: String(model.subtitle)
showRecommendedBadge: !!model.recommended showRecommendedBadge: !!model.recommended
recommendedText: qsTr("Recommended") recommendedText: qsTr("Recommended")
@@ -207,7 +207,7 @@ PageType {
if (!plan) { if (!plan) {
return qsTr("Continue") return qsTr("Continue")
} }
return qsTr("Subscribe — %1 for %2").arg(String(plan.primaryLeft)).arg(String(plan.primaryRight)) return qsTr("Subscribe — %1 for %2").arg(String(plan.billingPeriod)).arg(String(plan.priceLabel))
} }
clickedFunc: function() { clickedFunc: function() {