Surrendering - trying delta updates only after successful initialization

This commit is contained in:
Sebastian J. Wolf 2020-08-20 18:45:56 +02:00
parent d29b3bd79b
commit d87b5b84fb
4 changed files with 49 additions and 5 deletions

View file

@ -127,6 +127,8 @@ CoverBackground {
width: parent.width - unreadMessagesCountText.width - Theme.paddingMedium
wrapMode: Text.Wrap
anchors.verticalCenter: unreadMessagesCountText.verticalCenter
maximumLineCount: 2
elide: Text.ElideRight
}
}

View file

@ -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();
}
}
}

View file

@ -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 &currentChatIndex, 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();
}
}
}

View file

@ -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 &currentChatIndex, const QVariantMap &updatedChat);