From 0fbc34141ce36adbdd71fb1e6a3cedc4e6154fa0 Mon Sep 17 00:00:00 2001 From: Barijaona Ramaholimihaso Date: Tue, 6 Aug 2024 05:12:52 +0300 Subject: [PATCH] Revert recovery procedure, prevent sibling loss 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 #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. --- Vienna/Sources/Main window/FoldersTree.m | 38 ++++++++++++++++-------- 1 file changed, 26 insertions(+), 12 deletions(-) diff --git a/Vienna/Sources/Main window/FoldersTree.m b/Vienna/Sources/Main window/FoldersTree.m index 426e7a5a43..336ddcec73 100644 --- a/Vienna/Sources/Main window/FoldersTree.m +++ b/Vienna/Sources/Main window/FoldersTree.m @@ -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]; @@ -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; @@ -272,6 +285,7 @@ -(BOOL)loadTree:(NSArray *)listOfFolders rootNode:(TreeNode *)node return NO; } } + predecessorId = nextChildId; nextChildId = folder.nextSiblingId; ++index; }