NCShareExtension+DataSource.swift 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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),
  29. let serverUrl = CCUtility.stringAppendServerUrl(metadata.serverUrl, addFileName: metadata.fileName) else {
  30. return showAlert(description: "_invalid_url_")
  31. }
  32. if metadata.e2eEncrypted && !CCUtility.isEnd(toEndEnabled: activeAccount.account) {
  33. showAlert(title: "_info_", description: "_e2e_goto_settings_for_enable_")
  34. }
  35. self.serverUrl = serverUrl
  36. reloadDatasource(withLoadFolder: true)
  37. setNavigationBar(navigationTitle: metadata.fileNameView)
  38. }
  39. }
  40. extension NCShareExtension: UICollectionViewDataSource {
  41. func numberOfSections(in collectionView: UICollectionView) -> Int {
  42. return dataSource.numberOfSections()
  43. }
  44. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  45. let numberOfItems = dataSource.numberOfItemsInSection(section)
  46. emptyDataSet?.numberOfItemsInSection(numberOfItems, section: section)
  47. return numberOfItems
  48. }
  49. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  50. guard let metadata = dataSource.cellForItemAt(indexPath: indexPath), let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "listCell", for: indexPath) as? NCListCell else {
  51. return UICollectionViewCell()
  52. }
  53. cell.delegate = self
  54. cell.fileObjectId = metadata.ocId
  55. cell.fileUser = metadata.ownerId
  56. cell.labelTitle.text = metadata.fileNameView
  57. cell.labelTitle.textColor = .label
  58. cell.imageSelect.image = nil
  59. cell.imageStatus.image = nil
  60. cell.imageLocal.image = nil
  61. cell.imageFavorite.image = nil
  62. cell.imageShared.image = nil
  63. cell.imageMore.image = nil
  64. cell.imageItem.image = nil
  65. cell.imageItem.backgroundColor = nil
  66. cell.progressView.progress = 0.0
  67. if metadata.directory {
  68. setupDirectoryCell(cell, indexPath: indexPath, with: metadata)
  69. }
  70. // image Favorite
  71. if metadata.favorite {
  72. cell.imageFavorite.image = NCBrandColor.cacheImages.favorite
  73. }
  74. cell.imageSelect.isHidden = true
  75. cell.backgroundView = nil
  76. cell.hideButtonMore(true)
  77. cell.hideButtonShare(true)
  78. cell.selectMode(false)
  79. // Live Photo
  80. if metadata.livePhoto {
  81. cell.imageStatus.image = NCBrandColor.cacheImages.livePhoto
  82. }
  83. // Add TAGS
  84. cell.setTags(tags: Array(metadata.tags))
  85. // Remove last separator
  86. cell.separator.isHidden = collectionView.numberOfItems(inSection: indexPath.section) == indexPath.row + 1
  87. return cell
  88. }
  89. func setupDirectoryCell(_ cell: NCListCell, indexPath: IndexPath, with metadata: tableMetadata) {
  90. var isShare = false
  91. var isMounted = false
  92. if let metadataFolder = metadataFolder {
  93. isShare = metadata.permissions.contains(NCGlobal.shared.permissionShared) && !metadataFolder.permissions.contains(NCGlobal.shared.permissionShared)
  94. isMounted = metadata.permissions.contains(NCGlobal.shared.permissionMounted) && !metadataFolder.permissions.contains(NCGlobal.shared.permissionMounted)
  95. }
  96. if metadata.e2eEncrypted {
  97. cell.imageItem.image = NCBrandColor.cacheImages.folderEncrypted
  98. } else if isShare {
  99. cell.imageItem.image = NCBrandColor.cacheImages.folderSharedWithMe
  100. } else if !metadata.shareType.isEmpty {
  101. metadata.shareType.contains(3) ?
  102. (cell.imageItem.image = NCBrandColor.cacheImages.folderPublic) :
  103. (cell.imageItem.image = NCBrandColor.cacheImages.folderSharedWithMe)
  104. } else if metadata.mountType == "group" {
  105. cell.imageItem.image = NCBrandColor.cacheImages.folderGroup
  106. } else if isMounted {
  107. cell.imageItem.image = NCBrandColor.cacheImages.folderExternal
  108. } else if metadata.fileName == autoUploadFileName && metadata.serverUrl == autoUploadDirectory {
  109. cell.imageItem.image = NCBrandColor.cacheImages.folderAutomaticUpload
  110. } else {
  111. cell.imageItem.image = NCBrandColor.cacheImages.folder
  112. }
  113. cell.labelInfo.text = CCUtility.dateDiff(metadata.date as Date)
  114. let lockServerUrl = CCUtility.stringAppendServerUrl(metadata.serverUrl, addFileName: metadata.fileName)!
  115. let tableDirectory = NCManageDatabase.shared.getTableDirectory(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@", activeAccount.account, lockServerUrl))
  116. // Local image: offline
  117. if tableDirectory != nil && tableDirectory!.offline {
  118. cell.imageLocal.image = NCBrandColor.cacheImages.offlineFlag
  119. }
  120. }
  121. }
  122. // MARK: - Table View (uploading files)
  123. extension NCShareExtension: UITableViewDelegate {
  124. func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  125. return heightRowTableView
  126. }
  127. func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  128. guard !uploadStarted else { return }
  129. let fileName = filesName[indexPath.row]
  130. renameFile(named: fileName)
  131. }
  132. }
  133. extension NCShareExtension: UITableViewDataSource {
  134. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  135. filesName.count
  136. }
  137. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  138. guard let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as? NCShareCell else { return UITableViewCell() }
  139. let fileName = filesName[indexPath.row]
  140. cell.setup(fileName: fileName)
  141. cell.delegate = self
  142. return cell
  143. }
  144. }