From 5d066e5d5c1b8e8aa43c5014ec0e678cb800eb81 Mon Sep 17 00:00:00 2001 From: Brent Simmons Date: Sun, 9 Jun 2024 22:27:17 -0700 Subject: [PATCH] Add name parameter to PostponingBlock. Add notification appUnreadCountDidChange for when unread count changes for entire app. --- Core/Sources/Core/DataFile.swift | 2 +- Core/Sources/Core/PostponingBlock.swift | 6 ++++-- Mac/AppDelegate.swift | 1 + Shared/AppNotifications.swift | 1 + Shared/SmartFeeds/SmartFeed.swift | 11 +++++------ Shared/SmartFeeds/UnreadFeed.swift | 5 ++--- iOS/AppDelegate.swift | 1 + 7 files changed, 15 insertions(+), 12 deletions(-) diff --git a/Core/Sources/Core/DataFile.swift b/Core/Sources/Core/DataFile.swift index fa074903b..8913b3869 100644 --- a/Core/Sources/Core/DataFile.swift +++ b/Core/Sources/Core/DataFile.swift @@ -32,7 +32,7 @@ public protocol DataFileDelegate: AnyObject { private let fileURL: URL private lazy var postponingBlock: PostponingBlock = { - PostponingBlock(delayInterval: 1.0) { [weak self] in + PostponingBlock(delayInterval: 1.0, name: "DataFile \(fileURL.absoluteString)") { [weak self] in self?.saveToDiskIfNeeded() } }() diff --git a/Core/Sources/Core/PostponingBlock.swift b/Core/Sources/Core/PostponingBlock.swift index 01d919c9d..35b881591 100644 --- a/Core/Sources/Core/PostponingBlock.swift +++ b/Core/Sources/Core/PostponingBlock.swift @@ -12,12 +12,14 @@ import Foundation private let block: () -> Void private let delayInterval: TimeInterval + private let name: String // For debugging private var timer: Timer? - public init(delayInterval: TimeInterval, block: @escaping () -> Void) { + public init(delayInterval: TimeInterval, name: String, block: @escaping () -> Void) { - self.block = block self.delayInterval = delayInterval + self.name = name + self.block = block } /// Run the block in `delayInterval` seconds, canceling any run about to happen before then. diff --git a/Mac/AppDelegate.swift b/Mac/AppDelegate.swift index f3342d537..ef846e6ed 100644 --- a/Mac/AppDelegate.swift +++ b/Mac/AppDelegate.swift @@ -74,6 +74,7 @@ import Sparkle didSet { if unreadCount != oldValue { CoalescingQueue.standard.add(self, #selector(updateDockBadge)) + NotificationCenter.default.post(name: .appUnreadCountDidChange, object: self, userInfo: nil) postUnreadCountDidChangeNotification() } } diff --git a/Shared/AppNotifications.swift b/Shared/AppNotifications.swift index 7207f2430..7ed361f89 100644 --- a/Shared/AppNotifications.swift +++ b/Shared/AppNotifications.swift @@ -11,6 +11,7 @@ import Articles extension Notification.Name { + static let appUnreadCountDidChange = Notification.Name("TimelineSelectionDidChangeNotification") static let InspectableObjectsDidChange = Notification.Name("TimelineSelectionDidChangeNotification") static let UserDidAddFeed = Notification.Name("UserDidAddFeedNotification") static let LaunchedFromExternalAction = Notification.Name("LaunchedFromExternalAction") diff --git a/Shared/SmartFeeds/SmartFeed.swift b/Shared/SmartFeeds/SmartFeed.swift index 219f5c866..3c0785352 100644 --- a/Shared/SmartFeeds/SmartFeed.swift +++ b/Shared/SmartFeeds/SmartFeed.swift @@ -49,7 +49,7 @@ import Images #endif private lazy var postponingBlock: PostponingBlock = { - PostponingBlock(delayInterval: 1.0) { + PostponingBlock(delayInterval: 1.0, name: "SmartFeed") { Task { try? await self.fetchUnreadCounts() } @@ -62,14 +62,13 @@ import Images init(delegate: SmartFeedDelegate) { self.delegate = delegate - NotificationCenter.default.addObserver(self, selector: #selector(unreadCountDidChange(_:)), name: .UnreadCountDidChange, object: nil) + NotificationCenter.default.addObserver(self, selector: #selector(appUnreadCountDidChange(_:)), name: .appUnreadCountDidChange, object: nil) queueFetchUnreadCounts() // Fetch unread count at startup } - @objc func unreadCountDidChange(_ note: Notification) { - if note.object is AppDelegate { - queueFetchUnreadCounts() - } + @objc func appUnreadCountDidChange(_ note: Notification) { + + queueFetchUnreadCounts() } func fetchUnreadCounts() async throws { diff --git a/Shared/SmartFeeds/UnreadFeed.swift b/Shared/SmartFeeds/UnreadFeed.swift index 2d690c1e0..383f92750 100644 --- a/Shared/SmartFeeds/UnreadFeed.swift +++ b/Shared/SmartFeeds/UnreadFeed.swift @@ -54,12 +54,11 @@ final class UnreadFeed: PseudoFeed { @MainActor init() { self.unreadCount = appDelegate.unreadCount - NotificationCenter.default.addObserver(self, selector: #selector(unreadCountDidChange(_:)), name: .UnreadCountDidChange, object: appDelegate) + NotificationCenter.default.addObserver(self, selector: #selector(appUnreadCountDidChange(_:)), name: .appUnreadCountDidChange, object: nil) } - @objc @MainActor func unreadCountDidChange(_ note: Notification) { + @objc @MainActor func appUnreadCountDidChange(_ note: Notification) { - assert(note.object is AppDelegate) unreadCount = appDelegate.unreadCount } } diff --git a/iOS/AppDelegate.swift b/iOS/AppDelegate.swift index 5efc411e7..5aa31a419 100644 --- a/iOS/AppDelegate.swift +++ b/iOS/AppDelegate.swift @@ -50,6 +50,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterD var unreadCount = 0 { didSet { if unreadCount != oldValue { + NotificationCenter.default.post(name: .appUnreadCountDidChange, object: self, userInfo: nil) postUnreadCountDidChangeNotification() UNUserNotificationCenter.current().setBadgeCount(unreadCount) }