From 5c16a13b2514a02c05f81ead9ac5c972dc4ccdf2 Mon Sep 17 00:00:00 2001 From: "Sebastian J. Wolf" Date: Sat, 29 Aug 2020 21:39:57 +0200 Subject: [PATCH] Add display of unread message count to chat --- qml/pages/ChatPage.qml | 55 +++++++++++++++++++++++++++++++++++------ qml/pages/CoverPage.qml | 2 +- src/chatmodel.cpp | 29 ++++++++++++++++------ src/chatmodel.h | 9 ++++--- 4 files changed, 77 insertions(+), 18 deletions(-) diff --git a/qml/pages/ChatPage.qml b/qml/pages/ChatPage.qml index e8acbeb..9a4eb68 100644 --- a/qml/pages/ChatPage.qml +++ b/qml/pages/ChatPage.qml @@ -84,6 +84,7 @@ Page { } function initializePage() { + chatView.currentIndex = -1; var chatType = chatInformation.type['@type']; isPrivateChat = ( chatType === "chatTypePrivate" ); isBasicGroup = ( chatType === "chatTypeBasicGroup" ); @@ -112,7 +113,7 @@ Page { tdLibWrapper.openChat(chatInformation.id); } if (status === PageStatus.Active) { - chatModel.initialize(chatInformation.id); + chatModel.initialize(chatInformation); } if (status === PageStatus.Deactivating) { tdLibWrapper.closeChat(chatInformation.id); @@ -151,11 +152,18 @@ Page { Connections { target: chatModel onMessagesReceived: { - chatPage.loading = false; + console.log("[ChatPage] Messages received, view has " + chatView.count + " messages, setting view to index " + modelIndex); + chatView.currentIndex = modelIndex; } onNewMessageReceived: { // Notify user about new messages... } + onUnreadCountUpdated: { + console.log("[ChatPage] Unread count updated, new count: " + unreadCount); + chatInformation.unread_count = unreadCount; + chatUnreadMessagesCountBackground.visible = ( !chatPage.loading && unreadCount > 0 ) + chatUnreadMessagesCount.text = unreadCount > 99 ? "99+" : unreadCount + } } Timer { @@ -239,6 +247,16 @@ Page { width: parent.width height: parent.height - ( 2 * Theme.paddingMedium ) - headerRow.height - newMessageColumn.height + Timer { + id: chatViewLoadingTimer + interval: 500 + repeat: false + running: false + onTriggered: { + chatPage.loading = false; + } + } + SilicaListView { id: chatView @@ -246,9 +264,9 @@ Page { opacity: chatPage.loading ? 0 : 1 Behavior on opacity { NumberAnimation {} } - visible: !chatPage.loading clip: true + highlightFollowsCurrentItem: true function handleScrollPositionChanged() { tdLibWrapper.viewMessage(chatInformation.id, chatView.itemAt(chatView.contentX, ( chatView.contentY + chatView.height - Theme.horizontalPageMargin )).myMessage.id); @@ -257,6 +275,10 @@ Page { // } } + onContentYChanged: { + chatViewLoadingTimer.start(); + } + onMovementEnded: { handleScrollPositionChanged(); } @@ -267,10 +289,6 @@ Page { } } - onCurrentIndexChanged: { - tdLibWrapper.viewMessage(chatInformation.id, currentItem.myMessage.id); - } - model: chatModel delegate: ListItem { @@ -492,6 +510,29 @@ Page { } } + Rectangle { + id: chatUnreadMessagesCountBackground + color: Theme.highlightBackgroundColor + width: Theme.fontSizeHuge + height: Theme.fontSizeHuge + anchors.right: parent.right + anchors.rightMargin: Theme.paddingMedium + anchors.bottom: parent.bottom + anchors.bottomMargin: Theme.paddingMedium + radius: width / 2 + visible: !chatPage.loading && chatInformation.unread_count > 0 + } + + Text { + id: chatUnreadMessagesCount + font.pixelSize: Theme.fontSizeMedium + font.bold: true + color: Theme.primaryColor + anchors.centerIn: chatUnreadMessagesCountBackground + visible: chatUnreadMessagesCountBackground.visible + text: chatInformation.unread_count > 99 ? "99+" : chatInformation.unread_count + } + } Column { diff --git a/qml/pages/CoverPage.qml b/qml/pages/CoverPage.qml index 1b1fce0..eb4ae6d 100644 --- a/qml/pages/CoverPage.qml +++ b/qml/pages/CoverPage.qml @@ -165,7 +165,7 @@ CoverBackground { color: Theme.highlightColor visible: coverPage.authenticated width: parent.width - maximumLineCount: 2 + maximumLineCount: 3 wrapMode: Text.Wrap } } diff --git a/src/chatmodel.cpp b/src/chatmodel.cpp index 0782acc..17f3e9b 100644 --- a/src/chatmodel.cpp +++ b/src/chatmodel.cpp @@ -11,6 +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))); } ChatModel::~ChatModel() @@ -46,13 +47,14 @@ bool ChatModel::insertRows(int row, int count, const QModelIndex &parent) return true; } -void ChatModel::initialize(const QString &chatId) +void ChatModel::initialize(const QVariantMap &chatInformation) { - this->chatId = chatId; + this->chatInformation = chatInformation; this->messages.clear(); this->messageIndexMap.clear(); this->messagesToBeAdded.clear(); - tdLibWrapper->getChatHistory(chatId); + this->chatId = chatInformation.value("id").toString(); + tdLibWrapper->getChatHistory(this->chatId); } void ChatModel::triggerLoadMoreHistory() @@ -84,9 +86,9 @@ void ChatModel::handleMessagesReceived(const QVariantList &messages) this->inReload = false; if (this->inIncrementalUpdate) { this->inIncrementalUpdate = false; - emit messagesIncrementalUpdate(); + emit messagesIncrementalUpdate(this->messages.size() - 1); } else { - emit messagesReceived(); + emit messagesReceived(this->messages.size() - 1); } } else { this->messagesMutex.lock(); @@ -103,6 +105,10 @@ void ChatModel::handleMessagesReceived(const QVariantList &messages) this->insertMessages(); this->messagesMutex.unlock(); + QString lastKnownMessageId = this->chatInformation.value("last_read_inbox_message_id").toString(); + int listPosition = this->messageIndexMap.value(lastKnownMessageId, this->messages.size() - 1).toInt(); + qDebug() << "[ChatModel] Last known message is at position" << listPosition; + // First call only returns a few messages, we need to get a little more than that... if (this->messagesToBeAdded.size() < 10 && !this->inReload) { qDebug() << "[ChatModel] Only a few messages received in first call, loading more..."; @@ -113,9 +119,9 @@ void ChatModel::handleMessagesReceived(const QVariantList &messages) this->inReload = false; if (this->inIncrementalUpdate) { this->inIncrementalUpdate = false; - emit messagesIncrementalUpdate(); + emit messagesIncrementalUpdate(listPosition); } else { - emit messagesReceived(); + emit messagesReceived(listPosition); } } } @@ -137,6 +143,15 @@ void ChatModel::handleNewMessageReceived(const QString &chatId, const QVariantMa } } +void ChatModel::handleChatReadInboxUpdated(const QString &chatId, const int &unreadCount) +{ + if (chatId == this->chatId) { + qDebug() << "[ChatModel] Updating chat unread count, unread messages " << unreadCount; + this->chatInformation.insert("unread_count", unreadCount); + emit unreadCountUpdated(unreadCount); + } +} + void ChatModel::insertMessages() { if (this->messages.isEmpty()) { diff --git a/src/chatmodel.h b/src/chatmodel.h index b2c8f49..288bc10 100644 --- a/src/chatmodel.h +++ b/src/chatmodel.h @@ -17,17 +17,19 @@ 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 initialize(const QString &chatId); + Q_INVOKABLE void initialize(const QVariantMap &chatInformation); Q_INVOKABLE void triggerLoadMoreHistory(); signals: - void messagesReceived(); - void messagesIncrementalUpdate(); + void messagesReceived(const int &modelIndex); + void messagesIncrementalUpdate(const int &modelIndex); void newMessageReceived(); + void unreadCountUpdated(const int &unreadCount); public slots: void handleMessagesReceived(const QVariantList &messages); void handleNewMessageReceived(const QString &chatId, const QVariantMap &message); + void handleChatReadInboxUpdated(const QString &chatId, const int &unreadCount); private: @@ -36,6 +38,7 @@ private: QVariantList messagesToBeAdded; QVariantMap messageIndexMap; QMutex messagesMutex; + QVariantMap chatInformation; QString chatId; bool inReload; bool inIncrementalUpdate;