Fix infinite loading for small amount of messages
This commit is contained in:
parent
63833b3c5c
commit
cca241ed9a
3 changed files with 35 additions and 30 deletions
|
@ -152,7 +152,6 @@ Page {
|
||||||
target: chatModel
|
target: chatModel
|
||||||
onMessagesReceived: {
|
onMessagesReceived: {
|
||||||
chatPage.loading = false;
|
chatPage.loading = false;
|
||||||
chatView.positionViewAtEnd();
|
|
||||||
}
|
}
|
||||||
onNewMessageReceived: {
|
onNewMessageReceived: {
|
||||||
// Notify user about new messages...
|
// Notify user about new messages...
|
||||||
|
@ -253,9 +252,9 @@ Page {
|
||||||
|
|
||||||
function handleScrollPositionChanged() {
|
function handleScrollPositionChanged() {
|
||||||
tdLibWrapper.viewMessage(chatInformation.id, chatView.itemAt(chatView.contentX, ( chatView.contentY + chatView.height - Theme.horizontalPageMargin )).myMessage.id);
|
tdLibWrapper.viewMessage(chatInformation.id, chatView.itemAt(chatView.contentX, ( chatView.contentY + chatView.height - Theme.horizontalPageMargin )).myMessage.id);
|
||||||
if (chatView.indexAt(chatView.contentX, chatView.contentY) < 10) {
|
// if (chatView.indexAt(chatView.contentX, chatView.contentY) < 10) {
|
||||||
chatModel.triggerLoadMoreHistory();
|
// chatModel.triggerLoadMoreHistory();
|
||||||
}
|
// }
|
||||||
}
|
}
|
||||||
|
|
||||||
onMovementEnded: {
|
onMovementEnded: {
|
||||||
|
|
|
@ -80,31 +80,7 @@ void ChatModel::handleMessagesReceived(const QVariantList &messages)
|
||||||
qDebug() << "[ChatModel] Receiving new messages :)" << messages.size();
|
qDebug() << "[ChatModel] Receiving new messages :)" << messages.size();
|
||||||
|
|
||||||
if (messages.size() == 0) {
|
if (messages.size() == 0) {
|
||||||
emit noMessagesAvailable();
|
qDebug() << "[ChatModel] No additional messages loaded, notifying chat UI...";
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
this->messagesMutex.lock();
|
|
||||||
this->messagesToBeAdded.clear();
|
|
||||||
QListIterator<QVariant> 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...";
|
|
||||||
this->inReload = false;
|
this->inReload = false;
|
||||||
if (this->inIncrementalUpdate) {
|
if (this->inIncrementalUpdate) {
|
||||||
this->inIncrementalUpdate = false;
|
this->inIncrementalUpdate = false;
|
||||||
|
@ -112,7 +88,38 @@ void ChatModel::handleMessagesReceived(const QVariantList &messages)
|
||||||
} else {
|
} else {
|
||||||
emit messagesReceived();
|
emit messagesReceived();
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
this->messagesMutex.lock();
|
||||||
|
this->messagesToBeAdded.clear();
|
||||||
|
QListIterator<QVariant> 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)
|
void ChatModel::handleNewMessageReceived(const QString &chatId, const QVariantMap &message)
|
||||||
|
|
|
@ -23,7 +23,6 @@ public:
|
||||||
signals:
|
signals:
|
||||||
void messagesReceived();
|
void messagesReceived();
|
||||||
void messagesIncrementalUpdate();
|
void messagesIncrementalUpdate();
|
||||||
void noMessagesAvailable();
|
|
||||||
void newMessageReceived();
|
void newMessageReceived();
|
||||||
|
|
||||||
public slots:
|
public slots:
|
||||||
|
|
Loading…
Reference in a new issue