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() {
Debug.log("[ChatPage] Initializing chat page...");
chatView.currentIndex = -1;
chatView.lastReadSentIndex = 0;
chatView.lastReadSentIndex = -1;
var chatType = chatInformation.type['@type'];
isPrivateChat = chatType === "chatTypePrivate";
isSecretChat = chatType === "chatTypeSecret";
@ -182,6 +182,7 @@ Page {
}
function getMessageStatusText(message, listItemIndex, lastReadSentIndex, useElapsed) {
Debug.log("Last read sent index: " + lastReadSentIndex);
var messageStatusSuffix = "";
if(!message) {
return "";
@ -443,7 +444,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, "")
@ -548,7 +548,7 @@ Page {
Connections {
target: chatModel
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 (chatPage.iterativeInitialization) {
chatPage.iterativeInitialization = false;
@ -595,6 +595,13 @@ 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);
}
if (chatView.height > chatView.contentHeight) {
Debug.log("[ChatPage] Chat content quite small...");
viewMessageTimer.queueViewMessage(chatView.count - 1);
}
chatViewCooldownTimer.restart();
}
onNotificationSettingsUpdated: {
@ -798,7 +805,7 @@ Page {
Rectangle {
id: chatSecretBackground
color: Theme.highlightBackgroundColor
color: Theme.rgba(Theme.overlayBackgroundColor, Theme.opacityFaint)
width: chatPage.isPortrait ? Theme.fontSizeLarge : Theme.fontSizeMedium
height: width
anchors.left: parent.left
@ -925,6 +932,12 @@ Page {
onTriggered: {
Debug.log("[ChatPage] Cooldown completed...");
chatView.inCooldown = false;
if (!chatPage.isInitialized) {
Debug.log("Page is initialized!");
chatPage.isInitialized = true;
chatView.handleScrollPositionChanged();
}
}
}
@ -952,7 +965,7 @@ Page {
clip: true
highlightMoveDuration: 0
highlightResizeDuration: 0
property int lastReadSentIndex: 0
property int lastReadSentIndex: -1
property bool inCooldown: false
property bool manuallyScrolledToBottom
property QtObject precalculatedValues: QtObject {

View file

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

View file

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

View file

@ -366,22 +366,7 @@ 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 (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;
}
}
return this->calculateLastKnownMessageId();
}
void ChatModel::setSearchQuery(const QString newSearchQuery)
@ -418,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);
@ -458,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);
@ -539,6 +526,7 @@ void ChatModel::handleMessageSendSucceeded(qlonglong messageId, qlonglong oldMes
const QModelIndex messageIndex(index(pos));
emit dataChanged(messageIndex, messageIndex, changedRoles);
emit lastReadSentMessageUpdated(calculateLastReadSentMessageId());
tdLibWrapper->viewMessage(QString::number(this->chatId), QString::number(messageId), false);
}
}
@ -756,14 +744,29 @@ int ChatModel::calculateLastKnownMessageId()
LOG("calculateLastKnownMessageId");
const qlonglong lastKnownMessageId = this->chatInformation.value(LAST_READ_INBOX_MESSAGE_ID).toLongLong();
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("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 listOwnPosition = messageIndexMap.value(lastOwnMessageId, -1);
if (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);
return listInboxPosition;
LOG("Last own message is at position" << listOwnPosition);
return (listInboxPosition > listOwnPosition) ? listInboxPosition : listOwnPosition ;
}
int ChatModel::calculateLastReadSentMessageId()
@ -773,11 +776,22 @@ int ChatModel::calculateLastReadSentMessageId()
LOG("lastReadSentMessageId" << lastReadSentMessageId);
LOG("size messageIndexMap" << messageIndexMap.size());
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);
emit lastReadSentMessageUpdated(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()
{
// 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);
int calculateLastKnownMessageId();
int calculateLastReadSentMessageId();
int calculateScrollPosition(int listInboxPosition);
bool isMostRecentMessageLoaded();
private: