From 1cbcb446ee874654452d150f8b89be0e17f4c5de Mon Sep 17 00:00:00 2001 From: ShikiSuen Date: Thu, 8 Sep 2022 15:56:29 +0800 Subject: [PATCH] Repo // Concatenate all UpdateAPI contents into one file. --- .../OVUpdateAPI}/apiUpdate.swift | 147 ++++++++++++++---- Source/Modules/AppDelegate.swift | 85 +--------- .../ctlInputMethod_Core.swift | 2 +- .../ctlInputMethod_Menu.swift | 2 +- vChewing.xcodeproj/project.pbxproj | 10 +- 5 files changed, 127 insertions(+), 119 deletions(-) rename Source/{Modules/IMEModules => 3rdParty/OVUpdateAPI}/apiUpdate.swift (55%) diff --git a/Source/Modules/IMEModules/apiUpdate.swift b/Source/3rdParty/OVUpdateAPI/apiUpdate.swift similarity index 55% rename from Source/Modules/IMEModules/apiUpdate.swift rename to Source/3rdParty/OVUpdateAPI/apiUpdate.swift index df47f8ff..e5dd2a41 100644 --- a/Source/Modules/IMEModules/apiUpdate.swift +++ b/Source/3rdParty/OVUpdateAPI/apiUpdate.swift @@ -10,39 +10,8 @@ import Cocoa -struct VersionUpdateReport { - var siteUrl: URL? - var currentShortVersion: String = "" - var currentVersion: String = "" - var remoteShortVersion: String = "" - var remoteVersion: String = "" - var versionDescription: String = "" -} - -enum VersionUpdateApiResult { - case shouldUpdate(report: VersionUpdateReport) - case noNeedToUpdate - case ignored -} - -enum VersionUpdateApiError: Error, LocalizedError { - case connectionError(message: String) - - var errorDescription: String? { - switch self { - case .connectionError(let message): - return String( - format: NSLocalizedString( - "There may be no internet connection or the server failed to respond.\n\nError message: %@", - comment: "" - ), message - ) - } - } -} - enum VersionUpdateApi { - static let kCheckUpdateAutomatically = "CheckUpdateAutomatically" + static let kCheckUpdateAutomatically = UserDef.kCheckUpdateAutomatically.rawValue static let kNextUpdateCheckDateKey = "NextUpdateCheckDate" static let kUpdateInfoEndpointKey = "UpdateInfoEndpoint" static let kUpdateInfoSiteKey = "UpdateInfoSite" @@ -161,4 +130,118 @@ enum VersionUpdateApi { task.resume() return task } + + private static var checkTask: URLSessionTask? + static func checkForUpdate(forced: Bool = false) { + if checkTask != nil { + // busy + return + } + + // time for update? + if !forced { + if !mgrPrefs.checkUpdateAutomatically { + return + } + let now = Date() + let date = UserDefaults.standard.object(forKey: VersionUpdateApi.kNextUpdateCheckDateKey) as? Date ?? now + if now.compare(date) == .orderedAscending { + return + } + } + + let nextUpdateDate = Date(timeInterval: VersionUpdateApi.kNextCheckInterval, since: Date()) + UserDefaults.standard.set(nextUpdateDate, forKey: VersionUpdateApi.kNextUpdateCheckDateKey) + + checkTask = VersionUpdateApi.check(forced: forced) { [self] result in + defer { + checkTask = nil + } + switch result { + case .success(let apiResult): + switch apiResult { + case .shouldUpdate(let report): + let content = String( + format: NSLocalizedString( + "You're currently using vChewing %@ (%@), a new version %@ (%@) is now available. Do you want to visit vChewing's website to download the version?%@", + comment: "" + ), + report.currentShortVersion, + report.currentVersion, + report.remoteShortVersion, + report.remoteVersion, + report.versionDescription + ) + IME.prtDebugIntel("vChewingDebug: \(content)") + let alert = NSAlert() + alert.messageText = NSLocalizedString("New Version Available", comment: "") + alert.informativeText = content + alert.addButton(withTitle: NSLocalizedString("Visit Website", comment: "")) + alert.addButton(withTitle: NSLocalizedString("Not Now", comment: "")) + NSApp.setActivationPolicy(.accessory) + let result = alert.runModal() + if result == NSApplication.ModalResponse.alertFirstButtonReturn { + if let siteURL = report.siteUrl { + NSWorkspace.shared.open(siteURL) + } + } + case .noNeedToUpdate, .ignored: + break + } + case .failure(let error): + switch error { + case VersionUpdateApiError.connectionError(let message): + let title = NSLocalizedString("Update Check Failed", comment: "") + 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)") + + let alert = NSAlert() + alert.messageText = title + alert.informativeText = content + alert.addButton(withTitle: buttonTitle) + alert.runModal() + NSApp.setActivationPolicy(.accessory) + default: + break + } + } + } + } + + struct VersionUpdateReport { + var siteUrl: URL? + var currentShortVersion: String = "" + var currentVersion: String = "" + var remoteShortVersion: String = "" + var remoteVersion: String = "" + var versionDescription: String = "" + } + + enum VersionUpdateApiResult { + case shouldUpdate(report: VersionUpdateReport) + case noNeedToUpdate + case ignored + } + + enum VersionUpdateApiError: Error, LocalizedError { + case connectionError(message: String) + + var errorDescription: String? { + switch self { + case .connectionError(let message): + return String( + format: NSLocalizedString( + "There may be no internet connection or the server failed to respond.\n\nError message: %@", + comment: "" + ), message + ) + } + } + } } diff --git a/Source/Modules/AppDelegate.swift b/Source/Modules/AppDelegate.swift index 97cb725f..96eb3585 100644 --- a/Source/Modules/AppDelegate.swift +++ b/Source/Modules/AppDelegate.swift @@ -27,7 +27,6 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSUserNotificationCenterDele @IBOutlet var window: NSWindow? private var ctlPrefWindowInstance: ctlPrefWindow? private var ctlAboutWindowInstance: ctlAboutWindow? // New About Window - private var checkTask: URLSessionTask? public lazy var folderMonitor = FolderMonitor( url: URL(fileURLWithPath: mgrLangModel.dataFolderPath(isDefaultFolder: false)) ) @@ -66,7 +65,7 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSUserNotificationCenterDele // 只要使用者沒有勾選檢查更新、沒有主動做出要檢查更新的操作,就不要檢查更新。 if mgrPrefs.checkUpdateAutomatically { - checkForUpdate() + VersionUpdateApi.checkForUpdate() } } @@ -103,88 +102,6 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSUserNotificationCenterDele NSApp.setActivationPolicy(.accessory) } - func checkForUpdate(forced: Bool = false) { - if checkTask != nil { - // busy - return - } - - // time for update? - if !forced { - if !mgrPrefs.checkUpdateAutomatically { - return - } - let now = Date() - let date = UserDefaults.standard.object(forKey: VersionUpdateApi.kNextUpdateCheckDateKey) as? Date ?? now - if now.compare(date) == .orderedAscending { - return - } - } - - let nextUpdateDate = Date(timeInterval: VersionUpdateApi.kNextCheckInterval, since: Date()) - UserDefaults.standard.set(nextUpdateDate, forKey: VersionUpdateApi.kNextUpdateCheckDateKey) - - checkTask = VersionUpdateApi.check(forced: forced) { [self] result in - defer { - checkTask = nil - } - switch result { - case .success(let apiResult): - switch apiResult { - case .shouldUpdate(let report): - let content = String( - format: NSLocalizedString( - "You're currently using vChewing %@ (%@), a new version %@ (%@) is now available. Do you want to visit vChewing's website to download the version?%@", - comment: "" - ), - report.currentShortVersion, - report.currentVersion, - report.remoteShortVersion, - report.remoteVersion, - report.versionDescription - ) - IME.prtDebugIntel("vChewingDebug: \(content)") - let alert = NSAlert() - alert.messageText = NSLocalizedString("New Version Available", comment: "") - alert.informativeText = content - alert.addButton(withTitle: NSLocalizedString("Visit Website", comment: "")) - alert.addButton(withTitle: NSLocalizedString("Not Now", comment: "")) - let result = alert.runModal() - if result == NSApplication.ModalResponse.alertFirstButtonReturn { - if let siteURL = report.siteUrl { - NSWorkspace.shared.open(siteURL) - } - } - NSApp.setActivationPolicy(.accessory) - case .noNeedToUpdate, .ignored: - break - } - case .failure(let error): - switch error { - case VersionUpdateApiError.connectionError(let message): - let title = NSLocalizedString("Update Check Failed", comment: "") - 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)") - - let alert = NSAlert() - alert.messageText = title - alert.informativeText = content - alert.addButton(withTitle: buttonTitle) - alert.runModal() - NSApp.setActivationPolicy(.accessory) - default: - break - } - } - } - } - func selfUninstall() { currentAlertType = "Uninstall" let content = String( diff --git a/Source/Modules/ControllerModules/ctlInputMethod_Core.swift b/Source/Modules/ControllerModules/ctlInputMethod_Core.swift index 87dd7639..2c9e398e 100644 --- a/Source/Modules/ControllerModules/ctlInputMethod_Core.swift +++ b/Source/Modules/ControllerModules/ctlInputMethod_Core.swift @@ -135,7 +135,7 @@ class ctlInputMethod: IMKInputController { setKeyLayout() handle(state: IMEState.ofEmpty()) } // 除此之外就不要動了,免得在點開輸入法自身的視窗時卡死。 - (NSApp.delegate as? AppDelegate)?.checkForUpdate() + VersionUpdateApi.checkForUpdate() } /// 停用輸入法時,會觸發該函式。 diff --git a/Source/Modules/ControllerModules/ctlInputMethod_Menu.swift b/Source/Modules/ControllerModules/ctlInputMethod_Menu.swift index d1ac194b..c1ab11bb 100644 --- a/Source/Modules/ControllerModules/ctlInputMethod_Menu.swift +++ b/Source/Modules/ControllerModules/ctlInputMethod_Menu.swift @@ -308,7 +308,7 @@ extension ctlInputMethod { } @objc func checkForUpdate(_: Any?) { - (NSApp.delegate as? AppDelegate)?.checkForUpdate(forced: true) + VersionUpdateApi.checkForUpdate(forced: true) } @objc func openUserDataFolder(_: Any?) { diff --git a/vChewing.xcodeproj/project.pbxproj b/vChewing.xcodeproj/project.pbxproj index e01dec94..ecfcab80 100644 --- a/vChewing.xcodeproj/project.pbxproj +++ b/vChewing.xcodeproj/project.pbxproj @@ -423,6 +423,14 @@ path = FolderMonitor; sourceTree = ""; }; + 5B16B84D28C9D6BF00ABA692 /* OVUpdateAPI */ = { + isa = PBXGroup; + children = ( + 5BDC1CF927FDF1310052C2B9 /* apiUpdate.swift */, + ); + path = OVUpdateAPI; + sourceTree = ""; + }; 5B18BA7527C7BF6D0056EB19 /* MiscRootFiles */ = { isa = PBXGroup; children = ( @@ -488,6 +496,7 @@ 5B84579B2871AD2200C93B01 /* HotenkaChineseConverter */, 5B949BD72816DC4400D87B5D /* LineReader */, 5B5F8AEC28C86AB3007C11F1 /* NSAttributedTextView */, + 5B16B84D28C9D6BF00ABA692 /* OVUpdateAPI */, 5BA58644289BCFAC0077D02F /* Qwertyyb */, 5B20430528BEE2F300BFC6FD /* Sandbox */, 5BA9FCEA27FED652002DE248 /* SindreSorhus */, @@ -530,7 +539,6 @@ 5B62A32227AE756300A19448 /* IMEModules */ = { isa = PBXGroup; children = ( - 5BDC1CF927FDF1310052C2B9 /* apiUpdate.swift */, 5B5E535127EF261400C6AA1E /* IME.swift */, 5B175FFA28C5CDDC0078D1B4 /* IMKHelper.swift */, 5B62A33527AE795800A19448 /* mgrPrefs.swift */,