From d87b5b84fbe62d14a53017801bbdc2ff5364c734 Mon Sep 17 00:00:00 2001 From: "Sebastian J. Wolf" Date: Thu, 20 Aug 2020 18:45:56 +0200 Subject: [PATCH] Surrendering - trying delta updates only after successful initialization --- qml/pages/CoverPage.qml | 2 ++ qml/pages/OverviewPage.qml | 24 ++++++++++++++++++++++-- src/chatlistmodel.cpp | 25 ++++++++++++++++++++++--- src/chatlistmodel.h | 3 +++ 4 files changed, 49 insertions(+), 5 deletions(-) diff --git a/qml/pages/CoverPage.qml b/qml/pages/CoverPage.qml index 00e583e..115d296 100644 --- a/qml/pages/CoverPage.qml +++ b/qml/pages/CoverPage.qml @@ -127,6 +127,8 @@ CoverBackground { width: parent.width - unreadMessagesCountText.width - Theme.paddingMedium wrapMode: Text.Wrap anchors.verticalCenter: unreadMessagesCountText.verticalCenter + maximumLineCount: 2 + elide: Text.ElideRight } } diff --git a/qml/pages/OverviewPage.qml b/qml/pages/OverviewPage.qml index 548beda..bdb9f7f 100644 --- a/qml/pages/OverviewPage.qml +++ b/qml/pages/OverviewPage.qml @@ -35,6 +35,7 @@ Page { property int authorizationState: TelegramAPI.Closed property int connectionState: TelegramAPI.WaitingForNetwork property int ownUserId; + property bool chatListCreated: false; onStatusChanged: { console.log("[OverviewPage] Status changed: " + status + ", initialization completed: " + initializationCompleted); @@ -43,6 +44,17 @@ Page { } } + Timer { + id: chatListCreatedTimer + interval: 500 + running: false + repeat: false + onTriggered: { + overviewPage.chatListCreated = true; + chatListModel.uiCreated(); + } + } + BusyLabel { text: qsTr("Loading...") running: overviewPage.loading @@ -117,9 +129,17 @@ Page { onOwnUserIdFound: { overviewPage.ownUserId = ownUserId; } + onChatLastMessageUpdated: { + if (!overviewPage.chatListCreated) { + chatListCreatedTimer.stop(); + chatListCreatedTimer.start(); + } + } onChatOrderUpdated: { - chatListSorterTimer.stop(); - chatListSorterTimer.start(); + if (!overviewPage.chatListCreated) { + chatListCreatedTimer.stop(); + chatListCreatedTimer.start(); + } } } diff --git a/src/chatlistmodel.cpp b/src/chatlistmodel.cpp index ab6a151..68bbfab 100644 --- a/src/chatlistmodel.cpp +++ b/src/chatlistmodel.cpp @@ -4,6 +4,7 @@ ChatListModel::ChatListModel(TDLibWrapper *tdLibWrapper) { this->tdLibWrapper = tdLibWrapper; + this->deltaUpdates = false; connect(this->tdLibWrapper, SIGNAL(newChatDiscovered(QString, QVariantMap)), this, SLOT(handleChatDiscovered(QString, QVariantMap))); connect(this->tdLibWrapper, SIGNAL(chatLastMessageUpdated(QString, QString, QVariantMap)), this, SLOT(handleChatLastMessageUpdated(QString, QString, QVariantMap))); connect(this->tdLibWrapper, SIGNAL(chatOrderUpdated(QString, QString)), this, SLOT(handleChatOrderUpdated(QString, QString))); @@ -37,6 +38,13 @@ bool ChatListModel::insertRows(int row, int count, const QModelIndex &parent) return true; } +void ChatListModel::uiCreated() +{ + qDebug() << "[ChatListModel] Chat list on UI created, enabling delta updates..."; + layoutChanged(); + this->deltaUpdates = true; +} + bool compareChats(const QVariant &chat1, const QVariant &chat2) { QVariantMap chatMap1 = chat1.toMap(); @@ -102,22 +110,33 @@ void ChatListModel::updateChatOrder(const int ¤tChatIndex, const QVariantM // Other alternative layoutChanged() after sorting resets the index position - there we would need to calculate the new position as well // If somebody has a better solution - go for it ;) int newChatIndex = 0; + QVariantMap previousChat; for (int i = 0; i < this->chatList.length(); i++) { QVariantMap otherChat = this->chatList.at(i).toMap(); if (compareChats(updatedChat, otherChat)) { - newChatIndex = i; + if (previousChat.value("id") == updatedChat.value("id")) { + newChatIndex = currentChatIndex; + } else { + newChatIndex = i; + } break; } + previousChat = otherChat; } if (newChatIndex != currentChatIndex) { // The updated chat now needs to go to the position of the other chat qDebug() << "[ChatListModel] Chat " << updatedChat.value("id").toString() << " will be moved from position " << currentChatIndex << " to " << newChatIndex; - beginMoveRows(QModelIndex(), currentChatIndex, currentChatIndex, QModelIndex(), (( newChatIndex < currentChatIndex ) ? newChatIndex : ( newChatIndex + 1 ))); + if (deltaUpdates) { + beginMoveRows(QModelIndex(), currentChatIndex, currentChatIndex, QModelIndex(), (( newChatIndex < currentChatIndex ) ? newChatIndex : ( newChatIndex + 1 ))); + } std::sort(this->chatList.begin(), this->chatList.end(), compareChats); this->chatIndexMap.clear(); for (int i = 0; i < this->chatList.length(); i++) { this->chatIndexMap.insert(this->chatList.at(i).toMap().value("id").toString(), i); } - endMoveRows(); + if (deltaUpdates) { + endMoveRows(); + } + } } diff --git a/src/chatlistmodel.h b/src/chatlistmodel.h index c029223..9a55741 100644 --- a/src/chatlistmodel.h +++ b/src/chatlistmodel.h @@ -17,6 +17,8 @@ public: virtual QVariant data(const QModelIndex &index, int role) const override; virtual bool insertRows(int row, int count, const QModelIndex &parent = QModelIndex()) override; + Q_INVOKABLE void uiCreated(); + public slots: void handleChatDiscovered(const QString &chatId, const QVariantMap &chatInformation); void handleChatLastMessageUpdated(const QString &chatId, const QString &order, const QVariantMap &lastMessage); @@ -28,6 +30,7 @@ private: QVariantMap chatToBeAdded; QVariantMap chatIndexMap; QMutex chatListMutex; + bool deltaUpdates; void updateChatOrder(const int ¤tChatIndex, const QVariantMap &updatedChat);