From 60f4587d95ef8e5665d7d5880ee5906324dabded Mon Sep 17 00:00:00 2001 From: John Preston Date: Fri, 15 Nov 2024 18:05:21 +0400 Subject: [PATCH] Sort birthday gifts to the front. --- Telegram/SourceFiles/api/api_premium.cpp | 1 + Telegram/SourceFiles/api/api_premium.h | 1 + Telegram/SourceFiles/boxes/star_gift_box.cpp | 40 +++++++++++++++++--- 3 files changed, 37 insertions(+), 5 deletions(-) diff --git a/Telegram/SourceFiles/api/api_premium.cpp b/Telegram/SourceFiles/api/api_premium.cpp index 91824922e..d88a5ddce 100644 --- a/Telegram/SourceFiles/api/api_premium.cpp +++ b/Telegram/SourceFiles/api/api_premium.cpp @@ -778,6 +778,7 @@ std::optional FromTL( .limitedCount = total.value_or_empty(), .firstSaleDate = data.vfirst_sale_date().value_or_empty(), .lastSaleDate = data.vlast_sale_date().value_or_empty(), + .birthday = data.is_birthday(), }; } diff --git a/Telegram/SourceFiles/api/api_premium.h b/Telegram/SourceFiles/api/api_premium.h index ef08eb8fb..23455ba18 100644 --- a/Telegram/SourceFiles/api/api_premium.h +++ b/Telegram/SourceFiles/api/api_premium.h @@ -82,6 +82,7 @@ struct StarGift { int limitedCount = 0; TimeId firstSaleDate = 0; TimeId lastSaleDate = 0; + bool birthday = false; friend inline bool operator==( const StarGift &, diff --git a/Telegram/SourceFiles/boxes/star_gift_box.cpp b/Telegram/SourceFiles/boxes/star_gift_box.cpp index 70fe207ab..781e008d2 100644 --- a/Telegram/SourceFiles/boxes/star_gift_box.cpp +++ b/Telegram/SourceFiles/boxes/star_gift_box.cpp @@ -139,6 +139,23 @@ private: }; +[[nodiscard]] bool SortForBirthday(not_null peer) { + const auto user = peer->asUser(); + if (!user) { + return false; + } + const auto birthday = user->birthday(); + if (!birthday) { + return false; + } + const auto is = [&](const QDate &date) { + return (date.day() == birthday.day()) + && (date.month() == birthday.month()); + }; + const auto now = QDate::currentDate(); + return is(now) || is(now.addDays(1)) || is(now.addDays(-1)); +} + PreviewDelegate::PreviewDelegate( not_null parent, not_null st, @@ -1068,10 +1085,23 @@ void SendGiftBox( const auto padding = st::giftBoxPadding; const auto available = width - padding.left() - padding.right(); const auto perRow = available / single.width(); + const auto count = int(gifts.list.size()); + + auto order = ranges::views::ints + | ranges::views::take(count) + | ranges::to_vector; + + if (SortForBirthday(peer)) { + ranges::stable_partition(order, [&](int i) { + const auto &gift = gifts.list[i]; + const auto stars = std::get_if(&gift); + return stars && stars->info.birthday; + }); + } auto x = padding.left(); auto y = padding.top(); - state->buttons.resize(gifts.list.size()); + state->buttons.resize(count); for (auto &button : state->buttons) { if (!button) { button = std::make_unique(raw, &state->delegate); @@ -1079,9 +1109,9 @@ void SendGiftBox( } } const auto api = gifts.api; - for (auto i = 0, count = int(gifts.list.size()); i != count; ++i) { + for (auto i = 0; i != count; ++i) { const auto button = state->buttons[i].get(); - const auto &descriptor = gifts.list[i]; + const auto &descriptor = gifts.list[order[i]]; button->setDescriptor(descriptor); const auto last = !((i + 1) % perRow); @@ -1108,12 +1138,12 @@ void SendGiftBox( } }); } - if (gifts.list.size() % perRow) { + if (count % perRow) { y += padding.bottom() + single.height(); } else { y += padding.bottom() - st::giftBoxGiftSkip.y(); } - raw->resize(raw->width(), gifts.list.empty() ? 0 : y); + raw->resize(raw->width(), count ? y : 0); }, raw->lifetime()); return result;