From a79e025151a2ed2f91b50dfd15831ab95bb0cf11 Mon Sep 17 00:00:00 2001 From: 23rd <23rd@vivaldi.net> Date: Mon, 9 Oct 2023 17:10:58 +0300 Subject: [PATCH] Slightly refactored code for info of statistic for single message. --- .../info_statistics_public_forwards.cpp | 88 ++++++++------- .../info_statistics_public_forwards.h | 9 +- .../statistics/info_statistics_widget.cpp | 100 +++++------------- 3 files changed, 76 insertions(+), 121 deletions(-) diff --git a/Telegram/SourceFiles/info/statistics/info_statistics_public_forwards.cpp b/Telegram/SourceFiles/info/statistics/info_statistics_public_forwards.cpp index c43bd1c4cf..2197e46bed 100644 --- a/Telegram/SourceFiles/info/statistics/info_statistics_public_forwards.cpp +++ b/Telegram/SourceFiles/info/statistics/info_statistics_public_forwards.cpp @@ -21,6 +21,13 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL namespace Info::Statistics { namespace { +struct Descriptor final { + Api::PublicForwards::Slice firstSlice; + Fn showPeerHistory; + not_null peer; + FullMsgId contextId; +}; + class PeerListRowWithMsgId : public PeerListRow { public: using PeerListRow::PeerListRow; @@ -43,10 +50,7 @@ MsgId PeerListRowWithMsgId::msgId() const { class PublicForwardsController final : public PeerListController { public: - explicit PublicForwardsController( - Fn showPeerHistory, - not_null peer, - FullMsgId contextId); + explicit PublicForwardsController(Descriptor d); Main::Session &session() const override; void prepare() override; @@ -57,11 +61,13 @@ public: private: bool appendRow(not_null peer, MsgId msgId); + void applySlice(const Api::PublicForwards::Slice &slice); const not_null _session; Fn _showPeerHistory; Api::PublicForwards _api; + Api::PublicForwards::Slice _firstSlice; Api::PublicForwards::OffsetToken _apiToken; bool _allLoaded = false; @@ -70,13 +76,11 @@ private: }; -PublicForwardsController::PublicForwardsController( - Fn showPeerHistory, - not_null peer, - FullMsgId contextId) -: _session(&peer->session()) -, _showPeerHistory(std::move(showPeerHistory)) -, _api(peer->asChannel(), contextId) { +PublicForwardsController::PublicForwardsController(Descriptor d) +: _session(&d.peer->session()) +, _showPeerHistory(std::move(d.showPeerHistory)) +, _api(d.peer->asChannel(), d.contextId) +, _firstSlice(std::move(d.firstSlice)) { } Main::Session &PublicForwardsController::session() const { @@ -84,7 +88,7 @@ Main::Session &PublicForwardsController::session() const { } void PublicForwardsController::prepare() { - loadMoreRows(); + applySlice(base::take(_firstSlice)); delegate()->peerListRefreshRows(); } @@ -93,19 +97,24 @@ void PublicForwardsController::loadMoreRows() { return; } _api.request(_apiToken, [=](const Api::PublicForwards::Slice &slice) { - _allLoaded = slice.allLoaded; - _apiToken = slice.token; - _totalCountChanges.fire_copy(slice.total); - - for (const auto &item : slice.list) { - if (const auto peer = session().data().peerLoaded(item.peer)) { - appendRow(peer, item.msg); - } - } - delegate()->peerListRefreshRows(); + applySlice(slice); }); } +void PublicForwardsController::applySlice( + const Api::PublicForwards::Slice &slice) { + _allLoaded = slice.allLoaded; + _apiToken = slice.token; + _totalCountChanges.fire_copy(slice.total); + + for (const auto &item : slice.list) { + if (const auto peer = session().data().peerLoaded(item.peer)) { + appendRow(peer, item.msg); + } + } + delegate()->peerListRefreshRows(); +} + void PublicForwardsController::rowClicked(not_null row) { const auto rowWithMsgId = static_cast(row.get()); crl::on_main([=, msgId = rowWithMsgId->msgId(), peer = row->peer()] { @@ -150,53 +159,42 @@ rpl::producer PublicForwardsController::totalCountChanges() const { } // namespace -PublicShares AddPublicForwards( +void AddPublicForwards( + const Api::MessageStatistics &firstSliceHolder, not_null container, Fn showPeerHistory, not_null peer, FullMsgId contextId) { if (!peer->isChannel()) { - return rpl::never(); + return; } struct State final { - State( - Fn c, - not_null p, - FullMsgId i) : controller(std::move(c), p, i) { + State(Descriptor d) : controller(std::move(d)) { } PeerListContentDelegateSimple delegate; PublicForwardsController controller; }; - const auto state = container->lifetime().make_state( + const auto state = container->lifetime().make_state(Descriptor{ + firstSliceHolder.firstSlice(), std::move(showPeerHistory), peer, - contextId); - - auto title = state->controller.totalCountChanges( - ) | rpl::distinct_until_changed( - ) | rpl::map([=](int total) { - return total - ? tr::lng_stats_overview_message_public_share( - tr::now, - lt_count_decimal, - total) - : QString(); + contextId, }); - { + if (const auto total = firstSliceHolder.firstSlice().total; total > 0) { const auto &subtitlePadding = st::settingsButton.padding; ::Settings::AddSubsectionTitle( container, - std::move(title), + tr::lng_stats_overview_message_public_share( + lt_count_decimal, + rpl::single(total)), { 0, -subtitlePadding.top(), 0, -subtitlePadding.bottom() }); } + state->delegate.setContent(container->add( object_ptr(container, &state->controller))); state->controller.setDelegate(&state->delegate); - - return state->controller.totalCountChanges( - ) | rpl::distinct_until_changed(); } } // namespace Info::Statistics diff --git a/Telegram/SourceFiles/info/statistics/info_statistics_public_forwards.h b/Telegram/SourceFiles/info/statistics/info_statistics_public_forwards.h index 89b83bba7f..503e9d9ea1 100644 --- a/Telegram/SourceFiles/info/statistics/info_statistics_public_forwards.h +++ b/Telegram/SourceFiles/info/statistics/info_statistics_public_forwards.h @@ -13,11 +13,14 @@ namespace Ui { class VerticalLayout; } // namespace Ui +namespace Api { +class MessageStatistics; +} // namespace Api + namespace Info::Statistics { -using PublicShares = rpl::producer; - -[[nodiscard]] PublicShares AddPublicForwards( +void AddPublicForwards( + const Api::MessageStatistics &firstSliceHolder, not_null container, Fn showPeerHistory, not_null peer, diff --git a/Telegram/SourceFiles/info/statistics/info_statistics_widget.cpp b/Telegram/SourceFiles/info/statistics/info_statistics_widget.cpp index 1966d9fb2f..f8eaa9688d 100644 --- a/Telegram/SourceFiles/info/statistics/info_statistics_widget.cpp +++ b/Telegram/SourceFiles/info/statistics/info_statistics_widget.cpp @@ -45,15 +45,7 @@ struct Descriptor final { struct AnyStats final { Data::ChannelStatistics channel; Data::SupergroupStatistics supergroup; - struct Message { - explicit operator bool() const { - return !graph.chart.empty(); - } - Data::StatisticalGraph graph; - int publicForwards = 0; - int privateForwards = 0; - int views = 0; - } message; + Data::MessageStatistics message; }; void ProcessZoom( @@ -221,7 +213,7 @@ void FillStatistic( // Type::StackLinear); } else if (const auto message = stats.message) { addChart( - message.graph, + message.messageInteractionGraph, tr::lng_chart_title_message_interaction(), Type::DoubleLinear); } @@ -628,72 +620,34 @@ Widget::Widget( }, inner->lifetime()); } else { const auto weak = base::make_weak(controller); + const auto lifetime = inner->lifetime( + ).make_state(); + const auto api = lifetime->make_state( + descriptor.peer->asChannel(), + contextId); - descriptor.api->requestMessage( - descriptor.peer, - contextId.msg - ) | rpl::take( - 1 - ) | rpl::start_with_next([=](const Data::StatisticalGraph &data) { - descriptor.api->request( - descriptor.peer - ) | rpl::start_with_done([=] { - const auto channel = descriptor.api->channelStats(); - auto info = Data::StatisticsMessageInteractionInfo(); - for (const auto &r : channel.recentMessageInteractions) { - if (r.messageId == contextId.msg) { - info = r; - break; - } + api->request([=](const Data::MessageStatistics &data) { + const auto stats = AnyStats{ .message = data }; + FillOverview(inner, stats); + FillStatistic(inner, descriptor, stats); + auto showPeerHistory = [=](FullMsgId fullId) { + if (const auto strong = weak.get()) { + controller->showPeerHistory( + fullId.peer, + Window::SectionShow::Way::Forward, + fullId.msg); } + }; + AddPublicForwards( + *api, + inner, + std::move(showPeerHistory), + descriptor.peer, + contextId); - const auto wrapAbove = inner->add( - object_ptr>( - inner, - object_ptr(inner))); - wrapAbove->toggle(false, anim::type::instant); - - const auto wrapBelow = inner->add( - object_ptr>( - inner, - object_ptr(inner))); - wrapBelow->toggle(false, anim::type::instant); - - auto showPeerHistory = [=](FullMsgId fullId) { - if (const auto strong = weak.get()) { - controller->showPeerHistory( - fullId.peer, - Window::SectionShow::Way::Forward, - fullId.msg); - } - }; - auto sharesCount = AddPublicForwards( - wrapBelow->entity(), - std::move(showPeerHistory), - descriptor.peer, - contextId); - - std::move(sharesCount) | rpl::take( - 1 - ) | rpl::start_with_next([=](int count) { - const auto stats = AnyStats{ - .message = { - .graph = data, - .publicForwards = count, - .privateForwards = info.forwardsCount - count, - .views = info.viewsCount, - } - }; - FillOverview(wrapAbove->entity(), stats); - FillStatistic(wrapAbove->entity(), descriptor, stats); - - wrapAbove->toggle(true, anim::type::instant); - wrapBelow->toggle(true, anim::type::instant); - - finishLoading(); - }, inner->lifetime()); - }, inner->lifetime()); - }, inner->lifetime()); + finishLoading(); + lifetime->destroy(); + }); } }, lifetime); }