diff --git a/qml/pages/ChatPage.qml b/qml/pages/ChatPage.qml index 864f56d..e8acbeb 100644 --- a/qml/pages/ChatPage.qml +++ b/qml/pages/ChatPage.qml @@ -152,7 +152,6 @@ Page { target: chatModel onMessagesReceived: { chatPage.loading = false; - chatView.positionViewAtEnd(); } onNewMessageReceived: { // Notify user about new messages... @@ -253,9 +252,9 @@ Page { 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(); - } +// if (chatView.indexAt(chatView.contentX, chatView.contentY) < 10) { +// chatModel.triggerLoadMoreHistory(); +// } } onMovementEnded: { diff --git a/src/chatmodel.cpp b/src/chatmodel.cpp index e0b698c..0782acc 100644 --- a/src/chatmodel.cpp +++ b/src/chatmodel.cpp @@ -80,31 +80,7 @@ void ChatModel::handleMessagesReceived(const QVariantList &messages) qDebug() << "[ChatModel] Receiving new messages :)" << messages.size(); if (messages.size() == 0) { - emit noMessagesAvailable(); - return; - } - - this->messagesMutex.lock(); - this->messagesToBeAdded.clear(); - QListIterator messagesIterator(messages); - while (messagesIterator.hasNext()) { - QVariantMap currentMessage = messagesIterator.next().toMap(); - if (currentMessage.value("chat_id").toString() == this->chatId) { - this->messagesToBeAdded.append(currentMessage); - } - } - std::sort(this->messagesToBeAdded.begin(), this->messagesToBeAdded.end(), compareMessages); - - this->insertMessages(); - this->messagesMutex.unlock(); - - // First call only returns a few messages, we need to get a little more than that... - if (this->messagesToBeAdded.size() < 10 && !this->inReload) { - qDebug() << "[ChatModel] Only one message received in first call, loading more..."; - this->inReload = true; - this->tdLibWrapper->getChatHistory(this->chatId, this->messagesToBeAdded.first().toMap().value("id").toLongLong()); - } else { - qDebug() << "[ChatModel] Messages loaded, notifying chat UI..."; + qDebug() << "[ChatModel] No additional messages loaded, notifying chat UI..."; this->inReload = false; if (this->inIncrementalUpdate) { this->inIncrementalUpdate = false; @@ -112,7 +88,38 @@ void ChatModel::handleMessagesReceived(const QVariantList &messages) } else { emit messagesReceived(); } + } else { + this->messagesMutex.lock(); + this->messagesToBeAdded.clear(); + QListIterator messagesIterator(messages); + while (messagesIterator.hasNext()) { + QVariantMap currentMessage = messagesIterator.next().toMap(); + if (currentMessage.value("chat_id").toString() == this->chatId) { + this->messagesToBeAdded.append(currentMessage); + } + } + std::sort(this->messagesToBeAdded.begin(), this->messagesToBeAdded.end(), compareMessages); + + this->insertMessages(); + this->messagesMutex.unlock(); + + // First call only returns a few messages, we need to get a little more than that... + if (this->messagesToBeAdded.size() < 10 && !this->inReload) { + qDebug() << "[ChatModel] Only a few messages received in first call, loading more..."; + this->inReload = true; + this->tdLibWrapper->getChatHistory(this->chatId, this->messagesToBeAdded.first().toMap().value("id").toLongLong()); + } else { + qDebug() << "[ChatModel] Messages loaded, notifying chat UI..."; + this->inReload = false; + if (this->inIncrementalUpdate) { + this->inIncrementalUpdate = false; + emit messagesIncrementalUpdate(); + } else { + emit messagesReceived(); + } + } } + } void ChatModel::handleNewMessageReceived(const QString &chatId, const QVariantMap &message) diff --git a/src/chatmodel.h b/src/chatmodel.h index 606425a..b2c8f49 100644 --- a/src/chatmodel.h +++ b/src/chatmodel.h @@ -23,7 +23,6 @@ public: signals: void messagesReceived(); void messagesIncrementalUpdate(); - void noMessagesAvailable(); void newMessageReceived(); public slots: