From 23d361a987d429616ab355930eeffaba2864605a Mon Sep 17 00:00:00 2001 From: Mikhail Barashkov Date: Sat, 15 Jun 2024 14:14:35 +0300 Subject: [PATCH] Avoid UI updates in Chat selection screen when forwarding a chat, by cloning the chat list model. --- src/chatlistmodel.cpp | 28 ++++++++++++++++++++++++++++ src/chatlistmodel.h | 1 + src/chatpermissionfiltermodel.cpp | 11 ++++++++++- src/chatpermissionfiltermodel.h | 2 +- 4 files changed, 40 insertions(+), 2 deletions(-) diff --git a/src/chatlistmodel.cpp b/src/chatlistmodel.cpp index a361c18..7edfcb4 100644 --- a/src/chatlistmodel.cpp +++ b/src/chatlistmodel.cpp @@ -91,6 +91,7 @@ public: QVector updateLastMessage(const QVariantMap &message); QVector updateGroup(const TDLibWrapper::Group *group); QVector updateSecretChat(const QVariantMap &secretChatDetails); + ChatData* clone(); TDLibWrapper *tdLibWrapper; public: @@ -384,6 +385,24 @@ QVector ChatListModel::ChatData::updateSecretChat(const QVariantMap &secret return changedRoles; } +ChatListModel::ChatData* ChatListModel::ChatData::clone() { + QVariantMap clonedChatData; + + QList keys = chatData.keys(); + for(int i = 0; i < keys.count(); i++) { + clonedChatData.insert(keys[i], QVariant(chatData[keys[i]])); + } + ChatData* res = new ChatData(tdLibWrapper, clonedChatData); + res->chatId = chatId; + res->order = order; + res->groupId = groupId; + res->verified = verified; + res->chatType = chatType; + res->memberStatus = memberStatus; + res->secretChatState = secretChatState; + return res; +} + ChatListModel::ChatListModel(TDLibWrapper *tdLibWrapper, AppSettings *appSettings) : showHiddenChats(false) { this->tdLibWrapper = tdLibWrapper; @@ -426,6 +445,15 @@ ChatListModel::~ChatListModel() qDeleteAll(hiddenChats.values()); } +ChatListModel* ChatListModel::clone() { + ChatListModel* res = new ChatListModel(tdLibWrapper, appSettings); + res->relativeTimeRefreshTimer->stop(); + for(int i = 0; i < chatList.count(); i++) { + res->chatList.append(chatList.at(i)->clone()); + } + return res; +} + void ChatListModel::reset() { chatList.clear(); diff --git a/src/chatlistmodel.h b/src/chatlistmodel.h index af875df..f40cd12 100644 --- a/src/chatlistmodel.h +++ b/src/chatlistmodel.h @@ -75,6 +75,7 @@ public: bool showAllChats() const; void setShowAllChats(bool showAll); + ChatListModel* clone(); private slots: void handleChatDiscovered(const QString &chatId, const QVariantMap &chatInformation); diff --git a/src/chatpermissionfiltermodel.cpp b/src/chatpermissionfiltermodel.cpp index cd99678..6a53cf5 100644 --- a/src/chatpermissionfiltermodel.cpp +++ b/src/chatpermissionfiltermodel.cpp @@ -33,7 +33,9 @@ ChatPermissionFilterModel::ChatPermissionFilterModel(QObject *parent) : QSortFil void ChatPermissionFilterModel::setSource(QObject *model) { - setSourceModel(qobject_cast(model)); + ChatListModel* chatListModel = qobject_cast(model); + ChatListModel* chatListModelClone = chatListModel->clone(); + setSourceModel(chatListModelClone); } void ChatPermissionFilterModel::setSourceModel(QAbstractItemModel *model) @@ -45,6 +47,13 @@ void ChatPermissionFilterModel::setSourceModel(QAbstractItemModel *model) } } +ChatPermissionFilterModel::~ChatPermissionFilterModel() { + QAbstractItemModel* _sourceModel = sourceModel(); + if(_sourceModel != nullptr) { + delete _sourceModel; + } +} + TDLibWrapper *ChatPermissionFilterModel::getTDLibWrapper() const { return tdLibWrapper; diff --git a/src/chatpermissionfiltermodel.h b/src/chatpermissionfiltermodel.h index d77b3f6..dc4f05b 100644 --- a/src/chatpermissionfiltermodel.h +++ b/src/chatpermissionfiltermodel.h @@ -31,7 +31,7 @@ class ChatPermissionFilterModel : public QSortFilterProxyModel public: ChatPermissionFilterModel(QObject *parent = Q_NULLPTR); - + ~ChatPermissionFilterModel() override; TDLibWrapper *getTDLibWrapper() const; void setTDLibWrapper(QObject* obj);