From 3dd894ad30936f7a3deeb456715abe41316b5be2 Mon Sep 17 00:00:00 2001 From: 23rd <23rd@vivaldi.net> Date: Fri, 24 May 2024 20:48:01 +0300 Subject: [PATCH] Improved SendCreditsBox for data from credits payment form. --- Telegram/SourceFiles/api/api_bot.cpp | 2 +- .../SourceFiles/boxes/send_credits_box.cpp | 64 ++++++++++++++----- Telegram/SourceFiles/boxes/send_credits_box.h | 6 +- 3 files changed, 55 insertions(+), 17 deletions(-) diff --git a/Telegram/SourceFiles/api/api_bot.cpp b/Telegram/SourceFiles/api/api_bot.cpp index befdc04e5..f4331cd1f 100644 --- a/Telegram/SourceFiles/api/api_bot.cpp +++ b/Telegram/SourceFiles/api/api_bot.cpp @@ -332,7 +332,7 @@ void ActivateBotCommand(ClickHandlerContext context, int row, int column) { case ButtonType::Buy: { if (Ui::IsCreditsInvoice(item)) { - controller->uiShow()->show(Box(Ui::SendCreditsBox, item)); + // controller->uiShow()->show(Box(Ui::SendCreditsBox, item)); } else { Payments::CheckoutProcess::Start( item, diff --git a/Telegram/SourceFiles/boxes/send_credits_box.cpp b/Telegram/SourceFiles/boxes/send_credits_box.cpp index ae45850bd..743acd5d7 100644 --- a/Telegram/SourceFiles/boxes/send_credits_box.cpp +++ b/Telegram/SourceFiles/boxes/send_credits_box.cpp @@ -8,6 +8,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "boxes/send_credits_box.h" #include "api/api_credits.h" +#include "apiwrap.h" #include "core/ui_integration.h" // Core::MarkedTextContext. #include "data/data_credits.h" #include "data/data_file_origin.h" @@ -17,8 +18,11 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "data/stickers/data_custom_emoji.h" #include "history/history.h" #include "history/history_item.h" +#include "info/channel_statistics/boosts/giveaway/boost_badge.h" // InfiniteRadialAnimationWidget. #include "lang/lang_keys.h" #include "main/main_session.h" +#include "payments/payments_checkout_process.h" +#include "payments/payments_form.h" #include "payments/payments_form.h" #include "settings/settings_credits.h" #include "ui/controls/userpic_button.h" @@ -38,21 +42,21 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "styles/style_settings.h" namespace Ui { -namespace { -} // namespace void SendCreditsBox( not_null box, - not_null item) { - const auto media = item->media(); - const auto invoice = media ? media->invoice() : nullptr; - if (!invoice) { + std::shared_ptr form) { + if (!form) { return; } + struct State { + rpl::variable confirmButtonBusy = false; + }; + const auto state = box->lifetime().make_state(); box->setStyle(st::giveawayGiftCodeBox); box->setNoContentMargin(true); - const auto session = &item->history()->owner().session(); + const auto session = form->invoice.session; const auto photoSize = st::defaultUserpicButton.photoSize; @@ -87,7 +91,9 @@ void SendCreditsBox( }, ministarsContainer->lifetime()); } - if (false && invoice->photo) { + const auto bot = session->data().user(form->botId); + + if (form->photo) { struct State { std::shared_ptr view; Image *image = nullptr; @@ -98,8 +104,8 @@ void SendCreditsBox( object_ptr>( content, object_ptr(content)))->entity(); - state->view = invoice->photo->createMediaView(); - state->view->wanted(Data::PhotoSize::Large, item->fullId()); + state->view = form->photo->createMediaView(); + form->photo->load(Data::PhotoSize::Thumbnail, {}); widget->resize(Size(photoSize)); @@ -135,7 +141,7 @@ void SendCreditsBox( content, object_ptr( content, - item->author(), + bot, st::defaultUserpicButton))); widget->setAttribute(Qt::WA_TransparentForMouseEvents); } @@ -156,18 +162,42 @@ void SendCreditsBox( box, tr::lng_credits_box_out_sure( lt_count, - rpl::single(invoice->amount) | tr::to_count(), + rpl::single(form->invoice.amount) | tr::to_count(), lt_text, - rpl::single(TextWithEntities{ invoice->title }), + rpl::single(TextWithEntities{ form->title }), lt_bot, - rpl::single(TextWithEntities{ item->author()->name() }), + rpl::single(TextWithEntities{ bot->name() }), Ui::Text::RichLangValue), st::creditsBoxAbout))); Ui::AddSkip(content); Ui::AddSkip(content); const auto button = box->addButton(rpl::single(QString()), [=] { + if (state->confirmButtonBusy.current()) { + return; + } + state->confirmButtonBusy = true; + session->api().request( + MTPpayments_SendStarsForm( + MTP_flags(0), + MTP_long(form->formId), + form->inputInvoice) + ).done([=](auto result) { + state->confirmButtonBusy = false; + box->closeBox(); + }).fail([=](const MTP::Error &error) { + state->confirmButtonBusy = false; + box->uiShow()->showToast(error.type()); + }).send(); }); + { + using namespace Info::Statistics; + const auto loadingAnimation = InfiniteRadialAnimationWidget( + button, + st::giveawayGiftCodeStartButton.height / 2); + AddChildToWidgetCenter(button.data(), loadingAnimation); + loadingAnimation->showOn(state->confirmButtonBusy.value()); + } { const auto emojiMargin = QMargins( 0, @@ -181,7 +211,7 @@ void SendCreditsBox( true)); auto buttonText = tr::lng_credits_box_out_confirm( lt_count, - rpl::single(invoice->amount) | tr::to_count(), + rpl::single(form->invoice.amount) | tr::to_count(), lt_emoji, rpl::single(buttonEmoji), Ui::Text::RichLangValue); @@ -208,6 +238,10 @@ void SendCreditsBox( (size.height() - buttonLabel->height()) / 2); }, buttonLabel->lifetime()); buttonLabel->setAttribute(Qt::WA_TransparentForMouseEvents); + state->confirmButtonBusy.value( + ) | rpl::start_with_next([=](bool busy) { + buttonLabel->setVisible(!busy); + }, buttonLabel->lifetime()); } const auto buttonWidth = st::boxWidth diff --git a/Telegram/SourceFiles/boxes/send_credits_box.h b/Telegram/SourceFiles/boxes/send_credits_box.h index fa877e305..37a093ca9 100644 --- a/Telegram/SourceFiles/boxes/send_credits_box.h +++ b/Telegram/SourceFiles/boxes/send_credits_box.h @@ -9,13 +9,17 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL class HistoryItem; +namespace Payments { +struct CreditsFormData; +} // namespace Payments + namespace Ui { class GenericBox; void SendCreditsBox( not_null box, - not_null item); + std::shared_ptr data); [[nodiscard]] bool IsCreditsInvoice(not_null item);