Format date (close to) the old way in qml timeline

This commit is contained in:
Nicolas Werner 2019-09-01 22:58:26 +02:00
parent ccedbde38b
commit 56e27ced25
3 changed files with 20 additions and 1 deletions

View File

@ -104,7 +104,7 @@ Rectangle {
id: dateBubble id: dateBubble
anchors.horizontalCenter: parent.horizontalCenter anchors.horizontalCenter: parent.horizontalCenter
visible: section.includes(" ") visible: section.includes(" ")
text: Qt.formatDate(new Date(Number(section.split(" ")[1]))) text: chat.model.formatDateSeparator(new Date(Number(section.split(" ")[1])))
height: contentHeight * 1.2 height: contentHeight * 1.2
width: contentWidth * 1.2 width: contentWidth * 1.2
horizontalAlignment: Text.AlignHCenter horizontalAlignment: Text.AlignHCenter

View File

@ -1,5 +1,7 @@
#include "TimelineModel.h" #include "TimelineModel.h"
#include <QRegularExpression>
#include "Logging.h" #include "Logging.h"
#include "Utils.h" #include "Utils.h"
@ -196,3 +198,18 @@ TimelineModel::displayName(QString id) const
{ {
return Cache::displayName(room_id_, id); return Cache::displayName(room_id_, id);
} }
QString
TimelineModel::formatDateSeparator(QDate date) const
{
auto now = QDateTime::currentDateTime();
QString fmt = QLocale::system().dateFormat(QLocale::LongFormat);
if (now.date().year() == date.year()) {
QRegularExpression rx("[^a-zA-Z]*y+[^a-zA-Z]*");
fmt = fmt.remove(rx);
}
return date.toString(fmt);
}

View File

@ -2,6 +2,7 @@
#include <QAbstractListModel> #include <QAbstractListModel>
#include <QColor> #include <QColor>
#include <QDate>
#include <QHash> #include <QHash>
#include <mtx/responses.hpp> #include <mtx/responses.hpp>
@ -30,6 +31,7 @@ public:
Q_INVOKABLE QColor userColor(QString id, QColor background); Q_INVOKABLE QColor userColor(QString id, QColor background);
Q_INVOKABLE QString displayName(QString id) const; Q_INVOKABLE QString displayName(QString id) const;
Q_INVOKABLE QString formatDateSeparator(QDate date) const;
void addEvents(const mtx::responses::Timeline &events); void addEvents(const mtx::responses::Timeline &events);