From 4f702e12b759331e4402650f68987af25161b453 Mon Sep 17 00:00:00 2001 From: John Preston Date: Mon, 30 Dec 2024 18:39:42 +0400 Subject: [PATCH] Improve upgrade/transfer toasts. --- Telegram/Resources/langs/lang.strings | 2 + Telegram/SourceFiles/boxes/star_gift_box.cpp | 63 ++++++++++++++----- Telegram/SourceFiles/boxes/star_gift_box.h | 7 ++- .../SourceFiles/boxes/transfer_gift_box.cpp | 16 ++++- 4 files changed, 67 insertions(+), 21 deletions(-) diff --git a/Telegram/Resources/langs/lang.strings b/Telegram/Resources/langs/lang.strings index 647d08302..f4a00467a 100644 --- a/Telegram/Resources/langs/lang.strings +++ b/Telegram/Resources/langs/lang.strings @@ -3311,6 +3311,8 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL "lng_gift_upgrade_add_comment" = "Add sender's name and comment"; "lng_gift_upgraded_title" = "Gift Upgraded"; "lng_gift_upgraded_about" = "Your gift {name} now has unique attributes and can be transferred to others"; +"lng_gift_transferred_title" = "Gift Transferred"; +"lng_gift_transferred_about" = "{name} was successfully transferred to {recipient}."; "lng_gift_transfer_title" = "Transfer {name}"; "lng_gift_transfer_via_blockchain" = "Send via Blockchain"; "lng_gift_transfer_unlocks_days#one" = "unlocks in {count} day"; diff --git a/Telegram/SourceFiles/boxes/star_gift_box.cpp b/Telegram/SourceFiles/boxes/star_gift_box.cpp index b84466c1d..515722379 100644 --- a/Telegram/SourceFiles/boxes/star_gift_box.cpp +++ b/Telegram/SourceFiles/boxes/star_gift_box.cpp @@ -1114,7 +1114,7 @@ void SendGift( void ShowGiftUpgradedToast( base::weak_ptr weak, not_null session, - const MTPUpdates & result) { + const MTPUpdates &result) { const auto gift = FindUniqueGift(session, result); if (const auto strong = gift ? weak.get() : nullptr) { strong->showToast({ @@ -1134,7 +1134,7 @@ void SendStarsFormRequest( Settings::SmallBalanceResult result, uint64 formId, MTPInputInvoice invoice, - Fn done) { + Fn done) { using BalanceResult = Settings::SmallBalanceResult; const auto session = &controller->session(); if (result == BalanceResult::Success @@ -1146,20 +1146,20 @@ void SendStarsFormRequest( )).done([=](const MTPpayments_PaymentResult &result) { result.match([&](const MTPDpayments_paymentResult &data) { session->api().applyUpdates(data.vupdates()); - ShowGiftUpgradedToast(weak, session, data.vupdates()); - }, [](const MTPDpayments_paymentVerificationNeeded &data) { + done(Payments::CheckoutResult::Paid, &data.vupdates()); + }, [&](const MTPDpayments_paymentVerificationNeeded &data) { + done(Payments::CheckoutResult::Failed, nullptr); }); - done(Payments::CheckoutResult::Paid); }).fail([=](const MTP::Error &error) { if (const auto strong = weak.get()) { strong->showToast(error.type()); } - done(Payments::CheckoutResult::Failed); + done(Payments::CheckoutResult::Failed, nullptr); }).send(); } else if (result == BalanceResult::Cancelled) { - done(Payments::CheckoutResult::Cancelled); + done(Payments::CheckoutResult::Cancelled, nullptr); } else { - done(Payments::CheckoutResult::Failed); + done(Payments::CheckoutResult::Failed, nullptr); } } @@ -1170,21 +1170,30 @@ void UpgradeGift( int stars, Fn done) { const auto session = &window->session(); + const auto weak = base::make_weak(window); + auto formDone = [=]( + Payments::CheckoutResult result, + const MTPUpdates *updates) { + if (result == Payments::CheckoutResult::Paid && updates) { + if (const auto strong = weak.get()) { + ShowGiftUpgradedToast(strong, session, *updates); + } + } + done(result); + }; if (stars <= 0) { using Flag = MTPpayments_UpgradeStarGift::Flag; - const auto weak = base::make_weak(window); session->api().request(MTPpayments_UpgradeStarGift( MTP_flags(keepDetails ? Flag::f_keep_original_details : Flag()), MTP_int(messageId.bare) )).done([=](const MTPUpdates &result) { session->api().applyUpdates(result); - ShowGiftUpgradedToast(weak, session, result); - done(Payments::CheckoutResult::Paid); + formDone(Payments::CheckoutResult::Paid, &result); }).fail([=](const MTP::Error &error) { if (const auto strong = weak.get()) { strong->showToast(error.type()); } - done(Payments::CheckoutResult::Failed); + formDone(Payments::CheckoutResult::Failed, nullptr); }).send(); return; } @@ -1194,7 +1203,7 @@ void UpgradeGift( MTP_inputInvoiceStarGiftUpgrade( MTP_flags(keepDetails ? Flag::f_keep_original_details : Flag()), MTP_int(messageId.bare)), - std::move(done)); + std::move(formDone)); } void SoldOutBox( @@ -2295,7 +2304,7 @@ void AddUniqueCloseButton(not_null box) { void RequestStarsFormAndSubmit( not_null window, MTPInputInvoice invoice, - Fn done) { + Fn done) { const auto weak = base::make_weak(window); window->session().api().request(MTPpayments_GetPaymentForm( MTP_flags(0), @@ -2307,7 +2316,7 @@ void RequestStarsFormAndSubmit( const auto prices = data.vinvoice().data().vprices().v; const auto strong = weak.get(); if (!strong) { - done(Payments::CheckoutResult::Failed); + done(Payments::CheckoutResult::Failed, nullptr); return; } const auto ready = [=](Settings::SmallBalanceResult result) { @@ -2319,14 +2328,34 @@ void RequestStarsFormAndSubmit( Settings::SmallBalanceDeepLink{}, ready); }, [&](const auto &) { - done(Payments::CheckoutResult::Failed); + done(Payments::CheckoutResult::Failed, nullptr); }); }).fail([=](const MTP::Error &error) { if (const auto strong = weak.get()) { strong->showToast(error.type()); } - done(Payments::CheckoutResult::Failed); + done(Payments::CheckoutResult::Failed, nullptr); }).send(); } +void ShowGiftTransferredToast( + base::weak_ptr weak, + not_null to, + const MTPUpdates &result) { + const auto gift = FindUniqueGift(&to->session(), result); + if (const auto strong = gift ? weak.get() : nullptr) { + strong->showToast({ + .title = tr::lng_gift_transferred_title(tr::now), + .text = tr::lng_gift_transferred_about( + tr::now, + lt_name, + Text::Bold(Data::UniqueGiftName(*gift)), + lt_recipient, + Text::Bold(to->shortName()), + Ui::Text::WithEntities), + .duration = kUpgradeDoneToastDuration, + }); + } +} + } // namespace Ui diff --git a/Telegram/SourceFiles/boxes/star_gift_box.h b/Telegram/SourceFiles/boxes/star_gift_box.h index d8789bb39..4aa4b5f10 100644 --- a/Telegram/SourceFiles/boxes/star_gift_box.h +++ b/Telegram/SourceFiles/boxes/star_gift_box.h @@ -67,6 +67,11 @@ void AddUniqueCloseButton(not_null box); void RequestStarsFormAndSubmit( not_null window, MTPInputInvoice invoice, - Fn done); + Fn done); + +void ShowGiftTransferredToast( + base::weak_ptr weak, + not_null to, + const MTPUpdates &result); } // namespace Ui diff --git a/Telegram/SourceFiles/boxes/transfer_gift_box.cpp b/Telegram/SourceFiles/boxes/transfer_gift_box.cpp index 190362d09..4406403f2 100644 --- a/Telegram/SourceFiles/boxes/transfer_gift_box.cpp +++ b/Telegram/SourceFiles/boxes/transfer_gift_box.cpp @@ -290,18 +290,28 @@ void TransferGift( const auto session = &window->session(); const auto weak = base::make_weak(window); + auto formDone = [=]( + Payments::CheckoutResult result, + const MTPUpdates *updates) { + if (result == Payments::CheckoutResult::Paid && updates) { + if (const auto strong = weak.get()) { + Ui::ShowGiftTransferredToast(strong, to, *updates); + } + } + done(result); + }; if (gift->starsForTransfer <= 0) { session->api().request(MTPpayments_TransferStarGift( MTP_int(messageId.bare), to->asUser()->inputUser )).done([=](const MTPUpdates &result) { session->api().applyUpdates(result); - done(Payments::CheckoutResult::Paid); + formDone(Payments::CheckoutResult::Paid, &result); }).fail([=](const MTP::Error &error) { if (const auto strong = weak.get()) { strong->showToast(error.type()); } - done(Payments::CheckoutResult::Failed); + formDone(Payments::CheckoutResult::Failed, nullptr); }).send(); return; } @@ -310,7 +320,7 @@ void TransferGift( MTP_inputInvoiceStarGiftTransfer( MTP_int(messageId.bare), to->asUser()->inputUser), - std::move(done)); + std::move(formDone)); } void ShowTransferToBox(