NCMedia+DragDrop.swift 4.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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(fileSelect: fileSelect)
  31. } else if let ocId = dataSource.getMetadata(indexPath: indexPath)?.ocId,
  32. let metadata = database.getMetadataFromOcId(ocId) {
  33. return NCDragDrop().performDrag(metadata: metadata)
  34. }
  35. return []
  36. }
  37. }
  38. // MARK: - Drop
  39. extension NCMedia: UICollectionViewDropDelegate {
  40. func collectionView(_ collectionView: UICollectionView, canHandle session: UIDropSession) -> Bool {
  41. return session.canLoadObjects(ofClass: UIImage.self) || session.hasItemsConforming(toTypeIdentifiers: [UTType.movie.identifier, global.metadataOcIdDataRepresentation])
  42. }
  43. func collectionView(_ collectionView: UICollectionView, performDropWith coordinator: UICollectionViewDropCoordinator) {
  44. DragDropHover.shared.cleanPushDragDropHover()
  45. DragDropHover.shared.sourceMetadatas = nil
  46. guard let tableAccount = database.getTableAccount(predicate: NSPredicate(format: "account == %@", session.account)) else { return }
  47. let serverUrl = NCUtilityFileSystem().getHomeServer(session: session) + tableAccount.mediaPath
  48. if let metadatas = NCDragDrop().performDrop(collectionView, performDropWith: coordinator, serverUrl: serverUrl, isImageVideo: true, controller: self.controller) {
  49. DragDropHover.shared.sourceMetadatas = metadatas
  50. openMenu(collectionView: collectionView, location: coordinator.session.location(in: collectionView))
  51. }
  52. }
  53. func collectionView(_ collectionView: UICollectionView, dropSessionDidExit session: UIDropSession) {
  54. DragDropHover.shared.cleanPushDragDropHover()
  55. }
  56. func collectionView(_ collectionView: UICollectionView, dropSessionDidEnd session: UIDropSession) {
  57. DragDropHover.shared.cleanPushDragDropHover()
  58. }
  59. // MARK: -
  60. private func openMenu(collectionView: UICollectionView, location: CGPoint) {
  61. var listMenuItems: [UIMenuItem] = []
  62. listMenuItems.append(UIMenuItem(title: NSLocalizedString("_copy_", comment: ""), action: #selector(copyMenuFile)))
  63. listMenuItems.append(UIMenuItem(title: NSLocalizedString("_move_", comment: ""), action: #selector(moveMenuFile)))
  64. UIMenuController.shared.menuItems = listMenuItems
  65. UIMenuController.shared.showMenu(from: collectionView, rect: CGRect(x: location.x, y: location.y, width: 0, height: 0))
  66. }
  67. @objc func copyMenuFile() {
  68. guard let sourceMetadatas = DragDropHover.shared.sourceMetadatas else { return }
  69. if let tableAccount = database.getTableAccount(predicate: NSPredicate(format: "account == %@", session.account)) {
  70. let serverUrl = NCUtilityFileSystem().getHomeServer(session: session) + tableAccount.mediaPath
  71. NCDragDrop().copyFile(metadatas: sourceMetadatas, serverUrl: serverUrl)
  72. }
  73. }
  74. @objc func moveMenuFile() {
  75. guard let sourceMetadatas = DragDropHover.shared.sourceMetadatas else { return }
  76. if let tableAccount = database.getTableAccount(predicate: NSPredicate(format: "account == %@", session.account)) {
  77. let serverUrl = NCUtilityFileSystem().getHomeServer(session: session) + tableAccount.mediaPath
  78. NCDragDrop().moveFile(metadatas: sourceMetadatas, serverUrl: serverUrl)
  79. }
  80. }
  81. }