From 169384f0fa41cc8a52428f27bc3042dc849dd8c9 Mon Sep 17 00:00:00 2001 From: Loren Burkholder Date: Tue, 19 Apr 2022 23:18:11 -0400 Subject: [PATCH] Add space notifications to room list --- resources/qml/RoomList.qml | 108 +++++++++++++++++------------- src/Utils.cpp | 15 +++++ src/Utils.h | 3 + src/timeline/CommunitiesModel.cpp | 17 +---- src/timeline/CommunitiesModel.h | 2 - src/timeline/RoomlistModel.cpp | 5 +- src/timeline/TimelineModel.cpp | 17 +++-- 7 files changed, 98 insertions(+), 69 deletions(-) diff --git a/resources/qml/RoomList.qml b/resources/qml/RoomList.qml index 910fc252..126d82be 100644 --- a/resources/qml/RoomList.qml +++ b/resources/qml/RoomList.qml @@ -348,58 +348,15 @@ Page { height: avatar.height spacing: Nheko.paddingSmall - RowLayout { - Layout.alignment: Qt.AlignTop - Layout.fillWidth: true - spacing: Nheko.paddingSmall - - ElidedLabel { - id: rN - Layout.alignment: Qt.AlignBaseline - color: roomItem.importantText - elideWidth: width - fullText: roomName - textFormat: Text.RichText - Layout.fillWidth: true - } - - Label { - id: timestamp - - visible: !isInvite && !isSpace - width: visible ? 0 : undefined - Layout.alignment: Qt.AlignRight | Qt.AlignBaseline - font.pixelSize: fontMetrics.font.pixelSize * 0.9 - color: roomItem.unimportantText - text: time - } - - } - - RowLayout { - Layout.fillWidth: true - spacing: 0 - visible: !isSpace - height: visible ? 0 : undefined - Layout.alignment: Qt.AlignBottom - - ElidedLabel { - color: roomItem.unimportantText - font.pixelSize: fontMetrics.font.pixelSize * 0.9 - elideWidth: width - fullText: lastMessage - textFormat: Text.RichText - Layout.fillWidth: true - } + Component { + id: notificationBubble Rectangle { - id: notificationBubble - visible: notificationCount > 0 Layout.alignment: Qt.AlignRight Layout.leftMargin: Nheko.paddingSmall height: notificationBubbleText.height + Nheko.paddingMedium - Layout.preferredWidth: Math.max(notificationBubbleText.width, height) + width: Math.max(notificationBubbleText.width, height) radius: height / 2 color: hasLoudNotification ? Nheko.theme.red : roomItem.bubbleBackground ToolTip.text: notificationCount @@ -425,7 +382,66 @@ Page { } } + } + RowLayout { + id: titleRow + + Layout.alignment: Qt.AlignTop + Layout.fillWidth: true + spacing: Nheko.paddingSmall + + ElidedLabel { + id: rN + Layout.alignment: Qt.AlignBaseline + color: roomItem.importantText + elideWidth: width + fullText: roomName + textFormat: Text.RichText + Layout.fillWidth: true + } + + Label { + id: timestamp + + visible: !isInvite && !isSpace + width: visible ? 0 : undefined + Layout.alignment: Qt.AlignRight | Qt.AlignBaseline + font.pixelSize: fontMetrics.font.pixelSize * 0.9 + color: roomItem.unimportantText + text: time + } + + Loader { + sourceComponent: notificationBubble + active: isSpace + } + + } + + RowLayout { + id: subtextRow + + Layout.fillWidth: true + spacing: 0 + visible: !isSpace + height: visible ? 0 : undefined + Layout.alignment: Qt.AlignBottom + + ElidedLabel { + color: roomItem.unimportantText + font.pixelSize: fontMetrics.font.pixelSize * 0.9 + elideWidth: width + fullText: lastMessage + textFormat: Text.RichText + Layout.fillWidth: true + } + + + Loader { + sourceComponent: notificationBubble + active: !isSpace + } } } diff --git a/src/Utils.cpp b/src/Utils.cpp index 0ac37d8e..aa36d3d9 100644 --- a/src/Utils.cpp +++ b/src/Utils.cpp @@ -27,6 +27,7 @@ #include #include "Cache.h" +#include "Cache_p.h" #include "Config.h" #include "EventAccessors.h" #include "Logging.h" @@ -880,3 +881,17 @@ utils::markRoomAsDirect(QString roomid, std::vector members) }); }); } + +int +utils::getChildNotificationsForSpace(const QString &spaceId) +{ + auto children = cache::getRoomInfo(cache::client()->getChildRoomIds(spaceId.toStdString())); + int total{0}; + for (const auto &[childId, child] : children) { + if (child.is_space) + total += utils::getChildNotificationsForSpace(childId); + else + total += child.notification_count; + } + return total; +} diff --git a/src/Utils.h b/src/Utils.h index 0b6034ac..c20544f2 100644 --- a/src/Utils.h +++ b/src/Utils.h @@ -311,4 +311,7 @@ removeDirectFromRoom(QString roomid); void markRoomAsDirect(QString roomid, std::vector members); + +int +getChildNotificationsForSpace(const QString &spaceId); } diff --git a/src/timeline/CommunitiesModel.cpp b/src/timeline/CommunitiesModel.cpp index 116fa0a0..724a6e60 100644 --- a/src/timeline/CommunitiesModel.cpp +++ b/src/timeline/CommunitiesModel.cpp @@ -12,6 +12,7 @@ #include "ChatPage.h" #include "Logging.h" #include "UserSettingsPage.h" +#include "Utils.h" CommunitiesModel::CommunitiesModel(QObject *parent) : QAbstractListModel(parent) @@ -131,7 +132,7 @@ CommunitiesModel::data(const QModelIndex &index, int role) const case CommunitiesModel::Roles::Id: return "space:" + id; case CommunitiesModel::Roles::UnreadMessages: - return getChildNotifications(id); + return utils::getChildNotificationsForSpace(id); } } else if (index.row() - 2 < tags_.size() + spaceOrder_.size()) { auto tag = tags_.at(index.row() - 2 - spaceOrder_.size()); @@ -465,20 +466,6 @@ CommunitiesModel::toggleTagId(QString tagId) emit hiddenTagsChanged(); } -int -CommunitiesModel::getChildNotifications(const QString &space_id) const -{ - auto children = cache::getRoomInfo(cache::client()->getChildRoomIds(space_id.toStdString())); - int total{0}; - for (const auto &[child_id, child] : children) { - if (child.is_space) - total += getChildNotifications(child_id); - else - total += child.notification_count; - } - return total; -} - FilteredCommunitiesModel::FilteredCommunitiesModel(CommunitiesModel *model, QObject *parent) : QSortFilterProxyModel(parent) { diff --git a/src/timeline/CommunitiesModel.h b/src/timeline/CommunitiesModel.h index 30980f00..b29a5385 100644 --- a/src/timeline/CommunitiesModel.h +++ b/src/timeline/CommunitiesModel.h @@ -148,8 +148,6 @@ signals: void containsSubspacesChanged(); private: - int getChildNotifications(const QString &space_id) const; - QStringList tags_; QString currentTagId_; QStringList hiddenTagIds_; diff --git a/src/timeline/RoomlistModel.cpp b/src/timeline/RoomlistModel.cpp index 1cf16243..3b46c053 100644 --- a/src/timeline/RoomlistModel.cpp +++ b/src/timeline/RoomlistModel.cpp @@ -330,10 +330,13 @@ RoomlistModel::addRoom(const QString &room_id, bool suppressInsertNotification) Qt::DisplayRole, }); + if (getRoomById(room_id)->isSpace()) + return; // no need to update space notifications + int total_unread_msgs = 0; for (const auto &room : qAsConst(models)) { - if (!room.isNull()) + if (!room.isNull() && !room->isSpace()) total_unread_msgs += room->notificationCount(); } diff --git a/src/timeline/TimelineModel.cpp b/src/timeline/TimelineModel.cpp index 3fe4c07f..308d358b 100644 --- a/src/timeline/TimelineModel.cpp +++ b/src/timeline/TimelineModel.cpp @@ -352,16 +352,23 @@ TimelineModel::TimelineModel(TimelineViewManager *manager, QString room_id, QObj { this->isEncrypted_ = cache::isRoomEncrypted(room_id_.toStdString()); - auto roomInfo = cache::singleRoomInfo(room_id_.toStdString()); - this->isSpace_ = roomInfo.is_space; - this->notification_count = roomInfo.notification_count; - this->highlight_count = roomInfo.highlight_count; - lastMessage_.timestamp = roomInfo.approximate_last_modification_ts; + auto roomInfo = cache::singleRoomInfo(room_id_.toStdString()); + this->isSpace_ = roomInfo.is_space; + this->notification_count = + isSpace_ ? utils::getChildNotificationsForSpace(room_id_) : roomInfo.notification_count; + this->highlight_count = roomInfo.highlight_count; + lastMessage_.timestamp = roomInfo.approximate_last_modification_ts; // this connection will simplify adding the plainRoomNameChanged() signal everywhere that it // needs to be connect(this, &TimelineModel::roomNameChanged, this, &TimelineModel::plainRoomNameChanged); + if (isSpace_) + connect(ChatPage::instance(), &ChatPage::unreadMessages, this, [this](int) { + notification_count = utils::getChildNotificationsForSpace(room_id_); + emit notificationsChanged(); + }); + connect( this, &TimelineModel::redactionFailed,