From cc19df82bab213bd628792e0b3eca8c2ee986506 Mon Sep 17 00:00:00 2001 From: Dustin Mierau Date: Wed, 20 Dec 2023 12:47:09 -0800 Subject: Add relative dates to news list on macOS. Also add full date/time display in news reader view. --- Hotline/macOS/NewsView.swift | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) (limited to 'Hotline/macOS') diff --git a/Hotline/macOS/NewsView.swift b/Hotline/macOS/NewsView.swift index aa32d6a..c59b446 100644 --- a/Hotline/macOS/NewsView.swift +++ b/Hotline/macOS/NewsView.swift @@ -11,11 +11,20 @@ struct NewsItemView: View { static var dateFormatter: DateFormatter = { var dateFormatter = DateFormatter() - dateFormatter.dateFormat = "MM/dd/yy, h:mm a" + dateFormatter.dateStyle = .long + dateFormatter.timeStyle = .short dateFormatter.timeZone = .gmt return dateFormatter }() + static var relativeDateFormatter: RelativeDateTimeFormatter = { + var formatter = RelativeDateTimeFormatter() + formatter.unitsStyle = .full + formatter.dateTimeStyle = .named + formatter.formattingContext = .listItem + return formatter + }() + var body: some View { HStack { if news.type == .bundle || news.type == .category { @@ -44,6 +53,7 @@ struct NewsItemView: View { if news.type == .article && news.articleUsername != nil { Text(news.articleUsername!).foregroundStyle(.secondary).lineLimit(1) } + Spacer() if news.type == .bundle || news.type == .category { Text("^[\(news.count) \(news.type == .bundle ? "Category" : "Post")](inflect: true)") @@ -54,10 +64,9 @@ struct NewsItemView: View { .padding([.top, .bottom], 2) .background(Capsule(style: .circular).stroke(.secondary.opacity(0.3), lineWidth: 1)) } - Spacer() if news.type == .article && news.articleUsername != nil { if let d = news.articleDate { - Text(NewsItemView.dateFormatter.string(from: d)).foregroundStyle(.secondary) + Text(NewsItemView.relativeDateFormatter.localizedString(for: d, relativeTo: Date.now)).lineLimit(1).foregroundStyle(.secondary) } } } -- cgit