Skip to content

Commit

Permalink
Revert recovery procedure, prevent sibling loss
Browse files Browse the repository at this point in the history
I experienced that the recovery procedure led to an incoherent tree,
so the best workaround remains switching back to alphabetical auto sort
by reverting commit e3751a9. However, reselect user's choice after that.

As mail exchanges with user @liamgreenwood around issue ViennaRSS#1768 hinted
that "loss" of a sibling is the likely origin of -loadTree:rootNode's
failure, try to find it and reattach it as soon as it is reported missing.
  • Loading branch information
barijaona committed Aug 6, 2024
1 parent 24ee441 commit e1584f8
Showing 1 changed file with 26 additions and 12 deletions.
38 changes: 26 additions & 12 deletions Vienna/Sources/Main window/FoldersTree.m
Original file line number Diff line number Diff line change
Expand Up @@ -156,16 +156,14 @@ -(void)reloadDatabase:(NSArray *)stateArray
{
[self.rootNode removeChildren];
if (![self loadTree:[[Database sharedManager] arrayOfFolders:VNAFolderTypeRoot] rootNode:self.rootNode]) {
// recover from problems by putting missing folders under root node
NSArray *allFolders = [[Database sharedManager] arrayOfAllFolders]; // all RSS and group folders
NSArray *installedFolders = [self folders:0]; // RSS folders already present
for (Folder *folder in allFolders) {
if ((folder.type == VNAFolderTypeRSS || folder.type == VNAFolderTypeOpenReader)
&& ![installedFolders containsObject:folder])
{
(void)[[TreeNode alloc] init:self.rootNode atIndex:-1 folder:folder canHaveChildren:NO];
}
}
// recover from problems by switching back to alphabetical auto sort of folders…
Preferences *prefs = [Preferences standardPreferences];
NSInteger selectedSortMethod = prefs.foldersTreeSortMethod;
prefs.foldersTreeSortMethod = VNAFolderSortByName;
[self.rootNode removeChildren];
[self loadTree:[[Database sharedManager] arrayOfFolders:VNAFolderTypeRoot] rootNode:self.rootNode];
// then restore user choice regarding sort method
prefs.foldersTreeSortMethod = selectedSortMethod;
}
[self.outlineView reloadData];
[self unarchiveState:stateArray];
Expand Down Expand Up @@ -255,13 +253,28 @@ -(BOOL)loadTree:(NSArray *)listOfFolders rootNode:(TreeNode *)node
NSArray * listOfFolderIds = [listOfFolders valueForKey:@"itemId"];
NSUInteger index = 0;
NSInteger nextChildId = (node == self.rootNode) ? [Database sharedManager].firstFolderId : node.folder.firstChildId;
NSInteger predecessorId = 0;
while (nextChildId > 0) {
NSUInteger listIndex = [listOfFolderIds indexOfObject:@(nextChildId)];
if (listIndex == NSNotFound) {
NSLog(@"Cannot find child with id %ld for folder with id %ld", (long)nextChildId, (long)node.nodeId);
return NO;
folder = [[Database sharedManager] folderFromID:nextChildId];
if (!folder || folder.parentId != node.nodeId) {
return NO;
}
if (predecessorId == 0) {
if (![[Database sharedManager] setFirstChild:nextChildId forFolder:node.nodeId]) {
return NO;
}
} else {
if (![[Database sharedManager] setNextSibling:nextChildId forFolder:predecessorId]) {
return NO;
}
}
NSLog(@"Repositioned folder %@ as child of folder with id %ld", folder, (long)node.nodeId);
} else {
folder = listOfFolders[listIndex];
}
folder = listOfFolders[listIndex];
NSArray * listOfSubFolders = [[Database sharedManager] arrayOfFolders:nextChildId];
NSUInteger count = listOfSubFolders.count;
TreeNode * subNode;
Expand All @@ -272,6 +285,7 @@ -(BOOL)loadTree:(NSArray *)listOfFolders rootNode:(TreeNode *)node
return NO;
}
}
predecessorId = nextChildId;
nextChildId = folder.nextSiblingId;
++index;
}
Expand Down

0 comments on commit e1584f8

Please sign in to comment.