From 8a3b07e537e853888a070fca34a8210b62e90fcc Mon Sep 17 00:00:00 2001 From: "Sebastian J. Wolf" Date: Wed, 26 Aug 2020 22:52:06 +0200 Subject: [PATCH] Load older chat messages if needed --- qml/pages/ChatPage.qml | 14 +++++++++++--- src/chatmodel.cpp | 17 ++++++++++++++++- src/chatmodel.h | 3 +++ 3 files changed, 30 insertions(+), 4 deletions(-) diff --git a/qml/pages/ChatPage.qml b/qml/pages/ChatPage.qml index 5ca70e6..ade2a98 100644 --- a/qml/pages/ChatPage.qml +++ b/qml/pages/ChatPage.qml @@ -154,7 +154,7 @@ Page { chatView.positionViewAtEnd(); } onNewMessageReceived: { - chatView.positionViewAtEnd(); + // Notify user about new messages... } } @@ -244,17 +244,25 @@ Page { clip: true visible: count > 0 - onMovementEnded: { + function handleScrollPositionChanged() { tdLibWrapper.viewMessage(chatInformation.id, chatView.itemAt(chatView.contentX, ( chatView.contentY + chatView.height - Theme.horizontalPageMargin )).myMessage.id); + if (chatView.indexAt(chatView.contentX, chatView.contentY) < 10) { + chatModel.triggerLoadMoreHistory(); + } + } + + onMovementEnded: { + handleScrollPositionChanged(); } onQuickScrollAnimatingChanged: { if (!quickScrollAnimating) { - tdLibWrapper.viewMessage(chatInformation.id, chatView.itemAt(chatView.contentX, ( chatView.contentY + chatView.height - Theme.horizontalPageMargin )).myMessage.id); + handleScrollPositionChanged(); } } onCurrentIndexChanged: { + console.log("Current index: " + currentIndex); tdLibWrapper.viewMessage(chatInformation.id, currentItem.myMessage.id); } diff --git a/src/chatmodel.cpp b/src/chatmodel.cpp index 22289d9..e050f29 100644 --- a/src/chatmodel.cpp +++ b/src/chatmodel.cpp @@ -6,6 +6,7 @@ ChatModel::ChatModel(TDLibWrapper *tdLibWrapper) { this->tdLibWrapper = tdLibWrapper; this->inReload = false; + this->inIncrementalUpdate = false; connect(this->tdLibWrapper, SIGNAL(messagesReceived(QVariantList)), this, SLOT(handleMessagesReceived(QVariantList))); connect(this->tdLibWrapper, SIGNAL(newMessageReceived(QString, QVariantMap)), this, SLOT(handleNewMessageReceived(QString, QVariantMap))); } @@ -51,6 +52,15 @@ void ChatModel::initialize(const QString &chatId) this->messagesToBeAdded.clear(); } +void ChatModel::triggerLoadMoreHistory() +{ + qDebug() << "[ChatModel] Trigger loading older history..."; + if (!this->inIncrementalUpdate) { + this->inIncrementalUpdate = true; + this->tdLibWrapper->getChatHistory(this->chatId, this->messages.first().toMap().value("id").toLongLong()); + } +} + bool compareMessages(const QVariant &message1, const QVariant &message2) { QVariantMap messageMap1 = message1.toMap(); @@ -93,7 +103,12 @@ void ChatModel::handleMessagesReceived(const QVariantList &messages) } else { qDebug() << "[ChatModel] Messages loaded, notifying chat UI..."; this->inReload = false; - emit messagesReceived(); + if (this->inIncrementalUpdate) { + this->inIncrementalUpdate = false; + emit messagesIncrementalUpdate(); + } else { + emit messagesReceived(); + } } } diff --git a/src/chatmodel.h b/src/chatmodel.h index 41cbd9a..f3b44d6 100644 --- a/src/chatmodel.h +++ b/src/chatmodel.h @@ -18,9 +18,11 @@ public: virtual bool insertRows(int row, int count, const QModelIndex &parent = QModelIndex()) override; Q_INVOKABLE void initialize(const QString &chatId); + Q_INVOKABLE void triggerLoadMoreHistory(); signals: void messagesReceived(); + void messagesIncrementalUpdate(); void noMessagesAvailable(); void newMessageReceived(); @@ -37,6 +39,7 @@ private: QMutex messagesMutex; QString chatId; bool inReload; + bool inIncrementalUpdate; void insertMessages(); };