From f7ab8298cf5bc5ec9804cef4d61c4df220911174 Mon Sep 17 00:00:00 2001
From: 23rd <23rd@vivaldi.net>
Date: Thu, 9 Nov 2023 00:53:31 +0300
Subject: [PATCH] Added show more button to statistics info for list of recent
 messages.

---
 .../info/statistics/info_statistics_common.h  |  1 +
 .../info_statistics_inner_widget.cpp          | 59 ++++++++++++++-----
 .../info_statistics_list_controllers.cpp      |  3 +-
 .../info_statistics_recent_message.cpp        |  1 +
 4 files changed, 47 insertions(+), 17 deletions(-)

diff --git a/Telegram/SourceFiles/info/statistics/info_statistics_common.h b/Telegram/SourceFiles/info/statistics/info_statistics_common.h
index 30a3e092f..cc943ca68 100644
--- a/Telegram/SourceFiles/info/statistics/info_statistics_common.h
+++ b/Telegram/SourceFiles/info/statistics/info_statistics_common.h
@@ -15,6 +15,7 @@ struct SavedState final {
 	Data::AnyStatistics stats;
 	base::flat_map<MsgId, QImage> recentPostPreviews;
 	Data::PublicForwardsSlice publicForwardsFirstSlice;
+	int recentPostsExpanded = 0;
 };
 
 } // namespace Info::Statistics
diff --git a/Telegram/SourceFiles/info/statistics/info_statistics_inner_widget.cpp b/Telegram/SourceFiles/info/statistics/info_statistics_inner_widget.cpp
index b5b2ae8b0..19704c779 100644
--- a/Telegram/SourceFiles/info/statistics/info_statistics_inner_widget.cpp
+++ b/Telegram/SourceFiles/info/statistics/info_statistics_inner_widget.cpp
@@ -9,6 +9,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
 
 #include "api/api_statistics.h"
 #include "apiwrap.h"
+#include "base/call_delayed.h"
 #include "base/event_filter.h"
 #include "data/data_peer.h"
 #include "data/data_session.h"
@@ -697,25 +698,53 @@ void InnerWidget::fillRecentPosts() {
 		}
 	};
 
-	auto foundLoaded = false;
-	for (const auto &recent : stats.recentMessageInteractions) {
-		const auto messageWrap = content->add(
-			object_ptr<Ui::VerticalLayout>(content));
-		const auto msgId = recent.messageId;
-		if (const auto item = _peer->owner().message(_peer, msgId)) {
-			addMessage(messageWrap, item, recent);
-			foundLoaded = true;
-			continue;
+	const auto buttonWrap = container->add(
+		object_ptr<Ui::SlideWrap<Ui::SettingsButton>>(
+			container,
+			object_ptr<Ui::SettingsButton>(
+				container,
+				tr::lng_stories_show_more())));
+
+	constexpr auto kPerPage = int(10);
+	const auto max = stats.recentMessageInteractions.size();
+	if (_state.recentPostsExpanded) {
+		_state.recentPostsExpanded = std::max(
+			_state.recentPostsExpanded - kPerPage,
+			0);
+	}
+	const auto showMore = [=] {
+		const auto from = _state.recentPostsExpanded;
+		_state.recentPostsExpanded = std::min(
+			int(max),
+			_state.recentPostsExpanded + kPerPage);
+		if (_state.recentPostsExpanded == max) {
+			buttonWrap->toggle(false, anim::type::instant);
 		}
-		const auto callback = crl::guard(content, [=] {
+		for (auto i = from; i < _state.recentPostsExpanded; i++) {
+			const auto &recent = stats.recentMessageInteractions[i];
+			const auto messageWrap = content->add(
+				object_ptr<Ui::VerticalLayout>(content));
+			const auto msgId = recent.messageId;
 			if (const auto item = _peer->owner().message(_peer, msgId)) {
 				addMessage(messageWrap, item, recent);
-				content->resizeToWidth(content->width());
+				continue;
 			}
-		});
-		_peer->session().api().requestMessageData(_peer, msgId, callback);
-	}
-	if (!foundLoaded) {
+			const auto callback = crl::guard(content, [=] {
+				if (const auto item = _peer->owner().message(_peer, msgId)) {
+					addMessage(messageWrap, item, recent);
+					content->resizeToWidth(content->width());
+				}
+			});
+			_peer->session().api().requestMessageData(_peer, msgId, callback);
+		}
+		container->resizeToWidth(container->width());
+	};
+	const auto delay = st::defaultRippleAnimation.hideDuration;
+	buttonWrap->entity()->setClickedCallback([=] {
+		base::call_delayed(delay, crl::guard(container, showMore));
+	});
+	showMore();
+	if (_messagePreviews.empty()) {
 		wrap->toggle(false, anim::type::instant);
 	}
 }
diff --git a/Telegram/SourceFiles/info/statistics/info_statistics_list_controllers.cpp b/Telegram/SourceFiles/info/statistics/info_statistics_list_controllers.cpp
index 0f2e93cd4..498acdbd1 100644
--- a/Telegram/SourceFiles/info/statistics/info_statistics_list_controllers.cpp
+++ b/Telegram/SourceFiles/info/statistics/info_statistics_list_controllers.cpp
@@ -696,7 +696,6 @@ void AddMembersList(
 				container,
 				tr::lng_stories_show_more())),
 		{ 0, -st::settingsButton.padding.top(), 0, 0 });
-	const auto button = wrap->entity();
 
 	const auto showMore = [=] {
 		state->limit = std::min(int(max), state->limit + kPerPage);
@@ -706,7 +705,7 @@ void AddMembersList(
 		}
 		container->resizeToWidth(container->width());
 	};
-	button->setClickedCallback(showMore);
+	wrap->entity()->setClickedCallback(showMore);
 	showMore();
 }
 
diff --git a/Telegram/SourceFiles/info/statistics/info_statistics_recent_message.cpp b/Telegram/SourceFiles/info/statistics/info_statistics_recent_message.cpp
index 1a85458e9..022da23a8 100644
--- a/Telegram/SourceFiles/info/statistics/info_statistics_recent_message.cpp
+++ b/Telegram/SourceFiles/info/statistics/info_statistics_recent_message.cpp
@@ -215,6 +215,7 @@ void MessagePreview::paintEvent(QPaintEvent *e) {
 		.spoiler = Ui::Text::DefaultSpoilerCache(),
 		.now = crl::now(),
 		.elisionHeight = st::statisticsDetailsPopupHeaderStyle.font->height,
+		.elisionLines = 1,
 	});
 	_views.draw(p, {
 		.position = { width() - _viewsWidth, topTextTop },