mirror of
https://github.com/Ranchero-Software/NetNewsWire
synced 2026-11-28 08:17:16 +00:00
Respect unread count display setting in widgets.
This commit is contained in:
1 parent
c4d88f466a
commit
bfd62718e4
8 files changed
+46
-8
No files matched your search
@@ -429,6 +429,7 @@
|
||||
84D35E0E2DB9F32D004AA60E /* PBXFileSystemSynchronizedBuildFileExceptionSet */ = {
|
||||
isa = PBXFileSystemSynchronizedBuildFileExceptionSet;
|
||||
membershipExceptions = (
|
||||
UnreadCountDisplay.swift,
|
||||
Widget/WidgetData.swift,
|
||||
Widget/WidgetDataDecoder.swift,
|
||||
Widget/WidgetDeepLinks.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 {
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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)!
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in new issue
Block a user