2021-09-07 23:58:06 +02:00
|
|
|
//
|
|
|
|
// ArticleThemesManager.sqift
|
|
|
|
// NetNewsWire
|
|
|
|
//
|
|
|
|
// Created by Brent Simmons on 9/26/15.
|
|
|
|
// Copyright © 2015 Ranchero Software, LLC. All rights reserved.
|
|
|
|
//
|
|
|
|
|
2021-09-12 16:59:03 +02:00
|
|
|
import Foundation
|
2021-09-07 23:58:06 +02:00
|
|
|
import RSCore
|
|
|
|
|
2021-09-08 21:19:56 +02:00
|
|
|
public extension Notification.Name {
|
|
|
|
static let ArticleThemeNamesDidChangeNotification = Notification.Name("ArticleThemeNamesDidChangeNotification")
|
|
|
|
static let CurrentArticleThemeDidChangeNotification = Notification.Name("CurrentArticleThemeDidChangeNotification")
|
|
|
|
}
|
2021-09-07 23:58:06 +02:00
|
|
|
|
2021-09-12 16:59:03 +02:00
|
|
|
final class ArticleThemesManager: NSObject, NSFilePresenter {
|
2021-09-07 23:58:06 +02:00
|
|
|
|
|
|
|
static var shared: ArticleThemesManager!
|
2021-09-12 03:32:15 +02:00
|
|
|
public let folderPath: String
|
2021-09-07 23:58:06 +02:00
|
|
|
|
2021-09-12 16:59:03 +02:00
|
|
|
lazy var presentedItemOperationQueue = OperationQueue.main
|
|
|
|
var presentedItemURL: URL? {
|
|
|
|
return URL(fileURLWithPath: folderPath)
|
|
|
|
}
|
|
|
|
|
2021-09-07 23:58:06 +02:00
|
|
|
var currentThemeName: String {
|
|
|
|
get {
|
|
|
|
return AppDefaults.shared.currentThemeName ?? AppDefaults.defaultThemeName
|
|
|
|
}
|
|
|
|
set {
|
|
|
|
if newValue != currentThemeName {
|
|
|
|
AppDefaults.shared.currentThemeName = newValue
|
2021-09-08 23:38:05 +02:00
|
|
|
updateCurrentTheme()
|
2021-09-07 23:58:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var currentTheme: ArticleTheme {
|
|
|
|
didSet {
|
2021-09-08 21:19:56 +02:00
|
|
|
NotificationCenter.default.post(name: .CurrentArticleThemeDidChangeNotification, object: self)
|
2021-09-07 23:58:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var themeNames = [AppDefaults.defaultThemeName] {
|
|
|
|
didSet {
|
2021-09-08 21:19:56 +02:00
|
|
|
NotificationCenter.default.post(name: .ArticleThemeNamesDidChangeNotification, object: self)
|
2021-09-07 23:58:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
init(folderPath: String) {
|
|
|
|
self.folderPath = folderPath
|
2021-09-12 16:59:03 +02:00
|
|
|
self.currentTheme = ArticleTheme.defaultTheme
|
2021-09-07 23:58:06 +02:00
|
|
|
|
2021-09-12 16:59:03 +02:00
|
|
|
super.init()
|
|
|
|
|
2021-09-07 23:58:06 +02:00
|
|
|
do {
|
|
|
|
try FileManager.default.createDirectory(atPath: folderPath, withIntermediateDirectories: true, attributes: nil)
|
|
|
|
} catch {
|
|
|
|
assertionFailure("Could not create folder for Themes.")
|
|
|
|
abort()
|
|
|
|
}
|
2021-09-12 18:53:34 +02:00
|
|
|
|
|
|
|
let themeFilenames = Bundle.main.paths(forResourcesOfType: ArticleTheme.nnwThemeSuffix, inDirectory: nil)
|
2021-09-23 17:00:53 +02:00
|
|
|
let installedThemes = readInstalledThemes() ?? [String: Date]()
|
2021-09-12 18:53:34 +02:00
|
|
|
for themeFilename in themeFilenames {
|
|
|
|
let themeName = ArticleTheme.themeNameForPath(themeFilename)
|
2021-09-23 17:00:53 +02:00
|
|
|
if !installedThemes.keys.contains(themeName) {
|
2021-09-12 18:53:34 +02:00
|
|
|
try? importTheme(filename: themeFilename)
|
|
|
|
}
|
|
|
|
}
|
2021-09-07 23:58:06 +02:00
|
|
|
|
|
|
|
updateThemeNames()
|
|
|
|
updateCurrentTheme()
|
|
|
|
|
2021-09-12 16:59:03 +02:00
|
|
|
NSFileCoordinator.addFilePresenter(self)
|
|
|
|
}
|
|
|
|
|
|
|
|
func presentedSubitemDidChange(at url: URL) {
|
|
|
|
updateThemeNames()
|
|
|
|
updateCurrentTheme()
|
2021-09-07 23:58:06 +02:00
|
|
|
}
|
|
|
|
|
2021-09-08 12:47:57 +02:00
|
|
|
// MARK: API
|
|
|
|
|
2021-09-09 13:03:28 +02:00
|
|
|
func themeExists(filename: String) -> Bool {
|
|
|
|
let filenameLastPathComponent = (filename as NSString).lastPathComponent
|
|
|
|
let toFilename = (folderPath as NSString).appendingPathComponent(filenameLastPathComponent)
|
|
|
|
return FileManager.default.fileExists(atPath: toFilename)
|
|
|
|
}
|
|
|
|
|
2021-09-08 12:47:57 +02:00
|
|
|
func importTheme(filename: String) throws {
|
|
|
|
let filenameLastPathComponent = (filename as NSString).lastPathComponent
|
|
|
|
let toFilename = (folderPath as NSString).appendingPathComponent(filenameLastPathComponent)
|
|
|
|
|
|
|
|
if FileManager.default.fileExists(atPath: toFilename) {
|
|
|
|
try FileManager.default.removeItem(atPath: toFilename)
|
|
|
|
}
|
|
|
|
|
|
|
|
try FileManager.default.copyItem(atPath: filename, toPath: toFilename)
|
2021-09-12 18:53:34 +02:00
|
|
|
|
|
|
|
let themeName = ArticleTheme.themeNameForPath(filename)
|
2021-09-23 17:00:53 +02:00
|
|
|
var installedThemes = readInstalledThemes() ?? [String: Date]()
|
|
|
|
installedThemes[themeName] = Date()
|
|
|
|
writeInstalledThemes(installedThemes)
|
2021-09-07 23:58:06 +02:00
|
|
|
}
|
2021-09-08 12:47:57 +02:00
|
|
|
|
2021-09-12 23:40:59 +02:00
|
|
|
func deleteTheme(themeName: String) {
|
|
|
|
if let filename = pathForThemeName(themeName, folder: folderPath) {
|
|
|
|
try? FileManager.default.removeItem(atPath: filename)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-08 12:47:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// MARK : Private
|
2021-09-07 23:58:06 +02:00
|
|
|
|
2021-09-08 12:47:57 +02:00
|
|
|
private extension ArticleThemesManager {
|
2021-09-07 23:58:06 +02:00
|
|
|
|
2021-09-08 12:47:57 +02:00
|
|
|
func updateThemeNames() {
|
2021-09-08 07:28:13 +02:00
|
|
|
let updatedThemeNames = allThemePaths(folderPath).map { ArticleTheme.themeNameForPath($0) }
|
2021-09-09 03:02:38 +02:00
|
|
|
let sortedThemeNames = updatedThemeNames.sorted(by: { $0.compare($1, options: .caseInsensitive) == .orderedAscending })
|
|
|
|
if sortedThemeNames != themeNames {
|
|
|
|
themeNames = sortedThemeNames
|
2021-09-07 23:58:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-08 12:47:57 +02:00
|
|
|
func articleThemeWithThemeName(_ themeName: String) -> ArticleTheme? {
|
2021-09-07 23:58:06 +02:00
|
|
|
if themeName == AppDefaults.defaultThemeName {
|
|
|
|
return ArticleTheme.defaultTheme
|
|
|
|
}
|
|
|
|
|
|
|
|
guard let path = pathForThemeName(themeName, folder: folderPath) else {
|
|
|
|
return nil
|
|
|
|
}
|
2021-09-21 03:22:45 +02:00
|
|
|
do {
|
|
|
|
return try ArticleTheme(path: path)
|
|
|
|
} catch {
|
|
|
|
NotificationCenter.default.post(name: .didFailToImportThemeWithError, object: nil, userInfo: ["error": error])
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-09-07 23:58:06 +02:00
|
|
|
}
|
|
|
|
|
2021-09-08 12:47:57 +02:00
|
|
|
func defaultArticleTheme() -> ArticleTheme {
|
2021-09-07 23:58:06 +02:00
|
|
|
return articleThemeWithThemeName(AppDefaults.defaultThemeName)!
|
|
|
|
}
|
|
|
|
|
2021-09-08 12:47:57 +02:00
|
|
|
func updateCurrentTheme() {
|
2021-09-07 23:58:06 +02:00
|
|
|
var themeName = currentThemeName
|
|
|
|
if !themeNames.contains(themeName) {
|
|
|
|
themeName = AppDefaults.defaultThemeName
|
|
|
|
currentThemeName = AppDefaults.defaultThemeName
|
|
|
|
}
|
|
|
|
|
|
|
|
var articleTheme = articleThemeWithThemeName(themeName)
|
|
|
|
if articleTheme == nil {
|
|
|
|
articleTheme = defaultArticleTheme()
|
|
|
|
currentThemeName = AppDefaults.defaultThemeName
|
|
|
|
}
|
|
|
|
|
|
|
|
if let articleTheme = articleTheme, articleTheme != currentTheme {
|
|
|
|
currentTheme = articleTheme
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-08 12:47:57 +02:00
|
|
|
func allThemePaths(_ folder: String) -> [String] {
|
2021-09-08 07:28:13 +02:00
|
|
|
let filepaths = FileManager.default.filePaths(inFolder: folder)
|
|
|
|
return filepaths?.filter { $0.hasSuffix(ArticleTheme.nnwThemeSuffix) } ?? []
|
|
|
|
}
|
2021-09-07 23:58:06 +02:00
|
|
|
|
2021-09-08 12:47:57 +02:00
|
|
|
func pathForThemeName(_ themeName: String, folder: String) -> String? {
|
2021-09-08 07:28:13 +02:00
|
|
|
for onePath in allThemePaths(folder) {
|
|
|
|
if ArticleTheme.pathIsPathForThemeName(themeName, path: onePath) {
|
|
|
|
return onePath
|
|
|
|
}
|
2021-09-07 23:58:06 +02:00
|
|
|
}
|
2021-09-08 07:28:13 +02:00
|
|
|
return nil
|
2021-09-07 23:58:06 +02:00
|
|
|
}
|
2021-09-08 07:28:13 +02:00
|
|
|
|
2021-09-23 17:00:53 +02:00
|
|
|
func readInstalledThemes() -> [String: Date]? {
|
|
|
|
let filePath = (folderPath as NSString).appendingPathComponent("InstalledThemes.plist")
|
2021-09-12 18:53:34 +02:00
|
|
|
return NSDictionary(contentsOfFile: filePath) as? [String: Date]
|
|
|
|
}
|
|
|
|
|
2021-09-23 17:00:53 +02:00
|
|
|
func writeInstalledThemes(_ dict: [String: Date]) {
|
|
|
|
let filePath = (folderPath as NSString).appendingPathComponent("InstalledThemes.plist")
|
2021-09-12 18:53:34 +02:00
|
|
|
(dict as NSDictionary).write(toFile: filePath, atomically: true)
|
|
|
|
}
|
|
|
|
|
2021-09-07 23:58:06 +02:00
|
|
|
}
|