NCCollectionViewCommon+CollectionViewDelegate.swift 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. //
  2. // NCCollectionViewCommon+CollectionViewDelegate.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 02/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 Foundation
  24. import UIKit
  25. import NextcloudKit
  26. extension NCCollectionViewCommon: UICollectionViewDelegate {
  27. func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  28. guard let metadata = dataSource.cellForItemAt(indexPath: indexPath), !metadata.isInvalidated else { return }
  29. if isEditMode {
  30. if let index = selectOcId.firstIndex(of: metadata.ocId) {
  31. selectOcId.remove(at: index)
  32. } else {
  33. selectOcId.append(metadata.ocId)
  34. }
  35. collectionView.reloadItems(at: [indexPath])
  36. tabBarSelect.update(selectOcId: selectOcId, metadatas: getSelectedMetadatas(), userId: appDelegate.userId)
  37. return
  38. }
  39. if metadata.e2eEncrypted {
  40. if NCGlobal.shared.capabilityE2EEEnabled {
  41. if !NCKeychain().isEndToEndEnabled(account: appDelegate.account) {
  42. let e2ee = NCEndToEndInitialize()
  43. e2ee.delegate = self
  44. e2ee.initEndToEndEncryption(viewController: self.tabBarController, metadata: metadata)
  45. return
  46. }
  47. } else {
  48. NCContentPresenter().showInfo(error: NKError(errorCode: NCGlobal.shared.errorE2EENotEnabled, errorDescription: "_e2e_server_disabled_"))
  49. return
  50. }
  51. }
  52. if metadata.directory {
  53. pushMetadata(metadata)
  54. } else {
  55. let imageIcon = UIImage(contentsOfFile: utilityFileSystem.getDirectoryProviderStorageIconOcId(metadata.ocId, etag: metadata.etag))
  56. if !metadata.isDirectoryE2EE && (metadata.isImage || metadata.isAudioOrVideo) {
  57. var metadatas: [tableMetadata] = []
  58. for metadata in dataSource.getMetadataSourceForAllSections() {
  59. if metadata.isImage || metadata.isAudioOrVideo {
  60. metadatas.append(metadata)
  61. }
  62. }
  63. NCViewer().view(viewController: self, metadata: metadata, metadatas: metadatas, imageIcon: imageIcon)
  64. return
  65. } else if metadata.isAvailableEditorView || utilityFileSystem.fileProviderStorageExists(metadata) {
  66. NCViewer().view(viewController: self, metadata: metadata, metadatas: [metadata], imageIcon: imageIcon)
  67. } else if NextcloudKit.shared.isNetworkReachable(),
  68. let metadata = NCManageDatabase.shared.setMetadatasSessionInWaitDownload(metadatas: [metadata],
  69. session: NextcloudKit.shared.nkCommonInstance.sessionIdentifierDownload,
  70. selector: NCGlobal.shared.selectorLoadFileView,
  71. sceneIdentifier: (self.tabBarController as? NCMainTabBarController)?.sceneIdentifier) {
  72. NCNetworking.shared.download(metadata: metadata, withNotificationProgressTask: true)
  73. } else {
  74. let error = NKError(errorCode: NCGlobal.shared.errorOffline, errorDescription: "_go_online_")
  75. NCContentPresenter().showInfo(error: error)
  76. }
  77. }
  78. }
  79. func collectionView(_ collectionView: UICollectionView, contextMenuConfigurationForItemAt indexPath: IndexPath, point: CGPoint) -> UIContextMenuConfiguration? {
  80. guard let metadata = dataSource.cellForItemAt(indexPath: indexPath) else { return nil }
  81. if isEditMode || metadata.classFile == NKCommon.TypeClassFile.url.rawValue { return nil }
  82. let identifier = indexPath as NSCopying
  83. var image: UIImage?
  84. let cell = collectionView.cellForItem(at: indexPath)
  85. if cell is NCListCell {
  86. image = (cell as? NCListCell)?.imageItem.image
  87. } else if cell is NCGridCell {
  88. image = (cell as? NCGridCell)?.imageItem.image
  89. } else if cell is NCPhotoCell {
  90. image = (cell as? NCPhotoCell)?.imageItem.image
  91. }
  92. return UIContextMenuConfiguration(identifier: identifier, previewProvider: {
  93. return NCViewerProviderContextMenu(metadata: metadata, image: image)
  94. }, actionProvider: { _ in
  95. return NCContextMenu().viewMenu(ocId: metadata.ocId, viewController: self, image: image)
  96. })
  97. }
  98. func collectionView(_ collectionView: UICollectionView, willPerformPreviewActionForMenuWith configuration: UIContextMenuConfiguration, animator: UIContextMenuInteractionCommitAnimating) {
  99. animator.addCompletion {
  100. if let indexPath = configuration.identifier as? IndexPath {
  101. self.collectionView(collectionView, didSelectItemAt: indexPath)
  102. }
  103. }
  104. }
  105. }