Only update profile selection display when switch success

This commit is contained in:
yicheng 2019-10-25 23:30:11 +08:00
parent fa50007dd5
commit d8d8ac09ec
9 changed files with 36 additions and 34 deletions

View File

@ -55,6 +55,7 @@
49D176A72355FE680093DD7B /* NetworkChangeNotifier.swift in Sources */ = {isa = PBXBuildFile; fileRef = 49D176A62355FE680093DD7B /* NetworkChangeNotifier.swift */; };
49D176A9235614340093DD7B /* ProxyGroupSpeedTestMenuItem.swift in Sources */ = {isa = PBXBuildFile; fileRef = 49D176A8235614340093DD7B /* ProxyGroupSpeedTestMenuItem.swift */; };
49D176AB23575BB20093DD7B /* ProxyGroupMenuItemView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 49D176AA23575BB20093DD7B /* ProxyGroupMenuItemView.swift */; };
F9203A26236342820020D57D /* AppDelegate+..swift in Sources */ = {isa = PBXBuildFile; fileRef = F9203A25236342820020D57D /* AppDelegate+..swift */; };
F935B2F02307C52E009E4D33 /* com.west2online.ClashX.ProxyConfigHelper in Copy Files */ = {isa = PBXBuildFile; fileRef = F9A7C0692306E874007163C7 /* com.west2online.ClashX.ProxyConfigHelper */; settings = {ATTRIBUTES = (CodeSignOnCopy, ); }; };
F935B2F42307CD32009E4D33 /* ProxyConfigHelper.m in Sources */ = {isa = PBXBuildFile; fileRef = F935B2F32307CD32009E4D33 /* ProxyConfigHelper.m */; };
F935B2FA23083EE6009E4D33 /* ProxySettingTool.m in Sources */ = {isa = PBXBuildFile; fileRef = F935B2F923083EE6009E4D33 /* ProxySettingTool.m */; };
@ -187,6 +188,7 @@
49E07A8A20D501A000A088A3 /* Main.storyboard */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; path = Main.storyboard; sourceTree = "<group>"; };
5217C006C5A22A1CEA24BFC1 /* Pods-ClashX.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-ClashX.debug.xcconfig"; path = "Pods/Target Support Files/Pods-ClashX/Pods-ClashX.debug.xcconfig"; sourceTree = "<group>"; };
A1485BCE642059532D01B8BA /* Pods-ClashX.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-ClashX.release.xcconfig"; path = "Pods/Target Support Files/Pods-ClashX/Pods-ClashX.release.xcconfig"; sourceTree = "<group>"; };
F9203A25236342820020D57D /* AppDelegate+..swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "AppDelegate+..swift"; sourceTree = "<group>"; };
F935B2EA2307B6BA009E4D33 /* Helper-Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = "Helper-Info.plist"; sourceTree = "<group>"; };
F935B2F12307C802009E4D33 /* Helper-Launchd.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = "Helper-Launchd.plist"; sourceTree = "<group>"; };
F935B2F22307CD32009E4D33 /* ProxyConfigHelper.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = ProxyConfigHelper.h; sourceTree = "<group>"; };
@ -243,6 +245,7 @@
499A485B22ED793C00F6C675 /* NSView+Nib.swift */,
499A485D22ED9B7C00F6C675 /* NSTableView+Reload.swift */,
4982F51E2344A216008804B0 /* Cgo+Convert.swift */,
F9203A25236342820020D57D /* AppDelegate+..swift */,
);
path = Extensions;
sourceTree = "<group>";
@ -681,6 +684,7 @@
49722FEF211F338B00650A41 /* FileEvent.swift in Sources */,
49D176A72355FE680093DD7B /* NetworkChangeNotifier.swift in Sources */,
4913C82321157D0200F6B87C /* Notification.swift in Sources */,
F9203A26236342820020D57D /* AppDelegate+..swift in Sources */,
499A485C22ED793C00F6C675 /* NSView+Nib.swift in Sources */,
492C4871210EF62E004554A0 /* ClashConfig.swift in Sources */,
492C4869210EE6B9004554A0 /* ApiRequest.swift in Sources */,

View File

@ -104,13 +104,6 @@ class AppDelegate: NSObject, NSApplicationDelegate {
func setupData() {
remoteConfigAutoupdateMenuItem.state = RemoteConfigManager.autoUpdateEnable ? .on : .off
NotificationCenter.default.rx.notification(kShouldUpDateConfig).bind {
[weak self] note in
guard let self = self else { return }
let showNotice = note.userInfo?["notification"] as? Bool ?? true
self.updateConfig(showNotification: showNotice)
}.disposed(by: disposeBag)
ConfigManager.shared
.showNetSpeedIndicatorObservable
.bind { [weak self] show in
@ -300,11 +293,13 @@ class AppDelegate: NSObject, NSApplicationDelegate {
ApiRequest.shared.resetStreamApis()
}
func updateConfig(showNotification: Bool = true) {
func updateConfig(configName: String? = nil, showNotification: Bool = true) {
startProxy()
guard ConfigManager.shared.isRunning else { return }
ApiRequest.requestConfigUpdate {
let config = configName ?? ConfigManager.selectConfigName
ApiRequest.requestConfigUpdate(configName: config) {
[weak self] err in
guard let self = self else { return }
if let error = err {
@ -323,6 +318,10 @@ class AppDelegate: NSObject, NSApplicationDelegate {
.post(title: NSLocalizedString("Reload Config Succeed", comment: ""),
info: NSLocalizedString("Succees", comment: ""))
}
if let newConfigName = configName {
ConfigManager.selectConfigName = newConfigName
}
}
}
}

View File

@ -0,0 +1,14 @@
//
// AppDelegate+.swift
// ClashX
//
// Created by yicheng on 2019/10/25.
// Copyright © 2019 west2online. All rights reserved.
//
import AppKit
extension AppDelegate {
static var shared: AppDelegate {
return NSApplication.shared.delegate as! AppDelegate
}
}

View File

@ -60,7 +60,7 @@ class UserNotificationCenterDelegate: NSObject, NSUserNotificationCenterDelegate
func userNotificationCenter(_ center: NSUserNotificationCenter, didActivate notification: NSUserNotification) {
switch notification.userInfo?["identifier"] as? String {
case "postConfigFileChangeDetectionNotice":
NotificationCenter.default.post(Notification(name: kShouldUpDateConfig))
AppDelegate.shared.updateConfig()
center.removeAllDeliveredNotifications()
default:
break

View File

@ -97,17 +97,9 @@ class ApiRequest {
completeHandler(config)
}
static func requestConfigUpdate(callback: @escaping ((ErrorString?) -> Void)) {
let filePath = "\(kConfigFolderPath)\(ConfigManager.selectConfigName).yaml"
static func requestConfigUpdate(configName: String, callback: @escaping ((ErrorString?) -> Void)) {
let filePath = "\(kConfigFolderPath)\(configName).yaml"
let placeHolderErrorDesp = "Error occoured, Please try to fix it by restarting ClashX. "
let errorHanlder: (ErrorString) -> Void = {
err in
if err.contains("no such file or directory") {
ConfigManager.selectConfigName = "config"
} else {
callback(err)
}
}
// DEV MODE: Use API
if !ConfigManager.builtInApiMode {
@ -118,7 +110,7 @@ class ApiRequest {
} else {
let errorJson = try? res.result.get()
let err = JSON(errorJson ?? "")["message"].string ?? placeHolderErrorDesp
errorHanlder(err)
callback(err)
}
}
return
@ -129,7 +121,7 @@ class ApiRequest {
if res == "success" {
callback(nil)
} else {
errorHanlder(res)
callback(res)
}
}

View File

@ -192,10 +192,7 @@ extension MenuItemFactory {
@objc static func actionSelectConfig(sender: NSMenuItem) {
let config = sender.title
ConfigManager.selectConfigName = config
NotificationCenter.default.post(name: kShouldUpDateConfig,
object: nil,
userInfo: ["notification": false])
AppDelegate.shared.updateConfig(configName: config, showNotification: false)
}
@objc static func empty() {}

View File

@ -125,9 +125,7 @@ class RemoteConfigManager {
NSUserNotificationCenter.default
.post(title: NSLocalizedString("Remote Config Update", comment: ""), info: info)
}
NotificationCenter.default.post(name: kShouldUpDateConfig,
object: nil,
userInfo: ["notification": false])
AppDelegate.shared.updateConfig(showNotification: false)
}
}
Logger.log("[Auto Upgrade] Finish \(config.name) result: \(error ?? "succeed")")

View File

@ -7,7 +7,6 @@
//
import Foundation
let kShouldUpDateConfig = Notification.Name("kShouldUpDateConfig")
let kConfigFileChange = Notification.Name("kConfigFileChange")
let kSystemNetworkStatusDidChange = Notification.Name("kSystemNetworkStatusDidChange")
let kSpeedTestFinishForProxy = Notification.Name("kSpeedTestFinishForProxy")

View File

@ -134,10 +134,9 @@ extension RemoteConfigViewController {
RemoteConfigManager.shared.saveConfigs()
if config.name == self.latestAddedConfigName {
ConfigManager.selectConfigName = config.name
}
if config.name == ConfigManager.selectConfigName {
NotificationCenter.default.post(Notification(name: kShouldUpDateConfig))
AppDelegate.shared.updateConfig(configName: config.name)
} else if config.name == ConfigManager.selectConfigName {
AppDelegate.shared.updateConfig()
}
}
self.tableView.reloadDataKeepingSelection()