Browse Source

Dragndrop

master
marcoschmickler 5 years ago
parent
commit
8fa777e37d
  1. 10
      kplayer/core/NetworkManager.swift
  2. 25
      kplayer/detail/DetailViewController.swift
  3. 119
      kplayer/master/MasterViewController.swift

10
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)
}

25
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()
}
}

119
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)
}
}
}
}
}
}
Loading…
Cancel
Save