From d1be7c1ff7f1cb08d416a0db2c9649fbf3c833c9 Mon Sep 17 00:00:00 2001 From: 23rd <23rd@vivaldi.net> Date: Thu, 11 Apr 2024 19:35:06 +0300 Subject: [PATCH] Simplified management of PasscodeBox within customCheckCallback. --- Telegram/SourceFiles/api/api_bot.cpp | 13 +++++------ Telegram/SourceFiles/api/api_earn.cpp | 22 +++++++++++++------ Telegram/SourceFiles/boxes/passcode_box.cpp | 2 +- Telegram/SourceFiles/boxes/passcode_box.h | 5 ++++- .../boxes/peers/edit_participant_box.cpp | 10 ++++----- .../payments/payments_checkout_process.cpp | 8 +++---- 6 files changed, 34 insertions(+), 26 deletions(-) diff --git a/Telegram/SourceFiles/api/api_bot.cpp b/Telegram/SourceFiles/api/api_bot.cpp index 706b7a7cb7..0284ae719d 100644 --- a/Telegram/SourceFiles/api/api_bot.cpp +++ b/Telegram/SourceFiles/api/api_bot.cpp @@ -236,14 +236,14 @@ void SendBotCallbackDataWithPassword( } else { return; } - const auto box = std::make_shared>(); auto fields = PasscodeBox::CloudFields::From(state); fields.customTitle = tr::lng_bots_password_confirm_title(); fields.customDescription = tr::lng_bots_password_confirm_description(tr::now); fields.customSubmitButton = tr::lng_passcode_submit(); fields.customCheckCallback = [=]( - const Core::CloudPasswordResult &result) { + const Core::CloudPasswordResult &result, + QPointer box) { if (const auto button = getButton()) { if (button->requestId) { return; @@ -257,18 +257,17 @@ void SendBotCallbackDataWithPassword( return; } SendBotCallbackData(strongController, item, row, column, result, [=] { - if (*box) { - (*box)->closeBox(); + if (box) { + box->closeBox(); } }, [=](const QString &error) { - if (*box) { - (*box)->handleCustomCheckError(error); + if (box) { + box->handleCustomCheckError(error); } }); } }; auto object = Box(session, fields); - *box = Ui::MakeWeak(object.data()); show->showBox(std::move(object), Ui::LayerOption::CloseOther); }, *lifetime); } diff --git a/Telegram/SourceFiles/api/api_earn.cpp b/Telegram/SourceFiles/api/api_earn.cpp index 61cfaf10f6..d6425ef69b 100644 --- a/Telegram/SourceFiles/api/api_earn.cpp +++ b/Telegram/SourceFiles/api/api_earn.cpp @@ -64,19 +64,27 @@ void HandleWithdrawalButton( = tr::lng_channel_earn_balance_password_description(tr::now); fields.customSubmitButton = tr::lng_passcode_submit(); fields.customCheckCallback = crl::guard(button, [=]( - const Core::CloudPasswordResult &result) { + const Core::CloudPasswordResult &result, + QPointer box) { + const auto done = [=](const QString &result) { + if (!result.isEmpty()) { + UrlClickHandler::Open(result); + if (box) { + box->closeBox(); + } + } + }; + const auto fail = [=](const QString &error) { + show->showToast(error); + }; session->api().request( MTPstats_GetBroadcastRevenueWithdrawalUrl( channel->inputChannel, result.result )).done([=](const MTPstats_BroadcastRevenueWithdrawalUrl &r) { - const auto url = qs(r.data().vurl()); - - if (!url.isEmpty()) { - UrlClickHandler::Open(url); - } + done(qs(r.data().vurl())); }).fail([=](const MTP::Error &error) { - show->showToast(error.type()); + fail(error.type()); }).send(); }); show->show(Box(session, fields)); diff --git a/Telegram/SourceFiles/boxes/passcode_box.cpp b/Telegram/SourceFiles/boxes/passcode_box.cpp index e320b434ff..840e4872d4 100644 --- a/Telegram/SourceFiles/boxes/passcode_box.cpp +++ b/Telegram/SourceFiles/boxes/passcode_box.cpp @@ -740,7 +740,7 @@ void PasscodeBox::submitOnlyCheckCloudPassword(const QString &oldPassword) { void PasscodeBox::sendOnlyCheckCloudPassword(const QString &oldPassword) { checkPassword(oldPassword, [=](const Core::CloudPasswordResult &check) { if (const auto onstack = _cloudFields.customCheckCallback) { - onstack(check); + onstack(check, Ui::MakeWeak(this)); } else { Assert(_cloudFields.turningOff); sendClearCloudPassword(check); diff --git a/Telegram/SourceFiles/boxes/passcode_box.h b/Telegram/SourceFiles/boxes/passcode_box.h index 684da859ae..b651cda64a 100644 --- a/Telegram/SourceFiles/boxes/passcode_box.h +++ b/Telegram/SourceFiles/boxes/passcode_box.h @@ -51,7 +51,10 @@ public: TimeId pendingResetDate = 0; // Check cloud password for some action. - Fn customCheckCallback; + using CustomCheck = Fn)>; + CustomCheck customCheckCallback; rpl::producer customTitle; std::optional customDescription; rpl::producer customSubmitButton; diff --git a/Telegram/SourceFiles/boxes/peers/edit_participant_box.cpp b/Telegram/SourceFiles/boxes/peers/edit_participant_box.cpp index c1c086ad14..39389d1d56 100644 --- a/Telegram/SourceFiles/boxes/peers/edit_participant_box.cpp +++ b/Telegram/SourceFiles/boxes/peers/edit_participant_box.cpp @@ -598,19 +598,17 @@ void EditAdminBox::requestTransferPassword(not_null channel) { ) | rpl::take( 1 ) | rpl::start_with_next([=](const Core::CloudPasswordState &state) { - const auto box = std::make_shared>(); auto fields = PasscodeBox::CloudFields::From(state); fields.customTitle = tr::lng_rights_transfer_password_title(); fields.customDescription = tr::lng_rights_transfer_password_description(tr::now); fields.customSubmitButton = tr::lng_passcode_submit(); fields.customCheckCallback = crl::guard(this, [=]( - const Core::CloudPasswordResult &result) { - sendTransferRequestFrom(*box, channel, result); + const Core::CloudPasswordResult &result, + QPointer box) { + sendTransferRequestFrom(box, channel, result); }); - *box = getDelegate()->show(Box( - &channel->session(), - fields)); + getDelegate()->show(Box(&channel->session(), fields)); }, lifetime()); } diff --git a/Telegram/SourceFiles/payments/payments_checkout_process.cpp b/Telegram/SourceFiles/payments/payments_checkout_process.cpp index b9e1b631dd..ad72996648 100644 --- a/Telegram/SourceFiles/payments/payments_checkout_process.cpp +++ b/Telegram/SourceFiles/payments/payments_checkout_process.cpp @@ -766,12 +766,12 @@ void CheckoutProcess::requestPassword() { (index < list.size()) ? list[index].title : QString()); fields.customSubmitButton = tr::lng_payments_password_submit(); fields.customCheckCallback = [=]( - const Core::CloudPasswordResult &result) { + const Core::CloudPasswordResult &result, + QPointer box) { + _enterPasswordBox = box; _form->submit(result); }; - auto owned = Box(_session, fields); - _enterPasswordBox = owned.data(); - _panel->showBox(std::move(owned)); + _panel->showBox(Box(_session, fields)); }); }