NCShareExtension+DataSource.swift 6.9 KB

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