diff --git a/NetNewsWire.xcodeproj/project.pbxproj b/NetNewsWire.xcodeproj/project.pbxproj index 73bfb787c..ce6b8cc53 100644 --- a/NetNewsWire.xcodeproj/project.pbxproj +++ b/NetNewsWire.xcodeproj/project.pbxproj @@ -429,6 +429,7 @@ 84D35E0E2DB9F32D004AA60E /* PBXFileSystemSynchronizedBuildFileExceptionSet */ = { isa = PBXFileSystemSynchronizedBuildFileExceptionSet; membershipExceptions = ( + UnreadCountDisplay.swift, Widget/WidgetData.swift, Widget/WidgetDataDecoder.swift, Widget/WidgetDeepLinks.swift, diff --git a/Shared/Widget/WidgetData.swift b/Shared/Widget/WidgetData.swift index d2e8ac0e3..5599a9b40 100644 --- a/Shared/Widget/WidgetData.swift +++ b/Shared/Widget/WidgetData.swift @@ -17,6 +17,12 @@ struct WidgetData: Codable { let starredArticles: [LatestArticle] let todayArticles: [LatestArticle] let lastUpdateTime: Date + let unreadCountDisplay: UnreadCountDisplay? + + /// Data written before this setting existed has no value, which means `.count`. + var effectiveUnreadCountDisplay: UnreadCountDisplay { + unreadCountDisplay ?? .count + } } struct LatestArticle: Codable, Identifiable, Hashable { diff --git a/Shared/Widget/WidgetDataDecoder.swift b/Shared/Widget/WidgetDataDecoder.swift index 249d1123b..d4b198d74 100644 --- a/Shared/Widget/WidgetDataDecoder.swift +++ b/Shared/Widget/WidgetDataDecoder.swift @@ -18,7 +18,7 @@ struct WidgetDataDecoder { let decodedWidgetData = try JSONDecoder().decode(WidgetData.self, from: Data(contentsOf: dataURL!)) return decodedWidgetData } else { - return WidgetData(totalUnreadCount: 0, totalTodayCount: 0, totalTodayUnreadCount: 0, totalStarredCount: 0, unreadArticles: [], starredArticles: [], todayArticles: [], lastUpdateTime: Date()) + return WidgetData(totalUnreadCount: 0, totalTodayCount: 0, totalTodayUnreadCount: 0, totalStarredCount: 0, unreadArticles: [], starredArticles: [], todayArticles: [], lastUpdateTime: Date(), unreadCountDisplay: nil) } } @@ -29,7 +29,7 @@ struct WidgetDataDecoder { let decoded = try JSONDecoder().decode(WidgetData.self, from: data) return decoded } catch { - return WidgetData(totalUnreadCount: 0, totalTodayCount: 0, totalTodayUnreadCount: 0, totalStarredCount: 0, unreadArticles: [], starredArticles: [], todayArticles: [], lastUpdateTime: Date()) + return WidgetData(totalUnreadCount: 0, totalTodayCount: 0, totalTodayUnreadCount: 0, totalStarredCount: 0, unreadArticles: [], starredArticles: [], todayArticles: [], lastUpdateTime: Date(), unreadCountDisplay: nil) } } diff --git a/Shared/Widget/WidgetDataEncoder.swift b/Shared/Widget/WidgetDataEncoder.swift index 4ce6e1324..f48ae575d 100644 --- a/Shared/Widget/WidgetDataEncoder.swift +++ b/Shared/Widget/WidgetDataEncoder.swift @@ -44,6 +44,12 @@ import Account Self.logger.error("WidgetDataEncoder: unable to create folder for images") return nil } + + NotificationCenter.default.addObserver(self, selector: #selector(handleUnreadCountDisplaySettingDidChange(_:)), name: .unreadCountDisplaySettingDidChange, object: nil) + } + + @objc func handleUnreadCountDisplaySettingDidChange(_ notification: Notification) { + encode() } /// Fire-and-forget. Sets `isRunning` synchronously so the background-task wait loop @@ -96,6 +102,12 @@ import Account func reloadTimelines(newData: WidgetData, existingData: WidgetData?) { if let existingData = existingData { + if existingData.effectiveUnreadCountDisplay != newData.effectiveUnreadCountDisplay { + WidgetCenter.shared.reloadAllTimelines() + Self.logger.debug("WidgetDataEncoder: Reloading all widgets because unread count display changed") + return + } + var shouldRefreshSummary = false if existingData.unreadArticles != newData.unreadArticles { @@ -148,7 +160,8 @@ import Account unreadArticles: unreadArticles, starredArticles: starredArticles, todayArticles: todayArticles, - lastUpdateTime: Date.now) + lastUpdateTime: Date.now, + unreadCountDisplay: AppDefaults.shared.unreadCountDisplay) return latestData } diff --git a/Widget/TimelineProvider.swift b/Widget/TimelineProvider.swift index 97d9e66ff..d499aefb2 100644 --- a/Widget/TimelineProvider.swift +++ b/Widget/TimelineProvider.swift @@ -51,7 +51,7 @@ struct Provider: TimelineProvider { let widgetData = try WidgetDataDecoder.decodeWidgetData() entry = WidgetTimelineEntry(date: date, widgetData: widgetData) } catch { - entry = WidgetTimelineEntry(date: date, widgetData: WidgetData(totalUnreadCount: 0, totalTodayCount: 0, totalTodayUnreadCount: 0, totalStarredCount: 0, unreadArticles: [], starredArticles: [], todayArticles: [], lastUpdateTime: Date.now)) + entry = WidgetTimelineEntry(date: date, widgetData: WidgetData(totalUnreadCount: 0, totalTodayCount: 0, totalTodayUnreadCount: 0, totalStarredCount: 0, unreadArticles: [], starredArticles: [], todayArticles: [], lastUpdateTime: Date.now, unreadCountDisplay: nil)) } let fallback = Calendar.current.date(byAdding: .minute, value: 30, to: Date.now)! diff --git a/Widget/Widget Views/LockScreenSummaryWidget.swift b/Widget/Widget Views/LockScreenSummaryWidget.swift index 70519b713..829a6e886 100644 --- a/Widget/Widget Views/LockScreenSummaryWidget.swift +++ b/Widget/Widget Views/LockScreenSummaryWidget.swift @@ -19,7 +19,7 @@ struct LockScreenSummaryWidgetView: View { unreadImage Text("label.text.unread", comment: "Unread") Spacer() - Text(verbatim: entry.widgetData.totalUnreadCount.formatted()) + Text(verbatim: unreadCountText) .frame(maxWidth: .infinity, alignment: .trailing) } HStack(alignment: .center) { @@ -40,6 +40,15 @@ struct LockScreenSummaryWidgetView: View { .font(.subheadline) } + /// Shows 0 when counts are showing, so the row doesn’t look broken. + var unreadCountText: String { + let widgetData = entry.widgetData + guard widgetData.effectiveUnreadCountDisplay == .count else { + return widgetData.effectiveUnreadCountDisplay.text(for: widgetData.totalUnreadCount) ?? "" + } + return widgetData.totalUnreadCount.formatted() + } + var starredImage: some View { Image(systemName: "star.fill") .resizable() diff --git a/Widget/Widget Views/TodayWidget.swift b/Widget/Widget Views/TodayWidget.swift index 5e6d24559..cecb297bb 100644 --- a/Widget/Widget Views/TodayWidget.swift +++ b/Widget/Widget Views/TodayWidget.swift @@ -35,7 +35,7 @@ struct TodayWidgetView: View { Spacer() .layoutPriority(0) if entry.widgetData.totalTodayCount > 0 { - Text(verbatim: entry.widgetData.totalTodayUnreadCount > 0 ? "\(entry.widgetData.totalTodayCount.formatted()), \(entry.widgetData.totalTodayUnreadCount.formatted()) unread" : entry.widgetData.totalTodayCount.formatted()) + Text(verbatim: countText) .font(.caption2) .bold() .foregroundColor(.secondary) @@ -56,6 +56,15 @@ struct TodayWidgetView: View { } } + /// The unread part is omitted unless unread counts are set to show — “3, • unread” would say nothing. + var countText: String { + let widgetData = entry.widgetData + guard widgetData.totalTodayUnreadCount > 0, widgetData.effectiveUnreadCountDisplay == .count else { + return widgetData.totalTodayCount.formatted() + } + return "\(widgetData.totalTodayCount.formatted()), \(widgetData.totalTodayUnreadCount.formatted()) unread" + } + var todayImage: some View { Image(systemName: "sun.max.fill") .resizable() diff --git a/Widget/Widget Views/UnreadWidget.swift b/Widget/Widget Views/UnreadWidget.swift index 380f93883..8a4941818 100644 --- a/Widget/Widget Views/UnreadWidget.swift +++ b/Widget/Widget Views/UnreadWidget.swift @@ -33,8 +33,8 @@ struct UnreadWidgetView: View { .layoutPriority(1) Spacer() .layoutPriority(0) - if entry.widgetData.totalUnreadCount - maxCount() > 0 { - Text(verbatim: entry.widgetData.totalUnreadCount.formatted()) + if entry.widgetData.totalUnreadCount - maxCount() > 0, let unreadCountText = entry.widgetData.effectiveUnreadCountDisplay.text(for: entry.widgetData.totalUnreadCount) { + Text(verbatim: unreadCountText) .font(.caption2) .bold() .foregroundColor(.secondary)