From 3176c3dc8c84bd4a0a12aceda0dfe0b427736a96 Mon Sep 17 00:00:00 2001 From: Sebastian Wolf Date: Tue, 2 Feb 2021 22:05:24 +0100 Subject: [PATCH] Chat page: Adjust initial scroll position, honoring #243 --- qml/pages/ChatPage.qml | 10 +++++++++- src/chatmodel.cpp | 12 ++++++++++++ src/chatmodel.h | 1 + 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/qml/pages/ChatPage.qml b/qml/pages/ChatPage.qml index 045b72b..54ba6e9 100644 --- a/qml/pages/ChatPage.qml +++ b/qml/pages/ChatPage.qml @@ -443,7 +443,6 @@ Page { chatModel.initialize(chatInformation); pageStack.pushAttached(Qt.resolvedUrl("ChatInformationPage.qml"), { "chatInformation" : chatInformation, "privateChatUserInformation": chatPartnerInformation, "groupInformation": chatGroupInformation, "chatOnlineMemberCount": chatOnlineMemberCount}); - chatPage.isInitialized = true; if(doSendBotStartMessage) { tdLibWrapper.sendBotStartMessage(chatInformation.id, chatInformation.id, sendBotStartMessageParameter, "") @@ -595,6 +594,9 @@ Page { onMessagesIncrementalUpdate: { 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; + if (!chatPage.isInitialized) { + chatView.scrollToIndex(modelIndex); + } chatViewCooldownTimer.restart(); } onNotificationSettingsUpdated: { @@ -925,6 +927,12 @@ Page { onTriggered: { Debug.log("[ChatPage] Cooldown completed..."); chatView.inCooldown = false; + + if (!chatPage.isInitialized) { + Debug.log("Page is initialized!"); + chatPage.isInitialized = true; + chatView.handleScrollPositionChanged(); + } } } diff --git a/src/chatmodel.cpp b/src/chatmodel.cpp index 68695a3..7a38a31 100644 --- a/src/chatmodel.cpp +++ b/src/chatmodel.cpp @@ -403,6 +403,7 @@ void ChatModel::handleMessagesReceived(const QVariantList &messages, int totalCo this->inReload = false; int listInboxPosition = this->calculateLastKnownMessageId(); int listOutboxPosition = this->calculateLastReadSentMessageId(); + listInboxPosition = this->calculateScrollPosition(listInboxPosition); if (this->inIncrementalUpdate) { this->inIncrementalUpdate = false; emit messagesIncrementalUpdate(listInboxPosition, listOutboxPosition); @@ -443,6 +444,7 @@ void ChatModel::handleMessagesReceived(const QVariantList &messages, int totalCo this->inReload = false; int listInboxPosition = this->calculateLastKnownMessageId(); int listOutboxPosition = this->calculateLastReadSentMessageId(); + listInboxPosition = this->calculateScrollPosition(listInboxPosition); if (this->inIncrementalUpdate) { this->inIncrementalUpdate = false; emit messagesIncrementalUpdate(listInboxPosition, listOutboxPosition); @@ -779,6 +781,16 @@ int ChatModel::calculateLastReadSentMessageId() 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() { // Need to check if we can actually add messages (only possible if the previously latest messages are loaded) diff --git a/src/chatmodel.h b/src/chatmodel.h index 30ce20f..7424346 100644 --- a/src/chatmodel.h +++ b/src/chatmodel.h @@ -85,6 +85,7 @@ private: QVariantMap enhanceMessage(const QVariantMap &message); int calculateLastKnownMessageId(); int calculateLastReadSentMessageId(); + int calculateScrollPosition(int listInboxPosition); bool isMostRecentMessageLoaded(); private: