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:
James Graham
2026-01-25 13:01:31 +00:00
parent 72416884d4
commit 275d221f75
23 changed files with 222 additions and 186 deletions

View File

@@ -27,14 +27,9 @@ RowLayout {
required property var author
/**
* @brief The timestamp of the message.
* @brief The timestamp of the event as a NeoChatDateTime.
*/
required property var time
/**
* @brief The timestamp of the message as a string.
*/
required property string timeString
required property NeoChatDateTime dateTime
Layout.fillWidth: true
Layout.maximumWidth: Message.maxContentWidth
@@ -83,11 +78,11 @@ RowLayout {
}
QQC2.Label {
id: timeLabel
text: root.timeString
text: root.dateTime.hourMinuteString
horizontalAlignment: Text.AlignRight
color: Kirigami.Theme.disabledTextColor
QQC2.ToolTip.visible: timeHoverHandler.hovered
QQC2.ToolTip.text: root.time.toLocaleString(Qt.locale(), Locale.ShortFormat)
QQC2.ToolTip.text: root.dateTime.shortDateTime
QQC2.ToolTip.delay: Kirigami.Units.toolTipDelay
HoverHandler {

View File

@@ -28,9 +28,9 @@ QQC2.Control {
required property NeochatRoomMember author
/**
* @brief The timestamp of the message.
* @brief The timestamp of the event as a NeoChatDateTime.
*/
required property var time
required property NeoChatDateTime dateTime
/**
* @brief The display text of the message.
@@ -164,12 +164,12 @@ QQC2.Control {
QQC2.ToolTip.delay: Kirigami.Units.toolTipDelay
}
QQC2.Button {
visible: root.time.toString() !== "Invalid Date"
visible: root.dateTime.isValid
icon.name: "view-fullscreen"
text: i18nc("@action:button", "Maximize")
display: QQC2.AbstractButton.IconOnly
onClicked: RoomManager.maximizeCode(root.author, root.time, root.display, root.componentAttributes.class);
onClicked: RoomManager.maximizeCode(root.author, root.dateTime, root.display, root.componentAttributes.class);
QQC2.ToolTip.text: text
QQC2.ToolTip.visible: hovered

View File

@@ -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();

View File

@@ -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;

View File

@@ -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";

View File

@@ -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.