Keep fetching history until the scrollbar gets activated

This commit is contained in:
Konstantinos Sideris 2017-08-05 15:59:24 +03:00
parent 748eb949a7
commit af0f22fc91
5 changed files with 28 additions and 10 deletions

View file

@ -56,13 +56,14 @@ public slots:
protected:
void mousePressEvent(QMouseEvent *event) override;
void paintEvent(QPaintEvent *event) override;
void resizeEvent(QResizeEvent *event) override;
void contextMenuEvent(QContextMenuEvent *event) override;
private:
QString notificationText();
const int Padding = 7;
const int IconSize = 46;
const int IconSize = 48;
RippleOverlay *ripple_overlay_;

View file

@ -73,12 +73,12 @@ public:
int addEvents(const Timeline &timeline);
void addUserTextMessage(const QString &msg, int txn_id);
void updatePendingMessage(int txn_id, QString event_id);
void fetchHistory();
void scrollDown();
public slots:
void sliderRangeChanged(int min, int max);
void sliderMoved(int position);
void fetchHistory();
// Add old events at the top of the timeline.
void addBackwardsEvents(const QString &room_id, const RoomMessages &msgs);
@ -118,6 +118,8 @@ private:
const int SCROLL_BAR_GAP = 400;
QTimer *paginationTimer_;
int scroll_height_ = 0;
int previous_max_height_ = 0;

View file

@ -102,7 +102,7 @@ ChatPage::ChatPage(QSharedPointer<MatrixClient> client, QWidget *parent)
splitter->addWidget(sideBar_);
splitter->addWidget(content_);
room_list_ = new RoomList(client, this);
room_list_ = new RoomList(client, sideBar_);
sideBarMainLayout_->addWidget(room_list_);
top_bar_ = new TopRoomBar(this);

View file

@ -69,6 +69,16 @@ QString RoomInfoListItem::notificationText()
return tr("Enable notifications");
}
void RoomInfoListItem::resizeEvent(QResizeEvent *)
{
// Update ripple's clipping path.
QPainterPath path;
path.addRect(0, 0, width(), height());
ripple_overlay_->setClipPath(path);
ripple_overlay_->setClipping(true);
}
void RoomInfoListItem::paintEvent(QPaintEvent *event)
{
Q_UNUSED(event);

View file

@ -65,8 +65,10 @@ void TimelineView::sliderRangeChanged(int min, int max)
{
Q_UNUSED(min);
if (!scroll_area_->verticalScrollBar()->isVisible())
if (!scroll_area_->verticalScrollBar()->isVisible()) {
scroll_area_->verticalScrollBar()->setValue(max);
return;
}
if (max - scroll_area_->verticalScrollBar()->value() < SCROLL_BAR_GAP)
scroll_area_->verticalScrollBar()->setValue(max);
@ -84,7 +86,6 @@ void TimelineView::sliderRangeChanged(int min, int max)
newPosition = max;
scroll_area_->verticalScrollBar()->setValue(newPosition);
fetchHistory();
}
}
@ -92,11 +93,14 @@ void TimelineView::fetchHistory()
{
bool hasEnoughMessages = scroll_area_->verticalScrollBar()->value() != 0;
if (!hasEnoughMessages && !isTimelineFinished && !isPaginationInProgress_) {
if (!hasEnoughMessages && !isTimelineFinished) {
isPaginationInProgress_ = true;
client_->messages(room_id_, prev_batch_token_);
scroll_area_->verticalScrollBar()->setValue(scroll_area_->verticalScrollBar()->maximum());
paginationTimer_->start(500);
return;
}
paginationTimer_->stop();
}
void TimelineView::scrollDown()
@ -169,10 +173,8 @@ void TimelineView::addBackwardsEvents(const QString &room_id, const RoomMessages
oldPosition_ = scroll_area_->verticalScrollBar()->value();
oldHeight_ = scroll_widget_->size().height();
for (const auto &item : items) {
item->adjustSize();
for (const auto &item : items)
addTimelineItem(item, TimelineDirection::Top);
}
prev_batch_token_ = msgs.end();
isPaginationInProgress_ = false;
@ -323,6 +325,9 @@ void TimelineView::init()
setLayout(top_layout_);
paginationTimer_ = new QTimer(this);
connect(paginationTimer_, &QTimer::timeout, this, &TimelineView::fetchHistory);
connect(client_.data(), &MatrixClient::messagesRetrieved, this, &TimelineView::addBackwardsEvents);
connect(scroll_area_->verticalScrollBar(), SIGNAL(valueChanged(int)), this, SLOT(sliderMoved(int)));