Add name parameter to PostponingBlock. Add notification appUnreadCountDidChange for when unread count changes for entire app.

This commit is contained in:
Brent Simmons 2024-06-09 22:27:17 -07:00
parent 3e53dfbfc6
commit 5d066e5d5c
7 changed files with 15 additions and 12 deletions

View File

@ -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()
}
}()

View File

@ -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.

View File

@ -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()
}
}

View File

@ -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")

View File

@ -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 {

View File

@ -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
}
}

View File

@ -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)
}