From b9f87409542211adf93ee21a0f075b1c7392448a Mon Sep 17 00:00:00 2001 From: miruka Date: Wed, 24 Jun 2020 08:14:54 -0400 Subject: [PATCH] Move room date formatting logic to utils function --- src/gui/MainPane/Room.qml | 17 +---------------- src/gui/Utils.qml | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+), 16 deletions(-) diff --git a/src/gui/MainPane/Room.qml b/src/gui/MainPane/Room.qml index 7a02d820..6541da69 100644 --- a/src/gui/MainPane/Room.qml +++ b/src/gui/MainPane/Room.qml @@ -60,22 +60,7 @@ HTile { TitleRightInfoLabel { tile: room color: theme.mainPane.listView.room.lastEventDate - text: { - model.last_event_date < new Date(1) ? - "" : - - // e.g. "03:24" - utils.dateIsToday(model.last_event_date) ? - utils.formatTime(model.last_event_date, false) : - - // e.g. "5 Dec" - model.last_event_date.getFullYear() === - new Date().getFullYear() ? - Qt.formatDate(model.last_event_date, "d MMM") : - - // e.g. "Jan 2020" - Qt.formatDate(model.last_event_date, "MMM yyyy") - } + text: utils.smartFormatDate(model.last_event_date) } } diff --git a/src/gui/Utils.qml b/src/gui/Utils.qml index a6335248..17dd07be 100644 --- a/src/gui/Utils.qml +++ b/src/gui/Utils.qml @@ -299,6 +299,25 @@ QtObject { } + function smartFormatDate(date) { + return ( + date < new Date(1) ? + "" : + + // e.g. "03:24" + dateIsToday(date) ? + formatTime(date, false) : + + // e.g. "5 Dec" + date.getFullYear() === new Date().getFullYear() ? + Qt.formatDate(date, "d MMM") : + + // e.g. "Jan 2020" + Qt.formatDate(date, "MMM yyyy") + ) + } + + function formatDuration(milliseconds) { const totalSeconds = milliseconds / 1000