From d020e938070e9934aa8c463cd1902e0b02ab1d15 Mon Sep 17 00:00:00 2001 From: Maurice Parker Date: Sat, 20 Apr 2019 08:46:58 -0500 Subject: [PATCH] Make drop code work for special case of where the destination row is 0. --- iOS/Master/MasterViewController.swift | 28 +++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/iOS/Master/MasterViewController.swift b/iOS/Master/MasterViewController.swift index 4019595b4..7265ac476 100644 --- a/iOS/Master/MasterViewController.swift +++ b/iOS/Master/MasterViewController.swift @@ -324,20 +324,27 @@ class MasterViewController: UITableViewController, UndoableCommandRunner { override func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) { - let movementAdjustment = sourceIndexPath > destinationIndexPath ? 1 : 0 - let adjustedDestIndexPath = IndexPath(row: destinationIndexPath.row - movementAdjustment, section: destinationIndexPath.section) - - guard let sourceNode = nodeFor(sourceIndexPath), - let destNode = nodeFor(adjustedDestIndexPath), - let feed = sourceNode.representedObject as? Feed else { - return + guard let sourceNode = nodeFor(sourceIndexPath), let feed = sourceNode.representedObject as? Feed else { + return } - + + // Based on the drop we have to determine a node to start looking for a parent container. + let destNode: Node = { + if destinationIndexPath.row == 0 { + return treeController.rootNode.childAtIndex(destinationIndexPath.section)! + } else { + let movementAdjustment = sourceIndexPath > destinationIndexPath ? 1 : 0 + let adjustedDestIndexPath = IndexPath(row: destinationIndexPath.row - movementAdjustment, section: destinationIndexPath.section) + return nodeFor(adjustedDestIndexPath)! + } + }() + + // Now we start looking for the parent container let destParentNode: Node? = { - if destNode.representedObject is Folder { + if destNode.representedObject is Container { return destNode } else { - if destNode.parent?.representedObject is Folder { + if destNode.parent?.representedObject is Container { return destNode.parent! } else { return nil @@ -345,6 +352,7 @@ class MasterViewController: UITableViewController, UndoableCommandRunner { } }() + // Move the Feed let account = accountForNode(destNode) let sourceContainer = sourceNode.parent?.representedObject as? Container let destinationFolder = destParentNode?.representedObject as? Folder