From ebcd4eb97b825d827bfb7d2af625b1d59d7c12ad Mon Sep 17 00:00:00 2001 From: 23rd <23rd@vivaldi.net> Date: Tue, 7 Nov 2023 05:35:28 +0300 Subject: [PATCH] Added box for pending giftcodes. --- Telegram/Resources/langs/lang.strings | 3 + .../SourceFiles/boxes/gift_premium_box.cpp | 238 +++++++++++++----- Telegram/SourceFiles/boxes/gift_premium_box.h | 4 + .../info/boosts/info_boosts_inner_widget.cpp | 20 +- 4 files changed, 202 insertions(+), 63 deletions(-) diff --git a/Telegram/Resources/langs/lang.strings b/Telegram/Resources/langs/lang.strings index 979b9c783..6f61de92d 100644 --- a/Telegram/Resources/langs/lang.strings +++ b/Telegram/Resources/langs/lang.strings @@ -2199,6 +2199,9 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL "lng_gift_link_used_about" = "This link was used to activate\na **Telegram Premium** subscription."; "lng_gift_link_used_footer" = "This link was used on {date}."; "lng_gift_link_expired" = "Gift code link expired"; +"lng_gift_link_pending_about" = "This link allows {user} to activate\na **Telegram Premium** subscription."; +"lng_gift_link_pending_toast" = "Only the recipient can see the link."; +"lng_gift_link_pending_footer" = "This link hasn't been activated yet."; "lng_accounts_limit_title" = "Limit Reached"; "lng_accounts_limit1#one" = "You have reached the limit of **{count}** connected accounts."; diff --git a/Telegram/SourceFiles/boxes/gift_premium_box.cpp b/Telegram/SourceFiles/boxes/gift_premium_box.cpp index 9a87a58fc..e7f2dab7b 100644 --- a/Telegram/SourceFiles/boxes/gift_premium_box.cpp +++ b/Telegram/SourceFiles/boxes/gift_premium_box.cpp @@ -7,9 +7,9 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL */ #include "boxes/gift_premium_box.h" -#include "apiwrap.h" #include "api/api_premium.h" #include "api/api_premium_option.h" +#include "apiwrap.h" #include "base/unixtime.h" #include "base/weak_ptr.h" #include "boxes/peers/prepare_short_info_box.h" @@ -31,7 +31,9 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "ui/effects/premium_graphics.h" #include "ui/effects/premium_stars_colored.h" #include "ui/effects/premium_top_bar.h" +#include "ui/effects/spoiler_mess.h" #include "ui/layers/generic_box.h" +#include "ui/rect.h" #include "ui/text/text_utilities.h" #include "ui/widgets/checkbox.h" #include "ui/widgets/gradient_round_button.h" @@ -358,6 +360,73 @@ void AddTableRow( st::giveawayGiftCodePeerMargin); } +void AddTable( + not_null container, + not_null controller, + const Api::GiftCode ¤t, + bool skipReason) { + auto table = container->add( + object_ptr( + container, + st::giveawayGiftCodeTable), + st::giveawayGiftCodeTableMargin); + AddTableRow( + table, + tr::lng_gift_link_label_from(), + controller, + current.from); + if (current.to) { + AddTableRow( + table, + tr::lng_gift_link_label_to(), + controller, + current.to); + } else { + AddTableRow( + table, + tr::lng_gift_link_label_to(), + tr::lng_gift_link_label_to_unclaimed(Ui::Text::WithEntities)); + } + AddTableRow( + table, + tr::lng_gift_link_label_gift(), + tr::lng_gift_link_gift_premium( + lt_duration, + GiftDurationValue(current.months) | Ui::Text::ToWithEntities(), + Ui::Text::WithEntities)); + if (!skipReason) { + const auto reason = AddTableRow( + table, + tr::lng_gift_link_label_reason(), + (current.giveawayId + ? ((current.to + ? tr::lng_gift_link_reason_giveaway + : tr::lng_gift_link_reason_unclaimed)( + ) | Ui::Text::ToLink()) + : current.giveaway + ? ((current.to + ? tr::lng_gift_link_reason_giveaway + : tr::lng_gift_link_reason_unclaimed)( + Ui::Text::WithEntities + ) | rpl::type_erased()) + : tr::lng_gift_link_reason_chosen(Ui::Text::WithEntities))); + reason->setClickHandlerFilter([=](const auto &...) { + controller->showPeerHistory( + current.from, + Window::SectionShow::Way::Forward, + current.giveawayId); + return false; + }); + } + if (current.date) { + AddTableRow( + table, + tr::lng_gift_link_label_date(), + rpl::single(Ui::Text::WithEntities( + langDateTime(base::unixtime::parse(current.date))))); + } +} + } // namespace GiftPremiumValidator::GiftPremiumValidator( @@ -462,65 +531,7 @@ void GiftCodeBox( MakeLinkCopyIcon(box)), st::giveawayGiftCodeLinkMargin); - auto table = box->addRow( - object_ptr( - box, - st::giveawayGiftCodeTable), - st::giveawayGiftCodeTableMargin); - const auto current = state->data.current(); - AddTableRow( - table, - tr::lng_gift_link_label_from(), - controller, - current.from); - if (current.to) { - AddTableRow( - table, - tr::lng_gift_link_label_to(), - controller, - current.to); - } else { - AddTableRow( - table, - tr::lng_gift_link_label_to(), - tr::lng_gift_link_label_to_unclaimed(Ui::Text::WithEntities)); - } - AddTableRow( - table, - tr::lng_gift_link_label_gift(), - tr::lng_gift_link_gift_premium( - lt_duration, - GiftDurationValue(current.months) | Ui::Text::ToWithEntities(), - Ui::Text::WithEntities)); - const auto reason = AddTableRow( - table, - tr::lng_gift_link_label_reason(), - (current.giveawayId - ? ((current.to - ? tr::lng_gift_link_reason_giveaway - : tr::lng_gift_link_reason_unclaimed)( - ) | Ui::Text::ToLink()) - : current.giveaway - ? ((current.to - ? tr::lng_gift_link_reason_giveaway - : tr::lng_gift_link_reason_unclaimed)( - Ui::Text::WithEntities - ) | rpl::type_erased()) - : tr::lng_gift_link_reason_chosen(Ui::Text::WithEntities))); - reason->setClickHandlerFilter([=](const auto &...) { - controller->showPeerHistory( - current.from, - Window::SectionShow::Way::Forward, - current.giveawayId); - return false; - }); - if (current.date) { - AddTableRow( - table, - tr::lng_gift_link_label_date(), - rpl::single(Ui::Text::WithEntities( - langDateTime(base::unixtime::parse(current.date))))); - } + AddTable(box->verticalLayout(), controller, state->data.current(), false); auto shareLink = tr::lng_gift_link_also_send_link( ) | rpl::map([](const QString &text) { @@ -603,6 +614,113 @@ void GiftCodeBox( }, button->lifetime()); } + +void GiftCodePendingBox( + not_null box, + not_null controller, + const Api::GiftCode &data) { + box->setWidth(st::boxWideWidth); + box->setStyle(st::giveawayGiftCodeBox); + box->setNoContentMargin(true); + + { + const auto peerTo = controller->session().data().peer(data.to); + const auto clickContext = [=, weak = base::make_weak(controller)] { + if (const auto strong = weak.get()) { + strong->uiShow()->showBox( + PrepareShortInfoBox(peerTo, strong)); + } + return QVariant(); + }; + const auto &st = st::giveawayGiftCodeCover; + const auto resultToName = st.about.style.font->elided( + peerTo->shortName(), + st.about.minWidth / 2, + Qt::ElideMiddle); + const auto bar = box->setPinnedToTopContent( + object_ptr( + box, + st, + clickContext, + tr::lng_gift_link_title(), + tr::lng_gift_link_pending_about( + lt_user, + rpl::single(Ui::Text::Link(resultToName)), + Ui::Text::RichLangValue), + true)); + + const auto max = st::giveawayGiftCodeTopHeight; + bar->setMaximumHeight(max); + bar->setMinimumHeight(st::infoLayerTopBarHeight); + + bar->resize(bar->width(), bar->maximumHeight()); + } + + { + const auto linkLabel = box->addRow( + Ui::MakeLinkLabel(box, nullptr, nullptr, nullptr, nullptr), + st::giveawayGiftCodeLinkMargin); + const auto spoiler = Ui::CreateChild(linkLabel); + spoiler->lifetime().make_state([=] { + spoiler->update(); + })->start(); + linkLabel->sizeValue( + ) | rpl::start_with_next([=](const QSize &s) { + spoiler->setGeometry(Rect(s)); + }, spoiler->lifetime()); + const auto spoilerCached = Ui::SpoilerMessCached( + Ui::DefaultTextSpoilerMask(), + st::giveawayGiftCodeLink.textFg->c); + const auto textHeight = st::giveawayGiftCodeLink.style.font->height; + spoiler->paintRequest( + ) | rpl::start_with_next([=] { + auto p = QPainter(spoiler); + const auto rect = spoiler->rect(); + const auto r = rect + - QMargins( + st::boxRowPadding.left(), + (rect.height() - textHeight) / 2, + st::boxRowPadding.right(), + (rect.height() - textHeight) / 2); + Ui::FillSpoilerRect(p, r, spoilerCached.frame()); + }, spoiler->lifetime()); + spoiler->setClickedCallback([show = box->uiShow()] { + show->showToast(tr::lng_gift_link_pending_toast(tr::now)); + }); + spoiler->show(); + } + + AddTable(box->verticalLayout(), controller, data, true); + + const auto footer = box->addRow( + object_ptr( + box, + tr::lng_gift_link_pending_footer(), + st::giveawayGiftCodeFooter), + st::giveawayGiftCodeFooterMargin); + + const auto close = Ui::CreateChild( + box.get(), + st::boxTitleClose); + const auto closeCallback = [=] { box->closeBox(); }; + close->setClickedCallback(closeCallback); + box->widthValue( + ) | rpl::start_with_next([=](int width) { + close->moveToRight(0, 0); + }, box->lifetime()); + + const auto button = box->addButton(tr::lng_close(), closeCallback); + const auto buttonPadding = st::giveawayGiftCodeBox.buttonPadding; + const auto buttonWidth = st::boxWideWidth + - buttonPadding.left() + - buttonPadding.right(); + button->widthValue() | rpl::filter([=] { + return (button->widthNoMargins() != buttonWidth); + }) | rpl::start_with_next([=] { + button->resizeToWidth(buttonWidth); + }, button->lifetime()); +} + void ResolveGiftCode( not_null controller, const QString &slug) { diff --git a/Telegram/SourceFiles/boxes/gift_premium_box.h b/Telegram/SourceFiles/boxes/gift_premium_box.h index a1062dead..1891ce5ff 100644 --- a/Telegram/SourceFiles/boxes/gift_premium_box.h +++ b/Telegram/SourceFiles/boxes/gift_premium_box.h @@ -50,6 +50,10 @@ void GiftCodeBox( not_null box, not_null controller, const QString &slug); +void GiftCodePendingBox( + not_null box, + not_null controller, + const Api::GiftCode &data); void ResolveGiftCode( not_null controller, const QString &slug); diff --git a/Telegram/SourceFiles/info/boosts/info_boosts_inner_widget.cpp b/Telegram/SourceFiles/info/boosts/info_boosts_inner_widget.cpp index 4a85789e6..2f85dd9a7 100644 --- a/Telegram/SourceFiles/info/boosts/info_boosts_inner_widget.cpp +++ b/Telegram/SourceFiles/info/boosts/info_boosts_inner_widget.cpp @@ -315,9 +315,23 @@ void InnerWidget::fill() { ResolveGiftCode(_controller, boost.giftCodeLink.slug); } else if (boost.userId) { const auto user = _peer->owner().user(boost.userId); - crl::on_main(this, [=] { - _controller->showPeerInfo(user); - }); + if (boost.isGift || boost.isGiveaway) { + constexpr auto kMonthsDivider = int(30 * 86400); + const auto date = TimeId(boost.date.toSecsSinceEpoch()); + const auto months = (boost.expiresAt - date) + / kMonthsDivider; + const auto d = Api::GiftCode{ + .from = _peer->id, + .to = user->id, + .date = date, + .months = int(months), + }; + _show->showBox(Box(GiftCodePendingBox, _controller, d)); + } else { + crl::on_main(this, [=] { + _controller->showPeerInfo(user); + }); + } } else if (!boost.isUnclaimed) { _show->showToast(tr::lng_boosts_list_pending_about(tr::now)); }