From ba279d2a6efd2df0decc475a5d53c174d3d9977a Mon Sep 17 00:00:00 2001 From: Maurice Parker Date: Tue, 24 Aug 2021 20:20:20 -0500 Subject: [PATCH] Make the Styles folder configurable so that it can be in different places on macOS and iOS. --- Mac/AppDelegate.swift | 1 + .../ArticleStyles/ArticleStylesManager.swift | 24 +++++++++---------- iOS/AppDelegate.swift | 9 +++++-- 3 files changed, 19 insertions(+), 15 deletions(-) diff --git a/Mac/AppDelegate.swift b/Mac/AppDelegate.swift index f55bd5d74..9cffc8840 100644 --- a/Mac/AppDelegate.swift +++ b/Mac/AppDelegate.swift @@ -120,6 +120,7 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSUserInterfaceValidations, SecretsManager.provider = Secrets() AccountManager.shared = AccountManager(accountsFolder: Platform.dataSubfolder(forApplication: nil, folderName: "Accounts")!) + ArticleStylesManager.shared = ArticleStylesManager(folderPath: Platform.dataSubfolder(forApplication: nil, folderName: "Styles")!) FeedProviderManager.shared.delegate = ExtensionPointManager.shared NotificationCenter.default.addObserver(self, selector: #selector(unreadCountDidChange(_:)), name: .UnreadCountDidChange, object: nil) diff --git a/Shared/ArticleStyles/ArticleStylesManager.swift b/Shared/ArticleStyles/ArticleStylesManager.swift index ddc23b3f8..06ff1a64e 100644 --- a/Shared/ArticleStyles/ArticleStylesManager.swift +++ b/Shared/ArticleStyles/ArticleStylesManager.swift @@ -19,7 +19,6 @@ let CurrentArticleStyleDidChangeNotification = "CurrentArticleStyleDidChangeNoti private let styleKey = "style" private let defaultStyleName = "Default" -private let stylesFolderName = "Styles" private let stylesInResourcesFolderName = "Styles" private let styleSuffix = ".netnewswirestyle" private let nnwStyleSuffix = ".nnwstyle" @@ -28,8 +27,8 @@ private let styleSuffixes = [styleSuffix, nnwStyleSuffix, cssStyleSuffix]; final class ArticleStylesManager { - static let shared = ArticleStylesManager() - private let folderPath = Platform.dataSubfolder(forApplication: nil, folderName: stylesFolderName)! + static var shared: ArticleStylesManager! + private let folderPath: String var currentStyleName: String { get { @@ -54,18 +53,17 @@ final class ArticleStylesManager { } } - init() { + init(folderPath: String) { + self.folderPath = folderPath + + do { + try FileManager.default.createDirectory(atPath: folderPath, withIntermediateDirectories: true, attributes: nil) + } catch { + assertionFailure("Could not create folder for Styles.") + abort() + } UserDefaults.standard.register(defaults: [styleKey: defaultStyleName]) -// -// let defaultStylesFolder = (Bundle.main.resourcePath! as NSString).appendingPathComponent(stylesInResourcesFolderName) -// do { -// try FileManager.default.rs_copyFiles(inFolder: defaultStylesFolder, destination: folderPath) -// } -// catch { -// print(error) -// } - currentStyle = ArticleStyle.defaultStyle updateStyleNames() diff --git a/iOS/AppDelegate.swift b/iOS/AppDelegate.swift index d4fa54768..68fabfd42 100644 --- a/iOS/AppDelegate.swift +++ b/iOS/AppDelegate.swift @@ -63,10 +63,15 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterD appDelegate = self SecretsManager.provider = Secrets() - let documentAccountURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first! - let documentAccountsFolder = documentAccountURL.appendingPathComponent("Accounts").absoluteString + 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 documentStylesFolder = documentFolder.appendingPathComponent("Styles").absoluteString + let documentStylesFolderPath = String(documentStylesFolder.suffix(from: documentAccountsFolder.index(documentStylesFolder.startIndex, offsetBy: 7))) + ArticleStylesManager.shared = ArticleStylesManager(folderPath: documentStylesFolderPath) + FeedProviderManager.shared.delegate = ExtensionPointManager.shared NotificationCenter.default.addObserver(self, selector: #selector(unreadCountDidChange(_:)), name: .UnreadCountDidChange, object: nil)