NCCollectionViewCommon+CollectionViewDelegate.swift 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 = self.dataSource.getMetadata(indexPath: indexPath),
  29. !metadata.isInvalidated,
  30. (metadata.name == global.appName || metadata.name == NCGlobal.shared.talkName)
  31. else { return }
  32. if isEditMode {
  33. if let index = fileSelect.firstIndex(of: metadata.ocId) {
  34. fileSelect.remove(at: index)
  35. } else {
  36. fileSelect.append(metadata.ocId)
  37. }
  38. collectionView.reloadItems(at: [indexPath])
  39. tabBarSelect.update(fileSelect: fileSelect, metadatas: getSelectedMetadatas(), userId: metadata.userId)
  40. return
  41. }
  42. if metadata.e2eEncrypted {
  43. if NCCapabilities.shared.getCapabilities(account: metadata.account).capabilityE2EEEnabled {
  44. if !NCKeychain().isEndToEndEnabled(account: metadata.account) {
  45. let e2ee = NCEndToEndInitialize()
  46. e2ee.delegate = self
  47. e2ee.initEndToEndEncryption(controller: self.controller, metadata: metadata)
  48. return
  49. }
  50. } else {
  51. NCContentPresenter().showInfo(error: NKError(errorCode: global.errorE2EENotEnabled, errorDescription: "_e2e_server_disabled_"))
  52. return
  53. }
  54. }
  55. if metadata.directory {
  56. pushMetadata(metadata)
  57. } else {
  58. let image = utility.getImage(ocId: metadata.ocId, etag: metadata.etag, ext: NCGlobal.shared.previewExt1024)
  59. if !metadata.isDirectoryE2EE, (metadata.isImage || metadata.isAudioOrVideo) {
  60. let metadatas = self.dataSource.getMetadatas()
  61. let ocIds = metadatas.filter { $0.classFile == NKCommon.TypeClassFile.image.rawValue ||
  62. $0.classFile == NKCommon.TypeClassFile.video.rawValue ||
  63. $0.classFile == NKCommon.TypeClassFile.audio.rawValue }.map(\.ocId)
  64. return NCViewer().view(viewController: self, metadata: metadata, ocIds: ocIds, image: image)
  65. } else if metadata.isAvailableEditorView ||
  66. utilityFileSystem.fileProviderStorageExists(metadata) ||
  67. metadata.name == NCGlobal.shared.talkName {
  68. NCViewer().view(viewController: self, metadata: metadata, image: image)
  69. } else if NextcloudKit.shared.isNetworkReachable(),
  70. let metadata = database.setMetadatasSessionInWaitDownload(metadatas: [metadata],
  71. session: NCNetworking.shared.sessionDownload,
  72. selector: global.selectorLoadFileView,
  73. sceneIdentifier: self.controller?.sceneIdentifier) {
  74. NCNetworking.shared.download(metadata: metadata, withNotificationProgressTask: true)
  75. } else {
  76. let error = NKError(errorCode: global.errorOffline, errorDescription: "_go_online_")
  77. NCContentPresenter().showInfo(error: error)
  78. }
  79. }
  80. }
  81. func collectionView(_ collectionView: UICollectionView, contextMenuConfigurationForItemAt indexPath: IndexPath, point: CGPoint) -> UIContextMenuConfiguration? {
  82. guard let metadata = self.dataSource.getMetadata(indexPath: indexPath) else { return nil }
  83. if isEditMode || metadata.classFile == NKCommon.TypeClassFile.url.rawValue { return nil }
  84. let identifier = indexPath as NSCopying
  85. var image = utility.getImage(ocId: metadata.ocId, etag: metadata.etag, ext: global.previewExt1024)
  86. if image == nil {
  87. let cell = collectionView.cellForItem(at: indexPath)
  88. if cell is NCListCell {
  89. image = (cell as? NCListCell)?.imageItem.image
  90. } else if cell is NCGridCell {
  91. image = (cell as? NCGridCell)?.imageItem.image
  92. } else if cell is NCPhotoCell {
  93. image = (cell as? NCPhotoCell)?.imageItem.image
  94. }
  95. }
  96. return UIContextMenuConfiguration(identifier: identifier, previewProvider: {
  97. return NCViewerProviderContextMenu(metadata: metadata, image: image)
  98. }, actionProvider: { _ in
  99. return NCContextMenu().viewMenu(ocId: metadata.ocId, viewController: self, image: image)
  100. })
  101. }
  102. func collectionView(_ collectionView: UICollectionView, willPerformPreviewActionForMenuWith configuration: UIContextMenuConfiguration, animator: UIContextMenuInteractionCommitAnimating) {
  103. animator.addCompletion {
  104. if let indexPath = configuration.identifier as? IndexPath {
  105. self.collectionView(collectionView, didSelectItemAt: indexPath)
  106. }
  107. }
  108. }
  109. }