Loading messages or not loading them...? Next step towards #136
This commit is contained in:
parent
18e88a8412
commit
9507024f51
2 changed files with 68 additions and 36 deletions
|
@ -30,6 +30,7 @@ namespace {
|
|||
const QString CHAT_ID("chat_id");
|
||||
const QString PHOTO("photo");
|
||||
const QString SMALL("small");
|
||||
const QString UNREAD_COUNT("unread_count");
|
||||
const QString LAST_READ_INBOX_MESSAGE_ID("last_read_inbox_message_id");
|
||||
const QString SENDER_USER_ID("sender_user_id");
|
||||
}
|
||||
|
@ -129,10 +130,22 @@ QVariantMap ChatModel::getMessage(int index)
|
|||
|
||||
int ChatModel::getLastReadMessageIndex()
|
||||
{
|
||||
LOG("Obtaining last read message index");
|
||||
if (this->messages.isEmpty()) {
|
||||
LOG("Messages are empty, nothing to do...");
|
||||
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 {
|
||||
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);
|
||||
}
|
||||
} else {
|
||||
if (this->isMostRecentMessageLoaded() || this->inIncrementalUpdate) {
|
||||
this->messagesMutex.lock();
|
||||
this->messagesToBeAdded.clear();
|
||||
QListIterator<QVariant> messagesIterator(messages);
|
||||
|
@ -199,6 +213,12 @@ void ChatModel::handleMessagesReceived(const QVariantList &messages, int totalCo
|
|||
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)
|
||||
{
|
||||
if (id.toLongLong() == chatId && !this->messageIndexMap.contains(id)) {
|
||||
if (this->isMostRecentMessageLoaded()) {
|
||||
LOG("New message received for this chat");
|
||||
this->messagesMutex.lock();
|
||||
|
||||
|
@ -215,6 +236,9 @@ void ChatModel::handleNewMessageReceived(const QString &id, const QVariantMap &m
|
|||
this->insertMessages();
|
||||
this->messagesMutex.unlock();
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
|
|
@ -85,6 +85,7 @@ private:
|
|||
int calculateLastKnownMessageId();
|
||||
int calculateLastReadSentMessageId();
|
||||
void calculateMessageIndexMap();
|
||||
bool isMostRecentMessageLoaded();
|
||||
};
|
||||
|
||||
#endif // CHATMODEL_H
|
||||
|
|
Loading…
Reference in a new issue