Permalink
Browse files

Fixed 2.x sorting

  • Loading branch information...
1 parent 931a24f commit 7e9c21b0595ff0f7820374546de8966509026773 Jason Rush committed Nov 15, 2011
Showing with 40 additions and 7 deletions.
  1. +2 −0 KeePassLib/Kdb.h
  2. +34 −0 KeePassLib/Kdb.m
  3. +4 −7 MiniKeePass/GroupViewController.m
View
2 KeePassLib/Kdb.h
@@ -42,9 +42,11 @@
- (NSUInteger)addGroup:(KdbGroup*)group;
- (void)removeGroup:(KdbGroup*)group;
+- (NSUInteger)moveGroup:(KdbGroup*)group;
- (NSUInteger)addEntry:(KdbEntry*)entry;
- (void)removeEntry:(KdbEntry*)entry;
+- (NSUInteger)moveEntry:(KdbEntry*)entry;
@end
View
34 KeePassLib/Kdb.m
@@ -63,6 +63,23 @@ - (void)removeGroup:(KdbGroup*)group {
[groups removeObject:group];
}
+- (NSUInteger)moveGroup:(KdbGroup*)group {
+ // Remove the group from the list of groups
+ [groups removeObject:group];
+
+ // Get the index where the group should be inserted to maintain sorted order
+ NSUInteger index = [groups indexOfObject:group inSortedRange:NSMakeRange(0, [groups count]) options:NSBinarySearchingInsertionIndex usingComparator:^(id obj1, id obj2) {
+ NSString *string1 = ((KdbGroup*)obj1).name;
+ NSString *string2 = ((KdbGroup*)obj2).name;
+ return [string1 localizedCaseInsensitiveCompare:string2];
+ }];
+
+ // Insert the group to the list of groups
+ [groups insertObject:group atIndex:index];
+
+ return index;
+}
+
- (NSUInteger)addEntry:(KdbEntry*)entry {
entry.parent = self;
@@ -84,6 +101,23 @@ - (void)removeEntry:(KdbEntry*)entry {
[entries removeObject:entry];
}
+- (NSUInteger)moveEntry:(KdbEntry*)entry {
+ // Remove the entry from the list of entries
+ [entries removeObject:entry];
+
+ // Get the index where the entry should be inserted to maintain sorted order
+ NSUInteger index = [entries indexOfObject:entry inSortedRange:NSMakeRange(0, [entries count]) options:NSBinarySearchingInsertionIndex usingComparator:^(id obj1, id obj2) {
+ NSString *string1 = ((KdbEntry*)obj1).title;
+ NSString *string2 = ((KdbEntry*)obj2).title;
+ return [string1 localizedCaseInsensitiveCompare:string2];
+ }];
+
+ // Insert the entry to the list of entries
+ [entries insertObject:entry atIndex:index];
+
+ return index;
+}
+
- (NSString*)description {
return [NSString stringWithFormat:@"KdbGroup [image=%d, name=%@, creationTime=%@, lastModificationTime=%@, lastAccessTime=%@, expiryTime=%@]", image, name, creationTime, lastModificationTime, lastAccessTime, expiryTime];
}
View
11 MiniKeePass/GroupViewController.m
@@ -63,10 +63,9 @@ - (void)viewWillAppear:(BOOL)animated {
// Reload the cell in case the title was changed by the entry view
if (selectedIndexPath != nil) {
if (selectedIndexPath.section == ENTRIES_SECTION) {
- // Remove the Entry and reinsert it
+ // Move the entry to the correct location
KdbEntry *e = [group.entries objectAtIndex:selectedIndexPath.row];
- [group removeEntry:e];
- NSUInteger index = [group addEntry:e];
+ NSUInteger index = [group moveEntry:e];
// Move or update the row
if (index != selectedIndexPath.row) {
@@ -312,10 +311,8 @@ - (void)formViewController:(FormViewController *)controller button:(FormViewCont
appDelegate.databaseDocument.dirty = YES;
[appDelegate.databaseDocument save];
- // Remove the Group and reinsert it
- [group removeGroup:g];
- NSUInteger index = [group addGroup:g];
- [g release];
+ // Move the group to the correct location
+ NSUInteger index = [group moveGroup:g];
// Move or update the row
if (index != indexPath.row) {

0 comments on commit 7e9c21b

Please sign in to comment.