From 5df1c1cd3e2bf57ea2f4f4a337d1cdc02bf67d62 Mon Sep 17 00:00:00 2001 From: Jackson Harper Date: Fri, 15 Mar 2024 19:59:50 +0800 Subject: [PATCH] Simplify code, handle crazy people with 12hr time --- .../Sources/Views/FeedItem/GridCard.swift | 6 +++ .../Views/FeedItem/LibraryItemCard.swift | 38 +++++++++++++++---- 2 files changed, 37 insertions(+), 7 deletions(-) diff --git a/apple/OmnivoreKit/Sources/Views/FeedItem/GridCard.swift b/apple/OmnivoreKit/Sources/Views/FeedItem/GridCard.swift index 15a79b8e5..cd96cbcac 100644 --- a/apple/OmnivoreKit/Sources/Views/FeedItem/GridCard.swift +++ b/apple/OmnivoreKit/Sources/Views/FeedItem/GridCard.swift @@ -12,11 +12,13 @@ public enum GridCardAction { public struct GridCard: View { @ObservedObject var item: Models.LibraryItem + let savedAtStr: String public init( item: Models.LibraryItem ) { self.item = item + self.savedAtStr = savedDateString(item.savedAt) } var imageBox: some View { @@ -198,6 +200,10 @@ public struct GridCard: View { $0.icon } + Text(savedAtStr) + .font(.footnote) + .foregroundColor(Color.themeLibraryItemSubtle) ++ Text("\(estimatedReadingTime)") .font(.caption2).fontWeight(.medium) .foregroundColor(Color.themeLibraryItemSubtle) diff --git a/apple/OmnivoreKit/Sources/Views/FeedItem/LibraryItemCard.swift b/apple/OmnivoreKit/Sources/Views/FeedItem/LibraryItemCard.swift index 261e45dc5..d301f13d6 100644 --- a/apple/OmnivoreKit/Sources/Views/FeedItem/LibraryItemCard.swift +++ b/apple/OmnivoreKit/Sources/Views/FeedItem/LibraryItemCard.swift @@ -44,14 +44,33 @@ public extension View { } } +func savedDateString(_ savedAt: Date?) -> String { + if let savedAt = savedAt { + let locale = Locale.current + let dateFormatter = DateFormatter() + if Calendar.current.isDateInToday(savedAt) { + dateFormatter.dateStyle = .none + dateFormatter.timeStyle = .short + } else { + dateFormatter.dateFormat = "MMM dd" + } + dateFormatter.locale = locale + return dateFormatter.string(from: savedAt) + " • " + } + return "" +} + public struct LibraryItemCard: View { let viewer: Viewer? @ObservedObject var item: Models.LibraryItem @State var noteLineLimit: Int? = 3 + let savedAtStr: String + public init(item: Models.LibraryItem, viewer: Viewer?) { self.item = item self.viewer = viewer + self.savedAtStr = savedDateString(item.savedAt) } public var body: some View { @@ -215,23 +234,28 @@ public struct LibraryItemCard: View { $0.icon } + Text(savedAtStr) + .font(.footnote) + .foregroundColor(Color.themeLibraryItemSubtle) + + + Text("\(estimatedReadingTime)") - .font(.caption2).fontWeight(.medium) + .font(.footnote) .foregroundColor(Color.themeLibraryItemSubtle) + Text("\(readingProgress)") - .font(.caption2).fontWeight(.medium) + .font(.footnote) .foregroundColor(isPartiallyRead ? Color.appGreenSuccess : Color.themeLibraryItemSubtle) + Text("\(highlightsText)") - .font(.caption2).fontWeight(.medium) + .font(.footnote) .foregroundColor(Color.themeLibraryItemSubtle) + Text("\(notesText)") - .font(.caption2).fontWeight(.medium) + .font(.footnote) .foregroundColor(Color.themeLibraryItemSubtle) } .frame(maxWidth: .infinity, alignment: .leading) @@ -281,13 +305,13 @@ public struct LibraryItemCard: View { var byLine: some View { if let origin = cardSiteName(item.pageURLString) { Text(bylineStr + " | " + origin) - .font(.caption2) + .font(.footnote) .foregroundColor(Color.themeLibraryItemSubtle) .frame(maxWidth: .infinity, alignment: .leading) .lineLimit(1) } else { Text(bylineStr) - .font(.caption2) + .font(.footnote) .foregroundColor(Color.themeLibraryItemSubtle) .frame(maxWidth: .infinity, alignment: .leading) .lineLimit(1) @@ -295,7 +319,7 @@ public struct LibraryItemCard: View { } public var articleInfo: some View { - VStack(alignment: .leading, spacing: 5) { + VStack(alignment: .leading, spacing: 7) { readInfo .dynamicTypeSize(.xSmall ... .medium)