From 9b204d1cf31d070609e287daff4aab0d46bfeeee Mon Sep 17 00:00:00 2001 From: Qiu Yuzhou Date: Tue, 29 Oct 2019 17:32:31 +0800 Subject: [PATCH] Fixes bug #1137 --- .../PreferencesWindowController.swift | 1 + ShadowsocksX-NG/ServerProfileManager.swift | 17 ++++++++++++++--- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/ShadowsocksX-NG/PreferencesWindowController.swift b/ShadowsocksX-NG/PreferencesWindowController.swift index e1e1332..9c716b7 100644 --- a/ShadowsocksX-NG/PreferencesWindowController.swift +++ b/ShadowsocksX-NG/PreferencesWindowController.swift @@ -130,6 +130,7 @@ class PreferencesWindowController: NSWindowController } @IBAction func cancel(_ sender: NSButton) { + profileMgr.reload() window?.performClose(self) } diff --git a/ShadowsocksX-NG/ServerProfileManager.swift b/ShadowsocksX-NG/ServerProfileManager.swift index 4fd64e4..27f1a8c 100644 --- a/ShadowsocksX-NG/ServerProfileManager.swift +++ b/ShadowsocksX-NG/ServerProfileManager.swift @@ -12,12 +12,10 @@ class ServerProfileManager: NSObject { static let instance:ServerProfileManager = ServerProfileManager() - var profiles:[ServerProfile] + var profiles:[ServerProfile] = [ServerProfile]() var activeProfileId: String? fileprivate override init() { - profiles = [ServerProfile]() - let defaults = UserDefaults.standard if let _profiles = defaults.array(forKey: "ServerProfiles") { for _profile in _profiles { @@ -50,6 +48,19 @@ class ServerProfileManager: NSObject { } } + func reload() { + profiles.removeAll() + + let defaults = UserDefaults.standard + if let _profiles = defaults.array(forKey: "ServerProfiles") { + for _profile in _profiles { + let profile = ServerProfile.fromDictionary(_profile as! [String: Any]) + profiles.append(profile) + } + } + activeProfileId = defaults.string(forKey: "ActiveServerProfileId") + } + func getActiveProfile() -> ServerProfile? { if let id = activeProfileId { for p in profiles {