Loading messages or not loading them...? Next step towards #136

This commit is contained in:
Sebastian Wolf 2020-11-16 14:22:32 +01:00
parent 18e88a8412
commit 9507024f51
2 changed files with 68 additions and 36 deletions

View file

@ -30,6 +30,7 @@ namespace {
const QString CHAT_ID("chat_id"); const QString CHAT_ID("chat_id");
const QString PHOTO("photo"); const QString PHOTO("photo");
const QString SMALL("small"); const QString SMALL("small");
const QString UNREAD_COUNT("unread_count");
const QString LAST_READ_INBOX_MESSAGE_ID("last_read_inbox_message_id"); const QString LAST_READ_INBOX_MESSAGE_ID("last_read_inbox_message_id");
const QString SENDER_USER_ID("sender_user_id"); const QString SENDER_USER_ID("sender_user_id");
} }
@ -129,10 +130,22 @@ QVariantMap ChatModel::getMessage(int index)
int ChatModel::getLastReadMessageIndex() int ChatModel::getLastReadMessageIndex()
{ {
LOG("Obtaining last read message index");
if (this->messages.isEmpty()) { if (this->messages.isEmpty()) {
LOG("Messages are empty, nothing to do...");
return 0; return 0;
} else if (this->messages.last().toMap().value(SENDER_USER_ID).toString() == tdLibWrapper->getUserInformation().value(ID).toString()) {
LOG("Last message is an own one, then simply set the last read to the last one...");
return this->messages.size() - 1;
} else { } else {
return (this->messages.last().toMap().value(SENDER_USER_ID).toString() == tdLibWrapper->getUserInformation().value(ID).toString()) ? (this->messages.size() - 1) : this->messageIndexMap.value(this->chatInformation.value(LAST_READ_INBOX_MESSAGE_ID).toString()).toInt(); int lastReadMessageIndex = this->messageIndexMap.value(this->chatInformation.value(LAST_READ_INBOX_MESSAGE_ID).toString()).toInt();
if (lastReadMessageIndex == 0) {
LOG("Last read message not found in the list of messages. That shouldn't happen, therefore setting the unread indicator to the end of the list.");
return this->messages.size() - 1;
} else {
LOG("Found last read message in the already loaded messages. Index: " << lastReadMessageIndex);
return lastReadMessageIndex;
}
} }
} }
@ -164,6 +177,7 @@ void ChatModel::handleMessagesReceived(const QVariantList &messages, int totalCo
emit messagesReceived(listInboxPosition, listOutboxPosition, totalCount); emit messagesReceived(listInboxPosition, listOutboxPosition, totalCount);
} }
} else { } else {
if (this->isMostRecentMessageLoaded() || this->inIncrementalUpdate) {
this->messagesMutex.lock(); this->messagesMutex.lock();
this->messagesToBeAdded.clear(); this->messagesToBeAdded.clear();
QListIterator<QVariant> messagesIterator(messages); QListIterator<QVariant> messagesIterator(messages);
@ -199,6 +213,12 @@ void ChatModel::handleMessagesReceived(const QVariantList &messages, int totalCo
emit messagesReceived(listInboxPosition, listOutboxPosition, totalCount); emit messagesReceived(listInboxPosition, listOutboxPosition, totalCount);
} }
} }
} else {
// Cleanup... Is that really needed? Well, let's see...
this->inReload = false;
this->inIncrementalUpdate = false;
LOG("New messages in this chat, but not relevant as less recent messages need to be loaded first!");
}
} }
} }
@ -206,6 +226,7 @@ void ChatModel::handleMessagesReceived(const QVariantList &messages, int totalCo
void ChatModel::handleNewMessageReceived(const QString &id, const QVariantMap &message) void ChatModel::handleNewMessageReceived(const QString &id, const QVariantMap &message)
{ {
if (id.toLongLong() == chatId && !this->messageIndexMap.contains(id)) { if (id.toLongLong() == chatId && !this->messageIndexMap.contains(id)) {
if (this->isMostRecentMessageLoaded()) {
LOG("New message received for this chat"); LOG("New message received for this chat");
this->messagesMutex.lock(); this->messagesMutex.lock();
@ -215,6 +236,9 @@ void ChatModel::handleNewMessageReceived(const QString &id, const QVariantMap &m
this->insertMessages(); this->insertMessages();
this->messagesMutex.unlock(); this->messagesMutex.unlock();
emit newMessageReceived(message); emit newMessageReceived(message);
} else {
LOG("New message in this chat, but not relevant as less recent messages need to be loaded first!");
}
} }
} }
@ -404,3 +428,10 @@ void ChatModel::calculateMessageIndexMap()
this->messageIndexMap.insert(this->messages.at(i).toMap().value(ID).toString(), i); this->messageIndexMap.insert(this->messages.at(i).toMap().value(ID).toString(), i);
} }
} }
bool ChatModel::isMostRecentMessageLoaded()
{
// Need to check if we can actually add messages (only possible if the previously latest messages are loaded)
// Trying with half of the size of an initial list to ensure that everything is there...
return this->getLastReadMessageIndex() >= this->messages.size() - 25;
}

View file

@ -85,6 +85,7 @@ private:
int calculateLastKnownMessageId(); int calculateLastKnownMessageId();
int calculateLastReadSentMessageId(); int calculateLastReadSentMessageId();
void calculateMessageIndexMap(); void calculateMessageIndexMap();
bool isMostRecentMessageLoaded();
}; };
#endif // CHATMODEL_H #endif // CHATMODEL_H