Improve: remove config update notifications on remote config autoupdate and switching configs

This commit is contained in:
yicheng 2019-08-18 13:23:28 +08:00 committed by yichengchen
parent fa5b7b4dfe
commit 3e5c04a22f
3 changed files with 50 additions and 41 deletions

View File

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

View File

@ -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(){}

View File

@ -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()
}
}