Use service message instead of unread bar.

This commit is contained in:
John Preston 2020-09-16 22:48:56 +03:00
parent a91c078fb1
commit afbc0c498f
6 changed files with 77 additions and 45 deletions

View file

@ -1359,6 +1359,8 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
"lng_comments_open_none" = "Leave a comment";
"lng_replies_view_original" = "View in chat";
"lng_replies_messages" = "Replies";
"lng_replies_discussion_started" = "Discussion started";
"lng_replies_no_comments" = "No comments here yet...";
"lng_archived_name" = "Archived chats";
"lng_archived_add" = "Archive";

View file

@ -9,12 +9,14 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include "history/history.h"
#include "history/history_item.h"
#include "history/history_service.h"
#include "main/main_session.h"
#include "data/data_histories.h"
#include "data/data_session.h"
#include "data/data_changes.h"
#include "data/data_channel.h"
#include "data/data_messages.h"
#include "lang/lang_keys.h"
#include "apiwrap.h"
namespace Data {
@ -22,6 +24,17 @@ namespace {
constexpr auto kMessagesPerPage = 50;
[[nodiscard]] HistoryService *GenerateDivider(
not_null<History*> history,
TimeId date,
const QString &text) {
return history->makeServiceMessage(
history->session().data().nextNonHistoryEntryId(),
MTPDmessage_ClientFlag::f_fake_history_item,
date,
HistoryService::PreparedText{ text });
}
} // namespace
struct RepliesList::Viewer {
@ -40,6 +53,9 @@ RepliesList::RepliesList(not_null<History*> history, MsgId rootId)
RepliesList::~RepliesList() {
histories().cancelRequest(base::take(_beforeId));
histories().cancelRequest(base::take(_afterId));
if (_divider) {
_divider->destroy();
}
}
rpl::producer<MessagesSlice> RepliesList::source(
@ -157,13 +173,21 @@ rpl::producer<int> RepliesList::fullCount() const {
return _fullCount.value() | rpl::filter_optional();
}
void RepliesList::injectRootMessageAndReverse(
not_null<MessagesSlice*> slice) {
injectRootMessage(slice);
ranges::reverse(slice->ids);
}
void RepliesList::injectRootMessage(not_null<MessagesSlice*> slice) {
if (slice->skippedBefore != 0) {
return;
}
if (const auto root = lookupRoot()) {
injectRootDivider(root, slice);
if (const auto group = _history->owner().groups().find(root)) {
for (const auto item : group->items) {
for (const auto item : ranges::view::reverse(group->items)) {
slice->ids.push_back(item->fullId());
}
if (slice->fullCount) {
@ -178,6 +202,28 @@ void RepliesList::injectRootMessage(not_null<MessagesSlice*> slice) {
}
}
void RepliesList::injectRootDivider(
not_null<HistoryItem*> root,
not_null<MessagesSlice*> slice) {
const auto withComments = !slice->ids.empty();
const auto text = [&] {
return withComments
? tr::lng_replies_discussion_started(tr::now)
: tr::lng_replies_no_comments(tr::now);
};
if (!_divider) {
_dividerWithComments = withComments;
_divider = GenerateDivider(
_history,
root->date(),
text());
} else if (_dividerWithComments != withComments) {
_dividerWithComments = withComments;
_divider->setServiceText(HistoryService::PreparedText{ text() });
}
slice->ids.push_back(_divider->fullId());
}
bool RepliesList::buildFromData(not_null<Viewer*> viewer) {
if (_list.empty() && _skippedBefore == 0 && _skippedAfter == 0) {
viewer->slice.ids.clear();
@ -185,7 +231,7 @@ bool RepliesList::buildFromData(not_null<Viewer*> viewer) {
= viewer->slice.skippedBefore
= viewer->slice.skippedAfter
= 0;
injectRootMessage(&viewer->slice);
injectRootMessageAndReverse(&viewer->slice);
return true;
}
const auto around = [&] {
@ -230,9 +276,7 @@ bool RepliesList::buildFromData(not_null<Viewer*> viewer) {
}
slice->fullCount = _fullCount.current();
injectRootMessage(slice);
ranges::reverse(slice->ids);
injectRootMessageAndReverse(slice);
if (_skippedBefore != 0 && useBefore < viewer->limitBefore + 1) {
loadBefore();

View file

@ -10,6 +10,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include "base/weak_ptr.h"
class History;
class HistoryService;
namespace Data {
@ -46,7 +47,11 @@ private:
[[nodiscard]] bool applyUpdate(
not_null<Viewer*> viewer,
const MessageUpdate &update);
void injectRootMessageAndReverse(not_null<MessagesSlice*> slice);
void injectRootMessage(not_null<MessagesSlice*> slice);
void injectRootDivider(
not_null<HistoryItem*> root,
not_null<MessagesSlice*> slice);
bool processMessagesIsEmpty(const MTPmessages_Messages &result);
void loadAround(MsgId id);
void loadBefore();
@ -60,6 +65,8 @@ private:
rpl::variable<std::optional<int>> _fullCount;
rpl::event_stream<> _partLoaded;
std::optional<MsgId> _loadingAround;
HistoryService *_divider = nullptr;
bool _dividerWithComments = false;
int _beforeId = 0;
int _afterId = 0;

View file

@ -108,6 +108,8 @@ public:
not_null<HistoryView::ElementDelegate*> delegate,
HistoryView::Element *replacing = nullptr) override;
void setServiceText(const PreparedText &prepared);
~HistoryService();
protected:
@ -115,8 +117,6 @@ protected:
void markMediaAsReadHook() override;
void setServiceText(const PreparedText &prepared);
QString fromLinkText() const;
ClickHandlerPtr fromLink() const;

View file

@ -123,7 +123,6 @@ RepliesWidget::RepliesWidget(
, _root(lookupRoot())
, _commentsRoot(lookupCommentsRoot())
, _areComments(computeAreComments())
, _scroll(this, st::historyScroll, false)
, _topBar(this, controller)
, _topBarShadow(this)
, _composeControls(std::make_unique<ComposeControls>(
@ -132,7 +131,8 @@ RepliesWidget::RepliesWidget(
ComposeControls::Mode::Normal))
, _rootView(this, object_ptr<Ui::RpWidget>(this))
, _rootShadow(this)
, _scrollDown(_scroll, st::historyToDown)
, _scroll(std::make_unique<Ui::ScrollArea>(this, st::historyScroll, false))
, _scrollDown(_scroll.get(), st::historyToDown)
, _readRequestTimer([=] { sendReadTillRequest(); }) {
setupRoot();
setupRootView();
@ -162,8 +162,9 @@ RepliesWidget::RepliesWidget(
clearSelected();
}, _topBar->lifetime());
_topBarShadow->raise();
_rootView->raise();
_rootShadow->raise();
_topBarShadow->raise();
updateAdaptiveLayout();
subscribe(Adaptive::Changed(), [=] { updateAdaptiveLayout(); });
@ -173,7 +174,7 @@ RepliesWidget::RepliesWidget(
static_cast<ListDelegate*>(this)));
_scroll->move(0, _topBar->height());
_scroll->show();
connect(_scroll, &Ui::ScrollArea::scrolled, [=] { onScroll(); });
connect(_scroll.get(), &Ui::ScrollArea::scrolled, [=] { onScroll(); });
_inner->editMessageRequested(
) | rpl::start_with_next([=](auto fullId) {
@ -1366,7 +1367,13 @@ void RepliesWidget::updatePinnedVisibility() {
setPinnedVisibility(true);
return;
}
const auto view = _inner->viewByPosition(_root->position());
const auto item = [&] {
if (const auto group = _history->owner().groups().find(_root)) {
return group->items.front().get();
}
return _root;
}();
const auto view = _inner->viewByPosition(item->position());
setPinnedVisibility(!view
|| (view->y() + view->height() <= _scroll->scrollTop()));
}
@ -1502,40 +1509,11 @@ void RepliesWidget::listVisibleItemsChanged(HistoryItemsList &&items) {
MessagesBarData RepliesWidget::listMessagesBar(
const std::vector<not_null<Element*>> &elements) {
if (!_commentsRoot || elements.empty()) {
return MessagesBarData();
return {};
}
const auto rootBar = [&] {
const auto fromRoot = (elements.front()->data().get() == _root);
if (elements.size() < 2 || !fromRoot) {
return MessagesBarData();
}
auto text = rpl::combine(
_replies->fullCount(),
_areComments.value()
) | rpl::map([=](int count, bool areComments) {
return count
? (areComments
? tr::lng_comments_header
: tr::lng_replies_header)(
lt_count,
rpl::single(count) | tr::to_count())
: (areComments
? tr::lng_comments_header_none
: tr::lng_replies_header_none)();
}) | rpl::flatten_latest();
return MessagesBarData{
// Designated initializers here crash MSVC 16.7.3.
MessagesBar{
.element = elements[1],
.focus = false,
},
std::move(text),
};
};
const auto till = _commentsRoot->commentsReadTill();
if (till < 2) {
return rootBar();
return {};
}
for (auto i = 0, count = int(elements.size()); i != count; ++i) {
const auto item = elements[i]->data();
@ -1554,7 +1532,7 @@ MessagesBarData RepliesWidget::listMessagesBar(
}
}
}
return rootBar();
return {};
}
void RepliesWidget::listContentRefreshed() {

View file

@ -237,7 +237,6 @@ private:
HistoryItem *_commentsRoot = nullptr;
std::shared_ptr<Data::RepliesList> _replies;
rpl::variable<bool> _areComments = false;
object_ptr<Ui::ScrollArea> _scroll;
QPointer<ListWidget> _inner;
object_ptr<TopBarWidget> _topBar;
object_ptr<Ui::PlainShadow> _topBarShadow;
@ -249,6 +248,8 @@ private:
object_ptr<Ui::SlideWrap<Ui::RpWidget>> _rootView;
object_ptr<Ui::PlainShadow> _rootShadow;
std::unique_ptr<Ui::ScrollArea> _scroll;
std::vector<MsgId> _replyReturns;
HistoryItem *_replyReturn = nullptr;