Make ArticleStatusSyncTimer a singleton.

This commit is contained in:
Brent Simmons committed 2025-11-03 21:32:24 -08:00
1 parent 85d533ec6d
commit f1e32c5d2e
4 files changed
+41 -40

No files matched your search

+7 -11
View File
@@ -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]? {
+1 -1
View File
@@ -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
}
+26 -16
View File
@@ -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()
}
}
+7 -12
View File
@@ -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 {