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 c797ecea3d
commit 92c58b0ea0
17 changed files with 191 additions and 172 deletions

View File

@@ -17,8 +17,9 @@
#include "enums/messagecomponenttype.h"
#include "eventhandler.h"
#include "events/pollevent.h"
#include "models/reactionmodel.h"
#include "models/eventmessagecontentmodel.h"
#include "models/reactionmodel.h"
#include "neochatdatetime.h"
#include "neochatroommember.h"
using namespace Quotient;
@@ -110,11 +111,8 @@ QVariant MessageModel::data(const QModelIndex &idx, int role) const
switch (role) {
case DelegateTypeRole:
return DelegateType::ReadMarker;
case TimeRole: {
const QDateTime eventDate = data(index(m_lastReadEventIndex.row() + 1, 0), TimeRole).toDateTime().toLocalTime();
static const KFormat format;
return format.formatRelativeDateTime(eventDate, QLocale::ShortFormat);
}
case DateTimeRole:
return data(index(m_lastReadEventIndex.row() + 1, 0), DateTimeRole);
case SpecialMarksRole:
// Check if all the earlier events in the timeline are hidden. If so hide this.
for (auto r = row - 1; r >= 0; --r) {
@@ -230,12 +228,8 @@ QVariant MessageModel::data(const QModelIndex &idx, int role) const
return {};
}
if (role == TimeRole) {
return EventHandler::time(eventRoom, &event.value().get(), isPending);
}
if (role == SectionRole) {
return EventHandler::timeString(eventRoom, &event.value().get(), true, QLocale::ShortFormat, isPending);
if (role == DateTimeRole) {
return QVariant::fromValue(EventHandler::dateTime(eventRoom, &event.value().get(), isPending));
}
if (role == IsThreadedRole) {
@@ -267,8 +261,8 @@ QVariant MessageModel::data(const QModelIndex &idx, int role) const
// While the row is removed the subsequent row indexes are not changed so we need to skip over the removed index.
// See - https://doc.qt.io/qt-5/qabstractitemmodel.html#beginRemoveRows
if (data(i, SpecialMarksRole) != EventStatus::Hidden && !itemData(i).empty()) {
const auto day = data(idx, TimeRole).toDateTime().toLocalTime().date().dayOfYear();
const auto previousEventDay = data(i, TimeRole).toDateTime().toLocalTime().date().dayOfYear();
const auto day = data(idx, DateTimeRole).value<NeoChatDateTime>().dateTime().toLocalTime().date().dayOfYear();
const auto previousEventDay = data(i, DateTimeRole).value<NeoChatDateTime>().dateTime().toLocalTime().date().dayOfYear();
return day != previousEventDay;
}
}
@@ -342,8 +336,7 @@ QHash<int, QByteArray> MessageModel::roleNames() const
QHash<int, QByteArray> roles = QAbstractItemModel::roleNames();
roles[DelegateTypeRole] = "delegateType";
roles[EventIdRole] = "eventId";
roles[TimeRole] = "time";
roles[SectionRole] = "section";
roles[DateTimeRole] = "dateTime";
roles[AuthorRole] = "author";
roles[HighlightRole] = "isHighlighted";
roles[SpecialMarksRole] = "marks";