Use singleton to pass edit/reply content to chatbox

This significantly reduce the complexity of everything.
This commit is contained in:
Carl Schwan
2021-03-20 14:00:29 +00:00
parent c7df3f903a
commit 743c9972b9
11 changed files with 315 additions and 163 deletions

View File

@@ -22,6 +22,7 @@ add_executable(neochat
filetypesingleton.cpp
login.cpp
stickerevent.cpp
chatboxhelper.cpp
../res.qrc
)

162
src/chatboxhelper.cpp Normal file
View File

@@ -0,0 +1,162 @@
// SPDX-FileCopyrightText: 2020 Carl Schwan <carlschwan@kde.org>
// SPDX-License-Identifier: GPl-3.0-or-later
#include "chatboxhelper.h"
#include <QDebug>
ChatBoxHelper::ChatBoxHelper(QObject *parent)
: QObject(parent)
{
}
bool ChatBoxHelper::isEditing() const
{
return !m_editEventId.isEmpty();
}
QString ChatBoxHelper::editEventId() const
{
return m_editEventId;
}
void ChatBoxHelper::setEditEventId(const QString& editEventId)
{
if (m_editEventId == editEventId) {
return;
}
m_editEventId = editEventId;
Q_EMIT editEventIdChanged(m_editEventId);
Q_EMIT isEditingChanged(!m_editEventId.isEmpty());
}
QString ChatBoxHelper::editContent() const
{
return m_editContent;
}
void ChatBoxHelper::setEditContent(const QString& editContent)
{
if (m_editContent == editContent) {
return;
}
m_editContent = editContent;
Q_EMIT editContentChanged();
}
QString ChatBoxHelper::replyEventId() const
{
return m_replyEventId;
}
void ChatBoxHelper::setReplyEventId(const QString& replyEventId)
{
if (m_replyEventId == replyEventId) {
return;
}
m_replyEventId = replyEventId;
Q_EMIT replyEventIdChanged(m_replyEventId);
}
QString ChatBoxHelper::replyEventContent() const
{
return m_replyEventContent;
}
void ChatBoxHelper::setReplyEventContent(const QString& replyEventContent)
{
if (m_replyEventContent == replyEventContent) {
return;
}
m_replyEventContent = replyEventContent;
Q_EMIT replyEventContentChanged(m_replyEventContent);
Q_EMIT isReplyingChanged(!m_replyEventContent.isEmpty());
}
bool ChatBoxHelper::isReplying() const
{
return !m_replyEventId.isEmpty();
}
QString ChatBoxHelper::attachmentPath() const
{
return m_attachmentPath;
}
void ChatBoxHelper::setAttachmentPath(const QString& attachmentPath)
{
if (m_attachmentPath == attachmentPath) {
return;
}
m_attachmentPath = attachmentPath;
Q_EMIT attachmentPathChanged(m_attachmentPath);
Q_EMIT hasAttachmentChanged(!m_attachmentPath.isEmpty());
}
bool ChatBoxHelper::hasAttachment() const
{
return !m_attachmentPath.isEmpty();
}
void ChatBoxHelper::replyToMessage(const QString &replyEventId, const QString &replyEvent, const QVariant &replyUser)
{
setEditEventId(QString());
setEditContent(QString());
setReplyEventId(replyEventId);
setReplyEventContent(replyEvent);
setReplyUser(replyUser);
}
QVariant ChatBoxHelper::replyUser() const
{
return m_replyUser;
}
void ChatBoxHelper::setReplyUser(const QVariant &replyUser)
{
if (m_replyUser == replyUser) {
return;
}
m_replyUser = replyUser;
Q_EMIT replyUserChanged();
}
void ChatBoxHelper::clear()
{
setEditEventId(QString());
setEditContent(QString());
setReplyEventId(QString());
setReplyEventContent(QString());
setAttachmentPath(QString());
setReplyUser(QVariant());
}
void ChatBoxHelper::edit(const QString& message, const QString& formattedBody, const QString& eventId)
{
setEditEventId(eventId);
setEditContent(message);
Q_EMIT editing(message, formattedBody);
}
void ChatBoxHelper::clearEditReply()
{
setEditEventId(QString());
setEditContent(QString());
setReplyEventId(QString());
setReplyEventContent(QString());
setReplyUser(QVariant());
Q_EMIT shouldClearText();
}
void ChatBoxHelper::clearAttachment()
{
setAttachmentPath(QString());
}

75
src/chatboxhelper.h Normal file
View File

@@ -0,0 +1,75 @@
// SPDX-FileCopyrightText: 2020 Carl Schwan <carlschwan@kde.org>
// SPDX-License-Identifier: GPl-3.0-or-later
#pragma once
#include <QObject>
#include <QVariant>
/// Helper singleton for keeping the chatbar state in sync in the application.
class ChatBoxHelper : public QObject
{
Q_OBJECT
/// True, iff the user is currently editing one of their previous message.
Q_PROPERTY(bool isEditing READ isEditing NOTIFY isEditingChanged)
Q_PROPERTY(QString editEventId READ editEventId WRITE setEditEventId NOTIFY editEventIdChanged)
Q_PROPERTY(QString editContent READ editContent WRITE setEditContent NOTIFY editContentChanged)
Q_PROPERTY(bool isReplying READ isReplying NOTIFY isReplyingChanged)
Q_PROPERTY(QString replyEventId READ replyEventId WRITE setReplyEventId NOTIFY replyEventIdChanged)
Q_PROPERTY(QString replyEventContent READ replyEventContent WRITE setReplyEventContent NOTIFY replyEventContentChanged)
Q_PROPERTY(QVariant replyUser READ replyUser WRITE setReplyUser NOTIFY replyUserChanged)
Q_PROPERTY(QString attachmentPath READ attachmentPath WRITE setAttachmentPath NOTIFY attachmentPathChanged)
Q_PROPERTY(bool hasAttachment READ hasAttachment NOTIFY hasAttachmentChanged)
public:
ChatBoxHelper(QObject *parent = nullptr);
~ChatBoxHelper() = default;
bool isEditing() const;
QString editEventId() const;
QString editContent() const;
QString replyEventId() const;
QString replyEventContent() const;
QVariant replyUser() const;
bool isReplying() const;
QString attachmentPath() const;
bool hasAttachment() const;
void setEditEventId(const QString& editEventId);
void setEditContent(const QString& editContent);
void setReplyEventId(const QString& replyEventId);
void setReplyEventContent(const QString& replyEventContent);
void setAttachmentPath(const QString& attachmentPath);
void setReplyUser(const QVariant &replyUser);
Q_INVOKABLE void replyToMessage(const QString &replyEventid, const QString &replyEvent, const QVariant &replyUser);
Q_INVOKABLE void edit(const QString &message, const QString &formattedBody, const QString &eventId);
Q_INVOKABLE void clear();
Q_INVOKABLE void clearEditReply();
Q_INVOKABLE void clearAttachment();
Q_SIGNALS:
void isEditingChanged(bool isEditing);
void editEventIdChanged(const QString& editEventId);
void editContentChanged();
void replyEventIdChanged(const QString& replyEventId);
void replyEventContentChanged(const QString& replyEventContent);
void replyUserChanged();
void isReplyingChanged(bool isReplying);
void attachmentPathChanged(const QString& attachmentPath);
void hasAttachmentChanged(bool hasAttachment);
void editing(const QString &message, const QString &formattedBody);
void shouldClearText();
private:
QString m_editEventId;
QString m_editContent;
QString m_replyEventId;
QString m_replyEventContent;
QVariant m_replyUser;
QString m_attachmentPath;
};

View File

@@ -47,6 +47,7 @@
#include "userdirectorylistmodel.h"
#include "userlistmodel.h"
#include "actionshandler.h"
#include "chatboxhelper.h"
using namespace Quotient;
@@ -108,12 +109,14 @@ int main(int argc, char *argv[])
FileTypeSingleton fileTypeSingleton;
Login *login = new Login();
ChatBoxHelper chatBoxHelper;
qmlRegisterSingletonInstance("org.kde.neochat", 1, 0, "Controller", &Controller::instance());
qmlRegisterSingletonInstance("org.kde.neochat", 1, 0, "Clipboard", &clipboard);
qmlRegisterSingletonInstance("org.kde.neochat", 1, 0, "Config", config);
qmlRegisterSingletonInstance("org.kde.neochat", 1, 0, "FileType", &fileTypeSingleton);
qmlRegisterSingletonInstance("org.kde.neochat", 1, 0, "LoginHelper", login);
qmlRegisterSingletonInstance("org.kde.neochat", 1, 0, "ChatBoxHelper", &chatBoxHelper);
qmlRegisterType<AccountListModel>("org.kde.neochat", 1, 0, "AccountListModel");
qmlRegisterType<ActionsHandler>("org.kde.neochat", 1, 0, "ActionsHandler");
qmlRegisterType<ChatDocumentHandler>("org.kde.neochat", 1, 0, "ChatDocumentHandler");