try? changed to try with error handling
This commit is contained in:
parent
78e0595708
commit
c29afd2677
@ -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(unreadCountDidChange(_:)), name: .UnreadCountDidChange, object: nil)
|
||||||
NotificationCenter.default.addObserver(self, selector: #selector(inspectableObjectsDidChange(_:)), name: .InspectableObjectsDidChange, 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(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)
|
NSWorkspace.shared.notificationCenter.addObserver(self, selector: #selector(didWakeNotification(_:)), name: NSWorkspace.didWakeNotification, object: nil)
|
||||||
|
|
||||||
appDelegate = self
|
appDelegate = self
|
||||||
@ -811,88 +811,94 @@ internal extension AppDelegate {
|
|||||||
groupArticlesByFeedMenuItem.state = groupByFeedEnabled ? .on : .off
|
groupArticlesByFeedMenuItem.state = groupByFeedEnabled ? .on : .off
|
||||||
}
|
}
|
||||||
|
|
||||||
func importTheme(filename: String) throws {
|
func importTheme(filename: String) {
|
||||||
guard let window = mainWindowController?.window else { return }
|
guard let window = mainWindowController?.window else { return }
|
||||||
|
|
||||||
let theme = try ArticleTheme(path: filename)
|
do {
|
||||||
|
let theme = try ArticleTheme(path: filename)
|
||||||
|
let alert = NSAlert()
|
||||||
|
alert.alertStyle = .informational
|
||||||
|
|
||||||
let alert = NSAlert()
|
let localizedMessageText = NSLocalizedString("Install theme “%@” by %@?", comment: "Theme message text")
|
||||||
alert.alertStyle = .informational
|
alert.messageText = NSString.localizedStringWithFormat(localizedMessageText as NSString, theme.name, theme.creatorName) as String
|
||||||
|
|
||||||
let localizedMessageText = NSLocalizedString("Install theme “%@” by %@?", comment: "Theme message text")
|
var attrs = [NSAttributedString.Key : Any]()
|
||||||
alert.messageText = NSString.localizedStringWithFormat(localizedMessageText as NSString, theme.name, theme.creatorName) as String
|
attrs[.font] = NSFont.systemFont(ofSize: NSFont.smallSystemFontSize)
|
||||||
|
attrs[.foregroundColor] = NSColor.textColor
|
||||||
|
|
||||||
var attrs = [NSAttributedString.Key : Any]()
|
if #available(macOS 11.0, *) {
|
||||||
attrs[.font] = NSFont.systemFont(ofSize: NSFont.smallSystemFontSize)
|
let titleParagraphStyle = NSMutableParagraphStyle()
|
||||||
attrs[.foregroundColor] = NSColor.textColor
|
titleParagraphStyle.alignment = .center
|
||||||
|
attrs[.paragraphStyle] = titleParagraphStyle
|
||||||
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
|
let websiteText = NSMutableAttributedString()
|
||||||
if result == NSApplication.ModalResponse.alertFirstButtonReturn {
|
websiteText.append(NSAttributedString(string: NSLocalizedString("Author's Website", comment: "Author's Website"), attributes: attrs))
|
||||||
|
|
||||||
if ArticleThemesManager.shared.themeExists(filename: filename) {
|
if #available(macOS 11.0, *) {
|
||||||
let alert = NSAlert()
|
websiteText.append(NSAttributedString(string: "\n"))
|
||||||
alert.alertStyle = .warning
|
} else {
|
||||||
|
websiteText.append(NSAttributedString(string: " "))
|
||||||
|
}
|
||||||
|
|
||||||
let localizedMessageText = NSLocalizedString("The theme “%@” already exists. Overwrite it?", comment: "Overwrite theme")
|
attrs[.link] = theme.creatorHomePage
|
||||||
alert.messageText = NSString.localizedStringWithFormat(localizedMessageText as NSString, theme.name) as String
|
websiteText.append(NSAttributedString(string: theme.creatorHomePage, attributes: attrs))
|
||||||
|
|
||||||
alert.addButton(withTitle: NSLocalizedString("Overwrite", comment: "Overwrite"))
|
let textViewWidth: CGFloat
|
||||||
alert.addButton(withTitle: NSLocalizedString("Cancel", comment: "Cancel Install Theme"))
|
if #available(macOS 11.0, *) {
|
||||||
|
textViewWidth = 200
|
||||||
|
} else {
|
||||||
|
textViewWidth = 400
|
||||||
|
}
|
||||||
|
|
||||||
alert.beginSheetModal(for: window) { result in
|
let textView = NSTextView(frame: CGRect(x: 0, y: 0, width: textViewWidth, height: 15))
|
||||||
if result == NSApplication.ModalResponse.alertFirstButtonReturn {
|
textView.isEditable = false
|
||||||
importTheme()
|
textView.drawsBackground = false
|
||||||
}
|
textView.textStorage?.setAttributedString(websiteText)
|
||||||
}
|
alert.accessoryView = textView
|
||||||
} else {
|
|
||||||
importTheme()
|
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])
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func confirmImportSuccess(themeName: String) {
|
func confirmImportSuccess(themeName: String) {
|
||||||
@ -918,8 +924,8 @@ internal extension AppDelegate {
|
|||||||
DispatchQueue.main.async {
|
DispatchQueue.main.async {
|
||||||
let alert = NSAlert()
|
let alert = NSAlert()
|
||||||
alert.alertStyle = .warning
|
alert.alertStyle = .warning
|
||||||
alert.messageText = NSLocalizedString("Theme Download Error", comment: "Theme download error")
|
alert.messageText = NSLocalizedString("Theme 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.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.addButton(withTitle: NSLocalizedString("OK", comment: "OK"))
|
||||||
alert.beginSheetModal(for: window)
|
alert.beginSheetModal(for: window)
|
||||||
}
|
}
|
||||||
|
@ -63,7 +63,7 @@ extension AppDelegate : AppDelegateAppleEvents {
|
|||||||
do {
|
do {
|
||||||
try ArticleThemeDownloader.shared.handleFile(at: location)
|
try ArticleThemeDownloader.shared.handleFile(at: location)
|
||||||
} catch {
|
} catch {
|
||||||
NotificationCenter.default.post(name: .didEndDownloadingThemeWithError, object: nil, userInfo: ["error": error])
|
NotificationCenter.default.post(name: .didFailToImportThemeWithError, object: nil, userInfo: ["error": error])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
task.resume()
|
task.resume()
|
||||||
|
@ -11,5 +11,5 @@ import Foundation
|
|||||||
extension Notification.Name {
|
extension Notification.Name {
|
||||||
static let didBeginDownloadingTheme = Notification.Name("didBeginDownloadingTheme")
|
static let didBeginDownloadingTheme = Notification.Name("didBeginDownloadingTheme")
|
||||||
static let didEndDownloadingTheme = Notification.Name("didEndDownloadingTheme")
|
static let didEndDownloadingTheme = Notification.Name("didEndDownloadingTheme")
|
||||||
static let didEndDownloadingThemeWithError = Notification.Name("didEndDownloadingThemeWithError")
|
static let didFailToImportThemeWithError = Notification.Name("didFailToImportThemeWithError")
|
||||||
}
|
}
|
||||||
|
@ -133,8 +133,13 @@ private extension ArticleThemesManager {
|
|||||||
guard let path = pathForThemeName(themeName, folder: folderPath) else {
|
guard let path = pathForThemeName(themeName, folder: folderPath) else {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
do {
|
||||||
|
return try ArticleTheme(path: path)
|
||||||
|
} catch {
|
||||||
|
NotificationCenter.default.post(name: .didFailToImportThemeWithError, object: nil, userInfo: ["error": error])
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
return try? ArticleTheme(path: path)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func defaultArticleTheme() -> ArticleTheme {
|
func defaultArticleTheme() -> ArticleTheme {
|
||||||
|
@ -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(accountDidDownloadArticles(_:)), name: .AccountDidDownloadArticles, object: nil)
|
||||||
NotificationCenter.default.addObserver(self, selector: #selector(willEnterForeground(_:)), name: UIApplication.willEnterForegroundNotification, 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(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 {
|
func start(for size: CGSize) -> UIViewController {
|
||||||
@ -1317,7 +1317,12 @@ class SceneCoordinator: NSObject, UndoableCommandRunner, UnreadCountProvider {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func importTheme(filename: String) {
|
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])
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -190,7 +190,7 @@ class SceneDelegate: UIResponder, UIWindowSceneDelegate {
|
|||||||
do {
|
do {
|
||||||
try ArticleThemeDownloader.shared.handleFile(at: location)
|
try ArticleThemeDownloader.shared.handleFile(at: location)
|
||||||
} catch {
|
} catch {
|
||||||
NotificationCenter.default.post(name: .didEndDownloadingThemeWithError, object: nil, userInfo: ["error": error])
|
NotificationCenter.default.post(name: .didFailToImportThemeWithError, object: nil, userInfo: ["error": error])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
task.resume()
|
task.resume()
|
||||||
|
@ -112,7 +112,11 @@ extension ArticleThemesTableViewController: UIDocumentPickerDelegate {
|
|||||||
|
|
||||||
func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
|
func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
|
||||||
guard let url = urls.first else { return }
|
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])
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user