From 8fa777e37d97a005a02d61d828815fc63e230e7c Mon Sep 17 00:00:00 2001 From: marcoschmickler Date: Tue, 4 May 2021 18:39:11 +0200 Subject: [PATCH] Dragndrop --- kplayer/core/NetworkManager.swift | 10 +- kplayer/detail/DetailViewController.swift | 25 ++++- kplayer/master/MasterViewController.swift | 119 +++++++++++++++++++++- 3 files changed, 147 insertions(+), 7 deletions(-) diff --git a/kplayer/core/NetworkManager.swift b/kplayer/core/NetworkManager.swift index c5755cb..9f9a1fc 100644 --- a/kplayer/core/NetworkManager.swift +++ b/kplayer/core/NetworkManager.swift @@ -40,10 +40,6 @@ class NetworkManager { } } - func loadFavFolders(_ rootParam: String, completionHandler: @escaping Weiter) -> Void { - - } - func loadFavDirs(_ rootParam: String, completionHandler: @escaping Weiter) -> Void { var res = [MediaItem]() @@ -60,7 +56,7 @@ class NetworkManager { var path = p.substringBefore("/") - let m = MediaItem(name: fileURL.lastPathComponent, path: path, root: "1", type: ItemType.VIDEO) + let m = MediaItem(name: fileURL.lastPathComponent, path: path, root: "fav", type: ItemType.VIDEO) m.local = true res.append(m) @@ -71,6 +67,10 @@ class NetworkManager { } } + let m = MediaItem(name: "new", path: "new", root: "fav", type: ItemType.FOLDER) + m.local = true + res.append(m) + completionHandler(res) } diff --git a/kplayer/detail/DetailViewController.swift b/kplayer/detail/DetailViewController.swift index 7f2b7f9..277e310 100644 --- a/kplayer/detail/DetailViewController.swift +++ b/kplayer/detail/DetailViewController.swift @@ -17,7 +17,7 @@ protocol DetailDelegate { func saveItem(selectedItem: MediaItem) } -class DetailViewController: UIViewController, UICollectionViewDelegateFlowLayout, UICollectionViewDataSource { +class DetailViewController: UIViewController, UICollectionViewDelegateFlowLayout, UICollectionViewDataSource, UICollectionViewDragDelegate { var delegate : DetailDelegate? @IBOutlet weak var detailDescriptionLabel: UILabel! @@ -67,6 +67,8 @@ class DetailViewController: UIViewController, UICollectionViewDelegateFlowLayout collectionView.register(ItemCell.self, forCellWithReuseIdentifier: "Cell") collectionView.backgroundColor = UIColor.lightGray collectionView.register(HeaderCell.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "HeaderView"); + collectionView.dragInteractionEnabled = true + collectionView.dragDelegate = self view.addSubview(collectionView) view.autoresizesSubviews = true @@ -392,5 +394,26 @@ class DetailViewController: UIViewController, UICollectionViewDelegateFlowLayout } + func collectionView(_ collectionView: UICollectionView, itemsForBeginning session: UIDragSession, at indexPath: IndexPath) -> [UIDragItem] { + if let detail: MediaItem = self.detailItem { + var selectedItem = detail.children[indexPath.section] + + if selectedItem.children.count > indexPath.item { + selectedItem = selectedItem.children[indexPath.count] + } + + if !selectedItem.local { + return [] + } + + let itemProvider = NSItemProvider(object: selectedItem.toJSON() as NSString) + return [UIDragItem(itemProvider: itemProvider)] + } + return [] + } + + func collectionView(_ collectionView: UICollectionView, dragSessionDidEnd session: UIDragSession) { + collectionView.reloadData() + } } diff --git a/kplayer/master/MasterViewController.swift b/kplayer/master/MasterViewController.swift index 91667c9..ea25164 100644 --- a/kplayer/master/MasterViewController.swift +++ b/kplayer/master/MasterViewController.swift @@ -18,11 +18,12 @@ protocol MasterDelegate : DetailDelegate { func loadItem(selectedItem: MediaItem, completionHandler: @escaping (MediaItem) -> Void) } -class MasterViewController: UITableViewController, UISearchResultsUpdating { +class MasterViewController: UITableViewController, UISearchResultsUpdating, UITableViewDropDelegate { let searchController = UISearchController(searchResultsController: nil) let model = ItemModel() var delegate : MasterDelegate? var detailDelegate : DetailDelegate? + var detailController : DetailViewController? var authenticated = false @@ -39,6 +40,7 @@ class MasterViewController: UITableViewController, UISearchResultsUpdating { searchController.dimsBackgroundDuringPresentation = false definesPresentationContext = true tableView.tableHeaderView = searchController.searchBar + tableView.dropDelegate = self } override func didReceiveMemoryWarning() { @@ -92,6 +94,29 @@ class MasterViewController: UITableViewController, UISearchResultsUpdating { } + func createFolder(_ item: MediaItem) { + let ac = UIAlertController(title: "Enter new folder name", message: nil, preferredStyle: .alert) + ac.addTextField() + + let submitAction = UIAlertAction(title: "Submit", style: .default) { [unowned ac] _ in + let answer = ac.textFields![0].text! + + do { + try FileHelper.createDir(name: answer) + item.name = answer + item.path = answer + self.tableView.reloadData() + } catch { + print(error) + } + } + + ac.addAction(submitAction) + ac.addAction(UIAlertAction(title: "cancel", style: .cancel)) + + present(ac, animated: true) + } + /** * Item ausgewaehlt */ @@ -105,6 +130,11 @@ class MasterViewController: UITableViewController, UISearchResultsUpdating { let selectedItem = model.items[indexPath.row] + if (selectedItem.local && selectedItem.name == "new") { + createFolder(selectedItem) + return + } + if selectedItem.isDetails() { gotoDetails(selectedItem) return @@ -156,6 +186,7 @@ class MasterViewController: UITableViewController, UISearchResultsUpdating { let controller = (segue.destination as! UINavigationController).topViewController as! DetailViewController controller.detailItem = item controller.delegate = delegate + detailController = controller if item.isPic() { // größere Vorschau für Fotos @@ -209,5 +240,91 @@ class MasterViewController: UITableViewController, UISearchResultsUpdating { cell.accessoryType = UITableViewCell.AccessoryType.none } + + func tableView(_ tableView: UITableView, dropSessionDidUpdate session: UIDropSession, withDestinationIndexPath destinationIndexPath: IndexPath?) -> UITableViewDropProposal { + + if tableView.indexPathForSelectedRow == destinationIndexPath { + return UITableViewDropProposal(operation: .forbidden) + } + + if let d = destinationIndexPath { + if model.items.count <= d.row { + return UITableViewDropProposal(operation: .forbidden) + } + if (model.items[d.row].path == "new") { + return UITableViewDropProposal(operation: .forbidden) + } + if (!model.items[d.row].local) { + return UITableViewDropProposal(operation: .forbidden) + } + } + else { + return UITableViewDropProposal(operation: .forbidden) + } + + if session.canLoadObjects(ofClass: NSString.self) { + return UITableViewDropProposal(operation: .move, intent: .insertAtDestinationIndexPath) + } else { + return UITableViewDropProposal(operation: .forbidden) + } + } + + func tableView(_ tableView: UITableView, performDropWith coordinator: UITableViewDropCoordinator) { + let destinationIndexPath: IndexPath + + if let indexPath = coordinator.destinationIndexPath { + destinationIndexPath = indexPath + } else { + return + } + + // attempt to load strings from the drop coordinator + let progress = coordinator.session.loadObjects(ofClass: NSString.self) { items in + // convert the item provider array to a string array or bail out + guard let strings = items as? [String] else { return } + + let r = destinationIndexPath.row + if r < self.model.items.count { + let destinationItem = self.model.items[r] + + // loop over all the strings we received + for (index, string) in strings.enumerated() { + print(string) + + let items = try! JSONDecoder().decode(MediaModel.self, from: string.data(using: .utf8)!) + let m = MediaItem(model: items) + m.local = true + let at = m.playerURL! + m.path = destinationItem.path + let to = m.playerURL! + print(destinationItem.name) + + do { + try FileManager.default.moveItem(at: at, to: to) + try FileManager.default.moveItem(at: at.appendingPathExtension("json"), to: to.appendingPathExtension("json")) + + let weiter:Weiter = { + (g) in + let fav = NetworkManager.sharedInstance.favorites + ItemModel().sortItems(selectedItem: fav, children: g) + self.model.items = fav.children + + + if let s = tableView.indexPathForSelectedRow { + let refreshed = self.model.items[s.row] + self.detailController!.detailItem = refreshed + } + } + + NetworkManager.sharedInstance.loadFavDirs("", completionHandler: weiter) + } catch { + print(error) + } + } + } + } + + + } }