From b7586da98953e572469bd6cb20872d3937f24d78 Mon Sep 17 00:00:00 2001 From: clowwindy Date: Tue, 4 Nov 2014 00:27:12 +0800 Subject: [PATCH] fix profiles --- AppProxyCap | 2 +- ShadowsocksX/ProfileManager.m | 6 ++++++ ShadowsocksX/SWBConfigWindowController.m | 24 ++++++++++++++---------- 3 files changed, 21 insertions(+), 11 deletions(-) diff --git a/AppProxyCap b/AppProxyCap index ca20ddd5..8ffade26 160000 --- a/AppProxyCap +++ b/AppProxyCap @@ -1 +1 @@ -Subproject commit ca20ddd568c0e4088465333c7e91497887e7cd97 +Subproject commit 8ffade26683001fb0f2ec575d74aa65b5b03bfe2 diff --git a/ShadowsocksX/ProfileManager.m b/ShadowsocksX/ProfileManager.m index 9a27c666..0afa5d50 100644 --- a/ShadowsocksX/ProfileManager.m +++ b/ShadowsocksX/ProfileManager.m @@ -45,6 +45,12 @@ + (Configuration *)configuration { } + (void)saveConfiguration:(Configuration *)configuration { + if (configuration.profiles.count == 0) { + configuration.current = -1; + } + if (configuration.current != -1 && configuration.current >= configuration.profiles.count) { + configuration.current = 0; + } [[NSUserDefaults standardUserDefaults] setObject:[configuration JSONData] forKey:CONFIG_DATA_KEY]; [ProfileManager reloadShadowsocksRunner]; } diff --git a/ShadowsocksX/SWBConfigWindowController.m b/ShadowsocksX/SWBConfigWindowController.m index 05652abc..2f64721b 100644 --- a/ShadowsocksX/SWBConfigWindowController.m +++ b/ShadowsocksX/SWBConfigWindowController.m @@ -58,18 +58,16 @@ - (BOOL)tableView:(NSTableView *)tableView shouldSelectRow:(NSInteger)row { return YES; } if (row >= 0 && row < configuration.profiles.count) { - return [self validateCurrentProfile]; + if ([self validateCurrentProfile]) { + [self saveCurrentProfile]; + } else { + return NO; + } } // always allow selection to no selection return YES; } -- (void)tableViewSelectionIsChanging:(NSNotification *)notification { - if (self.tableView.selectedRow >= 0) { - [self saveCurrentProfile]; - } -} - - (void)tableViewSelectionDidChange:(NSNotification *)notification { if (self.tableView.selectedRow >= 0) { [self loadCurrentProfile]; @@ -116,9 +114,14 @@ - (IBAction)remove:(id)sender { [((NSMutableArray *) configuration.profiles) removeObjectAtIndex:selection]; [self.tableView reloadData]; [self updateSettingsBoxVisible:self]; - } - if (configuration.profiles.count > 0) { - [self.tableView selectRowIndexes:[NSIndexSet indexSetWithIndex:(configuration.profiles.count - 1)] byExtendingSelection:NO]; + if (configuration.profiles.count > 0) { + [self.tableView selectRowIndexes:[NSIndexSet indexSetWithIndex:(configuration.profiles.count - 1)] byExtendingSelection:NO]; + } + [self loadCurrentProfile]; + if (configuration.current > selection) { + // select the original profile + configuration.current = configuration.current - 1; + } } } @@ -196,6 +199,7 @@ - (IBAction)OK:(id)sender { if ([self saveCurrentProfile]) { [self saveSettings]; [ShadowsocksRunner reloadConfig]; + [self.delegate configurationDidChange]; [self.window performClose:self]; } else { [self shakeWindow];