2019-11-21 03:28:50 +01:00
|
|
|
//
|
2024-02-26 17:37:15 +01:00
|
|
|
// FeedViewController+Drop.swift
|
2019-11-21 03:28:50 +01:00
|
|
|
// NetNewsWire-iOS
|
|
|
|
//
|
|
|
|
// Created by Maurice Parker on 11/20/19.
|
|
|
|
// Copyright © 2019 Ranchero Software. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import UIKit
|
|
|
|
import Account
|
2024-03-22 01:21:50 +01:00
|
|
|
import Tree
|
2024-03-21 04:54:21 +01:00
|
|
|
import Core
|
2019-11-21 03:28:50 +01:00
|
|
|
|
2024-03-04 07:51:53 +01:00
|
|
|
extension SidebarViewController: UITableViewDropDelegate {
|
2019-11-21 03:28:50 +01:00
|
|
|
|
|
|
|
func tableView(_ tableView: UITableView, canHandle session: UIDropSession) -> Bool {
|
2019-11-21 20:49:05 +01:00
|
|
|
return session.localDragSession != nil
|
2019-11-21 03:28:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func tableView(_ tableView: UITableView, dropSessionDidUpdate session: UIDropSession, withDestinationIndexPath destinationIndexPath: IndexPath?) -> UITableViewDropProposal {
|
2020-11-02 21:35:48 +01:00
|
|
|
guard let destIndexPath = destinationIndexPath, destIndexPath.section > 0, tableView.hasActiveDrag else {
|
|
|
|
return UITableViewDropProposal(operation: .forbidden)
|
|
|
|
}
|
|
|
|
|
2024-02-26 06:14:10 +01:00
|
|
|
guard let destFeed = coordinator.nodeFor(destIndexPath)?.representedObject as? SidebarItem,
|
2021-10-21 02:03:02 +02:00
|
|
|
let destAccount = destFeed.account,
|
|
|
|
let destCell = tableView.cellForRow(at: destIndexPath) else {
|
|
|
|
return UITableViewDropProposal(operation: .forbidden)
|
|
|
|
}
|
2020-11-01 02:14:50 +01:00
|
|
|
|
|
|
|
// Validate account specific behaviors...
|
|
|
|
if destAccount.behaviors.contains(.disallowFeedInMultipleFolders),
|
2021-10-21 02:03:02 +02:00
|
|
|
let sourceNode = session.localDragSession?.items.first?.localObject as? Node,
|
2024-02-26 08:12:21 +01:00
|
|
|
let sourceFeed = sourceNode.representedObject as? Feed,
|
|
|
|
sourceFeed.account?.accountID != destAccount.accountID && destAccount.hasFeed(withURL: sourceFeed.url) {
|
2020-11-01 02:14:50 +01:00
|
|
|
return UITableViewDropProposal(operation: .forbidden)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Determine the correct drop proposal
|
2021-10-21 02:03:02 +02:00
|
|
|
if destFeed is Folder {
|
2019-11-23 02:59:25 +01:00
|
|
|
if session.location(in: destCell).y >= 0 {
|
|
|
|
return UITableViewDropProposal(operation: .move, intent: .insertIntoDestinationIndexPath)
|
|
|
|
} else {
|
|
|
|
return UITableViewDropProposal(operation: .move, intent: .unspecified)
|
|
|
|
}
|
2019-11-21 20:22:33 +01:00
|
|
|
} else {
|
|
|
|
return UITableViewDropProposal(operation: .move, intent: .insertAtDestinationIndexPath)
|
2019-11-21 03:28:50 +01:00
|
|
|
}
|
2019-11-21 20:22:33 +01:00
|
|
|
|
2019-11-21 03:28:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func tableView(_ tableView: UITableView, performDropWith dropCoordinator: UITableViewDropCoordinator) {
|
|
|
|
guard let dragItem = dropCoordinator.items.first?.dragItem,
|
2021-10-21 02:03:02 +02:00
|
|
|
let dragNode = dragItem.localObject as? Node,
|
|
|
|
let source = dragNode.parent?.representedObject as? Container,
|
|
|
|
let destIndexPath = dropCoordinator.destinationIndexPath else {
|
|
|
|
return
|
|
|
|
}
|
2019-11-21 03:28:50 +01:00
|
|
|
|
2019-11-21 20:22:33 +01:00
|
|
|
let isFolderDrop: Bool = {
|
2021-10-21 02:03:02 +02:00
|
|
|
if coordinator.nodeFor(destIndexPath)?.representedObject is Folder, let propCell = tableView.cellForRow(at: destIndexPath) {
|
|
|
|
return dropCoordinator.session.location(in: propCell).y >= 0
|
2019-11-21 20:22:33 +01:00
|
|
|
}
|
|
|
|
return false
|
|
|
|
}()
|
|
|
|
|
2019-11-21 03:28:50 +01:00
|
|
|
// Based on the drop we have to determine a node to start looking for a parent container.
|
2021-10-21 02:03:02 +02:00
|
|
|
let destNode: Node? = {
|
2019-11-21 20:22:33 +01:00
|
|
|
|
2019-11-26 20:01:07 +01:00
|
|
|
if isFolderDrop {
|
2021-10-21 02:03:02 +02:00
|
|
|
return coordinator.nodeFor(destIndexPath)
|
2019-11-21 03:28:50 +01:00
|
|
|
} else {
|
2019-11-26 20:01:07 +01:00
|
|
|
if destIndexPath.row == 0 {
|
2021-10-21 02:03:02 +02:00
|
|
|
return coordinator.nodeFor(IndexPath(row: 0, section: destIndexPath.section))
|
2019-11-26 20:01:07 +01:00
|
|
|
} else if destIndexPath.row > 0 {
|
2021-10-21 02:03:02 +02:00
|
|
|
return coordinator.nodeFor(IndexPath(row: destIndexPath.row - 1, section: destIndexPath.section))
|
2019-11-21 20:22:33 +01:00
|
|
|
} else {
|
2019-11-26 20:01:07 +01:00
|
|
|
return nil
|
2019-11-21 20:22:33 +01:00
|
|
|
}
|
2019-11-21 03:28:50 +01:00
|
|
|
}
|
2019-11-26 20:01:07 +01:00
|
|
|
|
2019-11-21 03:28:50 +01:00
|
|
|
}()
|
|
|
|
|
|
|
|
// Now we start looking for the parent container
|
2020-06-16 01:03:20 +02:00
|
|
|
let destinationContainer: Container? = {
|
2021-10-21 02:03:02 +02:00
|
|
|
if let container = (destNode?.representedObject as? Container) ?? (destNode?.parent?.representedObject as? Container) {
|
|
|
|
return container
|
2019-11-21 03:28:50 +01:00
|
|
|
} else {
|
2020-11-02 21:35:48 +01:00
|
|
|
// If we got here, we are trying to drop on an empty section header. Go and find the Account for this section
|
|
|
|
return coordinator.rootNode.childAtIndex(destIndexPath.section)?.representedObject as? Account
|
2019-11-21 03:28:50 +01:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2024-02-26 08:12:21 +01:00
|
|
|
guard let destination = destinationContainer, let feed = dragNode.representedObject as? Feed else { return }
|
2019-11-21 03:28:50 +01:00
|
|
|
|
2020-06-16 01:03:20 +02:00
|
|
|
if source.account == destination.account {
|
2024-02-26 08:12:21 +01:00
|
|
|
moveFeedInAccount(feed: feed, sourceContainer: source, destinationContainer: destination)
|
2019-11-21 03:28:50 +01:00
|
|
|
} else {
|
2024-02-26 08:12:21 +01:00
|
|
|
moveFeedBetweenAccounts(feed: feed, sourceContainer: source, destinationContainer: destination)
|
2019-11-21 03:28:50 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-26 08:12:21 +01:00
|
|
|
func moveFeedInAccount(feed: Feed, sourceContainer: Container, destinationContainer: Container) {
|
2019-11-23 23:38:07 +01:00
|
|
|
guard sourceContainer !== destinationContainer else { return }
|
|
|
|
|
2019-11-21 03:28:50 +01:00
|
|
|
BatchUpdate.shared.start()
|
2024-04-03 05:17:03 +02:00
|
|
|
|
|
|
|
Task { @MainActor in
|
|
|
|
|
|
|
|
do {
|
|
|
|
try await sourceContainer.account?.moveFeed(feed, from: sourceContainer, to: destinationContainer)
|
|
|
|
} catch {
|
2019-11-21 03:28:50 +01:00
|
|
|
self.presentError(error)
|
|
|
|
}
|
2024-04-03 05:46:28 +02:00
|
|
|
|
|
|
|
BatchUpdate.shared.end()
|
2019-11-21 03:28:50 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-26 08:12:21 +01:00
|
|
|
func moveFeedBetweenAccounts(feed: Feed, sourceContainer: Container, destinationContainer: Container) {
|
2024-03-28 16:24:35 +01:00
|
|
|
|
2024-04-03 05:46:28 +02:00
|
|
|
let existingFeed = destinationContainer.account?.existingFeed(withURL: feed.url)
|
2024-03-28 16:24:35 +01:00
|
|
|
|
2024-04-03 05:46:28 +02:00
|
|
|
BatchUpdate.shared.start()
|
2024-03-28 16:24:35 +01:00
|
|
|
|
2024-04-03 05:46:28 +02:00
|
|
|
Task { @MainActor in
|
2024-03-28 16:24:35 +01:00
|
|
|
|
2024-04-03 05:46:28 +02:00
|
|
|
do {
|
|
|
|
if let existingFeed {
|
2024-03-28 16:24:35 +01:00
|
|
|
try await destinationContainer.account?.addFeed(existingFeed, to: destinationContainer)
|
2024-04-03 05:46:28 +02:00
|
|
|
} else {
|
|
|
|
_ = try await destinationContainer.account?.createFeed(url: feed.url, name: feed.editedName, container: destinationContainer, validateFeed: false)
|
2019-11-21 03:28:50 +01:00
|
|
|
}
|
2024-03-28 16:24:35 +01:00
|
|
|
|
2024-04-03 05:46:28 +02:00
|
|
|
try await sourceContainer.account?.removeFeed(feed, from: sourceContainer)
|
2024-03-28 17:28:16 +01:00
|
|
|
|
2024-04-03 05:46:28 +02:00
|
|
|
} catch {
|
|
|
|
self.presentError(error)
|
2019-11-21 03:28:50 +01:00
|
|
|
}
|
2024-04-03 05:46:28 +02:00
|
|
|
|
|
|
|
BatchUpdate.shared.end()
|
2019-11-21 03:28:50 +01:00
|
|
|
}
|
2024-04-03 05:46:28 +02:00
|
|
|
}
|
2019-11-21 03:28:50 +01:00
|
|
|
}
|