Completed automated sorting, proper UI updates still missing

This commit is contained in:
Sebastian J. Wolf 2020-08-20 17:06:47 +02:00
parent 3948920a93
commit d29b3bd79b
3 changed files with 41 additions and 37 deletions

View file

@ -48,16 +48,6 @@ Page {
running: overviewPage.loading running: overviewPage.loading
} }
Timer {
id: chatListSorterTimer
interval: 100
running: false
repeat: false
onTriggered: {
chatListModel.updateSorting();
}
}
function setPageStatus() { function setPageStatus() {
switch (overviewPage.connectionState) { switch (overviewPage.connectionState) {
case TelegramAPI.WaitingForNetwork: case TelegramAPI.WaitingForNetwork:
@ -131,10 +121,6 @@ Page {
chatListSorterTimer.stop(); chatListSorterTimer.stop();
chatListSorterTimer.start(); chatListSorterTimer.start();
} }
onChatLastMessageUpdated: {
chatListSorterTimer.stop();
chatListSorterTimer.start();
}
} }
Component.onCompleted: { Component.onCompleted: {

View file

@ -53,23 +53,6 @@ bool compareChats(const QVariant &chat1, const QVariant &chat2)
} }
} }
void ChatListModel::updateSorting()
{
this->chatListMutex.lock();
qDebug() << "[ChatListModel] List sorting will be updated...";
emit layoutAboutToBeChanged();
std::sort(this->chatList.begin(), this->chatList.end(), compareChats);
this->chatIndexMap.clear();
for (int i = 0; i < this->chatList.length(); i++) {
QString currentChatId = this->chatList.at(i).toMap().value("id").toString();
this->chatIndexMap.insert(currentChatId, i);
}
emit layoutChanged();
this->chatListMutex.unlock();
}
void ChatListModel::handleChatDiscovered(const QString &chatId, const QVariantMap &chatInformation) void ChatListModel::handleChatDiscovered(const QString &chatId, const QVariantMap &chatInformation)
{ {
this->chatListMutex.lock(); this->chatListMutex.lock();
@ -79,6 +62,8 @@ void ChatListModel::handleChatDiscovered(const QString &chatId, const QVariantMa
this->chatListMutex.unlock(); this->chatListMutex.unlock();
} }
void ChatListModel::handleChatLastMessageUpdated(const QString &chatId, const QString &order, const QVariantMap &lastMessage) void ChatListModel::handleChatLastMessageUpdated(const QString &chatId, const QString &order, const QVariantMap &lastMessage)
{ {
this->chatListMutex.lock(); this->chatListMutex.lock();
@ -89,6 +74,9 @@ void ChatListModel::handleChatLastMessageUpdated(const QString &chatId, const QS
currentChat.insert("order", order); currentChat.insert("order", order);
this->chatList.replace(chatIndex, currentChat); this->chatList.replace(chatIndex, currentChat);
emit dataChanged(this->index(chatIndex), this->index(chatIndex)); emit dataChanged(this->index(chatIndex), this->index(chatIndex));
this->updateChatOrder(chatIndex, currentChat);
this->chatListMutex.unlock(); this->chatListMutex.unlock();
} }
@ -96,10 +84,40 @@ void ChatListModel::handleChatOrderUpdated(const QString &chatId, const QString
{ {
this->chatListMutex.lock(); this->chatListMutex.lock();
qDebug() << "[ChatListModel] Updating chat order because of " << chatId << " new order " << order; qDebug() << "[ChatListModel] Updating chat order because of " << chatId << " new order " << order;
int currentChatIndex = this->chatIndexMap.value(chatId).toInt(); int chatIndex = this->chatIndexMap.value(chatId).toInt();
QVariantMap currentChat = this->chatList.at(currentChatIndex).toMap(); QVariantMap currentChat = this->chatList.at(chatIndex).toMap();
currentChat.insert("order", order); currentChat.insert("order", order);
this->chatList.replace(currentChatIndex, currentChat); this->chatList.replace(chatIndex, currentChat);
emit dataChanged(this->index(currentChatIndex), this->index(currentChatIndex)); emit dataChanged(this->index(chatIndex), this->index(chatIndex));
this->updateChatOrder(chatIndex, currentChat);
this->chatListMutex.unlock(); this->chatListMutex.unlock();
} }
void ChatListModel::updateChatOrder(const int &currentChatIndex, const QVariantMap &updatedChat)
{
// Finding the new position manually as information is needed by beginMoveRows()
// This seems to be the most convenient way of persisting the list position while changing the items
// 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;
for (int i = 0; i < this->chatList.length(); i++) {
QVariantMap otherChat = this->chatList.at(i).toMap();
if (compareChats(updatedChat, otherChat)) {
newChatIndex = i;
break;
}
}
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 )));
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();
}
}

View file

@ -17,8 +17,6 @@ public:
virtual QVariant data(const QModelIndex &index, int role) const override; virtual QVariant data(const QModelIndex &index, int role) const override;
virtual bool insertRows(int row, int count, const QModelIndex &parent = QModelIndex()) override; virtual bool insertRows(int row, int count, const QModelIndex &parent = QModelIndex()) override;
Q_INVOKABLE void updateSorting();
public slots: public slots:
void handleChatDiscovered(const QString &chatId, const QVariantMap &chatInformation); void handleChatDiscovered(const QString &chatId, const QVariantMap &chatInformation);
void handleChatLastMessageUpdated(const QString &chatId, const QString &order, const QVariantMap &lastMessage); void handleChatLastMessageUpdated(const QString &chatId, const QString &order, const QVariantMap &lastMessage);
@ -31,6 +29,8 @@ private:
QVariantMap chatIndexMap; QVariantMap chatIndexMap;
QMutex chatListMutex; QMutex chatListMutex;
void updateChatOrder(const int &currentChatIndex, const QVariantMap &updatedChat);
}; };
#endif // CHATLISTMODEL_H #endif // CHATLISTMODEL_H