NCMedia+CollectionViewDelegate.swift 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. //
  2. // NCMedia+CollectionViewDelegate.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 16/07/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 NextcloudKit
  25. import RealmSwift
  26. extension NCMedia: UICollectionViewDelegate {
  27. func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  28. var mediaCell: NCGridMediaCell?
  29. if let metadata = self.metadatas?[indexPath.row] {
  30. if let visibleCells = self.collectionView?.indexPathsForVisibleItems.compactMap({ self.collectionView?.cellForItem(at: $0) }) {
  31. for case let cell as NCGridMediaCell in visibleCells {
  32. if cell.ocId == metadata.ocId {
  33. mediaCell = cell
  34. }
  35. }
  36. }
  37. if isEditMode {
  38. if let index = selectOcId.firstIndex(of: metadata.ocId) {
  39. selectOcId.remove(at: index)
  40. mediaCell?.selected(false)
  41. } else {
  42. selectOcId.append(metadata.ocId)
  43. mediaCell?.selected(true)
  44. }
  45. tabBarSelect.selectCount = selectOcId.count
  46. } else {
  47. // ACTIVE SERVERURL
  48. serverUrl = metadata.serverUrl
  49. if let metadatas = self.metadatas?.getArray() {
  50. NCViewer().view(viewController: self, metadata: metadata, metadatas: metadatas, imageIcon: getImage(metadata: metadata))
  51. }
  52. }
  53. }
  54. }
  55. func collectionView(_ collectionView: UICollectionView, contextMenuConfigurationForItemAt indexPath: IndexPath, point: CGPoint) -> UIContextMenuConfiguration? {
  56. guard let cell = collectionView.cellForItem(at: indexPath) as? NCGridMediaCell,
  57. let metadata = self.metadatas?[indexPath.row] else { return nil }
  58. let identifier = indexPath as NSCopying
  59. let image = cell.imageItem.image
  60. self.serverUrl = metadata.serverUrl
  61. return UIContextMenuConfiguration(identifier: identifier, previewProvider: {
  62. return NCViewerProviderContextMenu(metadata: metadata, image: image)
  63. }, actionProvider: { _ in
  64. return NCContextMenu().viewMenu(ocId: metadata.ocId, viewController: self, image: image)
  65. })
  66. }
  67. func collectionView(_ collectionView: UICollectionView, willPerformPreviewActionForMenuWith configuration: UIContextMenuConfiguration, animator: UIContextMenuInteractionCommitAnimating) {
  68. animator.addCompletion {
  69. if let indexPath = configuration.identifier as? IndexPath {
  70. self.collectionView(collectionView, didSelectItemAt: indexPath)
  71. }
  72. }
  73. }
  74. }