Add name parameter to PostponingBlock. Add notification appUnreadCountDidChange for when unread count changes for entire app.
This commit is contained in:
parent
3e53dfbfc6
commit
5d066e5d5c
@ -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()
|
||||
}
|
||||
}()
|
||||
|
@ -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.
|
||||
|
@ -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()
|
||||
}
|
||||
}
|
||||
|
@ -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")
|
||||
|
@ -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 {
|
||||
|
@ -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
|
||||
}
|
||||
}
|
||||
|
@ -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)
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user