From 225eb7689922eb0ba3dfae8fad342e98e6858db9 Mon Sep 17 00:00:00 2001 From: Sebastian Wolf Date: Mon, 28 Dec 2020 14:35:27 +0100 Subject: [PATCH] Fix crash on incoming message deletions & minor other fixes --- qml/components/AudioPreview.qml | 8 ++++---- qml/pages/SearchChatsPage.qml | 4 ++++ src/chatmodel.cpp | 35 ++++++++++++++++++++++----------- 3 files changed, 32 insertions(+), 15 deletions(-) diff --git a/qml/components/AudioPreview.qml b/qml/components/AudioPreview.qml index 3901f6b..4c660cb 100644 --- a/qml/components/AudioPreview.qml +++ b/qml/components/AudioPreview.qml @@ -64,9 +64,9 @@ Item { audioType = ( audioData['@type'] === "voiceNote" ) ? "voice" : "audio"; audioFileId = audioData[audioType].id; if (typeof audioData.album_cover_thumbnail !== "undefined") { - previewFileId = audioData.album_cover_thumbnail.photo.id; - if (audioData.album_cover_thumbnail.photo.local.is_downloading_completed) { - placeholderImage.source = audioData.album_cover_thumbnail.photo.local.path; + previewFileId = audioData.album_cover_thumbnail.file.id; + if (audioData.album_cover_thumbnail.file.local.is_downloading_completed) { + placeholderImage.source = audioData.album_cover_thumbnail.file.local.path; } else { tdLibWrapper.downloadFile(previewFileId); } @@ -94,7 +94,7 @@ Item { if (typeof audioData === "object") { if (fileInformation.local.is_downloading_completed) { if (fileId === previewFileId) { - audioData.thumbnail.photo = fileInformation; + audioData.album_cover_thumbnail.file = fileInformation; placeholderImage.source = fileInformation.local.path; } if (fileId === audioFileId) { diff --git a/qml/pages/SearchChatsPage.qml b/qml/pages/SearchChatsPage.qml index 1bd590c..82fe385 100644 --- a/qml/pages/SearchChatsPage.qml +++ b/qml/pages/SearchChatsPage.qml @@ -52,6 +52,10 @@ Page { Debug.log(JSON.stringify(chats)); chatsFound = chats; } + onErrorReceived: { + searchChatsPage.isLoading = false; + Functions.handleErrorMessage(code, message); + } } property bool isLoading: false; diff --git a/src/chatmodel.cpp b/src/chatmodel.cpp index 4179db6..5d37916 100644 --- a/src/chatmodel.cpp +++ b/src/chatmodel.cpp @@ -480,27 +480,40 @@ void ChatModel::handleMessagesDeleted(qlonglong chatId, const QList & if (chatId == this->chatId) { const int count = messageIds.size(); LOG(count << "messages in this chat were deleted..."); - int firstDeleted = -1, lastDeleted = -2; - for (int i = 0; i < count; i++) { - const int pos = messageIndexMap.value(messageIds.at(i), -1); - if (pos >= 0) { - if (pos == lastDeleted + 1) { - lastDeleted = pos; // Extend the current range + + int firstPosition = count, lastPosition = count; + for (int i = (count - 1); i > -1; i--) { + const int position = messageIndexMap.value(messageIds.at(i), -1); + if (position >= 0) { + // We found at least one message in our list that needs to be deleted + if (lastPosition == count) { + lastPosition = position; + } + if (firstPosition == count) { + firstPosition = position; + } + if (position < (firstPosition - 1)) { + // Some gap in between, can remove previous range and reset positions + removeRange(firstPosition, lastPosition); + firstPosition = lastPosition = position; } else { - removeRange(firstDeleted, lastDeleted); - firstDeleted = lastDeleted = pos; // Start new range + // No gap in between, extend the range and continue loop + firstPosition = position; } } } - // Handle the last (and likely the only) range - removeRange(firstDeleted, lastDeleted); + // After all elements have been processed, there may be one last range to remove + // But only if we found at least one item to remove + if (firstPosition != count && lastPosition != count) { + removeRange(firstPosition, lastPosition); + } } } void ChatModel::removeRange(int firstDeleted, int lastDeleted) { if (firstDeleted >= 0 && firstDeleted <= lastDeleted) { - LOG("Removing range" << firstDeleted << "..." << lastDeleted); + LOG("Removing range" << firstDeleted << "..." << lastDeleted << "| current messages size" << messages.size()); beginRemoveRows(QModelIndex(), firstDeleted, lastDeleted); for (int i = firstDeleted; i <= lastDeleted; i++) { MessageData *message = messages.at(i);