diff --git a/notificationd/CMakeLists.txt b/notificationd/CMakeLists.txt index 9f055a1..e6448a5 100644 --- a/notificationd/CMakeLists.txt +++ b/notificationd/CMakeLists.txt @@ -1,5 +1,6 @@ set(SRCS main.cpp application.cpp + datehelper.cpp screenhelper.cpp notificationsmodel.cpp notificationserver.cpp diff --git a/notificationd/datehelper.cpp b/notificationd/datehelper.cpp new file mode 100644 index 0000000..396fd01 --- /dev/null +++ b/notificationd/datehelper.cpp @@ -0,0 +1,34 @@ +#include "datehelper.h" +#include + +DateHelper::DateHelper(QObject *parent) : QObject(parent) +{ + +} + +QString DateHelper::friendlyTime(const QDateTime &time) +{ + QDateTime now = QDateTime::currentDateTime(); + qint64 minutes = qRound64(time.secsTo(now) / 60.0f); + + if (minutes < 1) + return tr("Now"); + else if (minutes == 1) + return tr("1 minute ago"); + else if (minutes < 60) + return tr("%1 minutes ago").arg(minutes); + + qint64 hours = qRound64(minutes / 60.0f); + if (hours == 1) + return tr("1 hour ago"); + else if (hours < 24) + return tr("%1 hours ago").arg(hours); + + qint64 days = qRound64(hours / 24.0f); + if (days == 1) + return tr("1 day ago"); + else if (days <= 10) + return tr("%1 days ago").arg(days); + + return time.toString(Qt::DefaultLocaleShortDate); +} diff --git a/notificationd/datehelper.h b/notificationd/datehelper.h new file mode 100644 index 0000000..adf5db2 --- /dev/null +++ b/notificationd/datehelper.h @@ -0,0 +1,16 @@ +#ifndef DATEHELPER_H +#define DATEHELPER_H + +#include + +class DateHelper : public QObject +{ + Q_OBJECT + +public: + explicit DateHelper(QObject *parent = nullptr); + + Q_INVOKABLE static QString friendlyTime(const QDateTime &time); +}; + +#endif // DATEHELPER_H diff --git a/notificationd/historymodel.cpp b/notificationd/historymodel.cpp index 5970b06..8151bfd 100644 --- a/notificationd/historymodel.cpp +++ b/notificationd/historymodel.cpp @@ -18,6 +18,7 @@ */ #include "historymodel.h" +#include "datehelper.h" #include #include @@ -51,7 +52,9 @@ QVariant HistoryModel::data(const QModelIndex &index, int role) const case HistoryModel::ImageRole: return ""; case HistoryModel::CreatedRole: - return notification.created; + return DateHelper::friendlyTime(notification.created); + case HistoryModel::UpdatedRole: + return DateHelper::friendlyTime(notification.updated); case HistoryModel::BodyRole: return notification.body; case HistoryModel::IconNameRole: @@ -147,3 +150,8 @@ void HistoryModel::initDatas() QDataStream in(&listByteArray, QIODevice::ReadOnly); in >> m_notifications; } + +void HistoryModel::updateTime() +{ + emit layoutChanged(); +} diff --git a/notificationd/historymodel.h b/notificationd/historymodel.h index e58a5e7..7519cd8 100644 --- a/notificationd/historymodel.h +++ b/notificationd/historymodel.h @@ -53,6 +53,7 @@ public: Q_INVOKABLE void save(); void initDatas(); + void updateTime(); private: QVector m_notifications; diff --git a/notificationd/notificationwindow.cpp b/notificationd/notificationwindow.cpp index 95e9e6a..a61c89a 100644 --- a/notificationd/notificationwindow.cpp +++ b/notificationd/notificationwindow.cpp @@ -47,6 +47,7 @@ bool NotificationWindow::eventFilter(QObject *object, QEvent *event) } } else if (event->type() == QEvent::Show) { KWindowSystem::setState(winId(), NET::SkipTaskbar | NET::SkipPager | NET::SkipSwitcher); + HistoryModel::self()->updateTime(); } return QObject::eventFilter(object, event); diff --git a/notificationd/qml/NotificationWindow.qml b/notificationd/qml/NotificationWindow.qml index ec62eba..cec4721 100644 --- a/notificationd/qml/NotificationWindow.qml +++ b/notificationd/qml/NotificationWindow.qml @@ -35,7 +35,7 @@ Item { id: _background anchors.fill: parent color: FishUI.Theme.secondBackgroundColor - radius: NotificationDialog.width * 0.02 //FishUI.Theme.bigRadius + radius: NotificationDialog.width * 0.05 //FishUI.Theme.bigRadius opacity: 0.7 border.width: 1 @@ -126,10 +126,6 @@ Item { visible: _view.count === 0 } - addDisplaced: Transition { - NumberAnimation { properties: "x, y"; duration: 250 } - } - removeDisplaced: Transition { NumberAnimation { properties: "x, y"; duration: 250 } } @@ -185,16 +181,25 @@ Item { rightPadding: FishUI.Units.smallSpacing } - Label { - text: model.body - visible: text - rightPadding: FishUI.Units.smallSpacing - maximumLineCount: 2 - elide: Text.ElideRight - wrapMode: Text.Wrap - Layout.fillWidth: true - // Layout.fillHeight: true - Layout.alignment: Qt.AlignVCenter + RowLayout { + Label { + id: bodyLabel + text: model.body + visible: text + rightPadding: FishUI.Units.smallSpacing + maximumLineCount: 2 + elide: Text.ElideRight + wrapMode: Text.Wrap + Layout.fillWidth: true + // Layout.fillHeight: true + Layout.alignment: Qt.AlignVCenter + } + + Label { + text: model.created + rightPadding: FishUI.Units.smallSpacing + Layout.alignment: Qt.AlignRight + } } Item { diff --git a/notificationd/translations/en_US.ts b/notificationd/translations/en_US.ts index 05e633e..caba99f 100644 --- a/notificationd/translations/en_US.ts +++ b/notificationd/translations/en_US.ts @@ -1,6 +1,44 @@ + + DateHelper + + + Now + + + + + 1 minute ago + + + + + %1 minutes ago + + + + + 1 hour ago + + + + + %1 hours ago + + + + + 1 day ago + + + + + %1 days ago + + + NotificationWindow diff --git a/notificationd/translations/zh_CN.ts b/notificationd/translations/zh_CN.ts index da0f268..1780ce0 100644 --- a/notificationd/translations/zh_CN.ts +++ b/notificationd/translations/zh_CN.ts @@ -1,6 +1,44 @@ - + + + DateHelper + + + Now + 现在 + + + + 1 minute ago + 一分钟前 + + + + %1 minutes ago + %1分钟前 + + + + 1 hour ago + 一小时前 + + + + %1 hours ago + %1小时前 + + + + 1 day ago + 一天前 + + + + %1 days ago + %1天前 + + NotificationWindow