Skip to content

Commit

Permalink
fix: timestamp shows yesterday for a message received 3 days ago
Browse files Browse the repository at this point in the history
- introduce one global update timer, to be used in various components
for date/time sensitive updates
- this timer runs only when the app is/becomes active
- use the timer's `triggered()` signal to update the timestamp label
when needed; ie. if it's to display a relative timestamp and it's
currently visible

Fixes #11460
  • Loading branch information
caybro committed Feb 12, 2024
1 parent 4b24497 commit 19daf93
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 1 deletion.
17 changes: 17 additions & 0 deletions ui/StatusQ/include/StatusQ/sharedupdatetimer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#pragma once

#include <QObject>
#include <QTimer>

class SharedUpdateTimer : public QObject
{
Q_OBJECT
public:
explicit SharedUpdateTimer(QObject* parent = nullptr);

signals:
void triggered();

private:
QTimer m_timer;
};
19 changes: 18 additions & 1 deletion ui/StatusQ/src/StatusQ/Components/StatusTimeStampLabel.qml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,24 @@ StatusBaseText {
color: Theme.palette.baseColor1
font.pixelSize: 10
visible: !!text
text: showFullTimestamp ? LocaleUtils.formatDateTime(timestamp) : LocaleUtils.formatRelativeTimestamp(timestamp)
text: d.formattedLabel

QtObject {
id: d
property string formattedLabel: showFullTimestamp ? LocaleUtils.formatDateTime(root.timestamp) : LocaleUtils.formatRelativeTimestamp(root.timestamp)

readonly property var _conn: Connections {
target: SharedUpdateTimer
enabled: root.visible && root.timestamp && !root.showFullTimestamp
function onTriggered() {
const newFormattedLabel = LocaleUtils.formatRelativeTimestamp(root.timestamp)
if (newFormattedLabel !== d.formattedLabel) {
d.formattedLabel = newFormattedLabel
}
}
}
}

StatusToolTip {
id: tooltip
visible: hhandler.hovered && !!text
Expand Down
4 changes: 4 additions & 0 deletions ui/StatusQ/src/plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "StatusQ/permissionutilsinternal.h"
#include "StatusQ/rolesrenamingmodel.h"
#include "StatusQ/rxvalidator.h"
#include "StatusQ/sharedupdatetimer.h"
#include "StatusQ/statussyntaxhighlighter.h"
#include "StatusQ/statuswindow.h"
#include "StatusQ/stringutilsinternal.h"
Expand Down Expand Up @@ -57,6 +58,9 @@ class StatusQPlugin : public QQmlExtensionPlugin
qmlRegisterType<WritableProxyModel>("StatusQ", 0, 1, "WritableProxyModel");
qmlRegisterType<FormattedDoubleProperty>("StatusQ", 0, 1, "FormattedDoubleProperty");

qmlRegisterSingletonType<SharedUpdateTimer>(
"StatusQ.Core", 0, 1, "SharedUpdateTimer", [](QQmlEngine*, QJSEngine*) { return new SharedUpdateTimer; });

qmlRegisterSingletonType<QClipboardProxy>("StatusQ", 0, 1, "QClipboardProxy", &QClipboardProxy::qmlInstance);

qmlRegisterSingletonType<ModelUtilsInternal>(
Expand Down
23 changes: 23 additions & 0 deletions ui/StatusQ/src/sharedupdatetimer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include "StatusQ/sharedupdatetimer.h"

#include <QGuiApplication>

using namespace std::chrono_literals;

SharedUpdateTimer::SharedUpdateTimer(QObject* parent)
: QObject{parent}
{
m_timer.setTimerType(Qt::VeryCoarseTimer);
m_timer.setInterval(1s);

connect(&m_timer, &QTimer::timeout, this, &SharedUpdateTimer::triggered);
connect(qApp, &QGuiApplication::applicationStateChanged, this, [this](Qt::ApplicationState state) {
if (state == Qt::ApplicationActive) {
emit triggered(); // emulate QML Timer's `triggeredOnStart`
m_timer.start();
}
else {
m_timer.stop();
}
});
}

0 comments on commit 19daf93

Please sign in to comment.