From f1e32c5d2ee74b1c07dce24897eeae493e0f69b4 Mon Sep 17 00:00:00 2001 From: Brent Simmons Date: Mon, 3 Nov 2025 21:32:24 -0800 Subject: [PATCH] Make ArticleStatusSyncTimer a singleton. --- Mac/AppDelegate.swift | 18 ++++------ Mac/MainWindow/MainWindowController.swift | 2 +- Shared/Timer/ArticleStatusSyncTimer.swift | 42 ++++++++++++++--------- iOS/AppDelegate.swift | 19 ++++------ 4 files changed, 41 insertions(+), 40 deletions(-) diff --git a/Mac/AppDelegate.swift b/Mac/AppDelegate.swift index f189526e3..a2dfdfbc1 100644 --- a/Mac/AppDelegate.swift +++ b/Mac/AppDelegate.swift @@ -29,6 +29,8 @@ import Sparkle var appDelegate: AppDelegate! +let appName = Bundle.main.infoDictionary!["CFBundleName"]! as! String + @NSApplicationMain final class AppDelegate: NSObject, NSApplicationDelegate, NSUserInterfaceValidations, UNUserNotificationCenterDelegate, UnreadCountProvider, SPUStandardUserDriverDelegate, SPUUpdaterDelegate { @@ -38,10 +40,7 @@ final class AppDelegate: NSObject, NSApplicationDelegate, NSUserInterfaceValidat static let mainWindow = "mainWindow" } - var appName: String! - var refreshTimer: AccountRefreshTimer? - var syncTimer: ArticleStatusSyncTimer? var lastRefreshInterval = AppDefaults.shared.refreshInterval var shuttingDown = false { @@ -49,8 +48,7 @@ final class AppDelegate: NSObject, NSApplicationDelegate, NSUserInterfaceValidat if shuttingDown { refreshTimer?.shuttingDown = shuttingDown refreshTimer?.invalidate() - syncTimer?.shuttingDown = shuttingDown - syncTimer?.invalidate() + ArticleStatusSyncTimer.shared.stop() } } } @@ -155,8 +153,6 @@ final class AppDelegate: NSObject, NSApplicationDelegate, NSUserInterfaceValidat let imagesFolder = (cacheFolder as NSString).appendingPathComponent("Images") let imagesFolderURL = URL(fileURLWithPath: imagesFolder) try! FileManager.default.createDirectory(at: imagesFolderURL, withIntermediateDirectories: true, attributes: nil) - - appName = (Bundle.main.infoDictionary!["CFBundleExecutable"]! as! String) } func applicationDidFinishLaunching(_ note: Notification) { @@ -218,8 +214,8 @@ final class AppDelegate: NSObject, NSApplicationDelegate, NSUserInterfaceValidat ExtensionFeedAddRequestFile.shared.start() refreshTimer = AccountRefreshTimer() - syncTimer = ArticleStatusSyncTimer() - + ArticleStatusSyncTimer.shared.start() + UNUserNotificationCenter.current().requestAuthorization(options:[.badge]) { (granted, error) in } UNUserNotificationCenter.current().getNotificationSettings { (settings) in @@ -235,7 +231,7 @@ final class AppDelegate: NSObject, NSApplicationDelegate, NSUserInterfaceValidat #if DEBUG refreshTimer!.update() - syncTimer!.update() + ArticleStatusSyncTimer.shared.update() #else if AppDefaults.shared.suppressSyncOnLaunch { refreshTimer!.update() @@ -740,7 +736,7 @@ internal extension AppDelegate { // It’s possible there’s a refresh timer set to go off in the past. // In that case, refresh now and update the timer. refreshTimer?.fireOldTimer() - syncTimer?.fireOldTimer() + ArticleStatusSyncTimer.shared.fireOldTimer() } func objectsForInspector() -> [Any]? { diff --git a/Mac/MainWindow/MainWindowController.swift b/Mac/MainWindow/MainWindowController.swift index 0bc9eb9e2..1a5c99bd9 100644 --- a/Mac/MainWindow/MainWindowController.swift +++ b/Mac/MainWindow/MainWindowController.swift @@ -1276,7 +1276,7 @@ private extension MainWindowController { } guard let selectedObjects = selectedObjectsInSidebar(), selectedObjects.count > 0 else { - window?.title = appDelegate.appName! + window?.title = appName setSubtitle(appDelegate.unreadCount) return } diff --git a/Shared/Timer/ArticleStatusSyncTimer.swift b/Shared/Timer/ArticleStatusSyncTimer.swift index bc68dbc42..e3de07200 100644 --- a/Shared/Timer/ArticleStatusSyncTimer.swift +++ b/Shared/Timer/ArticleStatusSyncTimer.swift @@ -10,15 +10,16 @@ import Foundation import Account final class ArticleStatusSyncTimer { - + static let shared = ArticleStatusSyncTimer() + private static let intervalSeconds = Double(120) - + var shuttingDown = false - + private var internalTimer: Timer? private var lastTimedRefresh: Date? private let launchTime = Date() - + func fireOldTimer() { if let timer = internalTimer { if timer.fireDate < Date() { @@ -26,7 +27,16 @@ final class ArticleStatusSyncTimer { } } } - + + func start() { + shuttingDown = false + } + + func stop() { + shuttingDown = true + invalidate() + } + func invalidate() { guard let timer = internalTimer else { return @@ -36,13 +46,13 @@ final class ArticleStatusSyncTimer { } internalTimer = nil } - + func update() { - + guard !shuttingDown else { return } - + let lastRefreshDate = lastTimedRefresh ?? launchTime var nextRefreshTime = lastRefreshDate.addingTimeInterval(ArticleStatusSyncTimer.intervalSeconds) if nextRefreshTime < Date() { @@ -51,25 +61,25 @@ final class ArticleStatusSyncTimer { if let currentNextFireDate = internalTimer?.fireDate, currentNextFireDate == nextRefreshTime { return } - + invalidate() let timer = Timer(fireAt: nextRefreshTime, interval: 0, target: self, selector: #selector(timedRefresh(_:)), userInfo: nil, repeats: false) RunLoop.main.add(timer, forMode: .common) internalTimer = timer - + } - + @objc func timedRefresh(_ sender: Timer?) { - + guard !shuttingDown else { return } - + lastTimedRefresh = Date() update() - + AccountManager.shared.syncArticleStatusAll() - + } - + } diff --git a/iOS/AppDelegate.swift b/iOS/AppDelegate.swift index 78950d803..e312a460d 100644 --- a/iOS/AppDelegate.swift +++ b/iOS/AppDelegate.swift @@ -21,18 +21,15 @@ var appDelegate: AppDelegate! @UIApplicationMain final class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate, UnreadCountProvider { - private var bgTaskDispatchQueue = DispatchQueue.init(label: "BGTaskScheduler") - + private let backgroundTaskDispatchQueue = DispatchQueue.init(label: "BGTaskScheduler") + private var waitBackgroundUpdateTask = UIBackgroundTaskIdentifier.invalid private var syncBackgroundUpdateTask = UIBackgroundTaskIdentifier.invalid - var syncTimer: ArticleStatusSyncTimer? - var shuttingDown = false { didSet { if shuttingDown { - syncTimer?.shuttingDown = shuttingDown - syncTimer?.invalidate() + ArticleStatusSyncTimer.shared.stop() } } } @@ -111,10 +108,8 @@ final class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationC widgetDataEncoder = WidgetDataEncoder() - syncTimer = ArticleStatusSyncTimer() - #if DEBUG - syncTimer!.update() + ArticleStatusSyncTimer.shared.update() #endif return true @@ -176,7 +171,7 @@ final class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationC func prepareAccountsForBackground() { updateBadge() ExtensionFeedAddRequestFile.shared.suspend() - syncTimer?.invalidate() + ArticleStatusSyncTimer.shared.invalidate() scheduleBackgroundFeedRefresh() syncArticleStatus() widgetDataEncoder.encode() @@ -187,7 +182,7 @@ final class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationC func prepareAccountsForForeground() { updateBadge() ExtensionFeedAddRequestFile.shared.resume() - syncTimer?.update() + ArticleStatusSyncTimer.shared.update() if let lastRefresh = AppDefaults.shared.lastRefresh { if Date() > lastRefresh.addingTimeInterval(15 * 60) { @@ -365,7 +360,7 @@ private extension AppDelegate { // We send this to a dedicated serial queue because as of 11/05/19 on iOS 13.2 the call to the // task scheduler can hang indefinitely. - bgTaskDispatchQueue.async { + backgroundTaskDispatchQueue.async { do { try BGTaskScheduler.shared.submit(request) } catch {