Refactor EventHandler

Refactor EventHandler to be a Q_GADGET and create from a constructor
This commit is contained in:
James Graham
2024-01-26 17:17:53 +00:00
parent 27662f9a4a
commit 95fff4c9f7
9 changed files with 98 additions and 195 deletions

View File

@@ -114,10 +114,9 @@ QString ChatBarCache::relationMessage() const
qWarning() << "ChatBarCache created with incorrect parent, a NeoChatRoom must be set as the parent on creation.";
return {};
}
EventHandler eventhandler;
eventhandler.setRoom(room);
if (auto event = room->findInTimeline(m_relationId); event != room->historyEdge()) {
eventhandler.setEvent(&**event);
EventHandler eventhandler(room, &**event);
return eventhandler.getPlainBody();
}
return {};

View File

@@ -31,34 +31,10 @@
using namespace Quotient;
const NeoChatRoom *EventHandler::getRoom() const
EventHandler::EventHandler(const NeoChatRoom *room, const RoomEvent *event)
: m_room(room)
, m_event(event)
{
return m_room;
}
void EventHandler::setRoom(const NeoChatRoom *room)
{
if (room == m_room) {
return;
}
m_room = room;
}
const Quotient::Event *EventHandler::getEvent() const
{
return m_event;
}
void EventHandler::setEvent(const Quotient::RoomEvent *event)
{
if (m_room == nullptr) {
qCWarning(EventHandling) << "cannot setEvent when m_room is set to nullptr.";
return;
}
if (event == m_event) {
return;
}
m_event = event;
}
QString EventHandler::getId() const

View File

@@ -31,30 +31,12 @@ class ReactionModel;
* information. This is to minimize warnings from QML especially during startup
* and room changes.
*/
class EventHandler : public QObject
class EventHandler
{
Q_OBJECT
Q_GADGET
public:
/**
* @brief Return the current room the EventHandler is using.
*/
const NeoChatRoom *getRoom() const;
/**
* @brief Set the current room the EventHandler to using.
*/
void setRoom(const NeoChatRoom *room);
/**
* @brief Return the current event the EventHandler is using.
*/
const Quotient::Event *getEvent() const;
/**
* @brief Set the current event the EventHandler to using.
*/
void setEvent(const Quotient::RoomEvent *event);
EventHandler(const NeoChatRoom *room, const Quotient::RoomEvent *event);
/**
* @brief Return the Matrix ID of the event.

View File

@@ -481,9 +481,7 @@ QVariant MessageEventModel::data(const QModelIndex &idx, int role) const
const auto pendingIt = m_currentRoom->pendingEvents().crbegin() + std::min(row, timelineBaseIndex());
const auto &evt = isPending ? **pendingIt : **timelineIt;
EventHandler eventHandler;
eventHandler.setRoom(m_currentRoom);
eventHandler.setEvent(&evt);
EventHandler eventHandler(m_currentRoom, &evt);
if (role == Qt::DisplayRole) {
if (evt.isRedacted()) {

View File

@@ -122,9 +122,7 @@ void NotificationsModel::loadData()
const auto &authorAvatar = avatar.isValid() && avatar.scheme() == QStringLiteral("mxc") ? avatar : QUrl();
const auto &roomEvent = eventCast<const RoomEvent>(notification.event.get());
EventHandler eventHandler;
eventHandler.setRoom(dynamic_cast<NeoChatRoom *>(room));
eventHandler.setEvent(roomEvent);
EventHandler eventHandler(dynamic_cast<NeoChatRoom *>(room), roomEvent);
beginInsertRows({}, m_notifications.length(), m_notifications.length());
m_notifications += Notification{
.roomId = notification.roomId,

View File

@@ -348,9 +348,7 @@ QVariant RoomListModel::data(const QModelIndex &index, int role) const
if (room->lastEvent() == nullptr || room->lastEventIsSpoiler()) {
return QString();
}
EventHandler eventHandler;
eventHandler.setRoom(room);
eventHandler.setEvent(room->lastEvent());
EventHandler eventHandler(room, room->lastEvent());
return eventHandler.subtitleText();
}
if (role == AvatarImageRole) {

View File

@@ -79,9 +79,7 @@ QVariant SearchModel::data(const QModelIndex &index, int role) const
auto row = index.row();
const auto &event = *m_result->results[row].result;
EventHandler eventHandler;
eventHandler.setRoom(m_room);
eventHandler.setEvent(&event);
EventHandler eventHandler(m_room, &event);
switch (role) {
case DisplayRole:

View File

@@ -502,9 +502,7 @@ void NeoChatRoom::postHtmlMessage(const QString &text,
}
if (isThread) {
EventHandler eventHandler;
eventHandler.setRoom(this);
eventHandler.setEvent(&**replyIt);
EventHandler eventHandler(this, &**replyIt);
const bool isFallingBack = !eventHandler.isThreaded();
@@ -551,9 +549,7 @@ void NeoChatRoom::postHtmlMessage(const QString &text,
if (isReply) {
const auto &replyEvt = **replyIt;
EventHandler eventHandler;
eventHandler.setRoom(this);
eventHandler.setEvent(&**replyIt);
EventHandler eventHandler(this, &**replyIt);
// clang-format off
QJsonObject json{