Improve giveaway prices display.

This commit is contained in:
John Preston
2026-03-16 15:59:50 +04:00
parent 994dd2e05c
commit 53a2d4704d
4 changed files with 37 additions and 25 deletions
+3
View File
@@ -56,6 +56,9 @@ namespace {
+ QChar(0x00D7) + QChar(0x00D7)
+ ' ' + ' '
+ QString::number(tlOption.vusers().v); + QString::number(tlOption.vusers().v);
options[i].total = Ui::FillAmountAndCurrency(
tlOption.vamount().v,
currency);
options[i].currency = currency; options[i].currency = currency;
} }
return options; return options;
@@ -15,29 +15,33 @@ constexpr auto kDiscountDivider = 1.;
Data::PremiumSubscriptionOption CreateSubscriptionOption( Data::PremiumSubscriptionOption CreateSubscriptionOption(
int months, int months,
int monthlyAmount, float64 monthlyAmount,
int64 amount, int64 amount,
const QString &currency, const QString &currency,
const QString &botUrl) { const QString &botUrl) {
const auto baselineAmount = monthlyAmount * months;
const auto discount = [&] { const auto discount = [&] {
const auto percent = 1. - float64(amount) / (monthlyAmount * months); const auto percent = 1. - float64(amount) / baselineAmount;
return std::round(percent * 100. / kDiscountDivider) return base::SafeRound(percent * 100. / kDiscountDivider)
* kDiscountDivider; * kDiscountDivider;
}(); }();
const auto hasDiscount = (discount > 0);
return { return {
.months = months, .months = months,
.duration = Ui::FormatTTL(months * 86400 * 31), .duration = Ui::FormatTTL(months * 86400 * 31),
.discount = (discount > 0) .discount = hasDiscount
? QString::fromUtf8("\xe2\x88\x92%1%").arg(discount) ? QString::fromUtf8("\xe2\x88\x92%1%").arg(discount)
: QString(), : QString(),
.costPerMonth = Ui::FillAmountAndCurrency( .costPerMonth = Ui::FillAmountAndCurrency(
amount / float64(months), int64(base::SafeRound(amount / float64(months))),
currency),
.costNoDiscount = Ui::FillAmountAndCurrency(
monthlyAmount * months,
currency), currency),
.costNoDiscount = hasDiscount
? Ui::FillAmountAndCurrency(
int64(base::SafeRound(baselineAmount)),
currency)
: QString(),
.costPerYear = Ui::FillAmountAndCurrency( .costPerYear = Ui::FillAmountAndCurrency(
amount / float64(months / 12.), int64(base::SafeRound(amount / float64(months / 12.))),
currency), currency),
.botUrl = botUrl, .botUrl = botUrl,
}; };
@@ -13,7 +13,7 @@ namespace Api {
[[nodiscard]] Data::PremiumSubscriptionOption CreateSubscriptionOption( [[nodiscard]] Data::PremiumSubscriptionOption CreateSubscriptionOption(
int months, int months,
int monthlyAmount, float64 monthlyAmount,
int64 amount, int64 amount,
const QString &currency, const QString &currency,
const QString &botUrl); const QString &botUrl);
@@ -24,9 +24,9 @@ template<typename Option>
if (tlOpts.isEmpty()) { if (tlOpts.isEmpty()) {
return {}; return {};
} }
auto monthlyAmountPerCurrency = base::flat_map<QString, int>(); auto monthlyAmountPerCurrency = base::flat_map<QString, float64>();
auto result = Data::PremiumSubscriptionOptions(); auto result = Data::PremiumSubscriptionOptions();
const auto monthlyAmount = [&](const QString &currency) -> int { const auto monthlyAmount = [&](const QString &currency) -> float64 {
const auto it = monthlyAmountPerCurrency.find(currency); const auto it = monthlyAmountPerCurrency.find(currency);
if (it != end(monthlyAmountPerCurrency)) { if (it != end(monthlyAmountPerCurrency)) {
return it->second; return it->second;
@@ -938,24 +938,30 @@ void AddGiftOptions(
const auto costPerMonthIcon = info.costPerMonth.startsWith(kStar) const auto costPerMonthIcon = info.costPerMonth.startsWith(kStar)
? GenerateStars(costPerMonthFont->height, 1) ? GenerateStars(costPerMonthFont->height, 1)
: QImage(); : QImage();
auto leftText = TextWithEntities();
if (!info.costNoDiscount.isEmpty()) {
leftText.append(Ui::Text::Wrapped(
TextWithEntities{ info.costNoDiscount },
EntityType::StrikeOut));
leftText.append(' ');
}
leftText.append(costPerMonthIcon.isNull()
? info.costPerMonth
: removedStar(info.costPerMonth));
const auto costPerMonthLabel const auto costPerMonthLabel
= row->lifetime().make_state<Ui::Text::String>(); = row->lifetime().make_state<Ui::Text::String>();
costPerMonthLabel->setMarkedText( costPerMonthLabel->setMarkedText(
st::shareBoxListItem.nameStyle, st::shareBoxListItem.nameStyle,
TextWithEntities() std::move(leftText));
.append(Ui::Text::Wrapped( const auto rightText = info.total.isEmpty()
TextWithEntities{ info.costNoDiscount }, ? info.costPerYear
EntityType::StrikeOut)) : info.total;
.append(' ')
.append(costPerMonthIcon.isNull()
? info.costPerMonth
: removedStar(info.costPerMonth)));
const auto costPerYearEntry = [&] { const auto costPerYearEntry = [&] {
if (!info.costPerYear.startsWith(kStar)) { if (!rightText.startsWith(kStar)) {
return QImage(); return QImage();
} }
const auto text = removedStar(info.costPerYear); const auto text = removedStar(rightText);
const auto icon = GenerateStars(costPerYearFont->height, 1); const auto icon = GenerateStars(costPerYearFont->height, 1);
auto result = QImage( auto result = QImage(
QSize(costPerYearFont->spacew + costPerYearFont->width(text), 0) QSize(costPerYearFont->spacew + costPerYearFont->width(text), 0)
@@ -969,7 +975,6 @@ void AddGiftOptions(
p.drawImage(0, 0, icon); p.drawImage(0, 0, icon);
p.setPen(st::windowSubTextFg); p.setPen(st::windowSubTextFg);
p.setFont(costPerYearFont); p.setFont(costPerYearFont);
auto copy = info.costPerYear;
p.drawText( p.drawText(
Rect(result.size() / style::DevicePixelRatio()), Rect(result.size() / style::DevicePixelRatio()),
text, text,
@@ -1078,7 +1083,7 @@ void AddGiftOptions(
+ costPerMonthIcon.width() + costPerMonthIcon.width()
/ style::DevicePixelRatio()); / style::DevicePixelRatio());
const auto costPerYearWidth = costPerYearFont->width( const auto costPerYearWidth = costPerYearFont->width(
info.costPerYear); rightText);
const auto pos = perRect.translated(left, 0).topLeft(); const auto pos = perRect.translated(left, 0).topLeft();
const auto availableWidth = row->width() const auto availableWidth = row->width()
- pos.x() - pos.x()
@@ -1096,7 +1101,7 @@ void AddGiftOptions(
- QMargins(0, 0, st.rowMargins.right(), 0); - QMargins(0, 0, st.rowMargins.right(), 0);
if (costPerYearEntry.isNull()) { if (costPerYearEntry.isNull()) {
p.setFont(costPerYearFont); p.setFont(costPerYearFont);
p.drawText(totalRect, info.costPerYear, style::al_right); p.drawText(totalRect, rightText, style::al_right);
} else { } else {
const auto size = costPerYearEntry.size() const auto size = costPerYearEntry.size()
/ style::DevicePixelRatio(); / style::DevicePixelRatio();