Add display of unread message count to chat

This commit is contained in:
Sebastian J. Wolf 2020-08-29 21:39:57 +02:00
parent cca241ed9a
commit 5c16a13b25
4 changed files with 77 additions and 18 deletions

View file

@ -84,6 +84,7 @@ Page {
} }
function initializePage() { function initializePage() {
chatView.currentIndex = -1;
var chatType = chatInformation.type['@type']; var chatType = chatInformation.type['@type'];
isPrivateChat = ( chatType === "chatTypePrivate" ); isPrivateChat = ( chatType === "chatTypePrivate" );
isBasicGroup = ( chatType === "chatTypeBasicGroup" ); isBasicGroup = ( chatType === "chatTypeBasicGroup" );
@ -112,7 +113,7 @@ Page {
tdLibWrapper.openChat(chatInformation.id); tdLibWrapper.openChat(chatInformation.id);
} }
if (status === PageStatus.Active) { if (status === PageStatus.Active) {
chatModel.initialize(chatInformation.id); chatModel.initialize(chatInformation);
} }
if (status === PageStatus.Deactivating) { if (status === PageStatus.Deactivating) {
tdLibWrapper.closeChat(chatInformation.id); tdLibWrapper.closeChat(chatInformation.id);
@ -151,11 +152,18 @@ Page {
Connections { Connections {
target: chatModel target: chatModel
onMessagesReceived: { onMessagesReceived: {
chatPage.loading = false; console.log("[ChatPage] Messages received, view has " + chatView.count + " messages, setting view to index " + modelIndex);
chatView.currentIndex = modelIndex;
} }
onNewMessageReceived: { onNewMessageReceived: {
// Notify user about new messages... // 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 { Timer {
@ -239,6 +247,16 @@ Page {
width: parent.width width: parent.width
height: parent.height - ( 2 * Theme.paddingMedium ) - headerRow.height - newMessageColumn.height height: parent.height - ( 2 * Theme.paddingMedium ) - headerRow.height - newMessageColumn.height
Timer {
id: chatViewLoadingTimer
interval: 500
repeat: false
running: false
onTriggered: {
chatPage.loading = false;
}
}
SilicaListView { SilicaListView {
id: chatView id: chatView
@ -246,9 +264,9 @@ Page {
opacity: chatPage.loading ? 0 : 1 opacity: chatPage.loading ? 0 : 1
Behavior on opacity { NumberAnimation {} } Behavior on opacity { NumberAnimation {} }
visible: !chatPage.loading
clip: true clip: true
highlightFollowsCurrentItem: true
function handleScrollPositionChanged() { function handleScrollPositionChanged() {
tdLibWrapper.viewMessage(chatInformation.id, chatView.itemAt(chatView.contentX, ( chatView.contentY + chatView.height - Theme.horizontalPageMargin )).myMessage.id); 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: { onMovementEnded: {
handleScrollPositionChanged(); handleScrollPositionChanged();
} }
@ -267,10 +289,6 @@ Page {
} }
} }
onCurrentIndexChanged: {
tdLibWrapper.viewMessage(chatInformation.id, currentItem.myMessage.id);
}
model: chatModel model: chatModel
delegate: ListItem { 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 { Column {

View file

@ -165,7 +165,7 @@ CoverBackground {
color: Theme.highlightColor color: Theme.highlightColor
visible: coverPage.authenticated visible: coverPage.authenticated
width: parent.width width: parent.width
maximumLineCount: 2 maximumLineCount: 3
wrapMode: Text.Wrap wrapMode: Text.Wrap
} }
} }

View file

@ -11,6 +11,7 @@ ChatModel::ChatModel(TDLibWrapper *tdLibWrapper)
this->inIncrementalUpdate = false; this->inIncrementalUpdate = false;
connect(this->tdLibWrapper, SIGNAL(messagesReceived(QVariantList)), this, SLOT(handleMessagesReceived(QVariantList))); 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(newMessageReceived(QString, QVariantMap)), this, SLOT(handleNewMessageReceived(QString, QVariantMap)));
connect(this->tdLibWrapper, SIGNAL(chatReadInboxUpdated(QString, int)), this, SLOT(handleChatReadInboxUpdated(QString, int)));
} }
ChatModel::~ChatModel() ChatModel::~ChatModel()
@ -46,13 +47,14 @@ bool ChatModel::insertRows(int row, int count, const QModelIndex &parent)
return true; return true;
} }
void ChatModel::initialize(const QString &chatId) void ChatModel::initialize(const QVariantMap &chatInformation)
{ {
this->chatId = chatId; this->chatInformation = chatInformation;
this->messages.clear(); this->messages.clear();
this->messageIndexMap.clear(); this->messageIndexMap.clear();
this->messagesToBeAdded.clear(); this->messagesToBeAdded.clear();
tdLibWrapper->getChatHistory(chatId); this->chatId = chatInformation.value("id").toString();
tdLibWrapper->getChatHistory(this->chatId);
} }
void ChatModel::triggerLoadMoreHistory() void ChatModel::triggerLoadMoreHistory()
@ -84,9 +86,9 @@ void ChatModel::handleMessagesReceived(const QVariantList &messages)
this->inReload = false; this->inReload = false;
if (this->inIncrementalUpdate) { if (this->inIncrementalUpdate) {
this->inIncrementalUpdate = false; this->inIncrementalUpdate = false;
emit messagesIncrementalUpdate(); emit messagesIncrementalUpdate(this->messages.size() - 1);
} else { } else {
emit messagesReceived(); emit messagesReceived(this->messages.size() - 1);
} }
} else { } else {
this->messagesMutex.lock(); this->messagesMutex.lock();
@ -103,6 +105,10 @@ void ChatModel::handleMessagesReceived(const QVariantList &messages)
this->insertMessages(); this->insertMessages();
this->messagesMutex.unlock(); 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... // First call only returns a few messages, we need to get a little more than that...
if (this->messagesToBeAdded.size() < 10 && !this->inReload) { if (this->messagesToBeAdded.size() < 10 && !this->inReload) {
qDebug() << "[ChatModel] Only a few messages received in first call, loading more..."; 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; this->inReload = false;
if (this->inIncrementalUpdate) { if (this->inIncrementalUpdate) {
this->inIncrementalUpdate = false; this->inIncrementalUpdate = false;
emit messagesIncrementalUpdate(); emit messagesIncrementalUpdate(listPosition);
} else { } 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() void ChatModel::insertMessages()
{ {
if (this->messages.isEmpty()) { if (this->messages.isEmpty()) {

View file

@ -17,17 +17,19 @@ 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 initialize(const QString &chatId); Q_INVOKABLE void initialize(const QVariantMap &chatInformation);
Q_INVOKABLE void triggerLoadMoreHistory(); Q_INVOKABLE void triggerLoadMoreHistory();
signals: signals:
void messagesReceived(); void messagesReceived(const int &modelIndex);
void messagesIncrementalUpdate(); void messagesIncrementalUpdate(const int &modelIndex);
void newMessageReceived(); void newMessageReceived();
void unreadCountUpdated(const int &unreadCount);
public slots: public slots:
void handleMessagesReceived(const QVariantList &messages); void handleMessagesReceived(const QVariantList &messages);
void handleNewMessageReceived(const QString &chatId, const QVariantMap &message); void handleNewMessageReceived(const QString &chatId, const QVariantMap &message);
void handleChatReadInboxUpdated(const QString &chatId, const int &unreadCount);
private: private:
@ -36,6 +38,7 @@ private:
QVariantList messagesToBeAdded; QVariantList messagesToBeAdded;
QVariantMap messageIndexMap; QVariantMap messageIndexMap;
QMutex messagesMutex; QMutex messagesMutex;
QVariantMap chatInformation;
QString chatId; QString chatId;
bool inReload; bool inReload;
bool inIncrementalUpdate; bool inIncrementalUpdate;