From 140f5d7a2bf300ff82bf3188f47e0025e579e303 Mon Sep 17 00:00:00 2001 From: Stuart Breckenridge Date: Tue, 13 Jan 2026 22:22:27 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=9A=91=EF=B8=8F=20Fixes=20widget=20crash?= =?UTF-8?q?=20and=20alignment=20issues?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Shared/Widget/WidgetData.swift | 7 ++++--- Shared/Widget/WidgetDataDecoder.swift | 4 ++-- Shared/Widget/WidgetDataEncoder.swift | 13 ++++++++---- Widget/TimelineProvider.swift | 2 +- .../LockScreenSummaryWidget.swift | 11 ++++++---- Widget/Widget Views/StarredWidget.swift | 20 +++++++------------ Widget/Widget Views/TodayWidget.swift | 20 +++++++------------ Widget/Widget Views/UnreadWidget.swift | 20 +++++++------------ Widget/WidgetBundle.swift | 12 +++++++---- 9 files changed, 52 insertions(+), 57 deletions(-) diff --git a/Shared/Widget/WidgetData.swift b/Shared/Widget/WidgetData.swift index c12a19df9..d2e8ac0e3 100644 --- a/Shared/Widget/WidgetData.swift +++ b/Shared/Widget/WidgetData.swift @@ -9,9 +9,10 @@ import Foundation struct WidgetData: Codable { - let currentUnreadCount: Int - let currentTodayCount: Int - let currentStarredCount: Int + let totalUnreadCount: Int + let totalTodayCount: Int + let totalTodayUnreadCount: Int + let totalStarredCount: Int let unreadArticles: [LatestArticle] let starredArticles: [LatestArticle] let todayArticles: [LatestArticle] diff --git a/Shared/Widget/WidgetDataDecoder.swift b/Shared/Widget/WidgetDataDecoder.swift index a7f57064e..249d1123b 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(currentUnreadCount: 0, currentTodayCount: 0, currentStarredCount: 0, unreadArticles: [], starredArticles: [], todayArticles: [], lastUpdateTime: Date()) + return WidgetData(totalUnreadCount: 0, totalTodayCount: 0, totalTodayUnreadCount: 0, totalStarredCount: 0, unreadArticles: [], starredArticles: [], todayArticles: [], lastUpdateTime: Date()) } } @@ -29,7 +29,7 @@ struct WidgetDataDecoder { let decoded = try JSONDecoder().decode(WidgetData.self, from: data) return decoded } catch { - return WidgetData(currentUnreadCount: 0, currentTodayCount: 0, currentStarredCount: 0, unreadArticles: [], starredArticles: [], todayArticles: [], lastUpdateTime: Date()) + return WidgetData(totalUnreadCount: 0, totalTodayCount: 0, totalTodayUnreadCount: 0, totalStarredCount: 0, unreadArticles: [], starredArticles: [], todayArticles: [], lastUpdateTime: Date()) } } diff --git a/Shared/Widget/WidgetDataEncoder.swift b/Shared/Widget/WidgetDataEncoder.swift index ca320e7f8..c13021dc1 100644 --- a/Shared/Widget/WidgetDataEncoder.swift +++ b/Shared/Widget/WidgetDataEncoder.swift @@ -95,7 +95,7 @@ import Account Self.logger.debug("WidgetDataEncoder: Reloading Unread widget") } - if existingData.todayArticles != newData.todayArticles { + if existingData.todayArticles != newData.todayArticles || existingData.totalTodayUnreadCount != newData.totalTodayUnreadCount { WidgetCenter.shared.reloadTimelines(ofKind: "com.ranchero.NetNewsWire.TodayWidget") shouldRefreshSummary = true Self.logger.debug("WidgetDataEncoder: Reloading Today widget") @@ -126,15 +126,20 @@ import Account let starredArticles = sortedLatestArticles(fetchedStarredArticles) let fetchedTodayArticles = try AccountManager.shared.fetchArticles(.today(fetchLimit)) + let fetchedTodayTotal = try AccountManager.shared.fetchArticles(.today()) + let fetchedTodayTotalCount = fetchedTodayTotal.count + let fetchedTodayUnreadCount = fetchedTodayTotal.filter({ $0.status.read == false }).count let todayArticles = sortedLatestArticles(fetchedTodayArticles) - let latestData = WidgetData(currentUnreadCount: SmartFeedsController.shared.unreadFeed.unreadCount, - currentTodayCount: todayArticles.count, - currentStarredCount: (try? AccountManager.shared.fetchCountForStarredArticles()) ?? 0, + let latestData = WidgetData(totalUnreadCount: SmartFeedsController.shared.unreadFeed.unreadCount, + totalTodayCount: fetchedTodayTotalCount, + totalTodayUnreadCount: fetchedTodayUnreadCount, + totalStarredCount: (try? AccountManager.shared.fetchCountForStarredArticles()) ?? 0, unreadArticles: unreadArticles, starredArticles: starredArticles, todayArticles: todayArticles, lastUpdateTime: Date.now) + return latestData } diff --git a/Widget/TimelineProvider.swift b/Widget/TimelineProvider.swift index 35de0289e..97d9e66ff 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(currentUnreadCount: 0, currentTodayCount: 0, currentStarredCount: 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)) } 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 510177548..70519b713 100644 --- a/Widget/Widget Views/LockScreenSummaryWidget.swift +++ b/Widget/Widget Views/LockScreenSummaryWidget.swift @@ -19,39 +19,42 @@ struct LockScreenSummaryWidgetView: View { unreadImage Text("label.text.unread", comment: "Unread") Spacer() - Text(verbatim: entry.widgetData.currentUnreadCount.formatted()) + Text(verbatim: entry.widgetData.totalUnreadCount.formatted()) .frame(maxWidth: .infinity, alignment: .trailing) } HStack(alignment: .center) { todayImage Text("label.text.today", comment: "Today") Spacer() - Text(verbatim: entry.widgetData.currentTodayCount.formatted()) + Text(verbatim: entry.widgetData.totalTodayCount.formatted()) .frame(maxWidth: .infinity, alignment: .trailing) } HStack(alignment: .center) { starredImage Text("label.text.starred", comment: "Starred") Spacer() - Text(verbatim: entry.widgetData.currentStarredCount.formatted()) + Text(verbatim: entry.widgetData.totalStarredCount.formatted()) .frame(maxWidth: .infinity, alignment: .trailing) } } - .font(.caption) + .font(.subheadline) } var starredImage: some View { Image(systemName: "star.fill") + .resizable() .frame(width: 14, height: 14) } var unreadImage: some View { Image(systemName: "largecircle.fill.circle") + .resizable() .frame(width: 14, height: 14) } var todayImage: some View { Image(systemName: "sun.max.fill") + .resizable() .frame(width: 14, height: 14) } diff --git a/Widget/Widget Views/StarredWidget.swift b/Widget/Widget Views/StarredWidget.swift index 22b476fab..d0570da86 100644 --- a/Widget/Widget Views/StarredWidget.swift +++ b/Widget/Widget Views/StarredWidget.swift @@ -16,7 +16,7 @@ struct StarredWidgetView: View { var entry: Provider.Entry var body: some View { - if entry.widgetData.currentStarredCount == 0 { + if entry.widgetData.starredArticles.count == 0 { inboxZero .widgetURL(WidgetDeepLink.starred.url) } else { @@ -33,8 +33,8 @@ struct StarredWidgetView: View { .layoutPriority(1) Spacer() .layoutPriority(0) - if entry.widgetData.currentStarredCount - maxCount() > 0 { - Text(verbatim: entry.widgetData.currentStarredCount.formatted()) + if entry.widgetData.totalStarredCount - maxCount() > 0 { + Text(verbatim: entry.widgetData.totalStarredCount.formatted()) .font(.caption2) .bold() .foregroundColor(.secondary) @@ -47,14 +47,8 @@ struct StarredWidgetView: View { Divider() if entry.widgetData.starredArticles.count > 0 { ForEach(0..= 7 ? (7 - reduceAccessibilityCount) : entry.widgetData.currentStarredCount + return entry.widgetData.totalStarredCount >= 7 ? (7 - reduceAccessibilityCount) : entry.widgetData.totalStarredCount } - return entry.widgetData.currentStarredCount >= 3 ? (3 - reduceAccessibilityCount) : entry.widgetData.currentStarredCount + return entry.widgetData.totalStarredCount >= 3 ? (3 - reduceAccessibilityCount) : entry.widgetData.totalStarredCount } var inboxZero: some View { diff --git a/Widget/Widget Views/TodayWidget.swift b/Widget/Widget Views/TodayWidget.swift index 0e1a64275..942884618 100644 --- a/Widget/Widget Views/TodayWidget.swift +++ b/Widget/Widget Views/TodayWidget.swift @@ -17,7 +17,7 @@ struct TodayWidgetView: View { var entry: Provider.Entry var body: some View { - if entry.widgetData.currentTodayCount == 0 { + if entry.widgetData.totalTodayCount == 0 { inboxZero .widgetURL(WidgetDeepLink.today.url) } else { @@ -34,8 +34,8 @@ struct TodayWidgetView: View { .layoutPriority(1) Spacer() .layoutPriority(0) - if entry.widgetData.currentTodayCount > 0 { - Text(verbatim: entry.widgetData.currentTodayCount.formatted()) + if entry.widgetData.totalTodayCount > 0 { + Text(verbatim: entry.widgetData.totalTodayUnreadCount > 0 ? "\(entry.widgetData.totalTodayCount.formatted()), \(entry.widgetData.totalTodayUnreadCount.formatted()) unread" : entry.widgetData.totalTodayCount.formatted()) .font(.caption2) .bold() .foregroundColor(.secondary) @@ -48,14 +48,8 @@ struct TodayWidgetView: View { Divider() if entry.widgetData.todayArticles.count > 0 { ForEach(0..= 7 ? (7 - reduceAccessibilityCount) : entry.widgetData.currentTodayCount + return entry.widgetData.totalTodayCount >= 7 ? (7 - reduceAccessibilityCount) : entry.widgetData.totalTodayCount } - return entry.widgetData.todayArticles.count >= 3 ? (3 - reduceAccessibilityCount) : entry.widgetData.currentTodayCount + return entry.widgetData.totalTodayCount >= 3 ? (3 - reduceAccessibilityCount) : entry.widgetData.totalTodayCount } var inboxZero: some View { diff --git a/Widget/Widget Views/UnreadWidget.swift b/Widget/Widget Views/UnreadWidget.swift index fcf25169f..18853017d 100644 --- a/Widget/Widget Views/UnreadWidget.swift +++ b/Widget/Widget Views/UnreadWidget.swift @@ -16,7 +16,7 @@ struct UnreadWidgetView: View { var entry: Provider.Entry var body: some View { - if entry.widgetData.currentUnreadCount == 0 { + if entry.widgetData.totalUnreadCount == 0 { inboxZero .widgetURL(WidgetDeepLink.unread.url) } else { @@ -33,8 +33,8 @@ struct UnreadWidgetView: View { .layoutPriority(1) Spacer() .layoutPriority(0) - if entry.widgetData.currentUnreadCount - maxCount() > 0 { - Text(verbatim: entry.widgetData.currentUnreadCount.formatted()) + if entry.widgetData.totalUnreadCount - maxCount() > 0 { + Text(verbatim: entry.widgetData.totalUnreadCount.formatted()) .font(.caption2) .bold() .foregroundColor(.secondary) @@ -47,14 +47,8 @@ struct UnreadWidgetView: View { Divider() if entry.widgetData.unreadArticles.count > 0 { ForEach(0..= 7 ? (7 - reduceAccessibilityCount) : entry.widgetData.currentUnreadCount + return entry.widgetData.totalUnreadCount >= 7 ? (7 - reduceAccessibilityCount) : entry.widgetData.totalUnreadCount } - return entry.widgetData.currentUnreadCount >= 3 ? (3 - reduceAccessibilityCount) : entry.widgetData.currentUnreadCount + return entry.widgetData.totalUnreadCount >= 3 ? (3 - reduceAccessibilityCount) : entry.widgetData.totalUnreadCount } var inboxZero: some View { diff --git a/Widget/WidgetBundle.swift b/Widget/WidgetBundle.swift index 92342b636..20628dc51 100644 --- a/Widget/WidgetBundle.swift +++ b/Widget/WidgetBundle.swift @@ -18,10 +18,11 @@ struct UnreadWidget: Widget { return StaticConfiguration(kind: kind, provider: Provider(), content: { entry in UnreadWidgetView(entry: entry) - .frame(maxWidth: .infinity, maxHeight: .infinity) + .frame(maxHeight: .infinity, alignment: .top) .containerBackground(for: .widget) { Color.clear } + .clipped() }) .configurationDisplayName(Text("label.text.unread", comment: "Unread")) .description(Text("label.text.unread-widget-description", comment: "A description of the Unread widget.")) @@ -36,10 +37,11 @@ struct TodayWidget: Widget { return StaticConfiguration(kind: kind, provider: Provider(), content: { entry in TodayWidgetView(entry: entry) - .frame(maxWidth: .infinity, maxHeight: .infinity) + .frame(maxHeight: .infinity, alignment: .top) .containerBackground(for: .widget) { Color.clear } + .clipped() }) .configurationDisplayName(Text("label.text.today", comment: "Today")) .description(Text("label.text.today-widget-description", comment: "A description of the Today widget.")) @@ -54,10 +56,11 @@ struct StarredWidget: Widget { return StaticConfiguration(kind: kind, provider: Provider(), content: { entry in StarredWidgetView(entry: entry) - .frame(maxWidth: .infinity, maxHeight: .infinity) + .frame(maxHeight: .infinity, alignment: .top) .containerBackground(for: .widget) { Color.clear } + .clipped() }) .configurationDisplayName(Text("label.text.starred", comment: "Starred")) .description(Text("label.text.starred-widget-description", comment: "A description of the Starred widget.")) @@ -72,10 +75,11 @@ struct LockScreenSummaryWidget: Widget { return StaticConfiguration(kind: kind, provider: Provider(), content: { entry in LockScreenSummaryWidgetView(entry: entry) - .frame(maxWidth: .infinity, maxHeight: .infinity) + .frame(maxHeight: .infinity, alignment: .top) .containerBackground(for: .widget) { Color.clear } + .clipped() }) .configurationDisplayName(Text("label.text.lock-screen-summary", comment: "Summary")) .description(Text("label.text.lock-screen-summary-description", comment: "A description of the Lock Screen Summary widget."))