From 49881f809d073f2b651886ff479fbae0abb58de8 Mon Sep 17 00:00:00 2001 From: Tobias Fella Date: Wed, 25 Nov 2020 22:09:04 +0100 Subject: [PATCH] Improve the 'jump to last read message' button When loading a room, automatically load messages until the last read message is loaded #35 Don't show the button if the message is not loaded --- imports/NeoChat/Page/RoomPage.qml | 2 +- src/messageeventmodel.cpp | 13 +++++++++++++ src/neochatroom.cpp | 8 ++++++++ src/neochatroom.h | 4 ++++ 4 files changed, 26 insertions(+), 1 deletion(-) diff --git a/imports/NeoChat/Page/RoomPage.qml b/imports/NeoChat/Page/RoomPage.qml index da060256f..fd05ac36f 100644 --- a/imports/NeoChat/Page/RoomPage.qml +++ b/imports/NeoChat/Page/RoomPage.qml @@ -329,7 +329,7 @@ Kirigami.ScrollablePage { id: goReadMarkerFab - visible: currentRoom && currentRoom.hasUnreadMessages || !messageListView.atYEnd + visible: currentRoom && currentRoom.hasUnreadMessages && currentRoom.readMarkerLoaded || !messageListView.atYEnd action: Kirigami.Action { onTriggered: { if (currentRoom && currentRoom.hasUnreadMessages) { diff --git a/src/messageeventmodel.cpp b/src/messageeventmodel.cpp index e6305ccd0..026e0b717 100644 --- a/src/messageeventmodel.cpp +++ b/src/messageeventmodel.cpp @@ -55,6 +55,19 @@ MessageEventModel::MessageEventModel(QObject *parent) qmlRegisterAnonymousType("org.kde.neochat", 1); qRegisterMetaType(); qmlRegisterUncreatableType("org.kde.neochat", 1, 0, "EventStatus", "EventStatus is not an creatable type"); + + QTimer::singleShot(0, this, [=]() { + m_currentRoom->getPreviousContent(50); + connect(this, &QAbstractListModel::rowsInserted, this, [=](){ + if(m_currentRoom->readMarkerEventId().isEmpty()) { + return; + } + const auto it = m_currentRoom->findInTimeline(m_currentRoom->readMarkerEventId()); + if (it == m_currentRoom->timelineEdge()) { + m_currentRoom->getPreviousContent(50); + } + }); + }); } MessageEventModel::~MessageEventModel() diff --git a/src/neochatroom.cpp b/src/neochatroom.cpp index 6fc603101..ba0c8f2e1 100644 --- a/src/neochatroom.cpp +++ b/src/neochatroom.cpp @@ -56,6 +56,8 @@ NeoChatRoom::NeoChatRoom(Connection *connection, QString roomId, JoinState joinS NotificationsManager::instance().postNotification(id(), lastEvent->id(), displayName(), sender->displayname(this), eventToString(*lastEvent), avatar(128)); }); + + connect(this, &Room::aboutToAddHistoricalMessages, this, &NeoChatRoom::readMarkerLoadedChanged); } void NeoChatRoom::uploadFile(const QUrl &url, const QString &body) @@ -613,3 +615,9 @@ bool NeoChatRoom::canSendState(const QString &eventType) const return currentPl >= pl; } + +bool NeoChatRoom::readMarkerLoaded() const +{ + const auto it = findInTimeline(readMarkerEventId()); + return it != timelineEdge(); +} diff --git a/src/neochatroom.h b/src/neochatroom.h index eb0d1a44c..565033655 100644 --- a/src/neochatroom.h +++ b/src/neochatroom.h @@ -30,6 +30,7 @@ class NeoChatRoom : public Room Q_PROPERTY(bool hasFileUploading READ hasFileUploading WRITE setHasFileUploading NOTIFY hasFileUploadingChanged) Q_PROPERTY(int fileUploadingProgress READ fileUploadingProgress NOTIFY fileUploadingProgressChanged) Q_PROPERTY(QString avatarMediaId READ avatarMediaId NOTIFY avatarChanged STORED false) + Q_PROPERTY(bool readMarkerLoaded READ readMarkerLoaded NOTIFY readMarkerLoadedChanged) public: explicit NeoChatRoom(Connection *connection, QString roomId, JoinState joinState = {}); @@ -67,6 +68,8 @@ public: Q_EMIT fileUploadingProgressChanged(); } + bool readMarkerLoaded() const; + Q_INVOKABLE int savedTopVisibleIndex() const; Q_INVOKABLE int savedBottomVisibleIndex() const; Q_INVOKABLE void saveViewport(int topIndex, int bottomIndex); @@ -108,6 +111,7 @@ Q_SIGNALS: void hasFileUploadingChanged(); void fileUploadingProgressChanged(); void backgroundChanged(); + void readMarkerLoadedChanged(); public Q_SLOTS: void uploadFile(const QUrl &url, const QString &body = "");