Use a file presenter so that we don't have to switch back to NNW to observe theme edits.

This commit is contained in:
Maurice Parker 2021-09-12 09:59:03 -05:00
parent abd66ea1bb
commit 020c1f6141

View File

@ -6,12 +6,7 @@
// Copyright © 2015 Ranchero Software, LLC. All rights reserved. // Copyright © 2015 Ranchero Software, LLC. All rights reserved.
// //
#if os(macOS) import Foundation
import AppKit
#else
import UIKit
#endif
import RSCore import RSCore
public extension Notification.Name { public extension Notification.Name {
@ -19,11 +14,16 @@ public extension Notification.Name {
static let CurrentArticleThemeDidChangeNotification = Notification.Name("CurrentArticleThemeDidChangeNotification") static let CurrentArticleThemeDidChangeNotification = Notification.Name("CurrentArticleThemeDidChangeNotification")
} }
final class ArticleThemesManager { final class ArticleThemesManager: NSObject, NSFilePresenter {
static var shared: ArticleThemesManager! static var shared: ArticleThemesManager!
public let folderPath: String public let folderPath: String
lazy var presentedItemOperationQueue = OperationQueue.main
var presentedItemURL: URL? {
return URL(fileURLWithPath: folderPath)
}
var currentThemeName: String { var currentThemeName: String {
get { get {
return AppDefaults.shared.currentThemeName ?? AppDefaults.defaultThemeName return AppDefaults.shared.currentThemeName ?? AppDefaults.defaultThemeName
@ -50,6 +50,9 @@ final class ArticleThemesManager {
init(folderPath: String) { init(folderPath: String) {
self.folderPath = folderPath self.folderPath = folderPath
self.currentTheme = ArticleTheme.defaultTheme
super.init()
do { do {
try FileManager.default.createDirectory(atPath: folderPath, withIntermediateDirectories: true, attributes: nil) try FileManager.default.createDirectory(atPath: folderPath, withIntermediateDirectories: true, attributes: nil)
@ -58,16 +61,15 @@ final class ArticleThemesManager {
abort() abort()
} }
currentTheme = ArticleTheme.defaultTheme
updateThemeNames() updateThemeNames()
updateCurrentTheme() updateCurrentTheme()
#if os(macOS) NSFileCoordinator.addFilePresenter(self)
NotificationCenter.default.addObserver(self, selector: #selector(applicationDidBecomeActive(_:)), name: NSApplication.didBecomeActiveNotification, object: nil) }
#else
NotificationCenter.default.addObserver(self, selector: #selector(applicationDidBecomeActive(_:)), name: UIApplication.didBecomeActiveNotification, object: nil) func presentedSubitemDidChange(at url: URL) {
#endif updateThemeNames()
updateCurrentTheme()
} }
// MARK: API // MARK: API
@ -87,15 +89,6 @@ final class ArticleThemesManager {
} }
try FileManager.default.copyItem(atPath: filename, toPath: toFilename) try FileManager.default.copyItem(atPath: filename, toPath: toFilename)
updateThemeNames()
}
// MARK: Notifications
@objc dynamic func applicationDidBecomeActive(_ note: Notification) {
updateThemeNames()
updateCurrentTheme()
} }
} }