From c9338aee82e9b42a07e93625c3235977c3dbf1b4 Mon Sep 17 00:00:00 2001 From: stuconnolly Date: Wed, 2 Feb 2011 23:10:06 +0000 Subject: Fix favorites sorting. --- Source/SPArrayAdditions.h | 1 + Source/SPCategoryAdditions.h | 1 + Source/SPConnectionController.m | 25 +++++++++++++++++---- Source/SPMutableArrayAdditions.h | 37 +++++++++++++++++++++++++++++++ Source/SPMutableArrayAdditions.m | 48 ++++++++++++++++++++++++++++++++++++++++ 5 files changed, 108 insertions(+), 4 deletions(-) create mode 100644 Source/SPMutableArrayAdditions.h create mode 100644 Source/SPMutableArrayAdditions.m (limited to 'Source') diff --git a/Source/SPArrayAdditions.h b/Source/SPArrayAdditions.h index 206f15f8..5e22ac42 100644 --- a/Source/SPArrayAdditions.h +++ b/Source/SPArrayAdditions.h @@ -45,6 +45,7 @@ static inline void NSMutableArrayReplaceObject(NSArray *self, CFIndex idx, id an - (NSString *)componentsJoinedByPeriodAndBacktickQuoted; - (NSString *)componentsJoinedByPeriodAndBacktickQuotedAndIgnoreFirst; - (NSString *)componentsJoinedAsCSV; + - (NSArray *)subarrayWithIndexes:(NSIndexSet *)indexes; @end diff --git a/Source/SPCategoryAdditions.h b/Source/SPCategoryAdditions.h index 347dd957..daed3536 100644 --- a/Source/SPCategoryAdditions.h +++ b/Source/SPCategoryAdditions.h @@ -30,6 +30,7 @@ */ #import "SPArrayAdditions.h" +#import "SPMutableArrayAdditions.h" #import "SPStringAdditions.h" #import "SPTextViewAdditions.h" #import "SPWindowAdditions.h" diff --git a/Source/SPConnectionController.m b/Source/SPConnectionController.m index 9f542cec..8424b427 100644 --- a/Source/SPConnectionController.m +++ b/Source/SPConnectionController.m @@ -1193,6 +1193,9 @@ static NSComparisonResult compareFavoritesUsingKey(id favorite1, id favorite2, v { NSMutableArray *nodes = [[node mutableChildNodes] mutableCopy]; + // If this node only has one child and it's not another group node, don't bother proceeding + if (([nodes count] == 1) && (![[nodes objectAtIndex:0] isGroup])) return; + for (SPTreeNode *node in nodes) { if ([node isGroup]) { @@ -1200,20 +1203,34 @@ static NSComparisonResult compareFavoritesUsingKey(id favorite1, id favorite2, v } } + NSMutableIndexSet *indexes = [[NSMutableIndexSet alloc] init]; NSMutableArray *groupNodes = [[NSMutableArray alloc] init]; - for (node in nodes) + for (SPTreeNode *innerNode in nodes) { - if ([node isGroup]) { - [groupNodes addObject:node]; - [nodes removeObject:node]; + if ([innerNode isGroup]) { + [groupNodes addObject:innerNode]; + [indexes addIndex:[nodes indexOfObject:innerNode]]; } } + NSUInteger i = [indexes firstIndex]; + + while (i != NSNotFound) + { + [nodes removeObjectAtIndex:i]; + + i = [indexes indexGreaterThanIndex:i]; + } + + [indexes release]; + [nodes sortUsingFunction:compareFavoritesUsingKey context:key]; [nodes addObjectsFromArray:groupNodes]; + if (reverseFavoritesSort) [nodes reverse]; + [[node mutableChildNodes] setArray:nodes]; [groupNodes release]; diff --git a/Source/SPMutableArrayAdditions.h b/Source/SPMutableArrayAdditions.h new file mode 100644 index 00000000..c39e931a --- /dev/null +++ b/Source/SPMutableArrayAdditions.h @@ -0,0 +1,37 @@ +// +// $Id$ +// +// SPMutableArrayAdditions.h +// sequel-pro +// +// Created by Stuart Connolly (stuconnolly.com) on February 2, 2011 +// Copyright (c) 2011 Stuart Connolly. All rights reserved. +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +// +// More info at + +/** + * @category SPMutableArrayAdditionsTest SPMutableArrayAdditionsTest.h + * + * @author Stuart Connolly http://stuconnolly.com/ + * + * NSMutableArray additions category. + */ +@interface NSMutableArray (SPMutableArrayAdditions) + +- (void)reverse; + +@end diff --git a/Source/SPMutableArrayAdditions.m b/Source/SPMutableArrayAdditions.m new file mode 100644 index 00000000..529bf784 --- /dev/null +++ b/Source/SPMutableArrayAdditions.m @@ -0,0 +1,48 @@ +// +// $Id$ +// +// SPMutableArrayAdditions.m +// sequel-pro +// +// Created by Stuart Connolly (stuconnolly.com) on February 2, 2011 +// Copyright (c) 2011 Stuart Connolly. All rights reserved. +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +// +// More info at + +#import "SPMutableArrayAdditions.h" +#import "SPArrayAdditions.h" + +@implementation NSMutableArray (SPMutableArrayAdditions) + +- (void)reverse +{ + NSUInteger count = [self count]; + + for (NSUInteger i = 0; i < (count / 2); i++) + { + NSUInteger j = ((count - i) - 1); + + id obj = [NSArrayObjectAtIndex(self, i) retain]; + + [self replaceObjectAtIndex:i withObject:NSArrayObjectAtIndex(self, j)]; + [self replaceObjectAtIndex:j withObject:obj]; + + [obj release]; + } +} + +@end -- cgit v1.2.3