Improve time handling in NeoChat
This introduces a new NeoChatDateTime object that wraps a QDateTime. The intent is that it can be passed to QML and has a series of functions that format the QDateTime into the various string representations we need. This means we only have to send the single object to QML and then the correct string can be grabbed from there, simplifying the backend. It is also easy to add a new representation if needed as a function with a QString output and Q_PROPERTY can be added and then it will be available.
This commit is contained in:
@@ -16,6 +16,7 @@
|
||||
#include "contentprovider.h"
|
||||
#include "eventhandler.h"
|
||||
#include "models/reactionmodel.h"
|
||||
#include "neochatdatetime.h"
|
||||
#include "neochatroom.h"
|
||||
#include "texthandler.h"
|
||||
|
||||
@@ -123,22 +124,13 @@ void EventMessageContentModel::initializeModel()
|
||||
resetModel();
|
||||
}
|
||||
|
||||
QDateTime EventMessageContentModel::time() const
|
||||
NeoChatDateTime EventMessageContentModel::dateTime() const
|
||||
{
|
||||
const auto event = m_room->getEvent(m_eventId);
|
||||
if (event.first == nullptr) {
|
||||
return MessageContentModel::time();
|
||||
return MessageContentModel::dateTime();
|
||||
};
|
||||
return EventHandler::time(m_room, event.first, m_currentState == Pending);
|
||||
}
|
||||
|
||||
QString EventMessageContentModel::timeString() const
|
||||
{
|
||||
const auto event = m_room->getEvent(m_eventId);
|
||||
if (event.first == nullptr) {
|
||||
return MessageContentModel::timeString();
|
||||
};
|
||||
return EventHandler::timeString(m_room, event.first, u"hh:mm"_s, m_currentState == Pending);
|
||||
return EventHandler::dateTime(m_room, event.first, m_currentState == Pending);
|
||||
}
|
||||
|
||||
QString EventMessageContentModel::authorId() const
|
||||
@@ -254,12 +246,7 @@ void EventMessageContentModel::resetModel()
|
||||
return;
|
||||
}
|
||||
|
||||
m_components += MessageComponent{MessageComponentType::Author,
|
||||
QString(),
|
||||
{
|
||||
{u"time"_s, EventHandler::time(m_room, event.first, m_currentState == Pending)},
|
||||
{u"timeString"_s, EventHandler::timeString(m_room, event.first, u"hh:mm"_s, m_currentState == Pending)},
|
||||
}};
|
||||
m_components += MessageComponent{MessageComponentType::Author, {}, {}};
|
||||
|
||||
m_components += messageContentComponents();
|
||||
endResetModel();
|
||||
|
||||
@@ -52,8 +52,7 @@ Q_SIGNALS:
|
||||
private:
|
||||
void initializeModel();
|
||||
|
||||
QDateTime time() const override;
|
||||
QString timeString() const override;
|
||||
NeoChatDateTime dateTime() const override;
|
||||
QString authorId() const override;
|
||||
QString threadRootId() const override;
|
||||
|
||||
|
||||
@@ -10,6 +10,7 @@
|
||||
#include "chatbarcache.h"
|
||||
#include "contentprovider.h"
|
||||
#include "neochatconnection.h"
|
||||
#include "neochatdatetime.h"
|
||||
#include "texthandler.h"
|
||||
|
||||
using namespace Quotient;
|
||||
@@ -79,16 +80,11 @@ QString MessageContentModel::eventId() const
|
||||
return m_eventId;
|
||||
}
|
||||
|
||||
QDateTime MessageContentModel::time() const
|
||||
NeoChatDateTime MessageContentModel::dateTime() const
|
||||
{
|
||||
return QDateTime::currentDateTime();
|
||||
}
|
||||
|
||||
QString MessageContentModel::timeString() const
|
||||
{
|
||||
return time().toLocalTime().toString(u"hh:mm"_s);
|
||||
}
|
||||
|
||||
QString MessageContentModel::authorId() const
|
||||
{
|
||||
return m_room->localMember().id();
|
||||
@@ -136,11 +132,8 @@ QVariant MessageContentModel::data(const QModelIndex &index, int role) const
|
||||
if (role == EventIdRole) {
|
||||
return eventId();
|
||||
}
|
||||
if (role == TimeRole) {
|
||||
return time();
|
||||
}
|
||||
if (role == TimeStringRole) {
|
||||
return timeString();
|
||||
if (role == DateTimeRole) {
|
||||
return QVariant::fromValue(dateTime());
|
||||
}
|
||||
if (role == AuthorRole) {
|
||||
return QVariant::fromValue<NeochatRoomMember *>(author());
|
||||
@@ -199,8 +192,7 @@ QHash<int, QByteArray> MessageContentModel::roleNamesStatic()
|
||||
roles[MessageContentModel::ComponentTypeRole] = "componentType";
|
||||
roles[MessageContentModel::ComponentAttributesRole] = "componentAttributes";
|
||||
roles[MessageContentModel::EventIdRole] = "eventId";
|
||||
roles[MessageContentModel::TimeRole] = "time";
|
||||
roles[MessageContentModel::TimeStringRole] = "timeString";
|
||||
roles[MessageContentModel::DateTimeRole] = "dateTime";
|
||||
roles[MessageContentModel::AuthorRole] = "author";
|
||||
roles[MessageContentModel::FileTransferInfoRole] = "fileTransferInfo";
|
||||
roles[MessageContentModel::ItineraryModelRole] = "itineraryModel";
|
||||
|
||||
@@ -21,6 +21,8 @@
|
||||
#include "neochatroom.h"
|
||||
#include "neochatroommember.h"
|
||||
|
||||
class NeoChatDateTime;
|
||||
|
||||
/**
|
||||
* @class MessageContentModel
|
||||
*
|
||||
@@ -47,8 +49,7 @@ public:
|
||||
ComponentTypeRole = Qt::UserRole, /**< The type of component to visualise the message. */
|
||||
ComponentAttributesRole, /**< The attributes of the component. */
|
||||
EventIdRole, /**< The matrix event ID of the event. */
|
||||
TimeRole, /**< The timestamp for when the event was sent (as a QDateTime). */
|
||||
TimeStringRole, /**< The timestamp for when the event was sent as a string (in QLocale::ShortFormat). */
|
||||
DateTimeRole, /**< The timestamp for when the event was sent (as a NeoChatDateTime). */
|
||||
AuthorRole, /**< The author of the event. */
|
||||
FileTransferInfoRole, /**< FileTransferInfo for any downloading files. */
|
||||
ItineraryModelRole, /**< The itinerary model for a file. */
|
||||
@@ -125,18 +126,11 @@ protected:
|
||||
QString m_eventId;
|
||||
|
||||
/**
|
||||
* @brief QDateTime for the message.
|
||||
* @brief NeoChatDateTime for the message.
|
||||
*
|
||||
* The default implementation returns the current time.
|
||||
*/
|
||||
virtual QDateTime time() const;
|
||||
|
||||
/**
|
||||
* @brief Time for the message as a string in the from "hh:mm".
|
||||
*
|
||||
* The default implementation returns the current time.
|
||||
*/
|
||||
virtual QString timeString() const;
|
||||
virtual NeoChatDateTime dateTime() const;
|
||||
|
||||
/**
|
||||
* @brief The author of the message.
|
||||
|
||||
Reference in New Issue
Block a user