From b4d9423ff26797ecf14a6ac3087cf0243c8d21d5 Mon Sep 17 00:00:00 2001 From: Maurice Parker Date: Thu, 9 Sep 2021 06:03:28 -0500 Subject: [PATCH] Add alert to verify overlaying an existing theme --- Mac/AppDelegate.swift | 37 +++++++++++++++---- .../ArticleStyles/ArticleThemesManager.swift | 6 +++ 2 files changed, 35 insertions(+), 8 deletions(-) diff --git a/Mac/AppDelegate.swift b/Mac/AppDelegate.swift index 25d2bd969..64a8e1a7d 100644 --- a/Mac/AppDelegate.swift +++ b/Mac/AppDelegate.swift @@ -846,16 +846,37 @@ private extension AppDelegate { alert.addButton(withTitle: NSLocalizedString("Install Theme", comment: "Install Theme")) alert.addButton(withTitle: NSLocalizedString("Cancel", comment: "Cancel Install Theme")) - alert.beginSheetModal(for: window) { [weak self] result in + func importTheme() { + do { + try ArticleThemesManager.shared.importTheme(filename: filename) + confirmImportSuccess(themeName: theme.name) + } catch { + NSApplication.shared.presentError(error) + } + } + + alert.beginSheetModal(for: window) { result in if result == NSApplication.ModalResponse.alertFirstButtonReturn { - guard let self = self else { return } - - do { - try ArticleThemesManager.shared.importTheme(filename: filename) - self.confirmImportSuccess(themeName: theme.name) - } catch { - NSApplication.shared.presentError(error) + + if ArticleThemesManager.shared.themeExists(filename: filename) { + let alert = NSAlert() + alert.alertStyle = .warning + + let localizedMessageText = NSLocalizedString("The theme “%@” already exists. Overwrite it?", comment: "Overwrite theme") + alert.messageText = NSString.localizedStringWithFormat(localizedMessageText as NSString, theme.name) as String + + alert.addButton(withTitle: NSLocalizedString("Overwrite", comment: "Overwrite")) + alert.addButton(withTitle: NSLocalizedString("Cancel", comment: "Cancel Install Theme")) + + alert.beginSheetModal(for: window) { result in + if result == NSApplication.ModalResponse.alertFirstButtonReturn { + importTheme() + } + } + } else { + importTheme() } + } } } diff --git a/Shared/ArticleStyles/ArticleThemesManager.swift b/Shared/ArticleStyles/ArticleThemesManager.swift index 54138cbbe..b2d9d37af 100644 --- a/Shared/ArticleStyles/ArticleThemesManager.swift +++ b/Shared/ArticleStyles/ArticleThemesManager.swift @@ -72,6 +72,12 @@ final class ArticleThemesManager { // MARK: API + func themeExists(filename: String) -> Bool { + let filenameLastPathComponent = (filename as NSString).lastPathComponent + let toFilename = (folderPath as NSString).appendingPathComponent(filenameLastPathComponent) + return FileManager.default.fileExists(atPath: toFilename) + } + func importTheme(filename: String) throws { let filenameLastPathComponent = (filename as NSString).lastPathComponent let toFilename = (folderPath as NSString).appendingPathComponent(filenameLastPathComponent)