From a04332d4b6b582c76b7548a863cec3ebf65dc76e Mon Sep 17 00:00:00 2001 From: "Sebastian J. Wolf" Date: Mon, 31 Aug 2020 21:51:52 +0200 Subject: [PATCH] Fix chat positioning and sent/read indicator --- qml/pages/ChatPage.qml | 92 +++++++++++++----------------------------- src/chatlistmodel.cpp | 37 +++++++++++++++-- src/chatlistmodel.h | 4 +- src/chatmodel.cpp | 9 +++-- src/chatmodel.h | 4 +- src/tdlibreceiver.cpp | 2 +- src/tdlibreceiver.h | 2 +- src/tdlibwrapper.cpp | 6 +-- src/tdlibwrapper.h | 4 +- 9 files changed, 78 insertions(+), 82 deletions(-) diff --git a/qml/pages/ChatPage.qml b/qml/pages/ChatPage.qml index 8434cd4..02fa1ad 100644 --- a/qml/pages/ChatPage.qml +++ b/qml/pages/ChatPage.qml @@ -107,6 +107,30 @@ Page { } } + function getMessageStatusText(message, listItemIndex, lastReadSentIndex) { + var messageStatusSuffix = ""; + if (chatPage.myUserId === message.sender_user_id) { + messageStatusSuffix += "  " + if (listItemIndex <= lastReadSentIndex) { + // Read by other party + messageStatusSuffix += Emoji.emojify("✅", Theme.fontSizeTiny); + } else { + // Not yet read by other party + if (message.sending_state) { + if (message.sending_state['@type'] === "messageSendingStatePending") { + messageStatusSuffix += Emoji.emojify("🕙", Theme.fontSizeTiny); + } else { + // Sending failed... + messageStatusSuffix += Emoji.emojify("❌", Theme.fontSizeTiny); + } + } else { + messageStatusSuffix += Emoji.emojify("☑️", Theme.fontSizeTiny); + } + } + } + return Functions.getDateTimeElapsed(message.date) + messageStatusSuffix; + } + Component.onCompleted: { initializePage(); } @@ -482,26 +506,7 @@ Page { running: true repeat: true onTriggered: { - var messageStatusSuffix = ""; - if (chatPage.myUserId === display.sender_user_id) { - if (index <= chatView.lastReadSentIndex) { - // Read by other party - messageStatusSuffix += Emoji.emojify("✅", Theme.fontSizeTiny); - } else { - // Not yet read by other party - if (display.sending_state) { - if (display.sending_state['@type'] === "messageSendingStatePending") { - messageStatusSuffix += Emoji.emojify("🕙", Theme.fontSizeTiny); - } else { - // Sending failed... - messageStatusSuffix += Emoji.emojify("❌", Theme.fontSizeTiny); - } - } else { - messageStatusSuffix += Emoji.emojify("☑️", Theme.fontSizeTiny); - } - } - } - messageDateText.text = Functions.getDateTimeElapsed(display.date) + messageStatusSuffix; + messageDateText.text = getMessageStatusText(display, index, chatView.lastReadSentIndex); } } @@ -509,61 +514,18 @@ Page { target: chatModel onLastReadSentMessageUpdated: { console.log("[ChatModel] Messages in this chat were read, new last read: " + lastReadSentIndex + ", updating description for index " + index + ", status: " + (index <= lastReadSentIndex)); - var messageStatusSuffix = ""; - if (chatPage.myUserId === display.sender_user_id) { - if (index <= lastReadSentIndex) { - // Read by other party - messageStatusSuffix += Emoji.emojify("✅", Theme.fontSizeTiny); - } else { - // Not yet read by other party - if (display.sending_state) { - if (display.sending_state['@type'] === "messageSendingStatePending") { - messageStatusSuffix += Emoji.emojify("🕙", Theme.fontSizeTiny); - } else { - // Sending failed... - messageStatusSuffix += Emoji.emojify("❌", Theme.fontSizeTiny); - } - } else { - messageStatusSuffix += Emoji.emojify("☑️", Theme.fontSizeTiny); - } - } - } - messageDateText.text = Functions.getDateTimeElapsed(display.date) + messageStatusSuffix; + messageDateText.text = getMessageStatusText(display, index, lastReadSentIndex); } } Text { - - Component.onCompleted: { - var messageStatusSuffix = ""; - if (chatPage.myUserId === display.sender_user_id) { - messageStatusSuffix += " - " - if (index <= chatView.lastReadSentIndex) { - // Read by other party - messageStatusSuffix += Emoji.emojify("✅", Theme.fontSizeTiny); - } else { - // Not yet read by other party - if (display.sending_state) { - if (display.sending_state['@type'] === "messageSendingStatePending") { - messageStatusSuffix += Emoji.emojify("🕙", Theme.fontSizeTiny); - } else { - // Sending failed... - messageStatusSuffix += Emoji.emojify("❌", Theme.fontSizeTiny); - } - } else { - messageStatusSuffix += Emoji.emojify("☑️", Theme.fontSizeTiny); - } - } - } - text = Functions.getDateTimeElapsed(display.date) + messageStatusSuffix; - } - width: parent.width id: messageDateText font.pixelSize: Theme.fontSizeTiny color: (chatPage.myUserId === display.sender_user_id) ? Theme.secondaryHighlightColor : Theme.secondaryColor horizontalAlignment: (chatPage.myUserId === display.sender_user_id) ? Text.AlignRight : Text.AlignLeft + text: getMessageStatusText(display, index, chatView.lastReadSentIndex) } } diff --git a/src/chatlistmodel.cpp b/src/chatlistmodel.cpp index 04f2f53..8a46a3b 100644 --- a/src/chatlistmodel.cpp +++ b/src/chatlistmodel.cpp @@ -8,7 +8,9 @@ ChatListModel::ChatListModel(TDLibWrapper *tdLibWrapper) 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))); - connect(this->tdLibWrapper, SIGNAL(chatReadInboxUpdated(QString, int)), this, SLOT(handleChatReadInboxUpdated(QString, int))); + connect(this->tdLibWrapper, SIGNAL(chatReadInboxUpdated(QString, QString, int)), this, SLOT(handleChatReadInboxUpdated(QString, QString, int))); + connect(this->tdLibWrapper, SIGNAL(chatReadOutboxUpdated(QString, QString)), this, SLOT(handleChatReadOutboxUpdated(QString, QString))); + connect(this->tdLibWrapper, SIGNAL(messageSendSucceeded(QString, QString, QVariantMap)), this, SLOT(handleMessageSendSucceeded(QString, QString, QVariantMap))); } ChatListModel::~ChatListModel() @@ -106,19 +108,48 @@ void ChatListModel::handleChatOrderUpdated(const QString &chatId, const QString this->chatListMutex.unlock(); } -void ChatListModel::handleChatReadInboxUpdated(const QString &chatId, const int &unreadCount) +void ChatListModel::handleChatReadInboxUpdated(const QString &chatId, const QString &lastReadInboxMessageId, const int &unreadCount) { this->chatListMutex.lock(); - qDebug() << "[ChatListModel] Updating chat unread count for " << chatId << " unread messages " << unreadCount; + qDebug() << "[ChatListModel] Updating chat unread count for " << chatId << " unread messages " << unreadCount << ", last read message ID: " << lastReadInboxMessageId; int chatIndex = this->chatIndexMap.value(chatId).toInt(); QVariantMap currentChat = this->chatList.at(chatIndex).toMap(); currentChat.insert("unread_count", unreadCount); + currentChat.insert("last_read_inbox_message_id", lastReadInboxMessageId); this->chatList.replace(chatIndex, currentChat); emit dataChanged(this->index(chatIndex), this->index(chatIndex)); emit chatChanged(chatId); this->chatListMutex.unlock(); } +void ChatListModel::handleChatReadOutboxUpdated(const QString &chatId, const QString &lastReadOutboxMessageId) +{ + this->chatListMutex.lock(); + qDebug() << "[ChatListModel] Updating last read message for " << chatId << " last ID " << lastReadOutboxMessageId; + int chatIndex = this->chatIndexMap.value(chatId).toInt(); + QVariantMap currentChat = this->chatList.at(chatIndex).toMap(); + currentChat.insert("last_read_outbox_message_id", lastReadOutboxMessageId); + this->chatList.replace(chatIndex, currentChat); + emit dataChanged(this->index(chatIndex), this->index(chatIndex)); + emit chatChanged(chatId); + this->chatListMutex.unlock(); +} + +void ChatListModel::handleMessageSendSucceeded(const QString &messageId, const QString &oldMessageId, const QVariantMap &message) +{ + this->chatListMutex.lock(); + QString chatId = message.value("chat_id").toString(); + int chatIndex = this->chatIndexMap.value(chatId).toInt(); + qDebug() << "[ChatListModel] Updating last message for chat " << chatId << " at index " << chatIndex << ", as message was sent, old ID: " << oldMessageId << ", new ID: " << messageId; + QVariantMap currentChat = this->chatList.value(chatIndex).toMap(); + currentChat.insert("last_message", message); + this->chatList.replace(chatIndex, currentChat); + emit dataChanged(this->index(chatIndex), this->index(chatIndex)); + emit chatChanged(chatId); + + this->chatListMutex.unlock(); +} + void ChatListModel::updateChatOrder(const int ¤tChatIndex, const QVariantMap &updatedChat) { // Finding the new position manually as information is needed by beginMoveRows() diff --git a/src/chatlistmodel.h b/src/chatlistmodel.h index 1b73c50..2fbc8d1 100644 --- a/src/chatlistmodel.h +++ b/src/chatlistmodel.h @@ -26,7 +26,9 @@ public slots: void handleChatDiscovered(const QString &chatId, const QVariantMap &chatInformation); void handleChatLastMessageUpdated(const QString &chatId, const QString &order, const QVariantMap &lastMessage); void handleChatOrderUpdated(const QString &chatId, const QString &order); - void handleChatReadInboxUpdated(const QString &chatId, const int &unreadCount); + void handleChatReadInboxUpdated(const QString &chatId, const QString &lastReadInboxMessageId, const int &unreadCount); + void handleChatReadOutboxUpdated(const QString &chatId, const QString &lastReadOutboxMessageId); + void handleMessageSendSucceeded(const QString &messageId, const QString &oldMessageId, const QVariantMap &message); private: TDLibWrapper *tdLibWrapper; diff --git a/src/chatmodel.cpp b/src/chatmodel.cpp index 445f30f..86ca4ef 100644 --- a/src/chatmodel.cpp +++ b/src/chatmodel.cpp @@ -11,7 +11,7 @@ ChatModel::ChatModel(TDLibWrapper *tdLibWrapper) this->inIncrementalUpdate = false; connect(this->tdLibWrapper, SIGNAL(messagesReceived(QVariantList)), this, SLOT(handleMessagesReceived(QVariantList))); connect(this->tdLibWrapper, SIGNAL(newMessageReceived(QString, QVariantMap)), this, SLOT(handleNewMessageReceived(QString, QVariantMap))); - connect(this->tdLibWrapper, SIGNAL(chatReadInboxUpdated(QString, int)), this, SLOT(handleChatReadInboxUpdated(QString, int))); + connect(this->tdLibWrapper, SIGNAL(chatReadInboxUpdated(QString, QString, int)), this, SLOT(handleChatReadInboxUpdated(QString, QString, int))); connect(this->tdLibWrapper, SIGNAL(chatReadOutboxUpdated(QString, QString)), this, SLOT(handleChatReadOutboxUpdated(QString, QString))); connect(this->tdLibWrapper, SIGNAL(messageSendSucceeded(QString, QString, QVariantMap)), this, SLOT(handleMessageSendSucceeded(QString, QString, QVariantMap))); } @@ -143,12 +143,13 @@ void ChatModel::handleNewMessageReceived(const QString &chatId, const QVariantMa } } -void ChatModel::handleChatReadInboxUpdated(const QString &chatId, const int &unreadCount) +void ChatModel::handleChatReadInboxUpdated(const QString &chatId, const QString &lastReadInboxMessageId, const int &unreadCount) { if (chatId == this->chatId) { - qDebug() << "[ChatModel] Updating chat unread count, unread messages " << unreadCount; + qDebug() << "[ChatModel] Updating chat unread count, unread messages " << unreadCount << ", last read message ID: " << lastReadInboxMessageId; this->chatInformation.insert("unread_count", unreadCount); - emit unreadCountUpdated(unreadCount); + this->chatInformation.insert("last_read_inbox_message_id", lastReadInboxMessageId); + emit unreadCountUpdated(unreadCount, lastReadInboxMessageId); } } diff --git a/src/chatmodel.h b/src/chatmodel.h index 57f8aa6..5a53eb7 100644 --- a/src/chatmodel.h +++ b/src/chatmodel.h @@ -24,13 +24,13 @@ signals: void messagesReceived(const int &modelIndex, const int &lastReadSentIndex); void messagesIncrementalUpdate(const int &modelIndex, const int &lastReadSentIndex); void newMessageReceived(); - void unreadCountUpdated(const int &unreadCount); + void unreadCountUpdated(const int &unreadCount, const QString &lastReadInboxMessageId); void lastReadSentMessageUpdated(const int &lastReadSentIndex); public slots: void handleMessagesReceived(const QVariantList &messages); void handleNewMessageReceived(const QString &chatId, const QVariantMap &message); - void handleChatReadInboxUpdated(const QString &chatId, const int &unreadCount); + void handleChatReadInboxUpdated(const QString &chatId, const QString &lastReadInboxMessageId, const int &unreadCount); void handleChatReadOutboxUpdated(const QString &chatId, const QString &lastReadOutboxMessageId); void handleMessageSendSucceeded(const QString &messageId, const QString &oldMessageId, const QVariantMap &message); diff --git a/src/tdlibreceiver.cpp b/src/tdlibreceiver.cpp index 719e817..381d585 100644 --- a/src/tdlibreceiver.cpp +++ b/src/tdlibreceiver.cpp @@ -179,7 +179,7 @@ void TDLibReceiver::processUpdateChatOrder(const QVariantMap &receivedInformatio void TDLibReceiver::processUpdateChatReadInbox(const QVariantMap &receivedInformation) { qDebug() << "[TDLibReceiver] Chat read information updated for " << receivedInformation.value("chat_id").toString() << " unread count: " << receivedInformation.value("unread_count").toString(); - emit chatReadInboxUpdated(receivedInformation.value("chat_id").toString(), receivedInformation.value("unread_count").toInt()); + emit chatReadInboxUpdated(receivedInformation.value("chat_id").toString(), receivedInformation.value("last_read_inbox_message_id").toString(), receivedInformation.value("unread_count").toInt()); } void TDLibReceiver::processUpdateChatReadOutbox(const QVariantMap &receivedInformation) diff --git a/src/tdlibreceiver.h b/src/tdlibreceiver.h index 054e334..1eb288f 100644 --- a/src/tdlibreceiver.h +++ b/src/tdlibreceiver.h @@ -48,7 +48,7 @@ signals: void unreadChatCountUpdated(const QVariantMap &chatCountInformation); void chatLastMessageUpdated(const QString &chatId, const QString &order, const QVariantMap &lastMessage); void chatOrderUpdated(const QString &chatId, const QString &order); - void chatReadInboxUpdated(const QString &chatId, const int &unreadCount); + void chatReadInboxUpdated(const QString &chatId, const QString &lastReadInboxMessageId, const int &unreadCount); void chatReadOutboxUpdated(const QString &chatId, const QString &lastReadOutboxMessageId); void basicGroupUpdated(const QString &groupId, const QVariantMap &groupInformation); void superGroupUpdated(const QString &groupId, const QVariantMap &groupInformation); diff --git a/src/tdlibwrapper.cpp b/src/tdlibwrapper.cpp index 20a76ff..82bd14e 100644 --- a/src/tdlibwrapper.cpp +++ b/src/tdlibwrapper.cpp @@ -54,7 +54,7 @@ TDLibWrapper::TDLibWrapper(QObject *parent) : QObject(parent) connect(this->tdLibReceiver, SIGNAL(unreadChatCountUpdated(QVariantMap)), this, SLOT(handleUnreadChatCountUpdated(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))); - connect(this->tdLibReceiver, SIGNAL(chatReadInboxUpdated(QString, int)), this, SLOT(handleChatReadInboxUpdated(QString, int))); + connect(this->tdLibReceiver, SIGNAL(chatReadInboxUpdated(QString, QString, int)), this, SLOT(handleChatReadInboxUpdated(QString, QString, int))); connect(this->tdLibReceiver, SIGNAL(chatReadOutboxUpdated(QString, QString)), this, SLOT(handleChatReadOutboxUpdated(QString, QString))); connect(this->tdLibReceiver, SIGNAL(basicGroupUpdated(QString, QVariantMap)), this, SLOT(handleBasicGroupUpdated(QString, QVariantMap))); connect(this->tdLibReceiver, SIGNAL(superGroupUpdated(QString, QVariantMap)), this, SLOT(handleSuperGroupUpdated(QString, QVariantMap))); @@ -446,9 +446,9 @@ void TDLibWrapper::handleChatOrderUpdated(const QString &chatId, const QString & emit chatOrderUpdated(chatId, order); } -void TDLibWrapper::handleChatReadInboxUpdated(const QString &chatId, const int &unreadCount) +void TDLibWrapper::handleChatReadInboxUpdated(const QString &chatId, const QString &lastReadInboxMessageId, const int &unreadCount) { - emit chatReadInboxUpdated(chatId, unreadCount); + emit chatReadInboxUpdated(chatId, lastReadInboxMessageId, unreadCount); } void TDLibWrapper::handleChatReadOutboxUpdated(const QString &chatId, const QString &lastReadOutboxMessageId) diff --git a/src/tdlibwrapper.h b/src/tdlibwrapper.h index 1c7f7d6..944e9fb 100644 --- a/src/tdlibwrapper.h +++ b/src/tdlibwrapper.h @@ -96,7 +96,7 @@ signals: void unreadChatCountUpdated(const QVariantMap &chatCountInformation); void chatLastMessageUpdated(const QString &chatId, const QString &order, const QVariantMap &lastMessage); void chatOrderUpdated(const QString &chatId, const QString &order); - void chatReadInboxUpdated(const QString &chatId, const int &unreadCount); + void chatReadInboxUpdated(const QString &chatId, const QString &lastReadInboxMessageId, const int &unreadCount); void chatReadOutboxUpdated(const QString &chatId, const QString &lastReadOutboxMessageId); void userUpdated(const QString &userId, const QVariantMap &userInformation); void basicGroupUpdated(const QString &groupId, const QVariantMap &groupInformation); @@ -122,7 +122,7 @@ public slots: void handleUnreadChatCountUpdated(const QVariantMap &chatCountInformation); void handleChatLastMessageUpdated(const QString &chatId, const QString &order, const QVariantMap &lastMessage); void handleChatOrderUpdated(const QString &chatId, const QString &order); - void handleChatReadInboxUpdated(const QString &chatId, const int &unreadCount); + void handleChatReadInboxUpdated(const QString &chatId, const QString &lastReadInboxMessageId, const int &unreadCount); void handleChatReadOutboxUpdated(const QString &chatId, const QString &lastReadOutboxMessageId); void handleBasicGroupUpdated(const QString &groupId, const QVariantMap &groupInformation); void handleSuperGroupUpdated(const QString &groupId, const QVariantMap &groupInformation);