diff --git a/Telegram/SourceFiles/boxes/star_gift_box.cpp b/Telegram/SourceFiles/boxes/star_gift_box.cpp index cbc8f707ec..0a89d2b02b 100644 --- a/Telegram/SourceFiles/boxes/star_gift_box.cpp +++ b/Telegram/SourceFiles/boxes/star_gift_box.cpp @@ -239,37 +239,6 @@ struct SessionResalePrices { crl::time lastReceived = 0; }; -[[nodiscard]] CreditsAmount StarsFromTon( - not_null 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 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 ResalePrices( not_null session) { static auto result = base::flat_map< @@ -5460,4 +5429,35 @@ rpl::lifetime ShowStarGiftResale( }); } +CreditsAmount StarsFromTon( + not_null 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 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 diff --git a/Telegram/SourceFiles/boxes/star_gift_box.h b/Telegram/SourceFiles/boxes/star_gift_box.h index 520b1278d4..dac00b9b5f 100644 --- a/Telegram/SourceFiles/boxes/star_gift_box.h +++ b/Telegram/SourceFiles/boxes/star_gift_box.h @@ -165,4 +165,11 @@ void ShowResaleGiftBoughtToast( QString title, Fn finishRequesting); +[[nodiscard]] CreditsAmount StarsFromTon( + not_null session, + CreditsAmount ton); +[[nodiscard]] CreditsAmount TonFromStars( + not_null session, + CreditsAmount stars); + } // namespace Ui diff --git a/Telegram/SourceFiles/history/view/controls/history_view_suggest_options.cpp b/Telegram/SourceFiles/history/view/controls/history_view_suggest_options.cpp index 48125dc169..618d1b2793 100644 --- a/Telegram/SourceFiles/history/view/controls/history_view_suggest_options.cpp +++ b/Telegram/SourceFiles/history/view/controls/history_view_suggest_options.cpp @@ -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 { 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,