From ffea4b143854830d5c404651581f46c0e2533d46 Mon Sep 17 00:00:00 2001 From: CMK Date: Mon, 16 Aug 2021 14:48:24 +0800 Subject: [PATCH] chore: update "{time_abbr} ago" string --- .../Section/Status/NotificationSection.swift | 16 +++++-- Mastodon/Extension/Date.swift | 27 +++++++++++- .../Mastodon+Entity+Notification+Type.swift | 44 +++++++++---------- 3 files changed, 60 insertions(+), 27 deletions(-) diff --git a/Mastodon/Diffiable/Section/Status/NotificationSection.swift b/Mastodon/Diffiable/Section/Status/NotificationSection.swift index 11dba2146..215ba67cb 100644 --- a/Mastodon/Diffiable/Section/Status/NotificationSection.swift +++ b/Mastodon/Diffiable/Section/Status/NotificationSection.swift @@ -69,7 +69,17 @@ extension NotificationSection { // configure author name, notification description, timestamp let nameText = notification.account.displayNameWithFallback - let titleLabelText = "\(nameText) \(notification.notificationType.actionText)" + let titleLabelText: String = { + switch notification.notificationType { + case .favourite: return L10n.Scene.Notification.userFavoritedYourPost(nameText) + case .follow: return L10n.Scene.Notification.userFollowedYou(nameText) + case .followRequest: return L10n.Scene.Notification.userRequestedToFollowYou(nameText) + case .mention: return L10n.Scene.Notification.userMentionedYou(nameText) + case .poll: return L10n.Scene.Notification.userYourPollHasEnded(nameText) + case .reblog: return L10n.Scene.Notification.userRebloggedYourPost(nameText) + default: return "" + } + }() do { let nameContent = MastodonContent(content: nameText, emojis: notification.account.emojiMeta) @@ -94,12 +104,12 @@ extension NotificationSection { } let createAt = notification.createAt - cell.timestampLabel.text = createAt.localizedTimeAgoSinceNow + cell.timestampLabel.text = createAt.localizedSlowedTimeAgoSinceNow AppContext.shared.timestampUpdatePublisher .receive(on: DispatchQueue.main) .sink { [weak cell] _ in guard let cell = cell else { return } - cell.timestampLabel.text = createAt.localizedTimeAgoSinceNow + cell.timestampLabel.text = createAt.localizedSlowedTimeAgoSinceNow } .store(in: &cell.disposeBag) diff --git a/Mastodon/Extension/Date.swift b/Mastodon/Extension/Date.swift index ad6458687..51d70cc0d 100644 --- a/Mastodon/Extension/Date.swift +++ b/Mastodon/Extension/Date.swift @@ -33,7 +33,7 @@ extension Date { return L10n.Common.Controls.Timeline.Timestamp.now } else { if isAbbreviated { - return latestDate.shortTimeAgo(since: earlierDate) + return latestDate.localizedShortTimeAgo(since: earlierDate) } else { return Date.relativeTimestampFormatter.localizedString(for: earlierDate, relativeTo: latestDate) } @@ -44,6 +44,29 @@ extension Date { extension Date { + func localizedShortTimeAgo(since date: Date) -> String { + let earlierDate = date < self ? date : self + let latestDate = earlierDate == date ? self : date + + let components = Calendar.current.dateComponents([.year, .month, .day, .hour, .minute, .second], from: earlierDate, to: latestDate) + + if components.year! > 0 { + return L10n.Date.Year.Ago.abbr(components.year!) + } else if components.month! > 0 { + return L10n.Date.Month.Ago.abbr(components.month!) + } else if components.day! > 0 { + return L10n.Date.Day.Ago.abbr(components.day!) + } else if components.hour! > 0 { + return L10n.Date.Hour.Ago.abbr(components.hour!) + } else if components.minute! > 0 { + return L10n.Date.Minute.Ago.abbr(components.minute!) + } else if components.second! > 0 { + return L10n.Date.Year.Ago.abbr(components.second!) + } else { + return "" + } + } + func localizedTimeLeft() -> String { let date = Date() let earlierDate = date < self ? date : self @@ -52,7 +75,7 @@ extension Date { let components = Calendar.current.dateComponents([.year, .month, .day, .hour, .minute, .second], from: earlierDate, to: latestDate) if components.year! > 0 { - return L10n.Date.Year.left(components.second!) + return L10n.Date.Year.left(components.year!) } else if components.month! > 0 { return L10n.Date.Month.left(components.month!) } else if components.day! > 0 { diff --git a/Mastodon/Extension/MastodonSDK/Mastodon+Entity+Notification+Type.swift b/Mastodon/Extension/MastodonSDK/Mastodon+Entity+Notification+Type.swift index 2037f54a2..2bddd9e97 100644 --- a/Mastodon/Extension/MastodonSDK/Mastodon+Entity+Notification+Type.swift +++ b/Mastodon/Extension/MastodonSDK/Mastodon+Entity+Notification+Type.swift @@ -33,28 +33,28 @@ extension Mastodon.Entity.Notification.NotificationType { } } - public var actionText: String { - get { - var actionText: String - switch self { - case .follow: - actionText = L10n.Scene.Notification.Action.follow - case .favourite: - actionText = L10n.Scene.Notification.Action.favourite - case .reblog: - actionText = L10n.Scene.Notification.Action.reblog - case .mention: - actionText = L10n.Scene.Notification.Action.mention - case .poll: - actionText = L10n.Scene.Notification.Action.poll - case .followRequest: - actionText = L10n.Scene.Notification.Action.followRequest - default: - actionText = "" - } - return actionText - } - } +// public var actionText: String { +// get { +// var actionText: String +// switch self { +// case .follow: +// actionText = L10n.Scene.Notification.Action.follow +// case .favourite: +// actionText = L10n.Scene.Notification.Action.favourite +// case .reblog: +// actionText = L10n.Scene.Notification.Action.reblog +// case .mention: +// actionText = L10n.Scene.Notification.Action.mention +// case .poll: +// actionText = L10n.Scene.Notification.Action.poll +// case .followRequest: +// actionText = L10n.Scene.Notification.Action.followRequest +// default: +// actionText = "" +// } +// return actionText +// } +// } public var actionImageName: String { get {