diff --git a/Mac/AppDelegate.swift b/Mac/AppDelegate.swift index 7fe37c89f..220255be4 100644 --- a/Mac/AppDelegate.swift +++ b/Mac/AppDelegate.swift @@ -34,13 +34,12 @@ final class AppDelegate: NSObject, NSApplicationDelegate, NSUserInterfaceValidat var syncTimer: ArticleStatusSyncTimer? var lastRefreshInterval = AppDefaults.refreshInterval - var shuttingDown = false { + private var shuttingDown = false { didSet { if shuttingDown { refreshTimer?.shuttingDown = shuttingDown refreshTimer?.invalidate() - syncTimer?.shuttingDown = shuttingDown - syncTimer?.invalidate() + ArticleStatusSyncTimer.shared.stop() } } } @@ -201,7 +200,7 @@ final class AppDelegate: NSObject, NSApplicationDelegate, NSUserInterfaceValidat extensionFeedAddRequestFile = ExtensionFeedAddRequestFile() refreshTimer = AccountRefreshTimer() - syncTimer = ArticleStatusSyncTimer() + _ = ArticleStatusSyncTimer.shared UNUserNotificationCenter.current().requestAuthorization(options: [.badge]) { (_, _) in } @@ -218,15 +217,15 @@ final class AppDelegate: NSObject, NSApplicationDelegate, NSUserInterfaceValidat #if DEBUG refreshTimer!.update() - syncTimer!.update() + ArticleStatusSyncTimer.shared.update() #else if AppDefaults.suppressSyncOnLaunch { refreshTimer!.update() - syncTimer!.update() + ArticleStatusSyncTimer.shared.update() } else { DispatchQueue.main.async { self.refreshTimer!.timedRefresh(nil) - self.syncTimer!.timedRefresh(nil) + ArticleStatusSyncTimer.shared.timedRefresh(nil) } } #endif @@ -704,7 +703,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/Shared/Timer/ArticleStatusSyncTimer.swift b/Shared/Timer/ArticleStatusSyncTimer.swift index 3802a9972..d8607cc7d 100644 --- a/Shared/Timer/ArticleStatusSyncTimer.swift +++ b/Shared/Timer/ArticleStatusSyncTimer.swift @@ -11,6 +11,8 @@ import Account final class ArticleStatusSyncTimer { + static let shared = ArticleStatusSyncTimer() + private static let intervalSeconds = Double(120) var shuttingDown = false @@ -27,6 +29,11 @@ final class ArticleStatusSyncTimer { } } + func stop() { + shuttingDown = true + invalidate() + } + func invalidate() { guard let timer = internalTimer else { return diff --git a/iOS/AppDelegate.swift b/iOS/AppDelegate.swift index 18006c5d5..8ed77d222 100644 --- a/iOS/AppDelegate.swift +++ b/iOS/AppDelegate.swift @@ -23,17 +23,6 @@ final class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationC private var coordinator: SceneCoordinator? - var syncTimer: ArticleStatusSyncTimer? - - private var shuttingDown = false { - didSet { - if shuttingDown { - syncTimer?.shuttingDown = shuttingDown - syncTimer?.invalidate() - } - } - } - private let logger = Logger(subsystem: Bundle.main.bundleIdentifier!, category: "Application") var userNotificationManager: UserNotificationManager! @@ -106,10 +95,10 @@ final class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationC widgetDataEncoder = WidgetDataEncoder() - syncTimer = ArticleStatusSyncTimer() + _ = ArticleStatusSyncTimer.shared #if DEBUG - syncTimer!.update() + ArticleStatusSyncTimer.shared.update() #endif // Create window. @@ -156,7 +145,7 @@ final class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationC } func applicationWillTerminate(_ application: UIApplication) { - shuttingDown = true + ArticleStatusSyncTimer.shared.stop() } func applicationDidEnterBackground(_ application: UIApplication) { @@ -199,7 +188,7 @@ final class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationC func prepareAccountsForBackground() { extensionFeedAddRequestFile.suspend() - syncTimer?.invalidate() + ArticleStatusSyncTimer.shared.invalidate() scheduleBackgroundFeedRefresh() syncArticleStatus() widgetDataEncoder.encode() @@ -208,7 +197,7 @@ final class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationC func prepareAccountsForForeground() { extensionFeedAddRequestFile.resume() - syncTimer?.update() + ArticleStatusSyncTimer.shared.update() if let lastRefresh = AppDefaults.lastRefresh { if Date() > lastRefresh.addingTimeInterval(15 * 60) {