From b3a39b9c6943bf9a1f3f74698e7eb567816d4825 Mon Sep 17 00:00:00 2001 From: mrFq1 <1xxbx0il0@mozmail.com> Date: Sun, 3 Jul 2022 23:03:15 +0800 Subject: [PATCH] Updater. --- ClashX/AppDelegate.swift | 25 ++++++++++++++ ClashX/Base.lproj/Main.storyboard | 34 +++++++++---------- .../NSUserNotificationCenter+Extension.swift | 4 +++ 3 files changed, 46 insertions(+), 17 deletions(-) diff --git a/ClashX/AppDelegate.swift b/ClashX/AppDelegate.swift index e18142cf6..dcfe9e1a3 100644 --- a/ClashX/AppDelegate.swift +++ b/ClashX/AppDelegate.swift @@ -11,6 +11,7 @@ import Cocoa import LetsMove import RxCocoa import RxSwift +import SwiftyJSON private let statusItemLengthWithSpeed: CGFloat = 72 @@ -792,6 +793,30 @@ extension AppDelegate { sender.state = newState MenuItemFactory.hideUnselectable = newState.rawValue } + + @IBAction func checkForUpdate(_ sender: NSMenuItem) { + let unc = NSUserNotificationCenter.default + AF.request("https://api.github.com/repos/MetaCubeX/ClashX.Meta/releases/latest").responseString { + guard $0.error == nil, + let data = $0.data, + let tagName = try? JSON(data: data)["tag_name"].string else { + unc.postUpdateNotice(msg: "Some thing failed.") + return + } + + if tagName != AppVersionUtil.currentVersion { + let alert = NSAlert() + alert.messageText = "Open github release page to download \(tagName)" + alert.addButton(withTitle: NSLocalizedString("OK", comment: "")) + alert.addButton(withTitle: NSLocalizedString("Cancel", comment: "")) + if alert.runModal() == .alertFirstButtonReturn { + NSWorkspace.shared.open(.init(string: "https://github.com/MetaCubeX/ClashX.Meta/releases/latest")!) + } + } else { + unc.postUpdateNotice(msg: "No new release found.") + } + } + } } // MARK: crash hanlder diff --git a/ClashX/Base.lproj/Main.storyboard b/ClashX/Base.lproj/Main.storyboard index 61d9ba109..40860b089 100644 --- a/ClashX/Base.lproj/Main.storyboard +++ b/ClashX/Base.lproj/Main.storyboard @@ -1,8 +1,8 @@ - + - + @@ -171,7 +171,7 @@ - + @@ -381,7 +381,7 @@ - + @@ -480,7 +480,7 @@ - + @@ -556,7 +556,7 @@ - + @@ -570,7 +570,7 @@ - + @@ -653,7 +653,7 @@ - + @@ -684,7 +684,7 @@ - + @@ -723,7 +723,7 @@ - + @@ -762,7 +762,7 @@ - + @@ -885,9 +885,9 @@ - + - + @@ -919,7 +919,7 @@ - + @@ -964,7 +964,7 @@ - + @@ -1006,7 +1006,7 @@ - + @@ -1057,7 +1057,7 @@ - + diff --git a/ClashX/Extensions/NSUserNotificationCenter+Extension.swift b/ClashX/Extensions/NSUserNotificationCenter+Extension.swift index 4509b858a..7f7fc70e6 100644 --- a/ClashX/Extensions/NSUserNotificationCenter+Extension.swift +++ b/ClashX/Extensions/NSUserNotificationCenter+Extension.swift @@ -136,6 +136,10 @@ extension NSUserNotificationCenter { post(title: NSLocalizedString("System Proxy Changed", comment: ""), info: NSLocalizedString("Proxy settings are changed by another process. ClashX is no longer the default system proxy.", comment: ""), notiOnly: true) } + + func postUpdateNotice(msg: String) { + postNotificationAlert(title: "Update ClashX Meta", info: msg) + } } class UserNotificationCenterDelegate: NSObject, NSUserNotificationCenterDelegate, UNUserNotificationCenterDelegate {