From 33a21ea482ef2820d76ba2fe3dd9e1b65fd6a9d1 Mon Sep 17 00:00:00 2001 From: Brent Simmons Date: Wed, 5 Nov 2025 11:37:16 -0800 Subject: [PATCH] Make AccountManager a singleton. --- Mac/AppDelegate.swift | 2 +- .../Sources/Account/AccountManager.swift | 18 ++++++++++++++---- .../Sources/Account/DataExtensions.swift | 6 +----- iOS/AppDelegate.swift | 4 ++-- 4 files changed, 18 insertions(+), 12 deletions(-) diff --git a/Mac/AppDelegate.swift b/Mac/AppDelegate.swift index 635297a4c..a07c52c32 100644 --- a/Mac/AppDelegate.swift +++ b/Mac/AppDelegate.swift @@ -100,7 +100,7 @@ final class AppDelegate: NSObject, NSApplicationDelegate, NSUserInterfaceValidat crashReporter.enable() SecretsManager.provider = Secrets() - AccountManager.shared = AccountManager(accountsFolder: Platform.dataSubfolder(forApplication: nil, folderName: "Accounts")!) + AccountManager.shared.start() ArticleThemesManager.shared = ArticleThemesManager(folderPath: Platform.dataSubfolder(forApplication: nil, folderName: "Themes")!) NotificationCenter.default.addObserver(self, selector: #selector(unreadCountDidChange(_:)), name: .UnreadCountDidChange, object: nil) diff --git a/Modules/Account/Sources/Account/AccountManager.swift b/Modules/Account/Sources/Account/AccountManager.swift index 2da067422..4003cf61c 100644 --- a/Modules/Account/Sources/Account/AccountManager.swift +++ b/Modules/Account/Sources/Account/AccountManager.swift @@ -16,9 +16,9 @@ import RSDatabase // Main thread only. public final class AccountManager: UnreadCountProvider { + public static var shared = AccountManager() - public static var shared: AccountManager! - public static let netNewsWireNewsURL = "https://netnewswire.blog/feed.xml" + public static let netNewsWireNewsURL = "https://netnewswire.blog/feed.xml" private static let jsonNetNewsWireNewsURL = "https://netnewswire.blog/feed.json" public let defaultAccount: Account @@ -101,8 +101,10 @@ public final class AccountManager: UnreadCountProvider { public let combinedRefreshProgress = CombinedRefreshProgress() - public init(accountsFolder: String) { - self.accountsFolder = accountsFolder + private var isActive = false + + public init() { + self.accountsFolder = AppConfig.dataSubfolder(named: "Accounts").path // The local "On My Mac" account must always exist, even if it's empty. let localAccountFolder = (accountsFolder as NSString).appendingPathComponent("OnMyMac") @@ -118,6 +120,14 @@ public final class AccountManager: UnreadCountProvider { accountsDictionary[defaultAccount.accountID] = defaultAccount readAccountsFromDisk() + } + + public func start() { + guard !isActive else { + assertionFailure("start called when isActive is already true") + return + } + isActive = true NotificationCenter.default.addObserver(self, selector: #selector(unreadCountDidInitialize(_:)), name: .UnreadCountDidInitialize, object: nil) NotificationCenter.default.addObserver(self, selector: #selector(unreadCountDidChange(_:)), name: .UnreadCountDidChange, object: nil) diff --git a/Modules/Account/Sources/Account/DataExtensions.swift b/Modules/Account/Sources/Account/DataExtensions.swift index 15c445022..3c7dbbd5e 100644 --- a/Modules/Account/Sources/Account/DataExtensions.swift +++ b/Modules/Account/Sources/Account/DataExtensions.swift @@ -50,11 +50,7 @@ extension Feed { public extension Article { var account: Account? { - // The force unwrapped shared instance was crashing Account.framework unit tests. - guard let manager = AccountManager.shared else { - return nil - } - return manager.existingAccount(with: accountID) + return AccountManager.shared.existingAccount(with: accountID) } var feed: Feed? { diff --git a/iOS/AppDelegate.swift b/iOS/AppDelegate.swift index 898aa4867..caa8c1515 100644 --- a/iOS/AppDelegate.swift +++ b/iOS/AppDelegate.swift @@ -57,8 +57,8 @@ final class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationC SecretsManager.provider = Secrets() let documentFolder = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first! let documentAccountsFolder = documentFolder.appendingPathComponent("Accounts").absoluteString - let documentAccountsFolderPath = String(documentAccountsFolder.suffix(from: documentAccountsFolder.index(documentAccountsFolder.startIndex, offsetBy: 7))) - AccountManager.shared = AccountManager(accountsFolder: documentAccountsFolderPath) + + AccountManager.shared.start() let documentThemesFolder = documentFolder.appendingPathComponent("Themes").absoluteString let documentThemesFolderPath = String(documentThemesFolder.suffix(from: documentAccountsFolder.index(documentThemesFolder.startIndex, offsetBy: 7)))