NCShareExtension+DataSource.swift 6.8 KB

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