NCMedia+DragDrop.swift 4.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. //
  2. // NCMedia+DragDrop.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 21/04/24.
  6. // Copyright © 2024 Marino Faggiana. All rights reserved.
  7. //
  8. // Author Marino Faggiana <marino.faggiana@nextcloud.com>
  9. //
  10. // This program is free software: you can redistribute it and/or modify
  11. // it under the terms of the GNU General Public License as published by
  12. // the Free Software Foundation, either version 3 of the License, or
  13. // (at your option) any later version.
  14. //
  15. // This program is distributed in the hope that it will be useful,
  16. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. // GNU General Public License for more details.
  19. //
  20. // You should have received a copy of the GNU General Public License
  21. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. //
  23. import UIKit
  24. import UniformTypeIdentifiers
  25. import NextcloudKit
  26. // MARK: - Drag
  27. extension NCMedia: UICollectionViewDragDelegate {
  28. func collectionView(_ collectionView: UICollectionView, itemsForBeginning session: UIDragSession, at indexPath: IndexPath) -> [UIDragItem] {
  29. if isEditMode {
  30. return NCDragDrop().performDrag(selectOcId: selectOcId)
  31. } else if let metadata = self.metadatas?[indexPath.row] {
  32. return NCDragDrop().performDrag(metadata: metadata)
  33. }
  34. return []
  35. }
  36. }
  37. // MARK: - Drop
  38. extension NCMedia: UICollectionViewDropDelegate {
  39. func collectionView(_ collectionView: UICollectionView, canHandle session: UIDropSession) -> Bool {
  40. return session.canLoadObjects(ofClass: UIImage.self) || session.hasItemsConforming(toTypeIdentifiers: [UTType.movie.identifier, NCGlobal.shared.metadataOcIdDataRepresentation])
  41. }
  42. func collectionView(_ collectionView: UICollectionView, performDropWith coordinator: UICollectionViewDropCoordinator) {
  43. DragDropHover.shared.cleanPushDragDropHover()
  44. DragDropHover.shared.sourceMetadatas = nil
  45. guard let tableAccount = NCManageDatabase.shared.getAccount(predicate: NSPredicate(format: "account == %@", appDelegate.account)) else { return }
  46. let serverUrl = NCUtilityFileSystem().getHomeServer(urlBase: tableAccount.urlBase, userId: tableAccount.userId) + tableAccount.mediaPath
  47. if let metadatas = NCDragDrop().performDrop(collectionView, performDropWith: coordinator, serverUrl: serverUrl, isImageVideo: true) {
  48. DragDropHover.shared.sourceMetadatas = metadatas
  49. openMenu(collectionView: collectionView, location: coordinator.session.location(in: collectionView))
  50. }
  51. }
  52. func collectionView(_ collectionView: UICollectionView, dropSessionDidExit session: UIDropSession) {
  53. DragDropHover.shared.cleanPushDragDropHover()
  54. }
  55. func collectionView(_ collectionView: UICollectionView, dropSessionDidEnd session: UIDropSession) {
  56. DragDropHover.shared.cleanPushDragDropHover()
  57. }
  58. // MARK: -
  59. private func openMenu(collectionView: UICollectionView, location: CGPoint) {
  60. var listMenuItems: [UIMenuItem] = []
  61. listMenuItems.append(UIMenuItem(title: NSLocalizedString("_copy_", comment: ""), action: #selector(copyMenuFile)))
  62. listMenuItems.append(UIMenuItem(title: NSLocalizedString("_move_", comment: ""), action: #selector(moveMenuFile)))
  63. UIMenuController.shared.menuItems = listMenuItems
  64. UIMenuController.shared.showMenu(from: collectionView, rect: CGRect(x: location.x, y: location.y, width: 0, height: 0))
  65. }
  66. @objc func copyMenuFile() {
  67. guard let sourceMetadatas = DragDropHover.shared.sourceMetadatas,
  68. let tableAccount = NCManageDatabase.shared.getAccount(predicate: NSPredicate(format: "account == %@", appDelegate.account)) else { return }
  69. let serverUrl = NCUtilityFileSystem().getHomeServer(urlBase: tableAccount.urlBase, userId: tableAccount.userId) + tableAccount.mediaPath
  70. NCDragDrop().copyFile(metadatas: sourceMetadatas, serverUrl: serverUrl)
  71. }
  72. @objc func moveMenuFile() {
  73. guard let sourceMetadatas = DragDropHover.shared.sourceMetadatas,
  74. let tableAccount = NCManageDatabase.shared.getAccount(predicate: NSPredicate(format: "account == %@", appDelegate.account)) else { return }
  75. let serverUrl = NCUtilityFileSystem().getHomeServer(urlBase: tableAccount.urlBase, userId: tableAccount.userId) + tableAccount.mediaPath
  76. NCDragDrop().moveFile(metadatas: sourceMetadatas, serverUrl: serverUrl)
  77. }
  78. }