diff --git a/ShadowsocksX-NG/AppDelegate.swift b/ShadowsocksX-NG/AppDelegate.swift index 9c994d3..e131de8 100644 --- a/ShadowsocksX-NG/AppDelegate.swift +++ b/ShadowsocksX-NG/AppDelegate.swift @@ -118,7 +118,7 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSUserNotificationCenterDele , using: { (note) in let profileMgr = ServerProfileManager.instance - if profileMgr.activeProfileId == nil && + if profileMgr.getActiveProfileId() == "" && profileMgr.profiles.count > 0{ if profileMgr.profiles[0].isValid(){ profileMgr.setActiveProfiledId(profileMgr.profiles[0].uuid) @@ -480,7 +480,7 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSUserNotificationCenterDele let index = sender.tag let spMgr = ServerProfileManager.instance let newProfile = spMgr.profiles[index] - if newProfile.uuid != spMgr.activeProfileId { + if newProfile.uuid != spMgr.getActiveProfileId() { spMgr.setActiveProfiledId(newProfile.uuid) updateServersMenu() SyncSSLocal() @@ -542,7 +542,7 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSUserNotificationCenterDele let mgr = ServerProfileManager.instance for p in mgr.profiles { - if mgr.activeProfileId == p.uuid { + if mgr.getActiveProfileId() == p.uuid { if !p.remark.isEmpty { serverMenuText = p.remark } else { @@ -671,7 +671,7 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSUserNotificationCenterDele item.title += " - failed" } } - if mgr.activeProfileId == p.uuid { + if mgr.getActiveProfileId() == p.uuid { item.state = 1 } if !p.isValid() { @@ -687,7 +687,7 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSUserNotificationCenterDele groupSubmenuItem.title = p.ssrGroup serversMenuItem.submenu?.addItem(groupSubmenuItem) serversMenuItem.submenu?.setSubmenu(groupSubmenu, for: groupSubmenuItem) - if mgr.activeProfileId == p.uuid { + if mgr.getActiveProfileId() == p.uuid { item.state = 1 groupSubmenuItem.state = 1 } @@ -696,7 +696,7 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSUserNotificationCenterDele continue } else{ - if mgr.activeProfileId == p.uuid { + if mgr.getActiveProfileId() == p.uuid { item.state = 1 serversMenuItem.submenu?.item(withTitle: p.ssrGroup)?.state = 1 } diff --git a/ShadowsocksX-NG/LaunchAgentUtils.swift b/ShadowsocksX-NG/LaunchAgentUtils.swift index 901e81f..d525189 100644 --- a/ShadowsocksX-NG/LaunchAgentUtils.swift +++ b/ShadowsocksX-NG/LaunchAgentUtils.swift @@ -168,7 +168,7 @@ func SyncSSLocal() { var changed: Bool = false changed = changed || generateSSLocalLauchAgentPlist() let mgr = ServerProfileManager.instance - if mgr.activeProfileId != nil { + if mgr.getActiveProfileId() != "" { if mgr.getActiveProfile() != nil { changed = changed || writeSSLocalConfFile((mgr.getActiveProfile()?.toJsonConfig())!) } @@ -315,7 +315,7 @@ func SyncPrivoxy() { var changed: Bool = false changed = changed || generatePrivoxyLauchAgentPlist() let mgr = ServerProfileManager.instance - if mgr.activeProfileId != nil { + if mgr.getActiveProfileId() != "" { changed = changed || writePrivoxyConfFile() let on = UserDefaults.standard.bool(forKey: "LocalHTTPOn") diff --git a/ShadowsocksX-NG/PreferencesWindowController.swift b/ShadowsocksX-NG/PreferencesWindowController.swift index 502609e..09a7fbb 100644 --- a/ShadowsocksX-NG/PreferencesWindowController.swift +++ b/ShadowsocksX-NG/PreferencesWindowController.swift @@ -260,7 +260,7 @@ class PreferencesWindowController: NSWindowController func getDataAtRow(_ index:Int) -> (String, Bool) { let profile = profileMgr.profiles[index] - let isActive = (profileMgr.activeProfileId == profile.uuid) + let isActive = (profileMgr.getActiveProfileId() == profile.uuid) if !profile.remark.isEmpty { return (profile.remark, isActive) } else { diff --git a/ShadowsocksX-NG/ServerProfileManager.swift b/ShadowsocksX-NG/ServerProfileManager.swift index 9e5ab41..235ce7d 100644 --- a/ShadowsocksX-NG/ServerProfileManager.swift +++ b/ShadowsocksX-NG/ServerProfileManager.swift @@ -19,16 +19,15 @@ class ServerProfileManager: NSObject { profiles = [ServerProfile]() let defaults = UserDefaults.standard + activeProfileId = defaults.string(forKey: "ActiveServerProfileId") + var didFindActiveProfileId = false if let _profiles = defaults.array(forKey: "ServerProfiles") { for _profile in _profiles { let profile = ServerProfile.fromDictionary(_profile as! [String : AnyObject]) profiles.append(profile) - } - } - activeProfileId = defaults.string(forKey: "ActiveServerProfileId") - for p in profiles { - if p.uuid == activeProfileId { - return + if profile.uuid == activeProfileId { + didFindActiveProfileId = true + } } } if profiles.count == 0{ @@ -38,7 +37,9 @@ class ServerProfileManager: NSObject { NSUserNotificationCenter.default.deliver(notice) return } - activeProfileId = profiles[0].uuid + if !didFindActiveProfileId { + activeProfileId = profiles[0].uuid + } } func setActiveProfiledId(_ id: String) { @@ -47,6 +48,16 @@ class ServerProfileManager: NSObject { defaults.set(id, forKey: "ActiveServerProfileId") } + func getActiveProfileId() -> String { + for p in profiles { + if p.uuid == activeProfileId { + return activeProfileId! + } + } + if profiles.count == 0 {return ""} + return profiles[0].uuid + } + func save() { let defaults = UserDefaults.standard var _profiles = [AnyObject]() diff --git a/ShadowsocksX-NG/Subscribe.swift b/ShadowsocksX-NG/Subscribe.swift index 7320f69..0fa81f4 100644 --- a/ShadowsocksX-NG/Subscribe.swift +++ b/ShadowsocksX-NG/Subscribe.swift @@ -159,6 +159,7 @@ class Subscribe: NSObject{ self.profileMgr.save() pushNotification(title: "成功更新订阅", subtitle: "", info: "更新来自\(subscribeFeed)的订阅") (NSApplication.shared().delegate as! AppDelegate).updateServersMenu() + (NSApplication.shared().delegate as! AppDelegate).updateRunningModeMenu() } if (!isActive){ return }