Preload chats in support switch.

This commit is contained in:
John Preston 2022-02-05 18:15:24 +03:00
parent 0aa20b4479
commit d89d8b09da
12 changed files with 200 additions and 12 deletions

View File

@ -3025,6 +3025,24 @@ void InnerWidget::updateRowCornerStatusShown(
}
}
RowDescriptor InnerWidget::resolveChatNext(RowDescriptor from) const {
const auto row = from.key ? from : _controller->activeChatEntryCurrent();
return row.key
? computeJump(
chatListEntryAfter(row),
JumpSkip::NextOrEnd)
: row;
}
RowDescriptor InnerWidget::resolveChatPrevious(RowDescriptor from) const {
const auto row = from.key ? from : _controller->activeChatEntryCurrent();
return row.key
? computeJump(
chatListEntryBefore(row),
JumpSkip::PreviousOrBegin)
: row;
}
void InnerWidget::setupShortcuts() {
Shortcuts::Requests(
) | rpl::filter([=] {
@ -3197,7 +3215,7 @@ void InnerWidget::setupShortcuts() {
RowDescriptor InnerWidget::computeJump(
const RowDescriptor &to,
JumpSkip skip) {
JumpSkip skip) const {
auto result = to;
if (result.key) {
const auto down = (skip == JumpSkip::NextOrEnd)

View File

@ -125,6 +125,9 @@ public:
[[nodiscard]] rpl::producer<ChosenRow> chosenRow() const;
[[nodiscard]] rpl::producer<> updated() const;
[[nodiscard]] RowDescriptor resolveChatNext(RowDescriptor from = {}) const;
[[nodiscard]] RowDescriptor resolveChatPrevious(RowDescriptor from = {}) const;
~InnerWidget();
public Q_SLOTS:
@ -238,7 +241,7 @@ private:
void setupShortcuts();
RowDescriptor computeJump(
const RowDescriptor &to,
JumpSkip skip);
JumpSkip skip) const;
bool jumpToDialogRow(RowDescriptor to);
RowDescriptor chatListEntryBefore(const RowDescriptor &which) const;

View File

@ -1685,6 +1685,14 @@ void Widget::updateForwardBar() {
update();
}
RowDescriptor Widget::resolveChatNext(RowDescriptor from) const {
return _inner->resolveChatNext(from);
}
RowDescriptor Widget::resolveChatPrevious(RowDescriptor from) const {
return _inner->resolveChatPrevious(from);
}
void Widget::keyPressEvent(QKeyEvent *e) {
if (e->key() == Qt::Key_Escape) {
if (_openedFolder) {

View File

@ -91,6 +91,9 @@ public:
[[nodiscard]] rpl::producer<> closeForwardBarRequests() const;
[[nodiscard]] RowDescriptor resolveChatNext(RowDescriptor from = {}) const;
[[nodiscard]] RowDescriptor resolveChatPrevious(RowDescriptor from = {}) const;
// Float player interface.
bool floatPlayerHandleWheelEvent(QEvent *e) override;
QRect floatPlayerAvailableRect() override;

View File

@ -818,6 +818,13 @@ HistoryWidget::HistoryWidget(
}
}, lifetime());
if (session().supportMode()) {
session().data().chatListEntryRefreshes(
) | rpl::start_with_next([=] {
crl::on_main(this, [=] { checkSupportPreload(true); });
}, lifetime());
}
setupScheduledToggle();
setupSendAsToggle();
orderWidgets();
@ -2299,9 +2306,11 @@ void HistoryWidget::setHistory(History *history) {
history->forceFullResize();
}
};
if (_history) {
unregisterDraftSources();
clearAllLoadRequests();
clearSupportPreloadRequest();
const auto wasHistory = base::take(_history);
const auto wasMigrated = base::take(_migrated);
unloadHeavyViewParts(wasHistory);
@ -2371,6 +2380,16 @@ void HistoryWidget::clearDelayedShowAtRequest() {
}
}
void HistoryWidget::clearSupportPreloadRequest() {
Expects(_history != nullptr);
if (_supportPreloadRequest) {
auto &histories = _history->owner().histories();
histories.cancelRequest(_supportPreloadRequest);
_supportPreloadRequest = 0;
}
}
void HistoryWidget::clearAllLoadRequests() {
Expects(_history != nullptr);
@ -2990,6 +3009,9 @@ void HistoryWidget::messagesReceived(PeerData *peer, const MTPmessages_Messages
setMsgId(_delayedShowAtMsgId);
historyLoaded();
}
if (session().supportMode()) {
crl::on_main(this, [=] { checkSupportPreload(); });
}
}
void HistoryWidget::historyLoaded() {
@ -3337,6 +3359,96 @@ void HistoryWidget::preloadHistoryByScroll() {
if (scrollTop <= kPreloadHeightsCount * scrollHeight) {
loadMessages();
}
if (session().supportMode()) {
crl::on_main(this, [=] { checkSupportPreload(); });
}
}
void HistoryWidget::checkSupportPreload(bool force) {
if (!_history
|| _firstLoadRequest
|| _preloadRequest
|| _preloadDownRequest
|| (_supportPreloadRequest && !force)
|| controller()->activeChatEntryCurrent().key.history() != _history) {
return;
}
const auto setting = session().settings().supportSwitch();
const auto command = Support::GetSwitchCommand(setting);
const auto descriptor = !command
? Dialogs::RowDescriptor()
: (*command == Shortcuts::Command::ChatNext)
? controller()->resolveChatNext()
: controller()->resolveChatPrevious();
auto history = descriptor.key.history();
if (!history || _supportPreloadHistory == history) {
return;
}
clearSupportPreloadRequest();
_supportPreloadHistory = history;
auto offsetId = MsgId();
auto offset = 0;
auto loadCount = kMessagesPerPage;
if (const auto around = history->loadAroundId()) {
history->getReadyFor(ShowAtUnreadMsgId);
offset = -loadCount / 2;
offsetId = around;
}
const auto offsetDate = 0;
const auto maxId = 0;
const auto minId = 0;
const auto historyHash = uint64(0);
const auto tmp = history->peer->name.toStdString();
const auto tmp2 = _history->peer->name.toStdString();
LOG(("PRELOADING FROM: %1 FOR: %2").arg(_history->peer->name).arg(history->peer->name));
const auto type = Data::Histories::RequestType::History;
auto &histories = history->owner().histories();
_supportPreloadRequest = histories.sendRequest(history, type, [=](Fn<void()> finish) {
return history->session().api().request(MTPmessages_GetHistory(
history->peer->input,
MTP_int(offsetId),
MTP_int(offsetDate),
MTP_int(offset),
MTP_int(loadCount),
MTP_int(maxId),
MTP_int(minId),
MTP_long(historyHash)
)).done([=](const MTPmessages_Messages &result) {
if (const auto around = history->loadAroundId()) {
if (around != offsetId) {
LOG(("RE-PRELOADING FOR: %1").arg(history->peer->name));
_supportPreloadRequest = 0;
_supportPreloadHistory = nullptr;
crl::on_main(this, [=] { checkSupportPreload(); });
return;
}
history->clear(History::ClearType::Unload);
history->getReadyFor(ShowAtUnreadMsgId);
} else if (offsetId) {
LOG(("RE-PRELOADING FOR: %1").arg(history->peer->name));
_supportPreloadRequest = 0;
_supportPreloadHistory = nullptr;
crl::on_main(this, [=] { checkSupportPreload(); });
return;
} else {
history->clear(History::ClearType::Unload);
history->getReadyFor(ShowAtTheEndMsgId);
}
LOG(("PRELOADED FOR: %1").arg(history->peer->name));
auto count = 0;
const QVector<MTPMessage> emptyList, *histList = &emptyList;
result.match([](const MTPDmessages_messagesNotModified&) {
}, [&](const auto &data) {
history->owner().processUsers(data.vusers());
history->owner().processChats(data.vchats());
history->addOlderSlice(data.vmessages().v);
});
finish();
}).fail([=](const MTP::Error &error) {
finish();
}).send();
});
}
void HistoryWidget::checkReplyReturns() {
@ -7534,10 +7646,7 @@ HistoryWidget::~HistoryWidget() {
// Saving a draft on account switching.
saveFieldToHistoryLocalDraft();
session().api().saveDraftToCloudDelayed(_history);
clearAllLoadRequests();
setHistory(nullptr);
unregisterDraftSources();
}
setTabbedPanel(nullptr);
}

View File

@ -234,6 +234,7 @@ public:
Ui::ReportReason reason,
Fn<void(MessageIdsList)> callback);
void clearAllLoadRequests();
void clearSupportPreloadRequest();
void clearDelayedShowAtRequest();
void clearDelayedShowAt();
void saveFieldToHistoryLocalDraft();
@ -598,6 +599,7 @@ private:
bool readyToForward() const;
bool hasSilentToggle() const;
void checkSupportPreload(bool force = false);
void handleSupportSwitch(not_null<History*> updated);
void inlineBotResolveDone(const MTPcontacts_ResolvedPeer &result);
@ -685,6 +687,9 @@ private:
MsgId _delayedShowAtMsgId = -1;
int _delayedShowAtRequest = 0; // Not real mtpRequestId.
History *_supportPreloadHistory = nullptr;
int _supportPreloadRequest = 0; // Not real mtpRequestId.
object_ptr<HistoryView::TopBarWidget> _topBar;
object_ptr<Ui::ContinuousScroll> _scroll;
QPointer<HistoryInner> _list;

View File

@ -1779,6 +1779,16 @@ bool MainWidget::isThirdSectionShown() const {
return _thirdSection != nullptr;
}
Dialogs::RowDescriptor MainWidget::resolveChatNext(
Dialogs::RowDescriptor from) const {
return _dialogs ? _dialogs->resolveChatNext(from) : Dialogs::RowDescriptor();
}
Dialogs::RowDescriptor MainWidget::resolveChatPrevious(
Dialogs::RowDescriptor from) const {
return _dialogs ? _dialogs->resolveChatPrevious(from) : Dialogs::RowDescriptor();
}
bool MainWidget::stackIsEmpty() const {
return _stack.empty();
}

View File

@ -128,6 +128,11 @@ public:
[[nodiscard]] bool isMainSectionShown() const;
[[nodiscard]] bool isThirdSectionShown() const;
[[nodiscard]] Dialogs::RowDescriptor resolveChatNext(
Dialogs::RowDescriptor from) const;
[[nodiscard]] Dialogs::RowDescriptor resolveChatPrevious(
Dialogs::RowDescriptor from) const;
void returnTabbedSelector();
void showAnimated(const QPixmap &bgAnimCache, bool back = false);

View File

@ -21,14 +21,19 @@ Qt::KeyboardModifiers SkipSwitchModifiers() {
return Qt::ControlModifier | Qt::ShiftModifier;
}
FnMut<bool()> GetSwitchMethod(SwitchSettings value) {
std::optional<Shortcuts::Command> GetSwitchCommand(SwitchSettings value) {
switch (value) {
case SwitchSettings::Next:
return Shortcuts::RequestHandler(Shortcuts::Command::ChatNext);
return Shortcuts::Command::ChatNext;
case SwitchSettings::Previous:
return Shortcuts::RequestHandler(Shortcuts::Command::ChatPrevious);
return Shortcuts::Command::ChatPrevious;
}
return nullptr;
return std::nullopt;
}
FnMut<bool()> GetSwitchMethod(SwitchSettings value) {
const auto command = GetSwitchCommand(value);
return command ? Shortcuts::RequestHandler(*command) : nullptr;
}
} // namespace Support

View File

@ -7,6 +7,10 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
*/
#pragma once
namespace Shortcuts {
enum class Command;
} // namespace Shortcuts
namespace Support {
enum class SwitchSettings {
@ -15,8 +19,10 @@ enum class SwitchSettings {
Previous,
};
Qt::KeyboardModifiers SkipSwitchModifiers();
bool HandleSwitch(Qt::KeyboardModifiers modifiers);
FnMut<bool()> GetSwitchMethod(SwitchSettings value);
[[nodiscard]] Qt::KeyboardModifiers SkipSwitchModifiers();
[[nodiscard]] bool HandleSwitch(Qt::KeyboardModifiers modifiers);
[[nodiscard]] std::optional<Shortcuts::Command> GetSwitchCommand(
SwitchSettings value);
[[nodiscard]] FnMut<bool()> GetSwitchMethod(SwitchSettings value);
} // namespace Support

View File

@ -795,6 +795,16 @@ bool SessionController::jumpToChatListEntry(Dialogs::RowDescriptor row) {
return false;
}
Dialogs::RowDescriptor SessionController::resolveChatNext(
Dialogs::RowDescriptor from) const {
return content()->resolveChatNext(from);
}
Dialogs::RowDescriptor SessionController::resolveChatPrevious(
Dialogs::RowDescriptor from) const {
return content()->resolveChatPrevious(from);
}
void SessionController::pushToChatEntryHistory(Dialogs::RowDescriptor row) {
if (!_chatEntryHistory.empty()
&& _chatEntryHistory[_chatEntryHistoryPosition] == row) {

View File

@ -301,6 +301,12 @@ public:
rpl::producer<Dialogs::RowDescriptor> activeChatEntryValue() const;
rpl::producer<Dialogs::Key> activeChatValue() const;
bool jumpToChatListEntry(Dialogs::RowDescriptor row);
[[nodiscard]] Dialogs::RowDescriptor resolveChatNext(
Dialogs::RowDescriptor from = {}) const;
[[nodiscard]] Dialogs::RowDescriptor resolveChatPrevious(
Dialogs::RowDescriptor from = {}) const;
void showEditPeerBox(PeerData *peer);
void enableGifPauseReason(GifPauseReason reason);