From 3af288c74eb6abfe032baec49714ccce81d2f213 Mon Sep 17 00:00:00 2001 From: 23rd <23rd@vivaldi.net> Date: Sat, 26 Oct 2024 19:10:19 +0300 Subject: [PATCH] Added currency balance to profile section of owned bots. --- Telegram/Resources/langs/lang.strings | 2 + .../info/profile/info_profile_actions.cpp | 85 ++++++++++++++++++- 2 files changed, 83 insertions(+), 4 deletions(-) diff --git a/Telegram/Resources/langs/lang.strings b/Telegram/Resources/langs/lang.strings index d6295d9d9..a21992a28 100644 --- a/Telegram/Resources/langs/lang.strings +++ b/Telegram/Resources/langs/lang.strings @@ -1594,6 +1594,8 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL "lng_manage_peer_bot_public_link" = "Public Link"; "lng_manage_peer_bot_public_links" = "Public Links"; "lng_manage_peer_bot_balance" = "Balance"; +"lng_manage_peer_bot_balance_currency" = "Toncoin"; +"lng_manage_peer_bot_balance_credits" = "Stars"; "lng_manage_peer_bot_edit_intro" = "Edit Intro"; "lng_manage_peer_bot_edit_commands" = "Edit Commands"; "lng_manage_peer_bot_edit_settings" = "Change Bot Settings"; diff --git a/Telegram/SourceFiles/info/profile/info_profile_actions.cpp b/Telegram/SourceFiles/info/profile/info_profile_actions.cpp index df05d47c3..ffcab486e 100644 --- a/Telegram/SourceFiles/info/profile/info_profile_actions.cpp +++ b/Telegram/SourceFiles/info/profile/info_profile_actions.cpp @@ -10,6 +10,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "api/api_blocked_peers.h" #include "api/api_chat_participants.h" #include "api/api_credits.h" +#include "api/api_statistics.h" #include "apiwrap.h" #include "base/options.h" #include "base/timer_rpl.h" @@ -45,13 +46,15 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "history/history_item_helpers.h" #include "history/view/history_view_item_preview.h" #include "info/bot/earn/info_bot_earn_widget.h" -#include "info/info_controller.h" -#include "info/info_memento.h" +#include "info/channel_statistics/earn/earn_format.h" +#include "info/channel_statistics/earn/info_channel_earn_list.h" // IconCurrency. #include "info/profile/info_profile_icon.h" #include "info/profile/info_profile_phone_menu.h" #include "info/profile/info_profile_text.h" #include "info/profile/info_profile_values.h" #include "info/profile/info_profile_widget.h" +#include "info/info_controller.h" +#include "info/info_memento.h" #include "inline_bots/bot_attach_web_view.h" #include "iv/iv_instance.h" #include "lang/lang_keys.h" @@ -79,6 +82,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "window/window_controller.h" // Window::Controller::show. #include "window/window_peer_menu.h" #include "window/window_session_controller.h" +#include "styles/style_channel_earn.h" // st::channelEarnCurrencyCommonMargins #include "styles/style_info.h" #include "styles/style_layers.h" #include "styles/style_menu_icons.h" @@ -835,6 +839,7 @@ public: private: void addInviteToGroupAction(not_null user); + void addCurrencyAction(not_null user); void addCreditsAction(not_null user); void addShareContactAction(not_null user); void addEditContactAction(not_null user); @@ -1935,6 +1940,77 @@ void ActionsFiller::addInviteToGroupAction(not_null user) { about->finishAnimating(); } +void ActionsFiller::addCurrencyAction(not_null user) { + struct State final { + rpl::variable balance; + }; + const auto state = _wrap->lifetime().make_state(); + const auto controller = _controller->parentController(); + const auto wrap = AddActionButton( + _wrap, + tr::lng_manage_peer_bot_balance_currency(), + state->balance.value() | rpl::map(rpl::mappers::_1 > 0), + [=] { controller->showSection(Info::BotEarn::Make(user)); }, + &st::infoIconBotBalance); + const auto balance = user->session().credits().balanceCurrency(user->id); + if (balance) { + state->balance = balance; + } + { + const auto weak = Ui::MakeWeak(_wrap.data()); + const auto currencyLoadLifetime + = std::make_shared(); + const auto currencyLoad + = currencyLoadLifetime->make_state(user); + currencyLoad->request( + ) | rpl::start_with_error_done([=](const QString &error) { + currencyLoadLifetime->destroy(); + }, [=] { + if (const auto strong = weak.get()) { + state->balance = currencyLoad->data().currentBalance; + currencyLoadLifetime->destroy(); + } + }, *currencyLoadLifetime); + } + const auto &st = st::infoSharedMediaButton; + const auto button = wrap->entity(); + const auto name = Ui::CreateChild(button, st.rightLabel); + const auto icon = Ui::Text::SingleCustomEmoji( + user->owner().customEmojiManager().registerInternalEmoji( + Info::ChannelEarn::IconCurrency( + st.rightLabel, + st.rightLabel.textFg->c), + st::channelEarnCurrencyCommonMargins, + false)); + name->show(); + rpl::combine( + button->widthValue(), + tr::lng_manage_peer_bot_balance_currency(), + state->balance.value() + ) | rpl::start_with_next([=, &st]( + int width, + const QString &button, + uint64 balance) { + const auto available = width + - rect::m::sum::h(st.padding) + - st.style.font->width(button) + - st::settingsButtonRightSkip; + name->setMarkedText( + base::duplicate(icon) + .append(QChar(' ')) + .append(Info::ChannelEarn::MajorPart(balance)) + .append(Info::ChannelEarn::MinorPart(balance)), + Core::MarkedTextContext{ + .session = &user->session(), + .customEmojiRepaint = [=] { name->update(); }, + }); + name->resizeToNaturalWidth(available); + name->moveToRight(st::settingsButtonRightSkip, st.padding.top()); + }, name->lifetime()); + name->setAttribute(Qt::WA_TransparentForMouseEvents); + wrap->finishAnimating(); +} + void ActionsFiller::addCreditsAction(not_null user) { struct State final { rpl::variable balance; @@ -1943,7 +2019,7 @@ void ActionsFiller::addCreditsAction(not_null user) { const auto controller = _controller->parentController(); const auto wrap = AddActionButton( _wrap, - tr::lng_manage_peer_bot_balance(), + tr::lng_manage_peer_bot_balance_credits(), state->balance.value() | rpl::map(rpl::mappers::_1 > 0), [=] { controller->showSection(Info::BotEarn::Make(user)); }, &st::infoIconBotBalance); @@ -1963,7 +2039,7 @@ void ActionsFiller::addCreditsAction(not_null user) { name->show(); rpl::combine( button->widthValue(), - tr::lng_manage_peer_bot_balance(), + tr::lng_manage_peer_bot_balance_credits(), state->balance.value() ) | rpl::start_with_next([=, &st]( int width, @@ -2209,6 +2285,7 @@ void ActionsFiller::addJoinChannelAction( void ActionsFiller::fillUserActions(not_null user) { if (user->isBot()) { + addCurrencyAction(user); addCreditsAction(user); addInviteToGroupAction(user); }