From c29afd26771a7c4d7e0691543e964ddd23218fdf Mon Sep 17 00:00:00 2001 From: Stuart Breckenridge Date: Tue, 21 Sep 2021 09:22:45 +0800 Subject: [PATCH] try? changed to try with error handling --- Mac/AppDelegate.swift | 162 +++++++++--------- .../AppDelegate+Scriptability.swift | 2 +- .../ArticleTheme+Notifications.swift | 2 +- .../ArticleStyles/ArticleThemesManager.swift | 9 +- iOS/SceneCoordinator.swift | 9 +- iOS/SceneDelegate.swift | 2 +- .../ArticleThemesTableViewController.swift | 6 +- 7 files changed, 106 insertions(+), 86 deletions(-) diff --git a/Mac/AppDelegate.swift b/Mac/AppDelegate.swift index e3e44e4cb..69636e937 100644 --- a/Mac/AppDelegate.swift +++ b/Mac/AppDelegate.swift @@ -126,7 +126,7 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSUserInterfaceValidations, NotificationCenter.default.addObserver(self, selector: #selector(unreadCountDidChange(_:)), name: .UnreadCountDidChange, object: nil) NotificationCenter.default.addObserver(self, selector: #selector(inspectableObjectsDidChange(_:)), name: .InspectableObjectsDidChange, object: nil) NotificationCenter.default.addObserver(self, selector: #selector(importDownloadedTheme(_:)), name: .didEndDownloadingTheme, object: nil) - NotificationCenter.default.addObserver(self, selector: #selector(themeImportError(_:)), name: .didEndDownloadingThemeWithError, object: nil) + NotificationCenter.default.addObserver(self, selector: #selector(themeImportError(_:)), name: .didFailToImportThemeWithError, object: nil) NSWorkspace.shared.notificationCenter.addObserver(self, selector: #selector(didWakeNotification(_:)), name: NSWorkspace.didWakeNotification, object: nil) appDelegate = self @@ -811,88 +811,94 @@ internal extension AppDelegate { groupArticlesByFeedMenuItem.state = groupByFeedEnabled ? .on : .off } - func importTheme(filename: String) throws { + func importTheme(filename: String) { guard let window = mainWindowController?.window else { return } - let theme = try ArticleTheme(path: filename) - - let alert = NSAlert() - alert.alertStyle = .informational + do { + let theme = try ArticleTheme(path: filename) + let alert = NSAlert() + alert.alertStyle = .informational - let localizedMessageText = NSLocalizedString("Install theme “%@” by %@?", comment: "Theme message text") - alert.messageText = NSString.localizedStringWithFormat(localizedMessageText as NSString, theme.name, theme.creatorName) as String - - var attrs = [NSAttributedString.Key : Any]() - attrs[.font] = NSFont.systemFont(ofSize: NSFont.smallSystemFontSize) - attrs[.foregroundColor] = NSColor.textColor - - if #available(macOS 11.0, *) { - let titleParagraphStyle = NSMutableParagraphStyle() - titleParagraphStyle.alignment = .center - attrs[.paragraphStyle] = titleParagraphStyle - } - - let websiteText = NSMutableAttributedString() - websiteText.append(NSAttributedString(string: NSLocalizedString("Author's Website", comment: "Author's Website"), attributes: attrs)) - - if #available(macOS 11.0, *) { - websiteText.append(NSAttributedString(string: "\n")) - } else { - websiteText.append(NSAttributedString(string: " ")) - } - - attrs[.link] = theme.creatorHomePage - websiteText.append(NSAttributedString(string: theme.creatorHomePage, attributes: attrs)) - - let textViewWidth: CGFloat - if #available(macOS 11.0, *) { - textViewWidth = 200 - } else { - textViewWidth = 400 - } - - let textView = NSTextView(frame: CGRect(x: 0, y: 0, width: textViewWidth, height: 15)) - textView.isEditable = false - textView.drawsBackground = false - textView.textStorage?.setAttributedString(websiteText) - alert.accessoryView = textView - - alert.addButton(withTitle: NSLocalizedString("Install Theme", comment: "Install Theme")) - alert.addButton(withTitle: NSLocalizedString("Cancel", comment: "Cancel Install Theme")) + let localizedMessageText = NSLocalizedString("Install theme “%@” by %@?", comment: "Theme message text") + alert.messageText = NSString.localizedStringWithFormat(localizedMessageText as NSString, theme.name, theme.creatorName) as String - func importTheme() { - do { - try ArticleThemesManager.shared.importTheme(filename: filename) - confirmImportSuccess(themeName: theme.name) - } catch { - NSApplication.shared.presentError(error) + var attrs = [NSAttributedString.Key : Any]() + attrs[.font] = NSFont.systemFont(ofSize: NSFont.smallSystemFontSize) + attrs[.foregroundColor] = NSColor.textColor + + if #available(macOS 11.0, *) { + let titleParagraphStyle = NSMutableParagraphStyle() + titleParagraphStyle.alignment = .center + attrs[.paragraphStyle] = titleParagraphStyle } + + let websiteText = NSMutableAttributedString() + websiteText.append(NSAttributedString(string: NSLocalizedString("Author's Website", comment: "Author's Website"), attributes: attrs)) + + if #available(macOS 11.0, *) { + websiteText.append(NSAttributedString(string: "\n")) + } else { + websiteText.append(NSAttributedString(string: " ")) + } + + attrs[.link] = theme.creatorHomePage + websiteText.append(NSAttributedString(string: theme.creatorHomePage, attributes: attrs)) + + let textViewWidth: CGFloat + if #available(macOS 11.0, *) { + textViewWidth = 200 + } else { + textViewWidth = 400 + } + + let textView = NSTextView(frame: CGRect(x: 0, y: 0, width: textViewWidth, height: 15)) + textView.isEditable = false + textView.drawsBackground = false + textView.textStorage?.setAttributedString(websiteText) + alert.accessoryView = textView + + alert.addButton(withTitle: NSLocalizedString("Install Theme", comment: "Install Theme")) + alert.addButton(withTitle: NSLocalizedString("Cancel", comment: "Cancel Install Theme")) + + 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 { + + 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() + } + + } + } + } catch { + NotificationCenter.default.post(name: .didFailToImportThemeWithError, object: nil, userInfo: ["error" : error]) } - alert.beginSheetModal(for: window) { result in - if result == NSApplication.ModalResponse.alertFirstButtonReturn { - - 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() - } - - } - } + + } func confirmImportSuccess(themeName: String) { @@ -918,8 +924,8 @@ internal extension AppDelegate { DispatchQueue.main.async { let alert = NSAlert() alert.alertStyle = .warning - alert.messageText = NSLocalizedString("Theme Download Error", comment: "Theme download error") - alert.informativeText = NSLocalizedString("This theme cannot be downloaded due to the following error: \(error.localizedDescription)", comment: "Theme download error information") + alert.messageText = NSLocalizedString("Theme Error", comment: "Theme download error") + alert.informativeText = NSLocalizedString("This theme cannot be imported due to the following error: \(error.localizedDescription)", comment: "Theme download error information") alert.addButton(withTitle: NSLocalizedString("OK", comment: "OK")) alert.beginSheetModal(for: window) } diff --git a/Mac/Scriptability/AppDelegate+Scriptability.swift b/Mac/Scriptability/AppDelegate+Scriptability.swift index ffa1686bd..132fe26be 100644 --- a/Mac/Scriptability/AppDelegate+Scriptability.swift +++ b/Mac/Scriptability/AppDelegate+Scriptability.swift @@ -63,7 +63,7 @@ extension AppDelegate : AppDelegateAppleEvents { do { try ArticleThemeDownloader.shared.handleFile(at: location) } catch { - NotificationCenter.default.post(name: .didEndDownloadingThemeWithError, object: nil, userInfo: ["error": error]) + NotificationCenter.default.post(name: .didFailToImportThemeWithError, object: nil, userInfo: ["error": error]) } } task.resume() diff --git a/Shared/ArticleStyles/ArticleTheme+Notifications.swift b/Shared/ArticleStyles/ArticleTheme+Notifications.swift index 08337aab4..4c2391ff4 100644 --- a/Shared/ArticleStyles/ArticleTheme+Notifications.swift +++ b/Shared/ArticleStyles/ArticleTheme+Notifications.swift @@ -11,5 +11,5 @@ import Foundation extension Notification.Name { static let didBeginDownloadingTheme = Notification.Name("didBeginDownloadingTheme") static let didEndDownloadingTheme = Notification.Name("didEndDownloadingTheme") - static let didEndDownloadingThemeWithError = Notification.Name("didEndDownloadingThemeWithError") + static let didFailToImportThemeWithError = Notification.Name("didFailToImportThemeWithError") } diff --git a/Shared/ArticleStyles/ArticleThemesManager.swift b/Shared/ArticleStyles/ArticleThemesManager.swift index 0ce833f42..6e2b6078c 100644 --- a/Shared/ArticleStyles/ArticleThemesManager.swift +++ b/Shared/ArticleStyles/ArticleThemesManager.swift @@ -133,8 +133,13 @@ private extension ArticleThemesManager { guard let path = pathForThemeName(themeName, folder: folderPath) else { return nil } - - return try? ArticleTheme(path: path) + do { + return try ArticleTheme(path: path) + } catch { + NotificationCenter.default.post(name: .didFailToImportThemeWithError, object: nil, userInfo: ["error": error]) + return nil + } + } func defaultArticleTheme() -> ArticleTheme { diff --git a/iOS/SceneCoordinator.swift b/iOS/SceneCoordinator.swift index d0c9fea94..f3a2c1abc 100644 --- a/iOS/SceneCoordinator.swift +++ b/iOS/SceneCoordinator.swift @@ -324,7 +324,7 @@ class SceneCoordinator: NSObject, UndoableCommandRunner, UnreadCountProvider { NotificationCenter.default.addObserver(self, selector: #selector(accountDidDownloadArticles(_:)), name: .AccountDidDownloadArticles, object: nil) NotificationCenter.default.addObserver(self, selector: #selector(willEnterForeground(_:)), name: UIApplication.willEnterForegroundNotification, object: nil) NotificationCenter.default.addObserver(self, selector: #selector(importDownloadedTheme(_:)), name: .didEndDownloadingTheme, object: nil) - NotificationCenter.default.addObserver(self, selector: #selector(themeDownloadDidFail(_:)), name: .didEndDownloadingThemeWithError, object: nil) + NotificationCenter.default.addObserver(self, selector: #selector(themeDownloadDidFail(_:)), name: .didFailToImportThemeWithError, object: nil) } func start(for size: CGSize) -> UIViewController { @@ -1317,7 +1317,12 @@ class SceneCoordinator: NSObject, UndoableCommandRunner, UnreadCountProvider { } func importTheme(filename: String) { - try? ArticleThemeImporter.importTheme(controller: rootSplitViewController, filename: filename); + do { + try ArticleThemeImporter.importTheme(controller: rootSplitViewController, filename: filename) + } catch { + NotificationCenter.default.post(name: .didFailToImportThemeWithError, object: nil, userInfo: ["error" : error]) + } + } } diff --git a/iOS/SceneDelegate.swift b/iOS/SceneDelegate.swift index 534cd0db2..af4069522 100644 --- a/iOS/SceneDelegate.swift +++ b/iOS/SceneDelegate.swift @@ -190,7 +190,7 @@ class SceneDelegate: UIResponder, UIWindowSceneDelegate { do { try ArticleThemeDownloader.shared.handleFile(at: location) } catch { - NotificationCenter.default.post(name: .didEndDownloadingThemeWithError, object: nil, userInfo: ["error": error]) + NotificationCenter.default.post(name: .didFailToImportThemeWithError, object: nil, userInfo: ["error": error]) } } task.resume() diff --git a/iOS/Settings/ArticleThemesTableViewController.swift b/iOS/Settings/ArticleThemesTableViewController.swift index d8b7e4104..26e28944f 100644 --- a/iOS/Settings/ArticleThemesTableViewController.swift +++ b/iOS/Settings/ArticleThemesTableViewController.swift @@ -112,7 +112,11 @@ extension ArticleThemesTableViewController: UIDocumentPickerDelegate { func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) { guard let url = urls.first else { return } - try? ArticleThemeImporter.importTheme(controller: self, filename: url.standardizedFileURL.path) + do { + try ArticleThemeImporter.importTheme(controller: self, filename: url.standardizedFileURL.path) + } catch { + NotificationCenter.default.post(name: .didFailToImportThemeWithError, object: nil, userInfo: ["error": error]) + } } }