From 882441d88dd89ef4b48fe77729822d31e07336e9 Mon Sep 17 00:00:00 2001 From: PikachuEXE Date: Fri, 24 Feb 2023 18:50:09 +0800 Subject: [PATCH 1/2] * Update subscriptions view to display video publish time in relative form even fetched via RSS --- .../components/ft-list-video/ft-list-video.js | 24 ++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/src/renderer/components/ft-list-video/ft-list-video.js b/src/renderer/components/ft-list-video/ft-list-video.js index 7daf6208dbce9..009a1836fe16c 100644 --- a/src/renderer/components/ft-list-video/ft-list-video.js +++ b/src/renderer/components/ft-list-video/ft-list-video.js @@ -322,6 +322,10 @@ export default defineComponent({ return this.historyCache[historyIndex].lastViewedPlaylistId }, + + currentLocale: function () { + return this.$i18n.locale.replace('_', '-') + }, }, mounted: function () { this.parseVideoData() @@ -420,7 +424,25 @@ export default defineComponent({ this.publishedText = this.data.publishedText } - if (typeof (this.data.publishedText) !== 'undefined' && this.data.publishedText !== null && !this.isLive) { + if (this.data.isRSS && this.data.publishedDate != null && !this.isLive) { + const now = new Date() + // Convert from ms to second to minute + let timeDiffFromNow = ((now - this.data.publishedDate) / 1000) / 60 + let timeUnit = 'minute' + + if (timeDiffFromNow > 60) { + timeDiffFromNow /= 60 + timeUnit = 'hour' + } + + if (timeUnit === 'hour' && timeDiffFromNow > 24) { + timeDiffFromNow /= 24 + timeUnit = 'day' + } + + // Using `Math.ceil` so that -1.x days ago displayed as 1 day ago + this.uploadedTime = new Intl.RelativeTimeFormat(this.currentLocale).format(Math.ceil(-timeDiffFromNow), timeUnit) + } else if (typeof (this.data.publishedText) !== 'undefined' && this.data.publishedText !== null && !this.isLive) { // produces a string according to the template in the locales string this.uploadedTime = toLocalePublicationString({ publishText: this.publishedText, From deaf4dda6a993b87ea98978e0105f5f296a427f7 Mon Sep 17 00:00:00 2001 From: PikachuEXE Date: Sun, 26 Feb 2023 09:00:14 +0800 Subject: [PATCH 2/2] * Update to support seconds, months, years Also add more code comments Also fix issue of display after switching back from another view --- .../components/ft-list-video/ft-list-video.js | 27 ++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/src/renderer/components/ft-list-video/ft-list-video.js b/src/renderer/components/ft-list-video/ft-list-video.js index 009a1836fe16c..7afe052fa25d6 100644 --- a/src/renderer/components/ft-list-video/ft-list-video.js +++ b/src/renderer/components/ft-list-video/ft-list-video.js @@ -426,11 +426,19 @@ export default defineComponent({ if (this.data.isRSS && this.data.publishedDate != null && !this.isLive) { const now = new Date() - // Convert from ms to second to minute - let timeDiffFromNow = ((now - this.data.publishedDate) / 1000) / 60 - let timeUnit = 'minute' + // Convert from ms to second + // For easier code interpretation the value is made to be positive + // `publishedDate` is sometimes a string, e.g. when switched back from another view + const publishedDate = Date.parse(this.data.publishedDate) + let timeDiffFromNow = ((now - publishedDate) / 1000) + let timeUnit = 'second' if (timeDiffFromNow > 60) { + timeDiffFromNow /= 60 + timeUnit = 'minute' + } + + if (timeUnit === 'minute' && timeDiffFromNow > 60) { timeDiffFromNow /= 60 timeUnit = 'hour' } @@ -440,7 +448,20 @@ export default defineComponent({ timeUnit = 'day' } + // Diff month might have diff no. of days + // To ensure the display is fine we use 31 + if (timeUnit === 'day' && timeDiffFromNow > 31) { + timeDiffFromNow /= 24 + timeUnit = 'month' + } + + if (timeUnit === 'month' && timeDiffFromNow > 12) { + timeDiffFromNow /= 12 + timeUnit = 'year' + } + // Using `Math.ceil` so that -1.x days ago displayed as 1 day ago + // Notice that the value is turned to negative to be displayed as "ago" this.uploadedTime = new Intl.RelativeTimeFormat(this.currentLocale).format(Math.ceil(-timeDiffFromNow), timeUnit) } else if (typeof (this.data.publishedText) !== 'undefined' && this.data.publishedText !== null && !this.isLive) { // produces a string according to the template in the locales string