From e9fc4c09ed2d14f53678abe63b268d4ca246ffac Mon Sep 17 00:00:00 2001 From: Brent Simmons Date: Sat, 1 Feb 2025 19:37:59 -0800 Subject: [PATCH] Make AccountManager and ArticleThemesManager self-initing. --- Mac/AppDelegate.swift | 4 ++-- Modules/Account/Sources/Account/AccountManager.swift | 10 ++++++---- Modules/Account/Sources/Account/DataExtensions.swift | 6 +----- Shared/ArticleStyles/ArticleThemesManager.swift | 7 ++++--- iOS/AppDelegate.swift | 12 +++--------- 5 files changed, 16 insertions(+), 23 deletions(-) diff --git a/Mac/AppDelegate.swift b/Mac/AppDelegate.swift index 870782fc5..7fe37c89f 100644 --- a/Mac/AppDelegate.swift +++ b/Mac/AppDelegate.swift @@ -101,8 +101,8 @@ final class AppDelegate: NSObject, NSApplicationDelegate, NSUserInterfaceValidat crashReporter = PLCrashReporter(configuration: crashReporterConfig) crashReporter.enable() - AccountManager.shared = AccountManager(accountsFolder: Platform.dataSubfolder(forApplication: nil, folderName: "Accounts")!) - ArticleThemesManager.shared = ArticleThemesManager(folderPath: Platform.dataSubfolder(forApplication: nil, folderName: "Themes")!) + _ = AccountManager.shared + _ = ArticleThemesManager.shared NotificationCenter.default.addObserver(self, selector: #selector(unreadCountDidChange(_:)), name: .UnreadCountDidChange, object: nil) NotificationCenter.default.addObserver(self, selector: #selector(inspectableObjectsDidChange(_:)), name: .InspectableObjectsDidChange, object: nil) diff --git a/Modules/Account/Sources/Account/AccountManager.swift b/Modules/Account/Sources/Account/AccountManager.swift index ce197ddbd..80274955f 100644 --- a/Modules/Account/Sources/Account/AccountManager.swift +++ b/Modules/Account/Sources/Account/AccountManager.swift @@ -18,7 +18,7 @@ import RSDatabase public final class AccountManager: UnreadCountProvider { - public static var shared: AccountManager! + public static let shared = AccountManager() public static let netNewsWireNewsURL = "https://netnewswire.blog/feed.xml" private static let jsonNetNewsWireNewsURL = "https://netnewswire.blog/feed.json" @@ -93,7 +93,10 @@ public final class AccountManager: UnreadCountProvider { public let combinedRefreshProgress = CombinedRefreshProgress() private let logger = Logger(subsystem: Bundle.main.bundleIdentifier!, category: "AccountManager") - public init(accountsFolder: String) { + public init() { + + let accountsFolderURL = AppConfig.dataSubfolder(named: "Accounts") + let accountsFolder = accountsFolderURL.path self.accountsFolder = accountsFolder // The local "On My Mac" account must always exist, even if it's empty. @@ -101,8 +104,7 @@ public final class AccountManager: UnreadCountProvider { do { try FileManager.default.createDirectory(atPath: localAccountFolder, withIntermediateDirectories: true, attributes: nil) } catch { - assertionFailure("Could not create folder for OnMyMac account.") - abort() + fatalError("Could not create folder for OnMyMac account.") } defaultAccount = Account(dataFolder: localAccountFolder, type: .onMyMac, accountID: defaultAccountIdentifier) diff --git a/Modules/Account/Sources/Account/DataExtensions.swift b/Modules/Account/Sources/Account/DataExtensions.swift index 3fe1a2886..f37d691e9 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) + AccountManager.shared.existingAccount(with: accountID) } var feed: Feed? { diff --git a/Shared/ArticleStyles/ArticleThemesManager.swift b/Shared/ArticleStyles/ArticleThemesManager.swift index b75c6c4a1..bf0c869d9 100644 --- a/Shared/ArticleStyles/ArticleThemesManager.swift +++ b/Shared/ArticleStyles/ArticleThemesManager.swift @@ -16,7 +16,7 @@ public extension Notification.Name { final class ArticleThemesManager: NSObject, NSFilePresenter { - static var shared: ArticleThemesManager! + static let shared = ArticleThemesManager() public let folderPath: String lazy var presentedItemOperationQueue = OperationQueue.main @@ -49,8 +49,9 @@ final class ArticleThemesManager: NSObject, NSFilePresenter { } } - init(folderPath: String) { - self.folderPath = folderPath + override init() { + let folderURL = AppConfig.dataSubfolder(named: "Themes") + self.folderPath = folderURL.path self.currentTheme = ArticleTheme.defaultTheme super.init() diff --git a/iOS/AppDelegate.swift b/iOS/AppDelegate.swift index c38b35ffc..90507981d 100644 --- a/iOS/AppDelegate.swift +++ b/iOS/AppDelegate.swift @@ -60,14 +60,8 @@ final class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationC override init() { super.init() - 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) - - let documentThemesFolder = documentFolder.appendingPathComponent("Themes").absoluteString - let documentThemesFolderPath = String(documentThemesFolder.suffix(from: documentAccountsFolder.index(documentThemesFolder.startIndex, offsetBy: 7))) - ArticleThemesManager.shared = ArticleThemesManager(folderPath: documentThemesFolderPath) + _ = AccountManager.shared + _ = ArticleThemesManager.shared NotificationCenter.default.addObserver(self, selector: #selector(unreadCountDidChange(_:)), name: .UnreadCountDidChange, object: nil) NotificationCenter.default.addObserver(self, selector: #selector(accountRefreshDidFinish(_:)), name: .AccountRefreshDidFinish, object: nil) @@ -293,7 +287,7 @@ private extension AppDelegate { func updateUserInterfaceStyle() { assert(Thread.isMainThread) - guard let window = self.window else { + guard let window else { // Could be nil legitimately — this can get called before window is set up. return }