Refactor the ArticleThemeManager code to simplify it and try to avoid any issues with app warm up on the iPhone

This commit is contained in:
Maurice Parker
2022-10-03 13:23:11 -05:00
parent 2e21018924
commit be75bd28d0
2 changed files with 13 additions and 43 deletions

View File

@@ -31,19 +31,18 @@ final class ArticleThemesManager: NSObject, NSFilePresenter, Logging {
set {
if newValue != currentThemeName {
AppDefaults.shared.currentThemeName = newValue
updateThemeNames()
updateCurrentTheme()
currentTheme = articleThemeWithThemeName(newValue)
}
}
}
var currentTheme: ArticleTheme {
lazy var currentTheme = { articleThemeWithThemeName(currentThemeName) }() {
didSet {
NotificationCenter.default.post(name: .CurrentArticleThemeDidChangeNotification, object: self)
}
}
var themeNames = [AppDefaults.defaultThemeName] {
lazy var themeNames = { buildThemeNames() }() {
didSet {
NotificationCenter.default.post(name: .ArticleThemeNamesDidChangeNotification, object: self)
}
@@ -51,7 +50,6 @@ final class ArticleThemesManager: NSObject, NSFilePresenter, Logging {
init(folderPath: String) {
self.folderPath = folderPath
self.currentTheme = ArticleTheme.defaultTheme
super.init()
@@ -63,15 +61,12 @@ final class ArticleThemesManager: NSObject, NSFilePresenter, Logging {
abort()
}
updateThemeNames()
updateCurrentTheme()
NSFileCoordinator.addFilePresenter(self)
}
func presentedSubitemDidChange(at url: URL) {
updateThemeNames()
updateCurrentTheme()
themeNames = buildThemeNames()
currentTheme = articleThemeWithThemeName(currentThemeName)
}
// MARK: API
@@ -93,7 +88,7 @@ final class ArticleThemesManager: NSObject, NSFilePresenter, Logging {
try FileManager.default.copyItem(atPath: filename, toPath: toFilename)
}
func articleThemeWithThemeName(_ themeName: String) -> ArticleTheme? {
func articleThemeWithThemeName(_ themeName: String) -> ArticleTheme {
if themeName == AppDefaults.defaultThemeName {
return ArticleTheme.defaultTheme
}
@@ -107,7 +102,7 @@ final class ArticleThemesManager: NSObject, NSFilePresenter, Logging {
path = installedPath
isAppTheme = false
} else {
return nil
return ArticleTheme.defaultTheme
}
do {
@@ -115,7 +110,7 @@ final class ArticleThemesManager: NSObject, NSFilePresenter, Logging {
} catch {
NotificationCenter.default.post(name: .didFailToImportThemeWithError, object: nil, userInfo: ["error": error])
logger.error("Failed to import theme: \(error.localizedDescription, privacy: .public)")
return nil
return ArticleTheme.defaultTheme
}
}
@@ -132,7 +127,7 @@ final class ArticleThemesManager: NSObject, NSFilePresenter, Logging {
private extension ArticleThemesManager {
func updateThemeNames() {
func buildThemeNames() -> [String] {
let appThemeFilenames = Bundle.main.paths(forResourcesOfType: ArticleTheme.nnwThemeSuffix, inDirectory: nil)
let appThemeNames = Set(appThemeFilenames.map { ArticleTheme.themeNameForPath($0) })
@@ -140,32 +135,7 @@ private extension ArticleThemesManager {
let allThemeNames = appThemeNames.union(installedThemeNames)
let sortedThemeNames = allThemeNames.sorted(by: { $0.compare($1, options: .caseInsensitive) == .orderedAscending })
if sortedThemeNames != themeNames {
themeNames = sortedThemeNames
}
}
func defaultArticleTheme() -> ArticleTheme {
return articleThemeWithThemeName(AppDefaults.defaultThemeName)!
}
func updateCurrentTheme() {
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
}
return allThemeNames.sorted(by: { $0.compare($1, options: .caseInsensitive) == .orderedAscending })
}
func allThemePaths(_ folder: String) -> [String] {

View File

@@ -72,9 +72,9 @@ class ArticleThemesTableViewController: UITableViewController, Logging {
override func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
guard let cell = tableView.cellForRow(at: indexPath),
let themeName = cell.textLabel?.text,
let theme = ArticleThemesManager.shared.articleThemeWithThemeName(themeName),
!theme.isAppTheme else { return nil }
let themeName = cell.textLabel?.text else { return nil }
guard !ArticleThemesManager.shared.articleThemeWithThemeName(themeName).isAppTheme else { return nil }
let deleteTitle = NSLocalizedString("Delete", comment: "Delete")
let deleteAction = UIContextualAction(style: .normal, title: deleteTitle) { [weak self] (action, view, completion) in