chore: update "{time_abbr} ago" string

This commit is contained in:
CMK 2021-08-16 14:48:24 +08:00
parent fea3b5d8b4
commit ffea4b1438
3 changed files with 60 additions and 27 deletions

View File

@ -69,7 +69,17 @@ extension NotificationSection {
// configure author name, notification description, timestamp // configure author name, notification description, timestamp
let nameText = notification.account.displayNameWithFallback 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 { do {
let nameContent = MastodonContent(content: nameText, emojis: notification.account.emojiMeta) let nameContent = MastodonContent(content: nameText, emojis: notification.account.emojiMeta)
@ -94,12 +104,12 @@ extension NotificationSection {
} }
let createAt = notification.createAt let createAt = notification.createAt
cell.timestampLabel.text = createAt.localizedTimeAgoSinceNow cell.timestampLabel.text = createAt.localizedSlowedTimeAgoSinceNow
AppContext.shared.timestampUpdatePublisher AppContext.shared.timestampUpdatePublisher
.receive(on: DispatchQueue.main) .receive(on: DispatchQueue.main)
.sink { [weak cell] _ in .sink { [weak cell] _ in
guard let cell = cell else { return } guard let cell = cell else { return }
cell.timestampLabel.text = createAt.localizedTimeAgoSinceNow cell.timestampLabel.text = createAt.localizedSlowedTimeAgoSinceNow
} }
.store(in: &cell.disposeBag) .store(in: &cell.disposeBag)

View File

@ -33,7 +33,7 @@ extension Date {
return L10n.Common.Controls.Timeline.Timestamp.now return L10n.Common.Controls.Timeline.Timestamp.now
} else { } else {
if isAbbreviated { if isAbbreviated {
return latestDate.shortTimeAgo(since: earlierDate) return latestDate.localizedShortTimeAgo(since: earlierDate)
} else { } else {
return Date.relativeTimestampFormatter.localizedString(for: earlierDate, relativeTo: latestDate) return Date.relativeTimestampFormatter.localizedString(for: earlierDate, relativeTo: latestDate)
} }
@ -44,6 +44,29 @@ extension Date {
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 { func localizedTimeLeft() -> String {
let date = Date() let date = Date()
let earlierDate = date < self ? date : self 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) let components = Calendar.current.dateComponents([.year, .month, .day, .hour, .minute, .second], from: earlierDate, to: latestDate)
if components.year! > 0 { if components.year! > 0 {
return L10n.Date.Year.left(components.second!) return L10n.Date.Year.left(components.year!)
} else if components.month! > 0 { } else if components.month! > 0 {
return L10n.Date.Month.left(components.month!) return L10n.Date.Month.left(components.month!)
} else if components.day! > 0 { } else if components.day! > 0 {

View File

@ -33,28 +33,28 @@ extension Mastodon.Entity.Notification.NotificationType {
} }
} }
public var actionText: String { // public var actionText: String {
get { // get {
var actionText: String // var actionText: String
switch self { // switch self {
case .follow: // case .follow:
actionText = L10n.Scene.Notification.Action.follow // actionText = L10n.Scene.Notification.Action.follow
case .favourite: // case .favourite:
actionText = L10n.Scene.Notification.Action.favourite // actionText = L10n.Scene.Notification.Action.favourite
case .reblog: // case .reblog:
actionText = L10n.Scene.Notification.Action.reblog // actionText = L10n.Scene.Notification.Action.reblog
case .mention: // case .mention:
actionText = L10n.Scene.Notification.Action.mention // actionText = L10n.Scene.Notification.Action.mention
case .poll: // case .poll:
actionText = L10n.Scene.Notification.Action.poll // actionText = L10n.Scene.Notification.Action.poll
case .followRequest: // case .followRequest:
actionText = L10n.Scene.Notification.Action.followRequest // actionText = L10n.Scene.Notification.Action.followRequest
default: // default:
actionText = "" // actionText = ""
} // }
return actionText // return actionText
} // }
} // }
public var actionImageName: String { public var actionImageName: String {
get { get {