Fix proxy group selection after switching configs
This commit is contained in:
parent
ddf2ab5c19
commit
3bfaa965ba
@ -56,6 +56,7 @@
|
||||
49D176AB23575BB20093DD7B /* ProxyGroupMenuItemView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 49D176AA23575BB20093DD7B /* ProxyGroupMenuItemView.swift */; };
|
||||
F915A4622366ADEF004840BE /* ClashConnection.swift in Sources */ = {isa = PBXBuildFile; fileRef = F915A4612366ADEF004840BE /* ClashConnection.swift */; };
|
||||
F9203A26236342820020D57D /* AppDelegate+..swift in Sources */ = {isa = PBXBuildFile; fileRef = F9203A25236342820020D57D /* AppDelegate+..swift */; };
|
||||
F92D0B24236BC12000575E15 /* SavedProxyModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = F92D0B23236BC12000575E15 /* SavedProxyModel.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 */; };
|
||||
@ -162,6 +163,7 @@
|
||||
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>"; };
|
||||
F915A4612366ADEF004840BE /* ClashConnection.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ClashConnection.swift; sourceTree = "<group>"; };
|
||||
F9203A25236342820020D57D /* AppDelegate+..swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "AppDelegate+..swift"; sourceTree = "<group>"; };
|
||||
F92D0B23236BC12000575E15 /* SavedProxyModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SavedProxyModel.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>"; };
|
||||
@ -262,6 +264,7 @@
|
||||
49C9EF63223E78F5005D8B6A /* ClashProxy.swift */,
|
||||
499A485722ED715200F6C675 /* RemoteConfigModel.swift */,
|
||||
F915A4612366ADEF004840BE /* ClashConnection.swift */,
|
||||
F92D0B23236BC12000575E15 /* SavedProxyModel.swift */,
|
||||
);
|
||||
path = Models;
|
||||
sourceTree = "<group>";
|
||||
@ -620,6 +623,7 @@
|
||||
49722FF1211F338B00650A41 /* Witness.swift in Sources */,
|
||||
49722FF0211F338B00650A41 /* EventStream.swift in Sources */,
|
||||
499A486522EEA3FD00F6C675 /* Array+Safe.swift in Sources */,
|
||||
F92D0B24236BC12000575E15 /* SavedProxyModel.swift in Sources */,
|
||||
4952C3BF2115C7CA004A4FA8 /* MenuItemFactory.swift in Sources */,
|
||||
F977FAAC2366790500C17F1F /* AutoUpgardeManager.swift in Sources */,
|
||||
499A485822ED715200F6C675 /* RemoteConfigModel.swift in Sources */,
|
||||
|
@ -74,6 +74,9 @@ class AppDelegate: NSObject, NSApplicationDelegate {
|
||||
ConfigFileManager.copySampleConfigIfNeed()
|
||||
|
||||
PFMoveToApplicationsFolderIfNecessary()
|
||||
|
||||
// claer not existed selected model
|
||||
removeUnExistProxyGroups()
|
||||
|
||||
// start proxy
|
||||
setupData()
|
||||
@ -315,7 +318,6 @@ class AppDelegate: NSObject, NSApplicationDelegate {
|
||||
} else {
|
||||
self.syncConfig()
|
||||
self.resetStreamApi()
|
||||
self.selectProxyGroupWithMemory()
|
||||
self.selectOutBoundModeWithMenory()
|
||||
self.selectAllowLanWithMenory()
|
||||
ConfigFileManager.checkFinalRuleAndShowAlert()
|
||||
@ -328,6 +330,7 @@ class AppDelegate: NSObject, NSApplicationDelegate {
|
||||
if let newConfigName = configName {
|
||||
ConfigManager.selectConfigName = newConfigName
|
||||
}
|
||||
self.selectProxyGroupWithMemory()
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -559,14 +562,28 @@ extension AppDelegate {
|
||||
|
||||
extension AppDelegate {
|
||||
func selectProxyGroupWithMemory() {
|
||||
for item in ConfigManager.selectedProxyMap {
|
||||
ApiRequest.updateProxyGroup(group: item.key, selectProxy: item.value) { success in
|
||||
let copy = [SavedProxyModel](ConfigManager.selectedProxyMap)
|
||||
for item in copy {
|
||||
guard item.config == ConfigManager.selectConfigName else {continue}
|
||||
ApiRequest.updateProxyGroup(group: item.group, selectProxy: item.selected) { success in
|
||||
if !success {
|
||||
ConfigManager.selectedProxyMap[item.key] = nil
|
||||
ConfigManager.selectedProxyMap.removeAll { model -> Bool in
|
||||
return model == item
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func removeUnExistProxyGroups() {
|
||||
let list = ConfigManager.getConfigFilesList()
|
||||
let unexists = ConfigManager.selectedProxyMap.filter {
|
||||
!list.contains($0.config)
|
||||
}
|
||||
ConfigManager.selectedProxyMap.removeAll {
|
||||
unexists.contains($0)
|
||||
}
|
||||
}
|
||||
|
||||
func selectOutBoundModeWithMenory() {
|
||||
ApiRequest.updateOutBoundMode(mode: ConfigManager.selectOutBoundMode) {
|
||||
|
@ -102,16 +102,12 @@ class ConfigManager {
|
||||
return "ws://127.0.0.1:\(shared.apiPort)"
|
||||
}
|
||||
|
||||
static var selectedProxyMap: [String: String] {
|
||||
get {
|
||||
let map = UserDefaults.standard.dictionary(forKey: "selectedProxyMap") as? [String: String] ?? ["Proxy": "ProxyAuto"]
|
||||
return map.count == 0 ? ["Proxy": "ProxyAuto"] : map
|
||||
}
|
||||
set {
|
||||
UserDefaults.standard.set(newValue, forKey: "selectedProxyMap")
|
||||
static var selectedProxyMap = SavedProxyModel.loadsFromUserDefault() {
|
||||
didSet {
|
||||
SavedProxyModel.save(selectedProxyMap)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static var selectOutBoundMode: ClashProxyMode {
|
||||
get {
|
||||
return ClashProxyMode(rawValue: UserDefaults.standard.string(forKey: "selectOutBoundMode") ?? "") ?? .rule
|
||||
|
@ -185,7 +185,11 @@ extension MenuItemFactory {
|
||||
}
|
||||
sender.state = .on
|
||||
// remember select proxy
|
||||
ConfigManager.selectedProxyMap[proxyGroup] = proxyName
|
||||
let newModel = SavedProxyModel(group: proxyGroup, selected: proxyName, config: ConfigManager.selectConfigName)
|
||||
ConfigManager.selectedProxyMap.removeAll { model -> Bool in
|
||||
return model == newModel
|
||||
}
|
||||
ConfigManager.selectedProxyMap.append(newModel)
|
||||
// terminal Connections for this group
|
||||
ConnectionManager.closeConnection(for: proxyGroup)
|
||||
}
|
||||
|
52
ClashX/Models/SavedProxyModel.swift
Normal file
52
ClashX/Models/SavedProxyModel.swift
Normal file
@ -0,0 +1,52 @@
|
||||
//
|
||||
// SavedProxyModel.swift
|
||||
// ClashX
|
||||
//
|
||||
// Created by yicheng on 2019/11/1.
|
||||
// Copyright © 2019 west2online. All rights reserved.
|
||||
//
|
||||
|
||||
import Cocoa
|
||||
|
||||
struct SavedProxyModel: Codable {
|
||||
let group: ClashProxyName
|
||||
let selected: ClashProxyName
|
||||
let config: String
|
||||
|
||||
static let key = "SavedProxyModels"
|
||||
|
||||
static func migrate() -> [SavedProxyModel]? {
|
||||
if let mapping = UserDefaults.standard.dictionary(forKey: "selectedProxyMap") as? [ClashProxyName: ClashProxyName] {
|
||||
var models = [SavedProxyModel]()
|
||||
for (group, selected) in mapping {
|
||||
models.append(SavedProxyModel(group: group, selected: selected, config: ConfigManager.selectConfigName))
|
||||
}
|
||||
UserDefaults.standard.removeObject(forKey: "selectedProxyMap")
|
||||
return models
|
||||
}
|
||||
return nil
|
||||
|
||||
}
|
||||
|
||||
static func loadsFromUserDefault() -> [SavedProxyModel] {
|
||||
if let data = UserDefaults.standard.object(forKey: key) as? Data,
|
||||
let models = try? JSONDecoder().decode([SavedProxyModel].self, from: data) {
|
||||
return models
|
||||
}
|
||||
if let models = migrate() {
|
||||
save(models)
|
||||
return models
|
||||
}
|
||||
return []
|
||||
}
|
||||
|
||||
static func save(_ models: [SavedProxyModel]) {
|
||||
if let data = try? JSONEncoder().encode(models) {
|
||||
UserDefaults.standard.set(data, forKey: key)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extension SavedProxyModel: Equatable {
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user