Prevent app themes from attempting to be deleted

This commit is contained in:
Maurice Parker 2022-02-07 16:41:00 -08:00
parent a1bfa4afdc
commit 0c183f4bdf
2 changed files with 30 additions and 33 deletions

View File

@ -49,10 +49,6 @@ final class ArticleThemesManager: NSObject, NSFilePresenter {
}
}
var themes: [ArticleTheme] {
return themeNames.compactMap { articleThemeWithThemeName($0) }
}
init(folderPath: String) {
self.folderPath = folderPath
self.currentTheme = ArticleTheme.defaultTheme
@ -96,32 +92,6 @@ final class ArticleThemesManager: NSObject, NSFilePresenter {
try FileManager.default.copyItem(atPath: filename, toPath: toFilename)
}
func deleteTheme(themeName: String) {
if let filename = pathForThemeName(themeName, folder: folderPath) {
try? FileManager.default.removeItem(atPath: filename)
}
}
}
// MARK : Private
private extension ArticleThemesManager {
func updateThemeNames() {
let appThemeFilenames = Bundle.main.paths(forResourcesOfType: ArticleTheme.nnwThemeSuffix, inDirectory: nil)
let appThemeNames = Set(appThemeFilenames.map { ArticleTheme.themeNameForPath($0) })
let installedThemeNames = Set(allThemePaths(folderPath).map { ArticleTheme.themeNameForPath($0) })
let allThemeNames = appThemeNames.union(installedThemeNames)
let sortedThemeNames = allThemeNames.sorted(by: { $0.compare($1, options: .caseInsensitive) == .orderedAscending })
if sortedThemeNames != themeNames {
themeNames = sortedThemeNames
}
}
func articleThemeWithThemeName(_ themeName: String) -> ArticleTheme? {
if themeName == AppDefaults.defaultThemeName {
return ArticleTheme.defaultTheme
@ -148,6 +118,32 @@ private extension ArticleThemesManager {
}
func deleteTheme(themeName: String) {
if let filename = pathForThemeName(themeName, folder: folderPath) {
try? FileManager.default.removeItem(atPath: filename)
}
}
}
// MARK : Private
private extension ArticleThemesManager {
func updateThemeNames() {
let appThemeFilenames = Bundle.main.paths(forResourcesOfType: ArticleTheme.nnwThemeSuffix, inDirectory: nil)
let appThemeNames = Set(appThemeFilenames.map { ArticleTheme.themeNameForPath($0) })
let installedThemeNames = Set(allThemePaths(folderPath).map { ArticleTheme.themeNameForPath($0) })
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)!
}

View File

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