Sorting and updating is complicated

This commit is contained in:
Sebastian J. Wolf 2020-08-20 14:58:32 +02:00
parent 0a04169f4f
commit 3948920a93
7 changed files with 19 additions and 14 deletions

View file

@ -50,7 +50,7 @@ Page {
Timer {
id: chatListSorterTimer
interval: 1000
interval: 100
running: false
repeat: false
onTriggered: {
@ -131,6 +131,10 @@ Page {
chatListSorterTimer.stop();
chatListSorterTimer.start();
}
onChatLastMessageUpdated: {
chatListSorterTimer.stop();
chatListSorterTimer.start();
}
}
Component.onCompleted: {

View file

@ -5,7 +5,7 @@ ChatListModel::ChatListModel(TDLibWrapper *tdLibWrapper)
{
this->tdLibWrapper = tdLibWrapper;
connect(this->tdLibWrapper, SIGNAL(newChatDiscovered(QString, QVariantMap)), this, SLOT(handleChatDiscovered(QString, QVariantMap)));
connect(this->tdLibWrapper, SIGNAL(chatLastMessageUpdated(QString, QVariantMap)), this, SLOT(handleChatLastMessageUpdated(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)));
}
@ -58,7 +58,7 @@ void ChatListModel::updateSorting()
this->chatListMutex.lock();
qDebug() << "[ChatListModel] List sorting will be updated...";
beginResetModel();
emit layoutAboutToBeChanged();
std::sort(this->chatList.begin(), this->chatList.end(), compareChats);
this->chatIndexMap.clear();
@ -66,7 +66,7 @@ void ChatListModel::updateSorting()
QString currentChatId = this->chatList.at(i).toMap().value("id").toString();
this->chatIndexMap.insert(currentChatId, i);
}
endResetModel();
emit layoutChanged();
this->chatListMutex.unlock();
}
@ -79,13 +79,14 @@ void ChatListModel::handleChatDiscovered(const QString &chatId, const QVariantMa
this->chatListMutex.unlock();
}
void ChatListModel::handleChatLastMessageUpdated(const QString &chatId, const QVariantMap &lastMessage)
void ChatListModel::handleChatLastMessageUpdated(const QString &chatId, const QString &order, const QVariantMap &lastMessage)
{
this->chatListMutex.lock();
int chatIndex = this->chatIndexMap.value(chatId).toInt();
qDebug() << "[ChatListModel] Updating last message for chat " << chatId << " at index " << chatIndex;
QVariantMap currentChat = this->chatList.value(chatIndex).toMap();
currentChat.insert("last_message", lastMessage);
currentChat.insert("order", order);
this->chatList.replace(chatIndex, currentChat);
emit dataChanged(this->index(chatIndex), this->index(chatIndex));
this->chatListMutex.unlock();

View file

@ -21,7 +21,7 @@ public:
public slots:
void handleChatDiscovered(const QString &chatId, const QVariantMap &chatInformation);
void handleChatLastMessageUpdated(const QString &chatId, const QVariantMap &lastMessage);
void handleChatLastMessageUpdated(const QString &chatId, const QString &order, const QVariantMap &lastMessage);
void handleChatOrderUpdated(const QString &chatId, const QString &order);
private:

View file

@ -141,8 +141,8 @@ void TDLibReceiver::processUpdateUnreadChatCount(const QVariantMap &receivedInfo
void TDLibReceiver::processUpdateChatLastMessage(const QVariantMap &receivedInformation)
{
QVariantMap lastMessage = receivedInformation.value("last_message").toMap();
qDebug() << "[TDLibReceiver] Last message of chat " << receivedInformation.value("chat_id").toString() << " updated, content: " << lastMessage.value("content").toString();
emit chatLastMessageUpdated(receivedInformation.value("chat_id").toString(), lastMessage);
qDebug() << "[TDLibReceiver] Last message of chat " << receivedInformation.value("chat_id").toString() << " updated, order " << receivedInformation.value("order").toString() << " content: " << lastMessage.value("content").toString();
emit chatLastMessageUpdated(receivedInformation.value("chat_id").toString(), receivedInformation.value("order").toString(), lastMessage);
}
void TDLibReceiver::processUpdateChatOrder(const QVariantMap &receivedInformation)

View file

@ -45,7 +45,7 @@ signals:
void newChatDiscovered(const QVariantMap &chatInformation);
void unreadMessageCountUpdated(const QVariantMap &messageCountInformation);
void unreadChatCountUpdated(const QVariantMap &chatCountInformation);
void chatLastMessageUpdated(const QString &chatId, const QVariantMap &lastMessage);
void chatLastMessageUpdated(const QString &chatId, const QString &order, const QVariantMap &lastMessage);
void chatOrderUpdated(const QString &chatId, const QString &order);
private:

View file

@ -45,7 +45,7 @@ TDLibWrapper::TDLibWrapper(QObject *parent) : QObject(parent)
connect(this->tdLibReceiver, SIGNAL(newChatDiscovered(QVariantMap)), this, SLOT(handleNewChatDiscovered(QVariantMap)));
connect(this->tdLibReceiver, SIGNAL(unreadMessageCountUpdated(QVariantMap)), this, SLOT(handleUnreadMessageCountUpdated(QVariantMap)));
connect(this->tdLibReceiver, SIGNAL(unreadChatCountUpdated(QVariantMap)), this, SLOT(handleUnreadChatCountUpdated(QVariantMap)));
connect(this->tdLibReceiver, SIGNAL(chatLastMessageUpdated(QString, QVariantMap)), this, SLOT(handleChatLastMessageUpdated(QString, QVariantMap)));
connect(this->tdLibReceiver, SIGNAL(chatLastMessageUpdated(QString, QString, QVariantMap)), this, SLOT(handleChatLastMessageUpdated(QString, QString, QVariantMap)));
connect(this->tdLibReceiver, SIGNAL(chatOrderUpdated(QString, QString)), this, SLOT(handleChatOrderUpdated(QString, QString)));
this->tdLibReceiver->start();
@ -277,9 +277,9 @@ void TDLibWrapper::handleUnreadChatCountUpdated(const QVariantMap &chatCountInfo
}
}
void TDLibWrapper::handleChatLastMessageUpdated(const QString &chatId, const QVariantMap &lastMessage)
void TDLibWrapper::handleChatLastMessageUpdated(const QString &chatId, const QString &order, const QVariantMap &lastMessage)
{
emit chatLastMessageUpdated(chatId, lastMessage);
emit chatLastMessageUpdated(chatId, order, lastMessage);
}
void TDLibWrapper::handleChatOrderUpdated(const QString &chatId, const QString &order)

View file

@ -83,7 +83,7 @@ signals:
void newChatDiscovered(const QString &chatId, const QVariantMap &chatInformation);
void unreadMessageCountUpdated(const QVariantMap &messageCountInformation);
void unreadChatCountUpdated(const QVariantMap &chatCountInformation);
void chatLastMessageUpdated(const QString &chatId, const QVariantMap &lastMessage);
void chatLastMessageUpdated(const QString &chatId, const QString &order, const QVariantMap &lastMessage);
void chatOrderUpdated(const QString &chatId, const QString &order);
public slots:
@ -96,7 +96,7 @@ public slots:
void handleNewChatDiscovered(const QVariantMap &chatInformation);
void handleUnreadMessageCountUpdated(const QVariantMap &messageCountInformation);
void handleUnreadChatCountUpdated(const QVariantMap &chatCountInformation);
void handleChatLastMessageUpdated(const QString &chatId, const QVariantMap &lastMessage);
void handleChatLastMessageUpdated(const QString &chatId, const QString &order, const QVariantMap &lastMessage);
void handleChatOrderUpdated(const QString &chatId, const QString &order);
private:
void *tdLibClient;