From 993c8fc22baf0f6a2bb56a45506d1060fc9798a3 Mon Sep 17 00:00:00 2001 From: ShikiSuen Date: Thu, 31 Mar 2022 18:39:29 +0800 Subject: [PATCH] AppDelegate // Define alert types. --- Source/Modules/AppDelegate.swift | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/Source/Modules/AppDelegate.swift b/Source/Modules/AppDelegate.swift index b8bcf8df..e9dc35c9 100644 --- a/Source/Modules/AppDelegate.swift +++ b/Source/Modules/AppDelegate.swift @@ -162,6 +162,7 @@ class AppDelegate: NSObject, NSApplicationDelegate, ctlNonModalAlertWindowDelega private var checkTask: URLSessionTask? private var updateNextStepURL: URL? private var fsStreamHelper = FSEventStreamHelper(path: mgrLangModel.dataFolderPath(isDefaultFolder: false), queue: DispatchQueue(label: "vChewing User Phrases")) + private var currentAlertType: String = "" // 補上 dealloc deinit { @@ -236,7 +237,7 @@ class AppDelegate: NSObject, NSApplicationDelegate, ctlNonModalAlertWindowDelega let nextUpdateDate = Date(timeInterval: kNextCheckInterval, since: Date()) UserDefaults.standard.set(nextUpdateDate, forKey: kNextUpdateCheckDateKey) - checkTask = VersionUpdateApi.check(forced: forced) { result in + checkTask = VersionUpdateApi.check(forced: forced) { [self] result in defer { self.checkTask = nil } @@ -252,6 +253,7 @@ class AppDelegate: NSObject, NSApplicationDelegate, ctlNonModalAlertWindowDelega report.remoteVersion, report.versionDescription) IME.prtDebugIntel("vChewingDebug: \(content)") + self.currentAlertType = "Update" ctlNonModalAlertWindow.shared.show(title: NSLocalizedString("New Version Available", comment: ""), content: content, confirmButtonTitle: NSLocalizedString("Visit Website", comment: ""), cancelButtonTitle: NSLocalizedString("Not Now", comment: ""), cancelAsDefault: false, delegate: self) NSApp.setActivationPolicy(.accessory) case .noNeedToUpdate, .ignored: @@ -264,6 +266,7 @@ class AppDelegate: NSObject, NSApplicationDelegate, ctlNonModalAlertWindowDelega let content = String(format: NSLocalizedString("There may be no internet connection or the server failed to respond.\n\nError message: %@", comment: ""), message) let buttonTitle = NSLocalizedString("Dismiss", comment: "") IME.prtDebugIntel("vChewingDebug: \(content)") + self.currentAlertType = "Update" ctlNonModalAlertWindow.shared.show(title: title, content: content, confirmButtonTitle: buttonTitle, cancelButtonTitle: nil, cancelAsDefault: false, delegate: nil) NSApp.setActivationPolicy(.accessory) default: @@ -274,14 +277,24 @@ class AppDelegate: NSObject, NSApplicationDelegate, ctlNonModalAlertWindowDelega } func ctlNonModalAlertWindowDidConfirm(_ controller: ctlNonModalAlertWindow) { - if let updateNextStepURL = updateNextStepURL { - NSWorkspace.shared.open(updateNextStepURL) + switch self.currentAlertType { + case "Update": + if let updateNextStepURL = self.updateNextStepURL { + NSWorkspace.shared.open(updateNextStepURL) + } + self.updateNextStepURL = nil + default: + break } - updateNextStepURL = nil } func ctlNonModalAlertWindowDidCancel(_ controller: ctlNonModalAlertWindow) { - updateNextStepURL = nil + switch self.currentAlertType { + case "Update": + self.updateNextStepURL = nil + default: + break + } } // New About Window