From e43e770f4a47d264f68142fc0a5bc27c64aee2e9 Mon Sep 17 00:00:00 2001 From: Maurice Parker Date: Sat, 1 Apr 2023 19:55:32 -0500 Subject: [PATCH 1/2] Fix automatic theme reloading --- .../ArticleStyles/ArticleThemesManager.swift | 43 +++++++++++++------ 1 file changed, 31 insertions(+), 12 deletions(-) diff --git a/Shared/ArticleStyles/ArticleThemesManager.swift b/Shared/ArticleStyles/ArticleThemesManager.swift index 7a9685d47..d5c94875e 100644 --- a/Shared/ArticleStyles/ArticleThemesManager.swift +++ b/Shared/ArticleStyles/ArticleThemesManager.swift @@ -20,9 +20,7 @@ final class ArticleThemesManager: NSObject, NSFilePresenter, Logging { public let folderPath: String lazy var presentedItemOperationQueue = OperationQueue.main - var presentedItemURL: URL? { - return URL(fileURLWithPath: folderPath) - } + var presentedItemURL: URL? var currentThemeName: String { get { @@ -33,6 +31,7 @@ final class ArticleThemesManager: NSObject, NSFilePresenter, Logging { do { currentTheme = try articleThemeWithThemeName(newValue) AppDefaults.shared.currentThemeName = newValue + updateFilePresenter() } catch { logger.error("Unable to set new theme: \(error.localizedDescription, privacy: .public)") } @@ -71,18 +70,22 @@ final class ArticleThemesManager: NSObject, NSFilePresenter, Logging { assertionFailure("Could not create folder for Themes.") abort() } - - NSFileCoordinator.addFilePresenter(self) + + #if os(macOS) + 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) + #endif + + updateFilePresenter() } func presentedSubitemDidChange(at url: URL) { - if url.lastPathComponent.localizedCaseInsensitiveContains("nnwtheme") { - themeNames = buildThemeNames() - do { - currentTheme = try articleThemeWithThemeName(currentThemeName) - } catch { - appDelegate.presentThemeImportError(error) - } + themeNames = buildThemeNames() + do { + currentTheme = try articleThemeWithThemeName(currentThemeName) + } catch { + appDelegate.presentThemeImportError(error) } } @@ -103,6 +106,8 @@ final class ArticleThemesManager: NSObject, NSFilePresenter, Logging { } try FileManager.default.copyItem(atPath: filename, toPath: toFilename) + + themeNames = buildThemeNames() } func articleThemeWithThemeName(_ themeName: String) throws -> ArticleTheme { @@ -128,6 +133,7 @@ final class ArticleThemesManager: NSObject, NSFilePresenter, Logging { func deleteTheme(themeName: String) { if let filename = pathForThemeName(themeName, folder: folderPath) { try? FileManager.default.removeItem(atPath: filename) + themeNames = buildThemeNames() } } @@ -136,6 +142,19 @@ final class ArticleThemesManager: NSObject, NSFilePresenter, Logging { // MARK : Private private extension ArticleThemesManager { + + @objc func applicationDidBecomeActive(_ note: Notification) { + themeNames = buildThemeNames() + } + + func updateFilePresenter() { + guard let currentThemePath = currentTheme.path else { + return + } + NSFileCoordinator.removeFilePresenter(self) + presentedItemURL = URL(fileURLWithPath: currentThemePath) + NSFileCoordinator.addFilePresenter(self) + } func buildThemeNames() -> [String] { let appThemeFilenames = Bundle.main.paths(forResourcesOfType: ArticleTheme.nnwThemeSuffix, inDirectory: nil) From 5bde27c9eba3b7b1a0b9f155e35c7fdb30f254db Mon Sep 17 00:00:00 2001 From: Maurice Parker Date: Sat, 1 Apr 2023 21:43:46 -0500 Subject: [PATCH 2/2] Fix framework imports --- Shared/ArticleStyles/ArticleThemesManager.swift | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/Shared/ArticleStyles/ArticleThemesManager.swift b/Shared/ArticleStyles/ArticleThemesManager.swift index d5c94875e..2ac447450 100644 --- a/Shared/ArticleStyles/ArticleThemesManager.swift +++ b/Shared/ArticleStyles/ArticleThemesManager.swift @@ -6,7 +6,12 @@ // Copyright © 2015 Ranchero Software, LLC. All rights reserved. // -import Foundation +#if os(macOS) +import AppKit +#else +import UIKit +#endif + import RSCore public extension Notification.Name {