Fill suggested prices for gifts between stars/TON.

This commit is contained in:
John Preston
2025-09-02 12:04:47 +04:00
parent d697677fab
commit 312c94f386
3 changed files with 71 additions and 38 deletions
+31 -31
View File
@@ -239,37 +239,6 @@ struct SessionResalePrices {
crl::time lastReceived = 0;
};
[[nodiscard]] CreditsAmount StarsFromTon(
not_null<Main::Session*> session,
CreditsAmount ton) {
const auto appConfig = &session->appConfig();
const auto starsRate = appConfig->starsWithdrawRate() / 100.;
const auto tonRate = appConfig->currencyWithdrawRate();
if (!starsRate) {
return {};
}
const auto count = (ton.value() * tonRate) / starsRate;
return CreditsAmount(int(base::SafeRound(count)));
}
[[nodiscard]] CreditsAmount TonFromStars(
not_null<Main::Session*> session,
CreditsAmount stars) {
const auto appConfig = &session->appConfig();
const auto starsRate = appConfig->starsWithdrawRate() / 100.;
const auto tonRate = appConfig->currencyWithdrawRate();
if (!tonRate) {
return {};
}
const auto count = (stars.value() * starsRate) / tonRate;
const auto whole = int(std::floor(count));
const auto cents = int(base::SafeRound((count - whole) * 100));
return CreditsAmount(
whole,
cents * (Ui::kNanosInOne / 100),
CreditsType::Ton);
}
[[nodiscard]] not_null<SessionResalePrices*> ResalePrices(
not_null<Main::Session*> session) {
static auto result = base::flat_map<
@@ -5460,4 +5429,35 @@ rpl::lifetime ShowStarGiftResale(
});
}
CreditsAmount StarsFromTon(
not_null<Main::Session*> session,
CreditsAmount ton) {
const auto appConfig = &session->appConfig();
const auto starsRate = appConfig->starsWithdrawRate() / 100.;
const auto tonRate = appConfig->currencyWithdrawRate();
if (!starsRate) {
return {};
}
const auto count = (ton.value() * tonRate) / starsRate;
return CreditsAmount(int(base::SafeRound(count)));
}
CreditsAmount TonFromStars(
not_null<Main::Session*> session,
CreditsAmount stars) {
const auto appConfig = &session->appConfig();
const auto starsRate = appConfig->starsWithdrawRate() / 100.;
const auto tonRate = appConfig->currencyWithdrawRate();
if (!tonRate) {
return {};
}
const auto count = (stars.value() * starsRate) / tonRate;
const auto whole = int(std::floor(count));
const auto cents = int(base::SafeRound((count - whole) * 100));
return CreditsAmount(
whole,
cents * (Ui::kNanosInOne / 100),
CreditsType::Ton);
}
} // namespace Ui
@@ -165,4 +165,11 @@ void ShowResaleGiftBoughtToast(
QString title,
Fn<void()> finishRequesting);
[[nodiscard]] CreditsAmount StarsFromTon(
not_null<Main::Session*> session,
CreditsAmount ton);
[[nodiscard]] CreditsAmount TonFromStars(
not_null<Main::Session*> session,
CreditsAmount stars);
} // namespace Ui
@@ -9,6 +9,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include "base/event_filter.h"
#include "base/unixtime.h"
#include "boxes/star_gift_box.h"
#include "chat_helpers/compose/compose_show.h"
#include "core/ui_integration.h"
#include "data/components/credits.h"
@@ -271,7 +272,7 @@ StarsTonPriceInput AddStarsTonPriceInput(
auto computeResult = [=]() -> std::optional<CreditsAmount> {
auto nanos = int64();
const auto ton = uint32(state->ton.current() ? 1 : 0);
const auto ton = state->ton.current();
if (ton) {
const auto text = tonField->getLastText();
const auto now = Ui::ParseTonAmountString(text);
@@ -302,16 +303,41 @@ StarsTonPriceInput AddStarsTonPriceInput(
}
state->updates.fire({});
};
QObject::connect(
starsField,
&Ui::NumberInput::changed,
starsField,
updatePrice);
const auto updateTonFromStars = [=] {
if (auto result = computeResult(); result && result->stars()) {
const auto v = Ui::TonFromStars(session, *result);
const auto amount = v.whole() * Ui::kNanosInOne + v.nano();
tonField->setText(
Ui::FormatTonAmount(amount, Ui::TonFormatFlag::Simple).full);
}
};
const auto updateStarsFromTon = [=] {
if (auto result = computeResult(); result && result->ton()) {
const auto v = Ui::StarsFromTon(session, *result);
starsField->setText(QString::number(v.whole()));
}
};
QObject::connect(starsField, &Ui::NumberInput::changed, starsField, [=] {
if (!state->ton.current()) {
updatePrice();
updateTonFromStars();
}
});
tonField->changes(
) | rpl::start_with_next(updatePrice, tonField->lifetime());
) | rpl::start_with_next([=] {
if (state->ton.current()) {
updatePrice();
updateStarsFromTon();
}
}, tonField->lifetime());
state->ton.changes(
) | rpl::start_with_next(updatePrice, container->lifetime());
if (state->ton.current()) {
updateStarsFromTon();
} else {
updateTonFromStars();
}
QObject::connect(
starsField,