Improve upgrade/transfer toasts.

This commit is contained in:
John Preston 2024-12-30 18:39:42 +04:00
parent 083400d1c2
commit 4f702e12b7
4 changed files with 67 additions and 21 deletions

View file

@ -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_upgrade_add_comment" = "Add sender's name and comment";
"lng_gift_upgraded_title" = "Gift Upgraded"; "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_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_title" = "Transfer {name}";
"lng_gift_transfer_via_blockchain" = "Send via Blockchain"; "lng_gift_transfer_via_blockchain" = "Send via Blockchain";
"lng_gift_transfer_unlocks_days#one" = "unlocks in {count} day"; "lng_gift_transfer_unlocks_days#one" = "unlocks in {count} day";

View file

@ -1114,7 +1114,7 @@ void SendGift(
void ShowGiftUpgradedToast( void ShowGiftUpgradedToast(
base::weak_ptr<Window::SessionController> weak, base::weak_ptr<Window::SessionController> weak,
not_null<Main::Session*> session, not_null<Main::Session*> session,
const MTPUpdates & result) { const MTPUpdates &result) {
const auto gift = FindUniqueGift(session, result); const auto gift = FindUniqueGift(session, result);
if (const auto strong = gift ? weak.get() : nullptr) { if (const auto strong = gift ? weak.get() : nullptr) {
strong->showToast({ strong->showToast({
@ -1134,7 +1134,7 @@ void SendStarsFormRequest(
Settings::SmallBalanceResult result, Settings::SmallBalanceResult result,
uint64 formId, uint64 formId,
MTPInputInvoice invoice, MTPInputInvoice invoice,
Fn<void(Payments::CheckoutResult)> done) { Fn<void(Payments::CheckoutResult, const MTPUpdates *)> done) {
using BalanceResult = Settings::SmallBalanceResult; using BalanceResult = Settings::SmallBalanceResult;
const auto session = &controller->session(); const auto session = &controller->session();
if (result == BalanceResult::Success if (result == BalanceResult::Success
@ -1146,20 +1146,20 @@ void SendStarsFormRequest(
)).done([=](const MTPpayments_PaymentResult &result) { )).done([=](const MTPpayments_PaymentResult &result) {
result.match([&](const MTPDpayments_paymentResult &data) { result.match([&](const MTPDpayments_paymentResult &data) {
session->api().applyUpdates(data.vupdates()); session->api().applyUpdates(data.vupdates());
ShowGiftUpgradedToast(weak, session, data.vupdates()); done(Payments::CheckoutResult::Paid, &data.vupdates());
}, [](const MTPDpayments_paymentVerificationNeeded &data) { }, [&](const MTPDpayments_paymentVerificationNeeded &data) {
done(Payments::CheckoutResult::Failed, nullptr);
}); });
done(Payments::CheckoutResult::Paid);
}).fail([=](const MTP::Error &error) { }).fail([=](const MTP::Error &error) {
if (const auto strong = weak.get()) { if (const auto strong = weak.get()) {
strong->showToast(error.type()); strong->showToast(error.type());
} }
done(Payments::CheckoutResult::Failed); done(Payments::CheckoutResult::Failed, nullptr);
}).send(); }).send();
} else if (result == BalanceResult::Cancelled) { } else if (result == BalanceResult::Cancelled) {
done(Payments::CheckoutResult::Cancelled); done(Payments::CheckoutResult::Cancelled, nullptr);
} else { } else {
done(Payments::CheckoutResult::Failed); done(Payments::CheckoutResult::Failed, nullptr);
} }
} }
@ -1170,21 +1170,30 @@ void UpgradeGift(
int stars, int stars,
Fn<void(Payments::CheckoutResult)> done) { Fn<void(Payments::CheckoutResult)> done) {
const auto session = &window->session(); 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) { if (stars <= 0) {
using Flag = MTPpayments_UpgradeStarGift::Flag; using Flag = MTPpayments_UpgradeStarGift::Flag;
const auto weak = base::make_weak(window);
session->api().request(MTPpayments_UpgradeStarGift( session->api().request(MTPpayments_UpgradeStarGift(
MTP_flags(keepDetails ? Flag::f_keep_original_details : Flag()), MTP_flags(keepDetails ? Flag::f_keep_original_details : Flag()),
MTP_int(messageId.bare) MTP_int(messageId.bare)
)).done([=](const MTPUpdates &result) { )).done([=](const MTPUpdates &result) {
session->api().applyUpdates(result); session->api().applyUpdates(result);
ShowGiftUpgradedToast(weak, session, result); formDone(Payments::CheckoutResult::Paid, &result);
done(Payments::CheckoutResult::Paid);
}).fail([=](const MTP::Error &error) { }).fail([=](const MTP::Error &error) {
if (const auto strong = weak.get()) { if (const auto strong = weak.get()) {
strong->showToast(error.type()); strong->showToast(error.type());
} }
done(Payments::CheckoutResult::Failed); formDone(Payments::CheckoutResult::Failed, nullptr);
}).send(); }).send();
return; return;
} }
@ -1194,7 +1203,7 @@ void UpgradeGift(
MTP_inputInvoiceStarGiftUpgrade( MTP_inputInvoiceStarGiftUpgrade(
MTP_flags(keepDetails ? Flag::f_keep_original_details : Flag()), MTP_flags(keepDetails ? Flag::f_keep_original_details : Flag()),
MTP_int(messageId.bare)), MTP_int(messageId.bare)),
std::move(done)); std::move(formDone));
} }
void SoldOutBox( void SoldOutBox(
@ -2295,7 +2304,7 @@ void AddUniqueCloseButton(not_null<GenericBox*> box) {
void RequestStarsFormAndSubmit( void RequestStarsFormAndSubmit(
not_null<Window::SessionController*> window, not_null<Window::SessionController*> window,
MTPInputInvoice invoice, MTPInputInvoice invoice,
Fn<void(Payments::CheckoutResult)> done) { Fn<void(Payments::CheckoutResult, const MTPUpdates *)> done) {
const auto weak = base::make_weak(window); const auto weak = base::make_weak(window);
window->session().api().request(MTPpayments_GetPaymentForm( window->session().api().request(MTPpayments_GetPaymentForm(
MTP_flags(0), MTP_flags(0),
@ -2307,7 +2316,7 @@ void RequestStarsFormAndSubmit(
const auto prices = data.vinvoice().data().vprices().v; const auto prices = data.vinvoice().data().vprices().v;
const auto strong = weak.get(); const auto strong = weak.get();
if (!strong) { if (!strong) {
done(Payments::CheckoutResult::Failed); done(Payments::CheckoutResult::Failed, nullptr);
return; return;
} }
const auto ready = [=](Settings::SmallBalanceResult result) { const auto ready = [=](Settings::SmallBalanceResult result) {
@ -2319,14 +2328,34 @@ void RequestStarsFormAndSubmit(
Settings::SmallBalanceDeepLink{}, Settings::SmallBalanceDeepLink{},
ready); ready);
}, [&](const auto &) { }, [&](const auto &) {
done(Payments::CheckoutResult::Failed); done(Payments::CheckoutResult::Failed, nullptr);
}); });
}).fail([=](const MTP::Error &error) { }).fail([=](const MTP::Error &error) {
if (const auto strong = weak.get()) { if (const auto strong = weak.get()) {
strong->showToast(error.type()); strong->showToast(error.type());
} }
done(Payments::CheckoutResult::Failed); done(Payments::CheckoutResult::Failed, nullptr);
}).send(); }).send();
} }
void ShowGiftTransferredToast(
base::weak_ptr<Window::SessionController> weak,
not_null<PeerData*> 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 } // namespace Ui

View file

@ -67,6 +67,11 @@ void AddUniqueCloseButton(not_null<GenericBox*> box);
void RequestStarsFormAndSubmit( void RequestStarsFormAndSubmit(
not_null<Window::SessionController*> window, not_null<Window::SessionController*> window,
MTPInputInvoice invoice, MTPInputInvoice invoice,
Fn<void(Payments::CheckoutResult)> done); Fn<void(Payments::CheckoutResult, const MTPUpdates *)> done);
void ShowGiftTransferredToast(
base::weak_ptr<Window::SessionController> weak,
not_null<PeerData*> to,
const MTPUpdates &result);
} // namespace Ui } // namespace Ui

View file

@ -290,18 +290,28 @@ void TransferGift(
const auto session = &window->session(); const auto session = &window->session();
const auto weak = base::make_weak(window); 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) { if (gift->starsForTransfer <= 0) {
session->api().request(MTPpayments_TransferStarGift( session->api().request(MTPpayments_TransferStarGift(
MTP_int(messageId.bare), MTP_int(messageId.bare),
to->asUser()->inputUser to->asUser()->inputUser
)).done([=](const MTPUpdates &result) { )).done([=](const MTPUpdates &result) {
session->api().applyUpdates(result); session->api().applyUpdates(result);
done(Payments::CheckoutResult::Paid); formDone(Payments::CheckoutResult::Paid, &result);
}).fail([=](const MTP::Error &error) { }).fail([=](const MTP::Error &error) {
if (const auto strong = weak.get()) { if (const auto strong = weak.get()) {
strong->showToast(error.type()); strong->showToast(error.type());
} }
done(Payments::CheckoutResult::Failed); formDone(Payments::CheckoutResult::Failed, nullptr);
}).send(); }).send();
return; return;
} }
@ -310,7 +320,7 @@ void TransferGift(
MTP_inputInvoiceStarGiftTransfer( MTP_inputInvoiceStarGiftTransfer(
MTP_int(messageId.bare), MTP_int(messageId.bare),
to->asUser()->inputUser), to->asUser()->inputUser),
std::move(done)); std::move(formDone));
} }
void ShowTransferToBox( void ShowTransferToBox(