Slightly refactored code for info of statistic for single message.

This commit is contained in:
23rd 2023-10-09 17:10:58 +03:00 committed by John Preston
parent 3fa168cee0
commit a79e025151
3 changed files with 76 additions and 121 deletions

View File

@ -21,6 +21,13 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
namespace Info::Statistics {
namespace {
struct Descriptor final {
Api::PublicForwards::Slice firstSlice;
Fn<void(FullMsgId)> showPeerHistory;
not_null<PeerData*> 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<void(FullMsgId)> showPeerHistory,
not_null<PeerData*> 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<PeerData*> peer, MsgId msgId);
void applySlice(const Api::PublicForwards::Slice &slice);
const not_null<Main::Session*> _session;
Fn<void(FullMsgId)> _showPeerHistory;
Api::PublicForwards _api;
Api::PublicForwards::Slice _firstSlice;
Api::PublicForwards::OffsetToken _apiToken;
bool _allLoaded = false;
@ -70,13 +76,11 @@ private:
};
PublicForwardsController::PublicForwardsController(
Fn<void(FullMsgId)> showPeerHistory,
not_null<PeerData*> 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<PeerListRow*> row) {
const auto rowWithMsgId = static_cast<PeerListRowWithMsgId*>(row.get());
crl::on_main([=, msgId = rowWithMsgId->msgId(), peer = row->peer()] {
@ -150,53 +159,42 @@ rpl::producer<int> PublicForwardsController::totalCountChanges() const {
} // namespace
PublicShares AddPublicForwards(
void AddPublicForwards(
const Api::MessageStatistics &firstSliceHolder,
not_null<Ui::VerticalLayout*> container,
Fn<void(FullMsgId)> showPeerHistory,
not_null<PeerData*> peer,
FullMsgId contextId) {
if (!peer->isChannel()) {
return rpl::never<int>();
return;
}
struct State final {
State(
Fn<void(FullMsgId)> c,
not_null<PeerData*> 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<State>(
const auto state = container->lifetime().make_state<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<float64>(total)),
{ 0, -subtitlePadding.top(), 0, -subtitlePadding.bottom() });
}
state->delegate.setContent(container->add(
object_ptr<PeerListContent>(container, &state->controller)));
state->controller.setDelegate(&state->delegate);
return state->controller.totalCountChanges(
) | rpl::distinct_until_changed();
}
} // namespace Info::Statistics

View File

@ -13,11 +13,14 @@ namespace Ui {
class VerticalLayout;
} // namespace Ui
namespace Api {
class MessageStatistics;
} // namespace Api
namespace Info::Statistics {
using PublicShares = rpl::producer<int>;
[[nodiscard]] PublicShares AddPublicForwards(
void AddPublicForwards(
const Api::MessageStatistics &firstSliceHolder,
not_null<Ui::VerticalLayout*> container,
Fn<void(FullMsgId)> showPeerHistory,
not_null<PeerData*> peer,

View File

@ -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<rpl::lifetime>();
const auto api = lifetime->make_state<Api::MessageStatistics>(
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<Ui::SlideWrap<Ui::VerticalLayout>>(
inner,
object_ptr<Ui::VerticalLayout>(inner)));
wrapAbove->toggle(false, anim::type::instant);
const auto wrapBelow = inner->add(
object_ptr<Ui::SlideWrap<Ui::VerticalLayout>>(
inner,
object_ptr<Ui::VerticalLayout>(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);
}