From fdc66bd3d3b7246f5b27d0393b92703d5e105131 Mon Sep 17 00:00:00 2001 From: Kitsune Ral Date: Thu, 24 May 2018 20:14:27 +0900 Subject: [PATCH] MessageEventModel: refresh the event adjacent to just added Closes #312. --- client/models/messageeventmodel.cpp | 18 ++++++++++++++---- client/models/messageeventmodel.h | 1 + 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/client/models/messageeventmodel.cpp b/client/models/messageeventmodel.cpp index 0b591d6a9..c618749e4 100644 --- a/client/models/messageeventmodel.cpp +++ b/client/models/messageeventmodel.cpp @@ -97,11 +97,21 @@ void MessageEventModel::changeRoom(QuaternionRoom* room) connect(m_currentRoom, &Room::aboutToAddHistoricalMessages, this, [=](RoomEventsRange events) { + if (rowCount() > 0) + nextNewerRow = rowCount() - 1; beginInsertRows(QModelIndex(), rowCount(), rowCount() + int(events.size()) - 1); }); - connect(m_currentRoom, &Room::addedMessages, - this, &MessageEventModel::endInsertRows); + connect(m_currentRoom, &Room::addedMessages, this, + [=] { + if (nextNewerRow > -1) + { + const auto idx = index(nextNewerRow); + emit dataChanged(idx, idx); + nextNewerRow = -1; + } + endInsertRows(); + }); connect(m_currentRoom, &Room::readMarkerMoved, this, [this] { refreshEventRoles( std::exchange(lastReadEventId, @@ -139,8 +149,8 @@ void MessageEventModel::refreshEventRoles(const QString& eventId, const auto it = m_currentRoom->findInTimeline(eventId); if (it != m_currentRoom->timelineEdge()) { - const auto row = it - m_currentRoom->messageEvents().rbegin(); - emit dataChanged(index(row), index(row), roles); + const auto idx = index(it - m_currentRoom->messageEvents().rbegin()); + emit dataChanged(idx, idx, roles); } } diff --git a/client/models/messageeventmodel.h b/client/models/messageeventmodel.h index 566aa7ab2..517f47929 100644 --- a/client/models/messageeventmodel.h +++ b/client/models/messageeventmodel.h @@ -47,6 +47,7 @@ class MessageEventModel: public QAbstractListModel private: QuaternionRoom* m_currentRoom; QString lastReadEventId; + int nextNewerRow = -1; QDateTime makeMessageTimestamp(QuaternionRoom::rev_iter_t baseIt) const; QString makeDateString(QuaternionRoom::rev_iter_t baseIt) const;