NCShareExtension+DataSource.swift 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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 = dataSource.cellForItemAt(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: activeAccount.account) {
  31. showAlert(title: "_info_", description: "_e2e_goto_settings_for_enable_")
  32. }
  33. self.serverUrl = serverUrl
  34. reloadDatasource(withLoadFolder: true)
  35. setNavigationBar(navigationTitle: metadata.fileNameView)
  36. }
  37. func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
  38. if kind == UICollectionView.elementKindSectionHeader {
  39. guard let header = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "sectionHeaderEmptyData", for: indexPath) as? NCSectionHeaderEmptyData else { return NCSectionHeaderEmptyData() }
  40. if self.dataSourceTask?.state == .running {
  41. header.emptyImage.image = utility.loadImage(named: "wifi", colors: [NCBrandColor.shared.brandElement])
  42. header.emptyTitle.text = NSLocalizedString("_request_in_progress_", comment: "")
  43. header.emptyDescription.text = ""
  44. } else {
  45. header.emptyImage.image = NCImageCache.images.folder
  46. header.emptyTitle.text = NSLocalizedString("_files_no_folders_", comment: "")
  47. header.emptyDescription.text = ""
  48. }
  49. return header
  50. } else {
  51. return UICollectionReusableView()
  52. }
  53. }
  54. }
  55. extension NCShareExtension: UICollectionViewDelegateFlowLayout {
  56. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
  57. var height: CGFloat = 0
  58. if dataSource.getMetadataSourceForAllSections().isEmpty {
  59. height = NCGlobal.shared.getHeightHeaderEmptyData(view: view, portraitOffset: 0, landscapeOffset: -50)
  60. }
  61. return CGSize(width: collectionView.frame.width, height: height)
  62. }
  63. }
  64. extension NCShareExtension: UICollectionViewDataSource {
  65. func numberOfSections(in collectionView: UICollectionView) -> Int {
  66. return dataSource.numberOfSections()
  67. }
  68. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  69. return dataSource.numberOfItemsInSection(section)
  70. }
  71. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  72. guard let metadata = dataSource.cellForItemAt(indexPath: indexPath), let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "listCell", for: indexPath) as? NCListCell else {
  73. return UICollectionViewCell()
  74. }
  75. cell.listCellDelegate = self
  76. cell.fileObjectId = metadata.ocId
  77. cell.indexPath = indexPath
  78. cell.fileUser = metadata.ownerId
  79. cell.labelTitle.text = metadata.fileNameView
  80. cell.labelTitle.textColor = NCBrandColor.shared.textColor
  81. cell.imageSelect.image = nil
  82. cell.imageStatus.image = nil
  83. cell.imageLocal.image = nil
  84. cell.imageFavorite.image = nil
  85. cell.imageShared.image = nil
  86. cell.imageMore.image = nil
  87. cell.imageItem.image = nil
  88. cell.imageItem.backgroundColor = nil
  89. cell.progressView.progress = 0.0
  90. if metadata.directory {
  91. setupDirectoryCell(cell, indexPath: indexPath, with: metadata)
  92. }
  93. // image Favorite
  94. if metadata.favorite {
  95. cell.imageFavorite.image = NCImageCache.images.favorite
  96. }
  97. cell.imageSelect.isHidden = true
  98. cell.backgroundView = nil
  99. cell.hideButtonMore(true)
  100. cell.hideButtonShare(true)
  101. cell.selectMode(false)
  102. // Live Photo
  103. if metadata.isLivePhoto {
  104. cell.imageStatus.image = NCImageCache.images.livePhoto
  105. }
  106. // Add TAGS
  107. cell.setTags(tags: Array(metadata.tags))
  108. // Remove last separator
  109. cell.separator.isHidden = collectionView.numberOfItems(inSection: indexPath.section) == indexPath.row + 1
  110. return cell
  111. }
  112. func setupDirectoryCell(_ cell: NCListCell, indexPath: IndexPath, with metadata: tableMetadata) {
  113. var isShare = false
  114. var isMounted = false
  115. let permissions = NCPermissions()
  116. if let metadataFolder = metadataFolder {
  117. isShare = metadata.permissions.contains(permissions.permissionShared) && !metadataFolder.permissions.contains(permissions.permissionShared)
  118. isMounted = metadata.permissions.contains(permissions.permissionMounted) && !metadataFolder.permissions.contains(permissions.permissionMounted)
  119. }
  120. if metadata.e2eEncrypted {
  121. cell.imageItem.image = NCImageCache.images.folderEncrypted
  122. } else if isShare {
  123. cell.imageItem.image = NCImageCache.images.folderSharedWithMe
  124. } else if !metadata.shareType.isEmpty {
  125. metadata.shareType.contains(3) ?
  126. (cell.imageItem.image = NCImageCache.images.folderPublic) :
  127. (cell.imageItem.image = NCImageCache.images.folderSharedWithMe)
  128. } else if metadata.mountType == "group" {
  129. cell.imageItem.image = NCImageCache.images.folderGroup
  130. } else if isMounted {
  131. cell.imageItem.image = NCImageCache.images.folderExternal
  132. } else if metadata.fileName == autoUploadFileName && metadata.serverUrl == autoUploadDirectory {
  133. cell.imageItem.image = NCImageCache.images.folderAutomaticUpload
  134. } else {
  135. cell.imageItem.image = NCImageCache.images.folder
  136. }
  137. cell.labelInfo.text = utility.dateDiff(metadata.date as Date)
  138. let lockServerUrl = utilityFileSystem.stringAppendServerUrl(metadata.serverUrl, addFileName: metadata.fileName)
  139. let tableDirectory = NCManageDatabase.shared.getTableDirectory(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@", activeAccount.account, lockServerUrl))
  140. // Local image: offline
  141. if tableDirectory != nil && tableDirectory!.offline {
  142. cell.imageLocal.image = NCImageCache.images.offlineFlag
  143. }
  144. }
  145. }
  146. // MARK: - Table View (uploading files)
  147. extension NCShareExtension: UITableViewDelegate {
  148. func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  149. return heightRowTableView
  150. }
  151. func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  152. guard !uploadStarted else { return }
  153. let fileName = filesName[indexPath.row]
  154. renameFile(named: fileName)
  155. }
  156. }
  157. extension NCShareExtension: UITableViewDataSource {
  158. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  159. filesName.count
  160. }
  161. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  162. guard let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as? NCShareCell else { return UITableViewCell() }
  163. let fileName = filesName[indexPath.row]
  164. cell.setup(fileName: fileName)
  165. cell.delegate = self
  166. return cell
  167. }
  168. }