Skip to content

Commit 86e3534

Browse files
committed
Feature: remember select proxy group last time
1 parent 2f5421d commit 86e3534

File tree

4 files changed

+29
-14
lines changed

4 files changed

+29
-14
lines changed

ClashX/AppDelegate.swift

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,10 @@ class AppDelegate: NSObject, NSApplicationDelegate {
102102
}
103103

104104
self.updateProxyList()
105+
106+
if (ConfigManager.shared.proxyPortAutoSet) {
107+
_ = ProxyConfigManager.setUpSystemProxy(port: config!.port,socksPort: config!.socketPort)
108+
}
105109
}.disposed(by: disposeBag)
106110

107111
LaunchAtLogin.shared
@@ -136,6 +140,16 @@ class AppDelegate: NSObject, NSApplicationDelegate {
136140
});
137141
}
138142

143+
func selectProxyGroupWithMemory(){
144+
for item in ConfigManager.selectedProxyMap {
145+
ApiRequest.updateProxyGroup(group: item.key, selectProxy: item.value) { (success) in
146+
if (!success) {
147+
ConfigManager.selectedProxyMap[item.key] = nil
148+
}
149+
}
150+
}
151+
}
152+
139153
func updateProxyList() {
140154
ProxyMenuItemFactory.menuItems { [unowned self] (menus) in
141155
let startIndex = self.statusMenu.items.index(of: self.separatorLineTop)! + 1
@@ -159,6 +173,7 @@ class AppDelegate: NSObject, NSApplicationDelegate {
159173
run()
160174
}
161175
syncConfig(){
176+
self.selectProxyGroupWithMemory()
162177
DispatchQueue.main.asyncAfter(deadline:DispatchTime.now() + 1, execute: {
163178
self.resetTrafficMonitor()
164179
})
@@ -169,12 +184,7 @@ class AppDelegate: NSObject, NSApplicationDelegate {
169184
ApiRequest.requestConfig{ (config) in
170185
guard config.port > 0 else {return}
171186
ConfigManager.shared.currentConfig = config
172-
173-
if ConfigManager.shared.proxyPortAutoSet {
174-
_ = ProxyConfigManager.setUpSystemProxy(port: config.port,socksPort: config.socketPort)
175-
self.updateProxyList()
176-
completeHandler?()
177-
}
187+
completeHandler?()
178188
}
179189
}
180190

ClashX/General/ApiRequest.swift

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,7 @@ class ApiRequest{
7575

7676
static func updateProxyGroup(group:String,selectProxy:String,callback:@escaping ((Bool)->())) {
7777
request(ConfigManager.apiUrl + "/proxies/\(group)", method: .put, parameters: ["name":selectProxy], encoding: JSONEncoding.default).responseJSON { (response) in
78-
switch response.result {
79-
case .success(_):
80-
callback(true)
81-
case .failure(_):
82-
callback(false)
83-
}
78+
callback(response.response?.statusCode == 200)
8479
}
8580
}
8681
}

ClashX/General/ProxyMenuItemFactory.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,10 @@ class ProxyMenuItemFactory {
7676
items.state = .off
7777
}
7878
sender.state = .on
79+
// remember select proxy
80+
ConfigManager.selectedProxyMap[proxyGroup] = proxyName
7981
}
8082
}
81-
82-
8383
}
8484

8585

ClashX/Managers/ConfigManager.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,14 @@ class ConfigManager {
5151
}
5252
}
5353

54+
static var selectedProxyMap:[String:String] {
55+
get{
56+
return ["Proxy":"ProxyAuto"]
57+
return UserDefaults.standard.dictionary(forKey: "selectedProxyMap") as? [String:String] ?? ["Proxy":"ProxyAuto"]
58+
}
59+
set {
60+
UserDefaults.standard.set(newValue, forKey: "selectedProxyMap")
61+
}
62+
}
63+
5464
}

0 commit comments

Comments
 (0)