diff --git a/Telegram/SourceFiles/info/profile/info_profile_actions.cpp b/Telegram/SourceFiles/info/profile/info_profile_actions.cpp index a904703a4..dae6a8298 100644 --- a/Telegram/SourceFiles/info/profile/info_profile_actions.cpp +++ b/Telegram/SourceFiles/info/profile/info_profile_actions.cpp @@ -775,6 +775,139 @@ template st)); } +rpl::producer AddCurrencyAction( + not_null user, + not_null wrap, + not_null controller) { + struct State final { + rpl::variable balance; + }; + const auto state = wrap->lifetime().make_state(); + const auto parentController = controller->parentController(); + const auto wrapButton = AddActionButton( + wrap, + tr::lng_manage_peer_bot_balance_currency(), + state->balance.value() | rpl::map(rpl::mappers::_1 > 0), + [=] { parentController->showSection(Info::ChannelEarn::Make(user)); }, + &st::infoIconBotBalance); + const auto balance = user->session().credits().balanceCurrency(user->id); + if (balance) { + state->balance = balance; + } + { + const auto weak = Ui::MakeWeak(wrap); + 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 = wrapButton->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); + wrapButton->finishAnimating(); + return state->balance.value(); +} + +rpl::producer AddCreditsAction( + not_null user, + not_null wrap, + not_null controller) { + struct State final { + rpl::variable balance; + }; + const auto state = wrap->lifetime().make_state(); + const auto parentController = controller->parentController(); + const auto wrapButton = AddActionButton( + wrap, + tr::lng_manage_peer_bot_balance_credits(), + state->balance.value() | rpl::map(rpl::mappers::_1 > 0), + [=] { parentController->showSection(Info::BotEarn::Make(user)); }, + &st::infoIconBotBalance); + if (const auto balance = user->session().credits().balance(user->id)) { + state->balance = balance; + } + { + const auto api = wrap->lifetime().make_state( + user); + api->request({}, [=](Data::CreditsStatusSlice data) { + state->balance = data.balance; + }); + } + const auto &st = st::infoSharedMediaButton; + const auto button = wrapButton->entity(); + const auto name = Ui::CreateChild(button, st.rightLabel); + const auto icon = user->owner().customEmojiManager().creditsEmoji(); + name->show(); + rpl::combine( + button->widthValue(), + tr::lng_manage_peer_bot_balance_credits(), + 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(QString::number(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); + wrapButton->finishAnimating(); + return state->balance.value(); +} + class DetailsFiller { public: DetailsFiller( @@ -838,9 +971,8 @@ public: object_ptr fill(); private: + void addBalanceActions(not_null user); 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); void addDeleteContactAction(not_null user); @@ -1916,6 +2048,25 @@ ActionsFiller::ActionsFiller( , _peer(peer) { } +void ActionsFiller::addBalanceActions(not_null user) { + const auto wrap = _wrap->add( + object_ptr>( + _wrap.data(), + object_ptr(_wrap.data()))); + const auto inner = wrap->entity(); + Ui::AddSubsectionTitle(inner, tr::lng_manage_peer_bot_balance()); + auto currencyBalance = AddCurrencyAction(user, inner, _controller); + auto creditsBalance = AddCreditsAction(user, inner, _controller); + Ui::AddSkip(inner); + Ui::AddDivider(inner); + Ui::AddSkip(inner); + wrap->toggleOn( + rpl::combine( + std::move(currencyBalance), + std::move(creditsBalance) + ) | rpl::map((rpl::mappers::_1 + rpl::mappers::_2) > 0)); +} + void ActionsFiller::addInviteToGroupAction(not_null user) { const auto notEmpty = [](const QString &value) { return !value.isEmpty(); @@ -1940,130 +2091,6 @@ 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::ChannelEarn::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; - }; - const auto state = _wrap->lifetime().make_state(); - const auto controller = _controller->parentController(); - const auto wrap = AddActionButton( - _wrap, - tr::lng_manage_peer_bot_balance_credits(), - state->balance.value() | rpl::map(rpl::mappers::_1 > 0), - [=] { controller->showSection(Info::BotEarn::Make(user)); }, - &st::infoIconBotBalance); - if (const auto balance = user->session().credits().balance(user->id)) { - state->balance = balance; - } - { - const auto api = _wrap->lifetime().make_state( - user); - api->request({}, [=](Data::CreditsStatusSlice data) { - state->balance = data.balance; - }); - } - const auto &st = st::infoSharedMediaButton; - const auto button = wrap->entity(); - const auto name = Ui::CreateChild(button, st.rightLabel); - name->show(); - rpl::combine( - button->widthValue(), - tr::lng_manage_peer_bot_balance_credits(), - 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( - user->owner().customEmojiManager().creditsEmoji() - .append(QChar(' ')) - .append(QString::number(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::addShareContactAction(not_null user) { const auto controller = _controller->parentController(); AddActionButton( @@ -2285,8 +2312,7 @@ void ActionsFiller::addJoinChannelAction( void ActionsFiller::fillUserActions(not_null user) { if (user->isBot()) { - addCurrencyAction(user); - addCreditsAction(user); + addBalanceActions(user); addInviteToGroupAction(user); } addShareContactAction(user);