AppDelegate // Define alert types.

This commit is contained in:
ShikiSuen 2022-03-31 18:39:29 +08:00
parent a07709fd4f
commit e4f907b37d
1 changed files with 18 additions and 5 deletions

View File

@ -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