Added subsection title to earn buttons in profile of owned bots.

This commit is contained in:
23rd 2024-10-27 10:29:01 +03:00
parent dbb0a5ad28
commit 187139473d

View file

@ -775,6 +775,139 @@ template <typename Text, typename ToggleOn, typename Callback>
st));
}
rpl::producer<uint64> AddCurrencyAction(
not_null<UserData*> user,
not_null<Ui::VerticalLayout*> wrap,
not_null<Controller*> controller) {
struct State final {
rpl::variable<uint64> balance;
};
const auto state = wrap->lifetime().make_state<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<rpl::lifetime>();
const auto currencyLoad
= currencyLoadLifetime->make_state<Api::EarnStatistics>(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<Ui::FlatLabel>(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<uint64> AddCreditsAction(
not_null<UserData*> user,
not_null<Ui::VerticalLayout*> wrap,
not_null<Controller*> controller) {
struct State final {
rpl::variable<uint64> balance;
};
const auto state = wrap->lifetime().make_state<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<Api::CreditsStatus>(
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<Ui::FlatLabel>(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<Ui::RpWidget> fill();
private:
void addBalanceActions(not_null<UserData*> user);
void addInviteToGroupAction(not_null<UserData*> user);
void addCurrencyAction(not_null<UserData*> user);
void addCreditsAction(not_null<UserData*> user);
void addShareContactAction(not_null<UserData*> user);
void addEditContactAction(not_null<UserData*> user);
void addDeleteContactAction(not_null<UserData*> user);
@ -1916,6 +2048,25 @@ ActionsFiller::ActionsFiller(
, _peer(peer) {
}
void ActionsFiller::addBalanceActions(not_null<UserData*> user) {
const auto wrap = _wrap->add(
object_ptr<Ui::SlideWrap<Ui::VerticalLayout>>(
_wrap.data(),
object_ptr<Ui::VerticalLayout>(_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<UserData*> user) {
const auto notEmpty = [](const QString &value) {
return !value.isEmpty();
@ -1940,130 +2091,6 @@ void ActionsFiller::addInviteToGroupAction(not_null<UserData*> user) {
about->finishAnimating();
}
void ActionsFiller::addCurrencyAction(not_null<UserData*> user) {
struct State final {
rpl::variable<uint64> balance;
};
const auto state = _wrap->lifetime().make_state<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<rpl::lifetime>();
const auto currencyLoad
= currencyLoadLifetime->make_state<Api::EarnStatistics>(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<Ui::FlatLabel>(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<UserData*> user) {
struct State final {
rpl::variable<uint64> balance;
};
const auto state = _wrap->lifetime().make_state<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<Api::CreditsStatus>(
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<Ui::FlatLabel>(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<UserData*> user) {
const auto controller = _controller->parentController();
AddActionButton(
@ -2285,8 +2312,7 @@ void ActionsFiller::addJoinChannelAction(
void ActionsFiller::fillUserActions(not_null<UserData*> user) {
if (user->isBot()) {
addCurrencyAction(user);
addCreditsAction(user);
addBalanceActions(user);
addInviteToGroupAction(user);
}
addShareContactAction(user);