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:
@@ -7,6 +7,7 @@
|
||||
#include <Quotient/room.h>
|
||||
|
||||
#include "messagefiltermodel.h"
|
||||
#include "neochatdatetime.h"
|
||||
#include "timelinemessagemodel.h"
|
||||
|
||||
using namespace Qt::StringLiterals;
|
||||
@@ -34,8 +35,9 @@ QVariant MediaMessageFilterModel::data(const QModelIndex &index, int role) const
|
||||
// We need to catch this one and return true if the next media object was
|
||||
// on a different day.
|
||||
if (role == TimelineMessageModel::ShowSectionRole) {
|
||||
const auto day = mapToSource(index).data(TimelineMessageModel::TimeRole).toDateTime().toLocalTime().date();
|
||||
const auto previousEventDay = mapToSource(this->index(index.row() + 1, 0)).data(TimelineMessageModel::TimeRole).toDateTime().toLocalTime().date();
|
||||
const auto day = mapToSource(index).data(TimelineMessageModel::DateTimeRole).value<NeoChatDateTime>().dateTime().toLocalTime().date();
|
||||
const auto previousEventDay =
|
||||
mapToSource(this->index(index.row() + 1, 0)).data(TimelineMessageModel::DateTimeRole).value<NeoChatDateTime>().dateTime().toLocalTime().date();
|
||||
return day != previousEventDay;
|
||||
}
|
||||
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
#include "enums/delegatetype.h"
|
||||
#include "messagemodel.h"
|
||||
#include "models/timelinemodel.h"
|
||||
#include "neochatdatetime.h"
|
||||
|
||||
using namespace Quotient;
|
||||
|
||||
@@ -179,9 +180,13 @@ bool MessageFilterModel::showAuthor(QModelIndex index) const
|
||||
if (data(i, TimelineMessageModel::SpecialMarksRole) != EventStatus::Hidden && !itemData(i).empty()) {
|
||||
return data(i, TimelineMessageModel::AuthorRole) != data(index, TimelineMessageModel::AuthorRole)
|
||||
|| data(i, TimelineMessageModel::DelegateTypeRole) == DelegateType::State
|
||||
|| data(i, TimelineMessageModel::TimeRole).toDateTime().msecsTo(data(index, TimelineMessageModel::TimeRole).toDateTime()) > 600000
|
||||
|| data(i, TimelineMessageModel::TimeRole).toDateTime().toLocalTime().date().day()
|
||||
!= data(index, TimelineMessageModel::TimeRole).toDateTime().toLocalTime().date().day();
|
||||
|| data(i, TimelineMessageModel::DateTimeRole)
|
||||
.value<NeoChatDateTime>()
|
||||
.dateTime()
|
||||
.msecsTo(data(index, TimelineMessageModel::DateTimeRole).value<NeoChatDateTime>().dateTime())
|
||||
> 600000
|
||||
|| data(i, TimelineMessageModel::DateTimeRole).value<NeoChatDateTime>().dateTime().toLocalTime().date().day()
|
||||
!= data(index, TimelineMessageModel::DateTimeRole).value<NeoChatDateTime>().dateTime().toLocalTime().date().day();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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";
|
||||
|
||||
@@ -60,8 +60,7 @@ public:
|
||||
enum EventRoles {
|
||||
DelegateTypeRole = Qt::UserRole + 1, /**< The delegate type of the message. */
|
||||
EventIdRole, /**< The matrix event ID of the event. */
|
||||
TimeRole, /**< The timestamp for when the event was sent (as a QDateTime). */
|
||||
SectionRole, /**< The date of the event as a string. */
|
||||
DateTimeRole, /**< The timestamp for when the event was sent (as a NeoChatDateTime). */
|
||||
AuthorRole, /**< The author of the event. */
|
||||
HighlightRole, /**< Whether the event should be highlighted. */
|
||||
SpecialMarksRole, /**< Whether the event is hidden or not. */
|
||||
|
||||
Reference in New Issue
Block a user