Load older chat messages if needed

This commit is contained in:
Sebastian J. Wolf 2020-08-26 22:52:06 +02:00
parent abdf9072e7
commit 8a3b07e537
3 changed files with 30 additions and 4 deletions

View file

@ -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);
}

View file

@ -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();
}
}
}

View file

@ -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();
};