Skip to content

Commit

Permalink
Add discount info to the payment dialog
Browse files Browse the repository at this point in the history
  • Loading branch information
dimkanovikov committed Aug 19, 2024
1 parent f603ad9 commit 00d186f
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/cloud
Submodule cloud updated from 94f715 to 7fff56
72 changes: 72 additions & 0 deletions src/core/management_layer/content/account/account_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -601,7 +601,13 @@ void AccountManager::buyProLifetme()
}
}

const int discount = proPaymentOptions.constFirst().discount;
const auto discountInfo = discount > 0
? tr("You have an additional %1% discount due to the promo code activation").arg(discount)
: "";

d->initPurchaseDialog();
d->purchaseDialog->setDiscountInfo(discountInfo);
d->purchaseDialog->setPaymentOptions(proPaymentOptions);
d->purchaseDialog->selectOption(proPaymentOptions.constFirst());
d->purchaseDialog->showDialog();
Expand All @@ -622,7 +628,13 @@ void AccountManager::renewPro()
return;
}

const int discount = proPaymentOptions.constFirst().discount;
const auto discountInfo = discount > 0
? tr("You have an additional %1% discount due to the promo code activation").arg(discount)
: "";

d->initPurchaseDialog();
d->purchaseDialog->setDiscountInfo(discountInfo);
d->purchaseDialog->setPaymentOptions(proPaymentOptions);
d->purchaseDialog->selectOption(proPaymentOptions.constLast());
d->purchaseDialog->showDialog();
Expand All @@ -640,7 +652,13 @@ void AccountManager::giftPro()
}
}

const int discount = proPaymentOptions.constFirst().discount;
const auto discountInfo = discount > 0
? tr("You have an additional %1% discount due to the promo code activation").arg(discount)
: "";

d->initPurchaseDialog();
d->purchaseDialog->setDiscountInfo(discountInfo);
d->purchaseDialog->setPaymentOptions(proPaymentOptions);
d->purchaseDialog->setPurchaseAvailable(false);
d->purchaseDialog->selectOption(proPaymentOptions.constFirst());
Expand Down Expand Up @@ -705,7 +723,30 @@ void AccountManager::renewCloud()
return;
}

const auto hasProLifetime
= std::find_if(d->accountInfo.subscriptions.begin(), d->accountInfo.subscriptions.end(),
[](const Domain::SubscriptionInfo& _subscription) {
return _subscription.type == Domain::SubscriptionType::ProLifetime;
});
const int discount = cloudPaymentOptions.constFirst().discount;
QString discountInfo;
if (hasProLifetime) {
if (discount > 20) {
discountInfo
= tr("You have an additional 20% discount due to PRO lifetime subscription "
"purchase, and %1% discount due to the promo code activation")
.arg(discount - 20);
} else {
discountInfo = tr("You have an additional 20% discount thanks to the purchasing of the "
"PRO lifetime subscription");
}
} else if (discount > 0) {
discountInfo = tr("You have an additional %1% discount due to the promo code activation")
.arg(discount);
}

d->initPurchaseDialog();
d->purchaseDialog->setDiscountInfo(discountInfo);
d->purchaseDialog->setPaymentOptions(cloudPaymentOptions);
d->purchaseDialog->selectOption(cloudPaymentOptions.constLast());
d->purchaseDialog->showDialog();
Expand All @@ -726,7 +767,31 @@ void AccountManager::giftCloud()
return;
}

const auto hasProLifetime
= std::find_if(d->accountInfo.subscriptions.begin(), d->accountInfo.subscriptions.end(),
[](const Domain::SubscriptionInfo& _subscription) {
return _subscription.type == Domain::SubscriptionType::ProLifetime;
});
const int discount = cloudPaymentOptions.constFirst().discount;
QString discountInfo;
if (hasProLifetime) {
if (discount > 20) {
discountInfo = tr("You have an additional 20% discount thanks to the purchasing of the "
"PRO lifetime subscription and %1% discount thanks to the activation "
"of the promo code")
.arg(discount - 20);
} else {
discountInfo = tr("You have an additional 20% discount thanks to the purchasing of the "
"PRO lifetime subscription");
}
} else if (discount > 0) {
discountInfo
= tr("You have an additional %1% discount thanks to the activation of the promo code")
.arg(discount);
}

d->initPurchaseDialog();
d->purchaseDialog->setDiscountInfo(discountInfo);
d->purchaseDialog->setPaymentOptions(cloudPaymentOptions);
d->purchaseDialog->setPurchaseAvailable(false);
d->purchaseDialog->selectOption(cloudPaymentOptions.constLast());
Expand Down Expand Up @@ -754,11 +819,18 @@ void AccountManager::buyCredits()
return;
}

const int discount = creditPaymentOptions.constFirst().discount;
const auto discountInfo = discount > 0
? tr("You have an additional %1% discount thanks to the activation of the promo code")
.arg(discount)
: "";

d->initPurchaseDialog();
d->purchaseDialog->setDescription(
tr("Credits are our internal currency. They are used for AI tools, such as text "
"generation.\n\n1 credit equals 1000 words processed by AI.\n1 credit equals 10 images "
"generated by AI."));
d->purchaseDialog->setDiscountInfo(discountInfo);
d->purchaseDialog->setPaymentOptions(creditPaymentOptions);
d->purchaseDialog->selectOption(creditPaymentOptions.constLast());
d->purchaseDialog->showDialog();
Expand Down
53 changes: 48 additions & 5 deletions src/core/ui/account/purchase_dialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <ui/design_system/design_system.h>
#include <ui/widgets/button/button.h>
#include <ui/widgets/label/label.h>
#include <utils/helpers/color_helper.h>

#include <QApplication>
#include <QHBoxLayout>
Expand All @@ -27,6 +28,9 @@ class PurchaseDialog::Implementation
Body1Label* descriptionLabel = nullptr;
Widget* content = nullptr;
QVector<PurchaseDialogOptionWidget*> options;
Body1Label* information = nullptr;
IconsMidLabel* informationIcon = nullptr;
Body2Label* informationText = nullptr;

QHBoxLayout* buttonsLayout = nullptr;
Button* giftButton = nullptr;
Expand All @@ -37,12 +41,17 @@ class PurchaseDialog::Implementation
PurchaseDialog::Implementation::Implementation(QWidget* _parent)
: descriptionLabel(new Body1Label(_parent))
, content(new Widget(_parent))
, information(new Body1Label(content))
, informationIcon(new IconsMidLabel(content))
, informationText(new Body2Label(content))
, buttonsLayout(new QHBoxLayout)
, giftButton(new Button(_parent))
, cancelButton(new Button(_parent))
, purchaseButton(new Button(_parent))
{
descriptionLabel->hide();
information->hide();
informationIcon->setIcon(u8"\U000F02FD");

buttonsLayout->setContentsMargins({});
buttonsLayout->setSpacing(0);
Expand Down Expand Up @@ -74,9 +83,18 @@ PurchaseDialog::PurchaseDialog(QWidget* _parent)
setAcceptButton(d->purchaseButton);
setRejectButton(d->cancelButton);

contentsLayout()->addWidget(d->descriptionLabel, 0, 0);
contentsLayout()->addWidget(d->content, 1, 0);
contentsLayout()->addLayout(d->buttonsLayout, 2, 0);
auto informationLayout = new QHBoxLayout;
informationLayout->setContentsMargins({});
informationLayout->setSpacing(0);
informationLayout->addWidget(d->informationIcon, 0, Qt::AlignTop);
informationLayout->addWidget(d->informationText, 1);
d->information->setLayout(informationLayout);

int row = 0;
contentsLayout()->addWidget(d->descriptionLabel, row++, 0);
contentsLayout()->addWidget(d->content, row++, 0);
contentsLayout()->addWidget(d->information, row++, 0);
contentsLayout()->addLayout(d->buttonsLayout, row++, 0);

connect(d->giftButton, &Button::clicked, this, [this] {
for (auto option : std::as_const(d->options)) {
Expand Down Expand Up @@ -105,6 +123,12 @@ void PurchaseDialog::setDescription(const QString& _description)
d->descriptionLabel->setVisible(!_description.isEmpty());
}

void PurchaseDialog::setDiscountInfo(const QString& _info)
{
d->informationText->setText(_info);
d->information->setVisible(!_info.isEmpty());
}

void PurchaseDialog::setPurchaseAvailable(bool _available)
{
d->purchaseButton->setEnabled(_available);
Expand Down Expand Up @@ -194,13 +218,32 @@ void PurchaseDialog::designSystemChangeEvent(DesignSystemChangeEvent* _event)

setContentFixedWidth(DesignSystem::dialog().maximumWidth());

d->content->setBackgroundColor(DesignSystem::color().background());

d->descriptionLabel->setBackgroundColor(DesignSystem::color().background());
d->descriptionLabel->setTextColor(DesignSystem::color().onBackground());
d->descriptionLabel->setContentsMargins(DesignSystem::layout().px24(), 0,
DesignSystem::layout().px24(), 0);

d->content->setBackgroundColor(DesignSystem::color().background());

d->information->setBackgroundColor(ColorHelper::transparent(
DesignSystem::color().accent(), DesignSystem::focusBackgroundOpacity()));
d->information->setContentsMargins(DesignSystem::layout().px24(), DesignSystem::layout().px24(),
DesignSystem::layout().px24(), 0);
d->information->setBorderRadius(DesignSystem::layout().px4());
for (auto label : std::vector<AbstractLabel*>{
d->informationIcon,
d->informationText,
}) {
label->setBackgroundColor(Qt::transparent);
label->setTextColor(DesignSystem::color().onBackground());
}
d->informationIcon->setContentsMargins(DesignSystem::layout().px12(),
DesignSystem::layout().px16(),
DesignSystem::layout().px12(), 0);
d->informationText->setContentsMargins(0, DesignSystem::layout().px16(),
DesignSystem::layout().px12(),
DesignSystem::layout().px16());

for (auto button : {
d->giftButton,
d->cancelButton,
Expand Down
5 changes: 5 additions & 0 deletions src/core/ui/account/purchase_dialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ class PurchaseDialog : public AbstractDialog
*/
void setDescription(const QString& _description);

/**
* @brief Задать информацию о скидке
*/
void setDiscountInfo(const QString& _info);

/**
* @brief Задать возможность покупки подписки для себя
*/
Expand Down

0 comments on commit 00d186f

Please sign in to comment.