NCShareExtension+DataSource.swift 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. //
  2. // NCShareExtension+DataSource.swift
  3. // Share
  4. //
  5. // Created by Henrik Storch on 29.12.21.
  6. // Copyright © 2021 Henrik Storch. All rights reserved.
  7. //
  8. // Author Henrik Storch <henrik.storch@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. // MARK: - Collection View (target folder)
  26. extension NCShareExtension: UICollectionViewDelegate {
  27. func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  28. guard let metadata = self.dataSource.getMetadata(indexPath: indexPath) else { return showAlert(description: "_invalid_url_") }
  29. let serverUrl = utilityFileSystem.stringAppendServerUrl(metadata.serverUrl, addFileName: metadata.fileName)
  30. if metadata.e2eEncrypted && !NCKeychain().isEndToEndEnabled(account: session.account) {
  31. showAlert(title: "_info_", description: "_e2e_goto_settings_for_enable_")
  32. }
  33. if let fileNameError = FileNameValidator.shared.checkFileName(metadata.fileNameView, account: session.account) {
  34. present(UIAlertController.warning(message: "\(fileNameError.errorDescription) \(NSLocalizedString("_please_rename_file_", comment: ""))"), animated: true)
  35. return
  36. }
  37. self.serverUrl = serverUrl
  38. reloadDatasource(withLoadFolder: true)
  39. setNavigationBar(navigationTitle: metadata.fileNameView)
  40. }
  41. func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
  42. if kind == UICollectionView.elementKindSectionHeader {
  43. guard let header = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "sectionFirstHeaderEmptyData", for: indexPath) as? NCSectionFirstHeaderEmptyData else { return NCSectionFirstHeaderEmptyData() }
  44. if self.dataSourceTask?.state == .running {
  45. header.emptyImage.image = utility.loadImage(named: "wifi", colors: [NCBrandColor.shared.getElement(account: session.account)])
  46. header.emptyTitle.text = NSLocalizedString("_request_in_progress_", comment: "")
  47. header.emptyDescription.text = ""
  48. } else {
  49. header.emptyImage.image = NCImageCache.shared.getFolder(account: self.session.account)
  50. header.emptyTitle.text = NSLocalizedString("_files_no_folders_", comment: "")
  51. header.emptyDescription.text = ""
  52. }
  53. return header
  54. } else {
  55. return UICollectionReusableView()
  56. }
  57. }
  58. }
  59. extension NCShareExtension: UICollectionViewDelegateFlowLayout {
  60. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
  61. var height: CGFloat = 0
  62. if self.dataSource.isEmpty() {
  63. height = NCUtility().getHeightHeaderEmptyData(view: view, portraitOffset: 0, landscapeOffset: -50)
  64. }
  65. return CGSize(width: collectionView.frame.width, height: height)
  66. }
  67. }
  68. extension NCShareExtension: UICollectionViewDataSource {
  69. func numberOfSections(in collectionView: UICollectionView) -> Int {
  70. return dataSource.numberOfSections()
  71. }
  72. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  73. return self.dataSource.numberOfItemsInSection(section)
  74. }
  75. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  76. let cell = (collectionView.dequeueReusableCell(withReuseIdentifier: "listCell", for: indexPath) as? NCListCell)!
  77. guard let metadata = self.dataSource.getMetadata(indexPath: indexPath) else {
  78. return cell
  79. }
  80. cell.fileOcId = metadata.ocId
  81. cell.fileOcIdTransfer = metadata.ocIdTransfer
  82. cell.fileUser = metadata.ownerId
  83. cell.labelTitle.text = metadata.fileNameView
  84. cell.labelTitle.textColor = NCBrandColor.shared.textColor
  85. cell.imageSelect.image = nil
  86. cell.imageStatus.image = nil
  87. cell.imageLocal.image = nil
  88. cell.imageFavorite.image = nil
  89. cell.imageShared.image = nil
  90. cell.imageMore.image = nil
  91. cell.imageItem.image = nil
  92. cell.imageItem.backgroundColor = nil
  93. if metadata.directory {
  94. setupDirectoryCell(cell, indexPath: indexPath, with: metadata)
  95. }
  96. if metadata.favorite {
  97. cell.imageFavorite.image = NCImageCache.shared.getImageFavorite()
  98. }
  99. cell.imageSelect.isHidden = true
  100. cell.backgroundView = nil
  101. cell.hideButtonMore(true)
  102. cell.hideButtonShare(true)
  103. cell.selected(false, isEditMode: false)
  104. if metadata.isLivePhoto {
  105. cell.imageStatus.image = utility.loadImage(named: "livephoto", colors: [NCBrandColor.shared.iconImageColor2])
  106. }
  107. cell.setTags(tags: Array(metadata.tags))
  108. cell.separator.isHidden = collectionView.numberOfItems(inSection: indexPath.section) == indexPath.row + 1
  109. return cell
  110. }
  111. func setupDirectoryCell(_ cell: NCListCell, indexPath: IndexPath, with metadata: tableMetadata) {
  112. var isShare = false
  113. var isMounted = false
  114. let permissions = NCPermissions()
  115. if let metadataFolder = metadataFolder {
  116. isShare = metadata.permissions.contains(permissions.permissionShared) && !metadataFolder.permissions.contains(permissions.permissionShared)
  117. isMounted = metadata.permissions.contains(permissions.permissionMounted) && !metadataFolder.permissions.contains(permissions.permissionMounted)
  118. }
  119. if metadata.e2eEncrypted {
  120. cell.imageItem.image = NCImageCache.shared.getFolderEncrypted(account: metadata.account)
  121. } else if isShare {
  122. cell.imageItem.image = NCImageCache.shared.getFolderSharedWithMe(account: metadata.account)
  123. } else if !metadata.shareType.isEmpty {
  124. metadata.shareType.contains(3) ?
  125. (cell.imageItem.image = NCImageCache.shared.getFolderPublic(account: metadata.account)) :
  126. (cell.imageItem.image = NCImageCache.shared.getFolderSharedWithMe(account: metadata.account))
  127. } else if metadata.mountType == "group" {
  128. cell.imageItem.image = NCImageCache.shared.getFolderGroup(account: metadata.account)
  129. } else if isMounted {
  130. cell.imageItem.image = NCImageCache.shared.getFolderExternal(account: metadata.account)
  131. } else if metadata.fileName == autoUploadFileName && metadata.serverUrl == autoUploadDirectory {
  132. cell.imageItem.image = NCImageCache.shared.getFolderAutomaticUpload(account: metadata.account)
  133. } else {
  134. cell.imageItem.image = NCImageCache.shared.getFolder(account: metadata.account)
  135. }
  136. cell.labelInfo.text = utility.dateDiff(metadata.date as Date)
  137. let lockServerUrl = utilityFileSystem.stringAppendServerUrl(metadata.serverUrl, addFileName: metadata.fileName)
  138. let tableDirectory = self.database.getTableDirectory(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@", session.account, lockServerUrl))
  139. // Local image: offline
  140. if tableDirectory != nil && tableDirectory!.offline {
  141. cell.imageLocal.image = NCImageCache.shared.getImageOfflineFlag()
  142. }
  143. }
  144. }
  145. // MARK: - Table View (uploading files)
  146. extension NCShareExtension: UITableViewDelegate {
  147. func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  148. return heightRowTableView
  149. }
  150. func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  151. guard !uploadStarted else { return }
  152. let fileName = filesName[indexPath.row]
  153. showRenameFileDialog(named: fileName, account: session.account)
  154. }
  155. }
  156. extension NCShareExtension: UITableViewDataSource {
  157. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  158. filesName.count
  159. }
  160. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  161. guard let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as? NCShareCell else { return UITableViewCell() }
  162. let fileName = filesName[indexPath.row]
  163. cell.setup(fileName: fileName, account: session.account)
  164. cell.delegate = self
  165. return cell
  166. }
  167. }