Merge pull request #359 from Wunderfitz/chatIndexHandling

Improved chat message index handling
This commit is contained in:
Sebastian Wolf 2021-02-03 21:42:01 +01:00 committed by GitHub
commit 1a3767e599
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 54 additions and 26 deletions

View file

@ -145,7 +145,7 @@ Page {
function initializePage() { function initializePage() {
Debug.log("[ChatPage] Initializing chat page..."); Debug.log("[ChatPage] Initializing chat page...");
chatView.currentIndex = -1; chatView.currentIndex = -1;
chatView.lastReadSentIndex = 0; chatView.lastReadSentIndex = -1;
var chatType = chatInformation.type['@type']; var chatType = chatInformation.type['@type'];
isPrivateChat = chatType === "chatTypePrivate"; isPrivateChat = chatType === "chatTypePrivate";
isSecretChat = chatType === "chatTypeSecret"; isSecretChat = chatType === "chatTypeSecret";
@ -182,6 +182,7 @@ Page {
} }
function getMessageStatusText(message, listItemIndex, lastReadSentIndex, useElapsed) { function getMessageStatusText(message, listItemIndex, lastReadSentIndex, useElapsed) {
Debug.log("Last read sent index: " + lastReadSentIndex);
var messageStatusSuffix = ""; var messageStatusSuffix = "";
if(!message) { if(!message) {
return ""; return "";
@ -443,7 +444,6 @@ Page {
chatModel.initialize(chatInformation); chatModel.initialize(chatInformation);
pageStack.pushAttached(Qt.resolvedUrl("ChatInformationPage.qml"), { "chatInformation" : chatInformation, "privateChatUserInformation": chatPartnerInformation, "groupInformation": chatGroupInformation, "chatOnlineMemberCount": chatOnlineMemberCount}); pageStack.pushAttached(Qt.resolvedUrl("ChatInformationPage.qml"), { "chatInformation" : chatInformation, "privateChatUserInformation": chatPartnerInformation, "groupInformation": chatGroupInformation, "chatOnlineMemberCount": chatOnlineMemberCount});
chatPage.isInitialized = true;
if(doSendBotStartMessage) { if(doSendBotStartMessage) {
tdLibWrapper.sendBotStartMessage(chatInformation.id, chatInformation.id, sendBotStartMessageParameter, "") tdLibWrapper.sendBotStartMessage(chatInformation.id, chatInformation.id, sendBotStartMessageParameter, "")
@ -548,7 +548,7 @@ Page {
Connections { Connections {
target: chatModel target: chatModel
onMessagesReceived: { onMessagesReceived: {
Debug.log("[ChatPage] Messages received, view has ", chatView.count, " messages, setting view to index ", modelIndex, ", own messages were read before index ", lastReadSentIndex); Debug.log("[ChatPage] Messages received, view has ", chatView.count, " messages, last known message index ", modelIndex, ", own messages were read before index ", lastReadSentIndex);
if (totalCount === 0) { if (totalCount === 0) {
if (chatPage.iterativeInitialization) { if (chatPage.iterativeInitialization) {
chatPage.iterativeInitialization = false; chatPage.iterativeInitialization = false;
@ -595,6 +595,13 @@ Page {
onMessagesIncrementalUpdate: { onMessagesIncrementalUpdate: {
Debug.log("Incremental update received. View now has ", chatView.count, " messages, view is on index ", modelIndex, ", own messages were read before index ", lastReadSentIndex); Debug.log("Incremental update received. View now has ", chatView.count, " messages, view is on index ", modelIndex, ", own messages were read before index ", lastReadSentIndex);
chatView.lastReadSentIndex = lastReadSentIndex; chatView.lastReadSentIndex = lastReadSentIndex;
if (!chatPage.isInitialized) {
chatView.scrollToIndex(modelIndex);
}
if (chatView.height > chatView.contentHeight) {
Debug.log("[ChatPage] Chat content quite small...");
viewMessageTimer.queueViewMessage(chatView.count - 1);
}
chatViewCooldownTimer.restart(); chatViewCooldownTimer.restart();
} }
onNotificationSettingsUpdated: { onNotificationSettingsUpdated: {
@ -798,7 +805,7 @@ Page {
Rectangle { Rectangle {
id: chatSecretBackground id: chatSecretBackground
color: Theme.highlightBackgroundColor color: Theme.rgba(Theme.overlayBackgroundColor, Theme.opacityFaint)
width: chatPage.isPortrait ? Theme.fontSizeLarge : Theme.fontSizeMedium width: chatPage.isPortrait ? Theme.fontSizeLarge : Theme.fontSizeMedium
height: width height: width
anchors.left: parent.left anchors.left: parent.left
@ -925,6 +932,12 @@ Page {
onTriggered: { onTriggered: {
Debug.log("[ChatPage] Cooldown completed..."); Debug.log("[ChatPage] Cooldown completed...");
chatView.inCooldown = false; chatView.inCooldown = false;
if (!chatPage.isInitialized) {
Debug.log("Page is initialized!");
chatPage.isInitialized = true;
chatView.handleScrollPositionChanged();
}
} }
} }
@ -952,7 +965,7 @@ Page {
clip: true clip: true
highlightMoveDuration: 0 highlightMoveDuration: 0
highlightResizeDuration: 0 highlightResizeDuration: 0
property int lastReadSentIndex: 0 property int lastReadSentIndex: -1
property bool inCooldown: false property bool inCooldown: false
property bool manuallyScrolledToBottom property bool manuallyScrolledToBottom
property QtObject precalculatedValues: QtObject { property QtObject precalculatedValues: QtObject {

View file

@ -12,7 +12,7 @@ Name: harbour-fernschreiber
Summary: Fernschreiber is a Telegram client for Sailfish OS Summary: Fernschreiber is a Telegram client for Sailfish OS
Version: 0.7 Version: 0.7
Release: 1 Release: 2
Group: Qt/Qt Group: Qt/Qt
License: LICENSE License: LICENSE
URL: http://werkwolf.eu/ URL: http://werkwolf.eu/

View file

@ -1,7 +1,7 @@
Name: harbour-fernschreiber Name: harbour-fernschreiber
Summary: Fernschreiber is a Telegram client for Sailfish OS Summary: Fernschreiber is a Telegram client for Sailfish OS
Version: 0.7 Version: 0.7
Release: 1 Release: 2
# The contents of the Group field should be one of the groups listed here: # The contents of the Group field should be one of the groups listed here:
# https://github.com/mer-tools/spectacle/blob/master/data/GROUPS # https://github.com/mer-tools/spectacle/blob/master/data/GROUPS
Group: Qt/Qt Group: Qt/Qt

View file

@ -366,22 +366,7 @@ QVariantMap ChatModel::getMessage(int index)
int ChatModel::getLastReadMessageIndex() int ChatModel::getLastReadMessageIndex()
{ {
LOG("Obtaining last read message index"); LOG("Obtaining last read message index");
if (this->messages.isEmpty()) { return this->calculateLastKnownMessageId();
LOG("Messages are empty, nothing to do...");
return 0;
} else if (messages.last()->senderUserId() == tdLibWrapper->getUserInformation().value(ID).toInt()) {
LOG("Last message is an own one, then simply set the last read to the last one...");
return this->messages.size() - 1;
} else {
const int lastReadMessageIndex = messageIndexMap.value(chatInformation.value(LAST_READ_INBOX_MESSAGE_ID).toLongLong(), -1);
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;
}
}
} }
void ChatModel::setSearchQuery(const QString newSearchQuery) void ChatModel::setSearchQuery(const QString newSearchQuery)
@ -418,6 +403,7 @@ void ChatModel::handleMessagesReceived(const QVariantList &messages, int totalCo
this->inReload = false; this->inReload = false;
int listInboxPosition = this->calculateLastKnownMessageId(); int listInboxPosition = this->calculateLastKnownMessageId();
int listOutboxPosition = this->calculateLastReadSentMessageId(); int listOutboxPosition = this->calculateLastReadSentMessageId();
listInboxPosition = this->calculateScrollPosition(listInboxPosition);
if (this->inIncrementalUpdate) { if (this->inIncrementalUpdate) {
this->inIncrementalUpdate = false; this->inIncrementalUpdate = false;
emit messagesIncrementalUpdate(listInboxPosition, listOutboxPosition); emit messagesIncrementalUpdate(listInboxPosition, listOutboxPosition);
@ -458,6 +444,7 @@ void ChatModel::handleMessagesReceived(const QVariantList &messages, int totalCo
this->inReload = false; this->inReload = false;
int listInboxPosition = this->calculateLastKnownMessageId(); int listInboxPosition = this->calculateLastKnownMessageId();
int listOutboxPosition = this->calculateLastReadSentMessageId(); int listOutboxPosition = this->calculateLastReadSentMessageId();
listInboxPosition = this->calculateScrollPosition(listInboxPosition);
if (this->inIncrementalUpdate) { if (this->inIncrementalUpdate) {
this->inIncrementalUpdate = false; this->inIncrementalUpdate = false;
emit messagesIncrementalUpdate(listInboxPosition, listOutboxPosition); emit messagesIncrementalUpdate(listInboxPosition, listOutboxPosition);
@ -539,6 +526,7 @@ void ChatModel::handleMessageSendSucceeded(qlonglong messageId, qlonglong oldMes
const QModelIndex messageIndex(index(pos)); const QModelIndex messageIndex(index(pos));
emit dataChanged(messageIndex, messageIndex, changedRoles); emit dataChanged(messageIndex, messageIndex, changedRoles);
emit lastReadSentMessageUpdated(calculateLastReadSentMessageId()); emit lastReadSentMessageUpdated(calculateLastReadSentMessageId());
tdLibWrapper->viewMessage(QString::number(this->chatId), QString::number(messageId), false);
} }
} }
@ -756,14 +744,29 @@ int ChatModel::calculateLastKnownMessageId()
LOG("calculateLastKnownMessageId"); LOG("calculateLastKnownMessageId");
const qlonglong lastKnownMessageId = this->chatInformation.value(LAST_READ_INBOX_MESSAGE_ID).toLongLong(); const qlonglong lastKnownMessageId = this->chatInformation.value(LAST_READ_INBOX_MESSAGE_ID).toLongLong();
LOG("lastKnownMessageId" << lastKnownMessageId); LOG("lastKnownMessageId" << lastKnownMessageId);
const int myUserId = tdLibWrapper->getUserInformation().value(ID).toInt();
qlonglong lastOwnMessageId = 0;
for (int i = (messages.size() - 1); i >= 0; i--) {
MessageData *currentMessage = messages.at(i);
if (currentMessage->senderUserId() == myUserId) {
lastOwnMessageId = currentMessage->messageId;
break;
}
}
LOG("size messageIndexMap" << messageIndexMap.size()); LOG("size messageIndexMap" << messageIndexMap.size());
LOG("contains ID?" << messageIndexMap.contains(lastKnownMessageId)); LOG("contains last read ID?" << messageIndexMap.contains(lastKnownMessageId));
LOG("contains last own ID?" << messageIndexMap.contains(lastOwnMessageId));
int listInboxPosition = messageIndexMap.value(lastKnownMessageId, messages.size() - 1); int listInboxPosition = messageIndexMap.value(lastKnownMessageId, messages.size() - 1);
int listOwnPosition = messageIndexMap.value(lastOwnMessageId, -1);
if (listInboxPosition > this->messages.size() - 1 ) { if (listInboxPosition > this->messages.size() - 1 ) {
listInboxPosition = this->messages.size() - 1; listInboxPosition = this->messages.size() - 1;
} }
if (listOwnPosition > this->messages.size() - 1 ) {
listOwnPosition = -1;
}
LOG("Last known message is at position" << listInboxPosition); LOG("Last known message is at position" << listInboxPosition);
return listInboxPosition; LOG("Last own message is at position" << listOwnPosition);
return (listInboxPosition > listOwnPosition) ? listInboxPosition : listOwnPosition ;
} }
int ChatModel::calculateLastReadSentMessageId() int ChatModel::calculateLastReadSentMessageId()
@ -773,11 +776,22 @@ int ChatModel::calculateLastReadSentMessageId()
LOG("lastReadSentMessageId" << lastReadSentMessageId); LOG("lastReadSentMessageId" << lastReadSentMessageId);
LOG("size messageIndexMap" << messageIndexMap.size()); LOG("size messageIndexMap" << messageIndexMap.size());
LOG("contains ID?" << messageIndexMap.contains(lastReadSentMessageId)); LOG("contains ID?" << messageIndexMap.contains(lastReadSentMessageId));
const int listOutboxPosition = messageIndexMap.value(lastReadSentMessageId, messages.size() - 1); const int listOutboxPosition = messageIndexMap.value(lastReadSentMessageId, -1);
LOG("Last read sent message is at position" << listOutboxPosition); LOG("Last read sent message is at position" << listOutboxPosition);
emit lastReadSentMessageUpdated(listOutboxPosition);
return listOutboxPosition; return listOutboxPosition;
} }
int ChatModel::calculateScrollPosition(int listInboxPosition)
{
LOG("Calculating new scroll position, current:" << listInboxPosition << ", list size:" << this->messages.size());
if ((this->messages.size() - 1) > listInboxPosition) {
return listInboxPosition + 1;
} else {
return listInboxPosition;
}
}
bool ChatModel::isMostRecentMessageLoaded() bool ChatModel::isMostRecentMessageLoaded()
{ {
// Need to check if we can actually add messages (only possible if the previously latest messages are loaded) // Need to check if we can actually add messages (only possible if the previously latest messages are loaded)

View file

@ -85,6 +85,7 @@ private:
QVariantMap enhanceMessage(const QVariantMap &message); QVariantMap enhanceMessage(const QVariantMap &message);
int calculateLastKnownMessageId(); int calculateLastKnownMessageId();
int calculateLastReadSentMessageId(); int calculateLastReadSentMessageId();
int calculateScrollPosition(int listInboxPosition);
bool isMostRecentMessageLoaded(); bool isMostRecentMessageLoaded();
private: private: