From 3e5c04a22f1e937a0ff567a6cfb5722eef6f2d9d Mon Sep 17 00:00:00 2001 From: yicheng Date: Sun, 18 Aug 2019 13:23:28 +0800 Subject: [PATCH] Improve: remove config update notifications on remote config autoupdate and switching configs --- ClashX/AppDelegate.swift | 64 +++++++++---------- ClashX/General/Managers/MenuItemFactory.swift | 4 +- .../Managers/RemoteConfigManager.swift | 23 ++++--- 3 files changed, 50 insertions(+), 41 deletions(-) diff --git a/ClashX/AppDelegate.swift b/ClashX/AppDelegate.swift index bd55bdf..d36b59c 100644 --- a/ClashX/AppDelegate.swift +++ b/ClashX/AppDelegate.swift @@ -74,7 +74,7 @@ class AppDelegate: NSObject, NSApplicationDelegate { // start proxy setupData() - actionUpdateConfig(self) + updateConfig(showNotification: false) updateLoggingLevel() hideFunctionIfNeed() @@ -134,7 +134,8 @@ class AppDelegate: NSObject, NSApplicationDelegate { NotificationCenter.default.rx.notification(kShouldUpDateConfig).bind { [weak self] (note) in guard let self = self else {return} - self.actionUpdateConfig(nil) + let showNotice = note.userInfo?["notification"] as? Bool ?? true + self.updateConfig(showNotification: showNotice) }.disposed(by: disposeBag) @@ -312,6 +313,32 @@ class AppDelegate: NSObject, NSApplicationDelegate { } } + func updateConfig(showNotification: Bool = true) { + startProxy() + guard ConfigManager.shared.isRunning else {return} + + ApiRequest.requestConfigUpdate() { [weak self] error in + guard let self = self else {return} + if (error == nil) { + self.syncConfig() + self.resetStreamApi() + self.selectProxyGroupWithMemory() + self.selectOutBoundModeWithMenory() + self.selectAllowLanWithMenory() + ConfigFileManager.checkFinalRuleAndShowAlert() + if showNotification { + NSUserNotificationCenter.default + .post(title: NSLocalizedString("Reload Config Succeed", comment: ""), + info: NSLocalizedString("Succees", comment: "")) + } + } else if showNotification { + NSUserNotificationCenter.default + .post(title: NSLocalizedString("Reload Config Fail", comment: ""), + info: error ?? "") + } + } + } + } // MARK: Main actions @@ -427,35 +454,8 @@ extension AppDelegate { NSWorkspace.shared.openFile(kConfigFolderPath) } - @IBAction func actionUpdateConfig(_ sender: Any?) { - startProxy() - guard ConfigManager.shared.isRunning else {return} - let notifaction = self != (sender as? NSObject) - ApiRequest.requestConfigUpdate() { [weak self] error in - guard let self = self else {return} - if (error == nil) { - self.syncConfig() - self.resetStreamApi() - self.selectProxyGroupWithMemory() - self.selectOutBoundModeWithMenory() - self.selectAllowLanWithMenory() - ConfigFileManager.checkFinalRuleAndShowAlert() - if notifaction{ - NSUserNotificationCenter - .default - .post(title: NSLocalizedString("Reload Config Succeed", comment: ""), - info: NSLocalizedString("Succees", comment: "")) - } - } else { - if (notifaction) { - NSUserNotificationCenter - .default - .post(title: NSLocalizedString("Reload Config Fail", comment: ""), - info: error ?? "") - } - } - - } + @IBAction func actionUpdateConfig(_ sender: AnyObject) { + updateConfig() } @IBAction func actionSetLogLevel(_ sender: NSMenuItem) { @@ -473,7 +473,7 @@ extension AppDelegate { @IBAction func actionUpdateRemoteConfig(_ sender: Any) { - RemoteConfigManager.shared.updateCheck(ignoreTimeLimit: true) + RemoteConfigManager.shared.updateCheck(ignoreTimeLimit: true, showNotification: true) } } diff --git a/ClashX/General/Managers/MenuItemFactory.swift b/ClashX/General/Managers/MenuItemFactory.swift index f981c57..cd82e82 100644 --- a/ClashX/General/Managers/MenuItemFactory.swift +++ b/ClashX/General/Managers/MenuItemFactory.swift @@ -191,7 +191,9 @@ extension MenuItemFactory { @objc static func actionSelectConfig(sender:NSMenuItem){ let config = sender.title ConfigManager.selectConfigName = config - NotificationCenter.default.post(Notification(name: kShouldUpDateConfig)) + NotificationCenter.default.post(name: kShouldUpDateConfig, + object: nil, + userInfo: ["notification": false]) } @objc static func empty(){} diff --git a/ClashX/General/Managers/RemoteConfigManager.swift b/ClashX/General/Managers/RemoteConfigManager.swift index 0393bf4..ff407cc 100644 --- a/ClashX/General/Managers/RemoteConfigManager.swift +++ b/ClashX/General/Managers/RemoteConfigManager.swift @@ -76,7 +76,7 @@ class RemoteConfigManager { updateCheck() } - func updateCheck(ignoreTimeLimit: Bool = false) { + func updateCheck(ignoreTimeLimit: Bool = false, showNotification: Bool = false) { let currentConfigName = ConfigManager.selectConfigName let group = DispatchGroup() @@ -107,15 +107,22 @@ class RemoteConfigManager { if isCurrentConfig { if let error = error { // Fail - NSUserNotificationCenter.default - .post(title: NSLocalizedString("Remote Config Update Fail", comment: ""), - info: error) + if showNotification { + NSUserNotificationCenter.default + .post(title: NSLocalizedString("Remote Config Update Fail", comment: "") , + info: "\(config.name): \(error)") + } + } else { // Success - NSUserNotificationCenter.default - .post(title: NSLocalizedString("Remote Config Update", comment: "") - , info: NSLocalizedString("Succeed!", comment: "")) - NotificationCenter.default.post(Notification(name: kShouldUpDateConfig)) + if showNotification { + let info = "\(config.name): \(NSLocalizedString("Succeed!", comment: ""))" + NSUserNotificationCenter.default + .post(title: NSLocalizedString("Remote Config Update", comment: ""), info:info) + } + NotificationCenter.default.post(name: kShouldUpDateConfig, + object: nil, + userInfo: ["notification": false]) RemoteConfigManager.didUpdateConfig() } }