From cca9467703fd5cbbc4724766b28f81a4078b0f28 Mon Sep 17 00:00:00 2001 From: Black Hat Date: Wed, 22 Aug 2018 23:21:39 +0800 Subject: [PATCH] Display room's latest event instead of topic when possible. --- qml/Room.qml | 2 ++ qml/form/RoomListForm.qml | 9 +++++++-- src/matriqueroom.cpp | 7 +++++++ src/matriqueroom.h | 2 ++ src/roomlistmodel.cpp | 4 ++++ src/roomlistmodel.h | 4 +++- 6 files changed, 25 insertions(+), 3 deletions(-) diff --git a/qml/Room.qml b/qml/Room.qml index a777ffb..04c9e32 100644 --- a/qml/Room.qml +++ b/qml/Room.qml @@ -16,6 +16,8 @@ Page { onRoomAdded: MatriqueSettings.lazyLoad ? {} : room.getPreviousContent(20) onNewMessage: window.active ? {} : matriqueController.showMessage(roomName, content, icon) + + onDataChanged: roomListForm.rawCurrentIndex = -1 } RowLayout { diff --git a/qml/form/RoomListForm.qml b/qml/form/RoomListForm.qml index aa43fea..4e89833 100644 --- a/qml/form/RoomListForm.qml +++ b/qml/form/RoomListForm.qml @@ -12,6 +12,7 @@ import "qrc:/qml/component" Item { property alias listModel: roomListProxyModel.sourceModel + property alias rawCurrentIndex: listView.currentIndex readonly property int currentIndex: roomListProxyModel.mapToSource(listView.currentIndex) readonly property var currentRoom: currentIndex != -1 ? listModel.roomAt(currentIndex) : null readonly property bool mini: MatriqueSettings.miniMode // Used as an indicator of whether the listform should be displayed as "Mini mode". @@ -96,6 +97,10 @@ Item { sorters: [ RoleSorter { roleName: "category" }, + RoleSorter { + roleName: "unreadCount" + sortOrder: Qt.DescendingOrder + }, StringSorter { roleName: "name" } ] } @@ -165,7 +170,7 @@ Item { Layout.fillWidth: true Layout.fillHeight: true - text: name ? name : "" + text: name || "No Name" font.pointSize: 16 elide: Text.ElideRight wrapMode: Text.NoWrap @@ -175,7 +180,7 @@ Item { Layout.fillWidth: true Layout.fillHeight: true - text: topic ? topic : "No topic yet." + text: lastEvent || topic elide: Text.ElideRight wrapMode: Text.NoWrap } diff --git a/src/matriqueroom.cpp b/src/matriqueroom.cpp index c7a13e4..b96e915 100644 --- a/src/matriqueroom.cpp +++ b/src/matriqueroom.cpp @@ -97,3 +97,10 @@ void MatriqueRoom::sendTypingNotification(bool isTyping) { connection()->callApi(BackgroundRequest, localUser()->id(), id(), isTyping, 10000); } + +QString MatriqueRoom::lastEvent() { + if (timelineSize() == 0) return ""; + const RoomEvent* lastEvent = messageEvents().rbegin()->get(); + return user(lastEvent->senderId())->displayname() + ": " + + lastEvent->contentJson().value("body").toString(); +} diff --git a/src/matriqueroom.h b/src/matriqueroom.h index 6047985..b44cbb5 100644 --- a/src/matriqueroom.h +++ b/src/matriqueroom.h @@ -40,6 +40,8 @@ class MatriqueRoom : public Room { bool hasUsersTyping(); QString getUsersTyping(); + QString lastEvent(); + private: QString m_cachedInput; bool m_isTyping; diff --git a/src/roomlistmodel.cpp b/src/roomlistmodel.cpp index d43c7f9..f780e33 100644 --- a/src/roomlistmodel.cpp +++ b/src/roomlistmodel.cpp @@ -62,6 +62,8 @@ void RoomListModel::connectRoomSignals(MatriqueRoom* room) { connect(room, &Room::joinStateChanged, this, [=] { refresh(room); }); connect(room, &Room::avatarChanged, this, [=] { refresh(room, {AvatarRole}); }); + connect(room, &Room::addedMessages, this, + [=] { refresh(room, {LastEventRole}); }); connect(room, &QMatrixClient::Room::aboutToAddNewMessages, this, [=](QMatrixClient::RoomEventsRange eventsRange) { RoomEvent* event = (eventsRange.end() - 1)->get(); @@ -150,6 +152,7 @@ QVariant RoomListModel::data(const QModelIndex& index, int role) const { return RoomType::Normal; } if (role == UnreadCountRole) return room->unreadCount(); + if (role == LastEventRole) return room->lastEvent(); return QVariant(); } @@ -180,5 +183,6 @@ QHash RoomListModel::roleNames() const { roles[TopicRole] = "topic"; roles[CategoryRole] = "category"; roles[UnreadCountRole] = "unreadCount"; + roles[LastEventRole] = "lastEvent"; return roles; } diff --git a/src/roomlistmodel.h b/src/roomlistmodel.h index b1d5a05..ee1c663 100644 --- a/src/roomlistmodel.h +++ b/src/roomlistmodel.h @@ -35,6 +35,7 @@ class RoomListModel : public QAbstractListModel { TopicRole, CategoryRole, UnreadCountRole, + LastEventRole }; RoomListModel(QObject* parent = 0); @@ -70,7 +71,8 @@ class RoomListModel : public QAbstractListModel { signals: void connectionChanged(); void roomAdded(MatriqueRoom* room); - void newMessage(const QString& roomName, const QString& content, const QIcon& icon); + void newMessage(const QString& roomName, const QString& content, + const QIcon& icon); }; #endif // ROOMLISTMODEL_H