From 1b96e79925fa13a36841aecb875481f688ada065 Mon Sep 17 00:00:00 2001 From: relikd Date: Mon, 8 Jul 2019 22:42:41 +0200 Subject: [PATCH] Append new items at end --- CHANGELOG.md | 1 + baRSS/Preferences/Feeds Tab/SettingsFeeds.m | 47 ++++++--------------- 2 files changed, 15 insertions(+), 33 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 11587dc..f6ba10f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,6 +22,7 @@ and this project does NOT adhere to [Semantic Versioning](https://semver.org/spe - Interface builder files replaced with code equivalent - Settings, Feeds: Single add button for feeds, groups, and separators - Refresh interval hotkeys set to: Cmd+1 … Cmd+6 +- Always append new items at the end ## [0.9.4] - 2019-04-02 diff --git a/baRSS/Preferences/Feeds Tab/SettingsFeeds.m b/baRSS/Preferences/Feeds Tab/SettingsFeeds.m index 9c04421..3bab9b9 100644 --- a/baRSS/Preferences/Feeds Tab/SettingsFeeds.m +++ b/baRSS/Preferences/Feeds Tab/SettingsFeeds.m @@ -299,44 +299,25 @@ static NSString *dragNodeType = @"baRSS-feed-drag"; }]; } -/// Insert @c FeedGroup item either after current selection or inside selected folder (if expanded) +/// Insert @c FeedGroup item at the end of the current folder (or inside if expanded) - (FeedGroup*)insertFeedGroupAtSelection:(FeedGroupType)type { + FeedGroup *selObj = self.dataStore.selectedObjects.firstObject; + NSTreeNode *selNode = self.dataStore.selectedNodes.firstObject; + // If group selected and expanded, insert into group. Else: append at end of current folder + if (![self.view.outline isItemExpanded:selNode]) { + selObj = selObj.parent; + selNode = selNode.parentNode; + } + // If no selection, append to root folder + if (!selNode) selNode = [self.dataStore arrangedObjects]; + // Insert new node + NSUInteger index = selNode.childNodes.count; FeedGroup *fg = [FeedGroup newGroup:type inContext:self.dataStore.managedObjectContext]; - NSIndexPath *pth = [self indexPathForInsertAtNode:[[self.dataStore selectedNodes] firstObject]]; - [self.dataStore insertObject:fg atArrangedObjectIndexPath:pth]; - - if (pth.length > 1) { // some subfolder and not root folder (has parent!) - NSTreeNode *parentNode = [[self.dataStore arrangedObjects] descendantNodeAtIndexPath:pth].parentNode; - fg.parent = parentNode.representedObject; - [self restoreOrderingAndIndexPathStr:parentNode]; - } else { - [self restoreOrderingAndIndexPathStr:[self.dataStore arrangedObjects]]; // .parent = nil - } + [self.dataStore insertObject:fg atArrangedObjectIndexPath:[selNode.indexPath indexPathByAddingIndex:index]]; + [fg setParent:selObj andSortIndex:(int32_t)index]; return fg; } -/** - Index path will be selected as follow: - - @b root: append at end - - @b folder (expanded): append at front - - @b else: append after item. - - @return indexPath where item will be inserted. - */ -- (NSIndexPath*)indexPathForInsertAtNode:(NSTreeNode*)node { - if (!node) { // append to root - return [NSIndexPath indexPathWithIndex:[self.dataStore arrangedObjects].childNodes.count]; // or 0 to append at front - } else if ([self.view.outline isItemExpanded:node]) { // append to group (if open) - return [node.indexPath indexPathByAddingIndex:0]; // or 'selection.childNodes.count' to append at end - } else { // append before / after selected item - NSIndexPath *pth = node.indexPath; - // remove the two lines below to insert infront of selection (instead of after selection) - NSUInteger lastIdx = [pth indexAtPosition:pth.length - 1]; - return [[pth indexPathByRemovingLastIndex] indexPathByAddingIndex:lastIdx + 1]; - } - // TODO: always append to end -} - /// Loop over all descendants and update @c sortIndex @c (FeedGroup) as well as all @c indexPath @c (Feed) - (void)restoreOrderingAndIndexPathStr:(NSTreeNode*)parent { NSArray *children = parent.childNodes;