NCMedia+CollectionViewDelegate.swift 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. guard let metadata = dataSource.getMetadata(indexPath: indexPath),
  29. let cell = collectionView.cellForItem(at: indexPath) as? NCMediaCell else { return }
  30. if isEditMode {
  31. if let index = fileSelect.firstIndex(of: metadata.ocId) {
  32. fileSelect.remove(at: index)
  33. cell.selected(false)
  34. } else {
  35. fileSelect.append(metadata.ocId)
  36. cell.selected(true)
  37. }
  38. tabBarSelect.selectCount = fileSelect.count
  39. } else if let metadata = database.getMetadataFromOcId(metadata.ocId) {
  40. let image = utility.getImage(ocId: metadata.ocId, etag: metadata.etag, ext: global.previewExt1024)
  41. let ocIds = dataSource.metadatas.map { $0.ocId }
  42. NCViewer().view(viewController: self, metadata: metadata, ocIds: ocIds, image: image)
  43. }
  44. }
  45. func collectionView(_ collectionView: UICollectionView, contextMenuConfigurationForItemAt indexPath: IndexPath, point: CGPoint) -> UIContextMenuConfiguration? {
  46. guard let ocId = dataSource.getMetadata(indexPath: indexPath)?.ocId,
  47. let metadata = database.getMetadataFromOcId(ocId)
  48. else { return nil }
  49. let identifier = indexPath as NSCopying
  50. let image = utility.getImage(ocId: metadata.ocId, etag: metadata.etag, ext: global.previewExt1024)
  51. return UIContextMenuConfiguration(identifier: identifier, previewProvider: {
  52. return NCViewerProviderContextMenu(metadata: metadata, image: image)
  53. }, actionProvider: { _ in
  54. return NCContextMenu().viewMenu(ocId: metadata.ocId, viewController: self, image: image)
  55. })
  56. }
  57. func collectionView(_ collectionView: UICollectionView, willPerformPreviewActionForMenuWith configuration: UIContextMenuConfiguration, animator: UIContextMenuInteractionCommitAnimating) {
  58. animator.addCompletion {
  59. if let indexPath = configuration.identifier as? IndexPath {
  60. self.collectionView(collectionView, didSelectItemAt: indexPath)
  61. }
  62. }
  63. }
  64. }