Added handler to subscribe box when balance is too small.

This commit is contained in:
23rd 2024-08-09 16:26:09 +03:00 committed by John Preston
parent a13f0cb11e
commit 92f70a0ebb
4 changed files with 74 additions and 18 deletions

View file

@ -2418,6 +2418,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
"lng_credits_small_balance_title#other" = "{count} Stars Needed";
"lng_credits_small_balance_about" = "Buy **Stars** and use them on **{bot}** and other miniapps.";
"lng_credits_small_balance_reaction" = "Buy **Stars** and send them to {channel} to support their posts.";
"lng_credits_small_balance_subscribe" = "Buy **Stars** and subscribe to **{channel}** and other channels.";
"lng_credits_purchase_blocked" = "Sorry, you can't purchase this item with Telegram Stars.";
"lng_credits_gift_title" = "Gift Telegram Stars";

View file

@ -10,6 +10,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include "apiwrap.h"
#include "boxes/premium_limits_box.h"
#include "core/application.h"
#include "data/components/credits.h"
#include "data/data_channel.h"
#include "data/data_file_origin.h"
#include "data/data_forum.h"
@ -21,6 +22,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include "info/profile/info_profile_badge.h"
#include "lang/lang_keys.h"
#include "main/main_session.h"
#include "settings/settings_credits_graphics.h"
#include "ui/boxes/confirm_box.h"
#include "ui/controls/userpic_button.h"
#include "ui/effects/premium_graphics.h"
@ -35,6 +37,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include "window/window_session_controller.h"
#include "styles/style_boxes.h"
#include "styles/style_chat_helpers.h"
#include "styles/style_credits.h"
#include "styles/style_info.h"
#include "styles/style_layers.h"
#include "styles/style_premium.h"
@ -247,15 +250,26 @@ void ConfirmSubscriptionBox(
Ui::Text::RichLangValue),
st::inviteLinkSubscribeBoxTerms)));
auto confirmText = tr::lng_channel_invite_subscription_button();
const auto weak = Ui::MakeWeak(box);
state->saveButton = box->addButton(std::move(confirmText), [=] {
if (state->api) {
return;
}
state->api.emplace(&session->mtp());
state->loading.force_assign(true);
{
const auto balance = Settings::AddBalanceWidget(
content,
session->credits().balanceValue(),
true);
session->credits().load(true);
rpl::combine(
balance->sizeValue(),
content->sizeValue()
) | rpl::start_with_next([=](const QSize &, const QSize &) {
balance->moveToRight(
st::creditsHistoryRightSkip * 2,
st::creditsHistoryRightSkip);
balance->update();
}, balance->lifetime());
}
const auto sendCredits = [=, weak = Ui::MakeWeak(box)] {
const auto show = box->uiShow();
const auto buttonWidth = state->saveButton
? state->saveButton->width()
: 0;
@ -264,22 +278,52 @@ void ConfirmSubscriptionBox(
MTP_flags(0),
MTP_long(formId),
MTP_inputInvoiceChatInviteSubscription(MTP_string(hash)))
).done([=](auto result) {
).done([=](const MTPpayments_PaymentResult &result) {
state->api = std::nullopt;
state->loading.force_assign(false);
result.match([&](const MTPDpayments_paymentResult &data) {
session->api().applyUpdates(data.vupdates());
}, [](const MTPDpayments_paymentVerificationNeeded &data) {
});
if (weak) {
state->api = std::nullopt;
box->closeBox();
}
}).fail([=, show = box->uiShow()](const MTP::Error &error) {
}).fail([=](const MTP::Error &error) {
const auto id = error.type();
if (weak) {
state->api = std::nullopt;
}
show->showToast(id);
state->loading.force_assign(false);
}).send();
if (state->saveButton) {
state->saveButton->resizeToWidth(buttonWidth);
}
};
auto confirmText = tr::lng_channel_invite_subscription_button();
state->saveButton = box->addButton(std::move(confirmText), [=] {
if (state->api) {
return;
}
state->api.emplace(&session->mtp());
state->loading.force_assign(true);
const auto done = [=](Settings::SmallBalanceResult result) {
if (result == Settings::SmallBalanceResult::Success) {
sendCredits();
} else {
state->api = std::nullopt;
state->loading.force_assign(false);
}
};
Settings::MaybeRequestBalanceIncrease(
Main::MakeSessionShow(box->uiShow(), session),
amount,
Settings::SmallBalanceSubscription{ .name = name },
done);
});
if (const auto saveButton = state->saveButton) {
using namespace Info::Statistics;
const auto loadingAnimation = InfiniteRadialAnimationWidget(

View file

@ -877,10 +877,12 @@ void SmallBalanceBox(
};
const auto owner = &show->session().data();
const auto peer = v::match(source, [&](SmallBalanceBot value) {
return owner->peer(peerFromUser(value.botId));
const auto name = v::match(source, [&](SmallBalanceBot value) {
return owner->peer(peerFromUser(value.botId))->name();
}, [&](SmallBalanceReaction value) {
return owner->peer(peerFromChannel(value.channelId));
return owner->peer(peerFromChannel(value.channelId))->name();
}, [](SmallBalanceSubscription value) {
return value.name;
});
auto needed = show->session().credits().balanceValue(
@ -897,14 +899,19 @@ void SmallBalanceBox(
rpl::duplicate(
needed
) | rpl::filter(rpl::mappers::_1 > 0) | tr::to_count()),
.about = (peer->isBroadcast()
.about = (v::is<SmallBalanceSubscription>(source)
? tr::lng_credits_small_balance_subscribe(
lt_channel,
rpl::single(Ui::Text::Bold(name)),
Ui::Text::RichLangValue)
: v::is<SmallBalanceReaction>(source)
? tr::lng_credits_small_balance_reaction(
lt_channel,
rpl::single(Ui::Text::Bold(peer->name())),
rpl::single(Ui::Text::Bold(name)),
Ui::Text::RichLangValue)
: tr::lng_credits_small_balance_about(
lt_bot,
rpl::single(TextWithEntities{ peer->name() }),
rpl::single(TextWithEntities{ name }),
Ui::Text::RichLangValue)),
.light = true,
.gradientStops = Ui::Premium::CreditsIconGradientStops(),

View file

@ -93,9 +93,13 @@ struct SmallBalanceBot {
struct SmallBalanceReaction {
ChannelId channelId = 0;
};
struct SmallBalanceSubscription {
QString name;
};
struct SmallBalanceSource : std::variant<
SmallBalanceBot,
SmallBalanceReaction> {
SmallBalanceReaction,
SmallBalanceSubscription> {
using variant::variant;
};