Revision: 28568
          http://sourceforge.net/p/bibdesk/svn/28568
Author:   hofman
Date:     2024-01-07 10:09:36 +0000 (Sun, 07 Jan 2024)
Log Message:
-----------
Don't collect groups to remove to retain, instead collect groups to select 
before removing or adding a group, and pass back to restore selection

Modified Paths:
--------------
    trunk/bibdesk/BDSKGroupsArray.m
    trunk/bibdesk/BibDocument_Groups.h
    trunk/bibdesk/BibDocument_Groups.m

Modified: trunk/bibdesk/BDSKGroupsArray.m
===================================================================
--- trunk/bibdesk/BDSKGroupsArray.m     2024-01-06 23:47:01 UTC (rev 28567)
+++ trunk/bibdesk/BDSKGroupsArray.m     2024-01-07 10:09:36 UTC (rev 28568)
@@ -203,8 +203,9 @@
         return;
     if ((groupType & BDSKTempRemovableGroupType) == 0)
         [[[self undoManager] prepareWithInvocationTarget:self] 
removeChildGroup:group];
+    NSArray *selectedGroups = [[self document] selectedGroups];
     [parent addChildGroup:group];
-    [[self document] reloadParentGroup:parent];
+    [[self document] reloadParentGroup:parent withSelection:selectedGroups];
 }
 
 - (void)removeChildGroup:(BDSKGroup *)group {
@@ -216,8 +217,9 @@
     if (([group groupType] & BDSKTempRemovableGroupType) == 0)
         [[[self undoManager] prepareWithInvocationTarget:self] 
addChildGroup:group];
     [[self document] willRemoveGroups:groupsToRemove];
+    NSArray *selectedGroups = [[self document] selectedGroups];
     [parent removeChildGroup:group];
-    [[self document] reloadParentGroup:parent];
+    [[self document] reloadParentGroup:parent withSelection:selectedGroups];
     groupsToRemove = nil;
 }
 

Modified: trunk/bibdesk/BibDocument_Groups.h
===================================================================
--- trunk/bibdesk/BibDocument_Groups.h  2024-01-06 23:47:01 UTC (rev 28567)
+++ trunk/bibdesk/BibDocument_Groups.h  2024-01-07 10:09:36 UTC (rev 28568)
@@ -81,7 +81,7 @@
 - (void)handleExternalGroupUpdatedNotification:(NSNotification *)notification;
 
 - (void)willRemoveGroups:(NSArray *)groupsToRemove;
-- (void)reloadParentGroup:(BDSKParentGroup *)parentGroup;
+- (void)reloadParentGroup:(BDSKParentGroup *)parentGroup 
withSelection:(NSArray *)selectedGroups;
 
 - (BDSKGroupCellView *)viewForGroup:(BDSKGroup *)group;
 - (void)updateCountBubbleForGroup:(BDSKGroup *)group inView:(BDSKGroupCellView 
*)view;

Modified: trunk/bibdesk/BibDocument_Groups.m
===================================================================
--- trunk/bibdesk/BibDocument_Groups.m  2024-01-06 23:47:01 UTC (rev 28567)
+++ trunk/bibdesk/BibDocument_Groups.m  2024-01-07 10:09:36 UTC (rev 28568)
@@ -347,6 +347,7 @@
     NSMutableSet *clients = [[[BDSKSharingBrowser sharedBrowser] 
sharingClients] mutableCopy];
     NSMutableArray *sharedGroups = [[NSMutableArray alloc] init];
     NSMutableArray *groupsToRemove = [[NSMutableArray alloc] init];
+    NSArray *selectedGroups = [self selectedGroups];
 
     // get the groups to keep
     for (BDSKSharedGroup *group in [groups sharedGroups]) {
@@ -371,7 +372,7 @@
                                                             object:self 
userInfo:@{BDSKDocumentGroupsKey:groupsToRemove}];
     
     [[groups externalParent] setSharedGroups:sharedGroups];
-    [self reloadParentGroup:[groups externalParent]];
+    [self reloadParentGroup:[groups externalParent] 
withSelection:selectedGroups];
     
     [groupsToRemove release];
     [sharedGroups release];
@@ -415,7 +416,7 @@
 }
 
 // reload (a section of) the outlineview and preserve the selection
-- (void)reloadParentGroup:(BDSKParentGroup *)parentGroup {
+- (void)reloadParentGroup:(BDSKParentGroup *)parentGroup 
withSelection:(NSArray *)selectedGroups {
     if ([parentGroup isKindOfClass:[BDSKParentGroup class]] && 
[groupOutlineView isItemExpanded:parentGroup] == NO)
         return;
     
@@ -425,8 +426,6 @@
     
     NSPoint scrollPoint = [tableView scrollPositionAsPercentage];
     
-    NSArray *selectedGroups = [self selectedGroups];
-
     if (parentGroup == nil) {
         [groupOutlineView reloadData];
     } else if ([parentGroup isKindOfClass:[BDSKParentGroup class]]) {
@@ -567,27 +566,24 @@
     // update the count for the first item, not sure if it should be done here
     [[groups libraryGroup] setCount:[publications count]];
     
+    NSArray *selectedGroups = [self selectedGroups];
+    
     // update the count for the smart groups when we're not loading a new set 
and they're displayed
     [[groups smartGroups] 
makeObjectsPerformSelector:@selector(invalidateCount)];
     if ([groupOutlineView isItemExpanded:[groups smartParent]] && 
[sortGroupsKey isEqualToString:BDSKDocumentCountKey])
         [[groups smartParent] resort];
     
-    // retain removed groups until after reloading and restoring the selection
-    NSMutableArray *removedGroups = [NSMutableArray array];
-    
     for (BDSKCategoryParentGroup *parent in [groups categoryParents]) {
         [self willRemoveGroups:[parent children]];
-        [removedGroups addObjectsFromArray:[parent children]];
         [self rebuildCategoryGroups:parent];
     }
     
-    [self reloadParentGroup:nil];
-    
-    removedGroups = nil;
+    [self reloadParentGroup:nil withSelection:selectedGroups];
 }
 
 - (BOOL)updateCategoryGroups:(BOOL)updateCategoryGroups 
smartGroups:(BOOL)updateSmartGroups {
     NSMutableArray *parentsToReload = [NSMutableArray array];
+    NSArray *selectedGroups = [self selectedGroups];
     BOOL isSelected = NO;
     
     if (updateSmartGroups) {
@@ -604,14 +600,9 @@
         }
     }
     
-    // retain removed groups until after reloading and restoring the selection
-    NSMutableArray *removedGroups = [NSMutableArray array];
-    
     if (updateCategoryGroups) {
         isSelected = isSelected || [self 
hasGroupTypeSelected:BDSKCategoryGroupType];
         for (BDSKCategoryParentGroup *parent in [groups categoryParents]) {
-            [self willRemoveGroups:[parent children]];
-            [removedGroups addObjectsFromArray:[parent children]];
             [self rebuildCategoryGroups:parent];
             if ([groupOutlineView isItemExpanded:parent])
                 [parentsToReload addObject:parent];
@@ -620,9 +611,9 @@
     
     BOOL success = YES;
     if ([parentsToReload count] == 1) {
-        [self reloadParentGroup:[parentsToReload firstObject]];
+        [self reloadParentGroup:[parentsToReload firstObject] 
withSelection:selectedGroups];
     } else if ([parentsToReload count]) {
-        [self reloadParentGroup:(id)parentsToReload];
+        [self reloadParentGroup:(id)parentsToReload 
withSelection:selectedGroups];
     } else if (isSelected) {
         // fix for bug #1362191: after changing a checkbox that removed an 
item from a smart group, the table scrolled to the top
         NSPoint scrollPoint = [tableView scrollPositionAsPercentage];
@@ -631,7 +622,6 @@
     } else {
         success = NO;
     }
-    removedGroups = nil;
     return success;
 }
 
@@ -768,10 +758,11 @@
 
 - (void)addCurrentGroupField:(NSString *)newField {
     if ([[self currentGroupFields] containsObject:newField] == NO) {
+        NSArray *selectedGroups = [self selectedGroups];
         BDSKCategoryParentGroup *group = [[[BDSKCategoryParentGroup alloc] 
initWithKey:newField] autorelease];
         [self rebuildCategoryGroups:group];
         [groups addCategoryParent:group];
-        [self reloadParentGroup:nil];
+        [self reloadParentGroup:nil withSelection:selectedGroups];
         // this is to set the sort descriptors and sorts the children
         [groupOutlineView expandItem:group];
         [[NSUserDefaults standardUserDefaults] setObject:[self 
currentGroupFields] forKey:BDSKCurrentGroupFieldsKey];
@@ -779,34 +770,26 @@
 }
 
 - (void)removeCurrentGroupField:(NSString *)oldField {
-    // retain removed groups until after reloading and restoring the selection
-    NSMutableArray *removedGroups = [NSMutableArray array];
+    NSArray *selectedGroups = [self selectedGroups];
     for (BDSKCategoryParentGroup *group in [groups categoryParents]) {
         if ([[group key] isEqualToString:oldField]) {
-            [self willRemoveGroups:[group children]];
-            [removedGroups addObjectsFromArray:[group children]];
-            [removedGroups addObject:group];
             [groups removeCategoryParent:group];
-            [self reloadParentGroup:nil];
+            [self reloadParentGroup:nil withSelection:selectedGroups];
             [[NSUserDefaults standardUserDefaults] setObject:[self 
currentGroupFields] forKey:BDSKCurrentGroupFieldsKey];
             break;
         }
     }
-    removedGroups = nil;
 }
 
 - (void)setCurrentGroupField:(NSString *)newField 
forGroup:(BDSKCategoryParentGroup *)group {
-    // retain removed groups until after reloading and restoring the selection
-    NSMutableArray *removedGroups = [NSMutableArray array];
+    NSArray *selectedGroups = [self selectedGroups];
     if ([[self currentGroupFields] containsObject:newField] == NO) {
         [group setKey:newField];
         [self willRemoveGroups:[group children]];
-        [removedGroups addObjectsFromArray:[group children]];
         [self rebuildCategoryGroups:group];
-        [self reloadParentGroup:group];
+        [self reloadParentGroup:group withSelection:selectedGroups];
         [[NSUserDefaults standardUserDefaults] setObject:[self 
currentGroupFields] forKey:BDSKCurrentGroupFieldsKey];
     }
-    removedGroups = nil;
 }
 
 #pragma mark Actions
@@ -1270,11 +1253,13 @@
     if ([self hasGroupTypeSelected:BDSKExternalGroupType])
         pubs = [self mergeInPublications:pubs];
     
+    NSArray *selectedGroups = [self selectedGroups];
+    
     [self addPublications:pubs toGroup:group];
     [groupOutlineView deselectAll:nil];
     
     [self rebuildCategoryGroups:parent];
-    [self reloadParentGroup:parent];
+    [self reloadParentGroup:parent withSelection:selectedGroups];
     
     dispatch_async(dispatch_get_main_queue(), ^{
         [self editGroupWithoutWarning:group];
@@ -1616,12 +1601,13 @@
         [[NSUserDefaults standardUserDefaults] 
setBool:docFlags.sortGroupsDescending forKey:BDSKSortGroupsDescendingKey];    
     }
     
+    NSArray *selectedGroups = [self selectedGroups];
     NSArray *sortDescriptors = [self groupSortDescriptors];
     for (BDSKParentGroup *parent in groups) {
         if ([groupOutlineView isItemExpanded:parent])
             [parent sortUsingDescriptors:sortDescriptors];
     }
-    [self reloadParentGroup:nil];
+    [self reloadParentGroup:nil withSelection:selectedGroups];
 }
 
 static inline BOOL identicalArrays(NSArray *array1, NSArray *array2) {
@@ -1637,12 +1623,13 @@
 
 // this should only be called when the item is expanded
 - (BOOL)resortParentGroup:(BDSKParentGroup *)parentGroup {
+    NSArray *selectedGroups = [self selectedGroups];
     NSArray *children = [[parentGroup children] copy];
     [parentGroup resort];
     BOOL unchanged = identicalArrays(children, [parentGroup children]);
     [children release];
     if (unchanged == NO)
-        [self reloadParentGroup:parentGroup];
+        [self reloadParentGroup:parentGroup withSelection:selectedGroups];
     return unchanged == NO;
 }
 

This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.



_______________________________________________
Bibdesk-commit mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/bibdesk-commit

Reply via email to