NCCollectionViewCommon+CollectionViewDataSource.swift 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  1. //
  2. // NCCollectionViewCommon+CollectionViewDataSource.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 02/07/24.
  6. // Copyright © 2024 Marino Faggiana. All rights reserved.
  7. //
  8. // Author Marino Faggiana <marino.faggiana@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 Foundation
  24. import UIKit
  25. import NextcloudKit
  26. extension NCCollectionViewCommon: UICollectionViewDataSource {
  27. func numberOfSections(in collectionView: UICollectionView) -> Int {
  28. return dataSource.numberOfSections()
  29. }
  30. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  31. return dataSource.numberOfItemsInSection(section)
  32. }
  33. func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
  34. guard let metadata = dataSource.cellForItemAt(indexPath: indexPath),
  35. let cell = (cell as? NCCellProtocol) else { return }
  36. let existsIcon = utilityFileSystem.fileProviderStoragePreviewIconExists(metadata.ocId, etag: metadata.etag)
  37. func downloadAvatar(fileName: String, user: String, dispalyName: String?) {
  38. if let image = NCManageDatabase.shared.getImageAvatarLoaded(fileName: fileName) {
  39. cell.fileAvatarImageView?.contentMode = .scaleAspectFill
  40. cell.fileAvatarImageView?.image = image
  41. } else {
  42. NCNetworking.shared.downloadAvatar(user: user, dispalyName: dispalyName, fileName: fileName, cell: cell, view: collectionView)
  43. }
  44. }
  45. /// CONTENT MODE
  46. cell.filePreviewImageView?.layer.borderWidth = 0
  47. if existsIcon {
  48. cell.filePreviewImageView?.contentMode = .scaleAspectFill
  49. } else {
  50. cell.filePreviewImageView?.contentMode = .scaleAspectFit
  51. }
  52. cell.fileAvatarImageView?.contentMode = .center
  53. /// THUMBNAIL
  54. if !metadata.directory {
  55. if metadata.hasPreviewBorder {
  56. cell.filePreviewImageView?.layer.borderWidth = 0.2
  57. cell.filePreviewImageView?.layer.borderColor = UIColor.lightGray.cgColor
  58. }
  59. if metadata.name == NCGlobal.shared.appName {
  60. if layoutForView?.layout == NCGlobal.shared.layoutPhotoRatio || layoutForView?.layout == NCGlobal.shared.layoutPhotoSquare {
  61. if let image = NCImageCache.shared.getPreviewImageCache(ocId: metadata.ocId, etag: metadata.etag) {
  62. cell.filePreviewImageView?.image = image
  63. } else if let image = UIImage(contentsOfFile: self.utilityFileSystem.getDirectoryProviderStoragePreviewOcId(metadata.ocId, etag: metadata.etag)) {
  64. cell.filePreviewImageView?.image = image
  65. NCImageCache.shared.addPreviewImageCache(metadata: metadata, image: image)
  66. }
  67. } else {
  68. if let image = NCImageCache.shared.getIconImageCache(ocId: metadata.ocId, etag: metadata.etag) {
  69. cell.filePreviewImageView?.image = image
  70. } else if metadata.hasPreview {
  71. cell.filePreviewImageView?.image = utility.getIcon(metadata: metadata)
  72. }
  73. }
  74. if cell.filePreviewImageView?.image == nil {
  75. if metadata.iconName.isEmpty {
  76. cell.filePreviewImageView?.image = NCImageCache.images.file
  77. } else {
  78. cell.filePreviewImageView?.image = utility.loadImage(named: metadata.iconName, useTypeIconFile: true)
  79. }
  80. if metadata.hasPreview && metadata.status == NCGlobal.shared.metadataStatusNormal && !existsIcon {
  81. for case let operation as NCCollectionViewDownloadThumbnail in NCNetworking.shared.downloadThumbnailQueue.operations where operation.metadata.ocId == metadata.ocId { return }
  82. NCNetworking.shared.downloadThumbnailQueue.addOperation(NCCollectionViewDownloadThumbnail(metadata: metadata, cell: cell, collectionView: collectionView))
  83. }
  84. }
  85. } else {
  86. /// APP NAME - UNIFIED SEARCH
  87. switch metadata.iconName {
  88. case let str where str.contains("contacts"):
  89. cell.filePreviewImageView?.image = NCImageCache.images.iconContacts
  90. case let str where str.contains("conversation"):
  91. cell.filePreviewImageView?.image = NCImageCache.images.iconTalk
  92. case let str where str.contains("calendar"):
  93. cell.filePreviewImageView?.image = NCImageCache.images.iconCalendar
  94. case let str where str.contains("deck"):
  95. cell.filePreviewImageView?.image = NCImageCache.images.iconDeck
  96. case let str where str.contains("mail"):
  97. cell.filePreviewImageView?.image = NCImageCache.images.iconMail
  98. case let str where str.contains("talk"):
  99. cell.filePreviewImageView?.image = NCImageCache.images.iconTalk
  100. case let str where str.contains("confirm"):
  101. cell.filePreviewImageView?.image = NCImageCache.images.iconConfirm
  102. case let str where str.contains("pages"):
  103. cell.filePreviewImageView?.image = NCImageCache.images.iconPages
  104. default:
  105. cell.filePreviewImageView?.image = NCImageCache.images.iconFile
  106. }
  107. if !metadata.iconUrl.isEmpty {
  108. if let ownerId = getAvatarFromIconUrl(metadata: metadata) {
  109. let fileName = metadata.userBaseUrl + "-" + ownerId + ".png"
  110. downloadAvatar(fileName: fileName, user: ownerId, dispalyName: nil)
  111. }
  112. }
  113. }
  114. }
  115. /// AVATAR
  116. if !metadata.ownerId.isEmpty,
  117. metadata.ownerId != appDelegate.userId,
  118. appDelegate.account == metadata.account {
  119. let fileName = metadata.userBaseUrl + "-" + metadata.ownerId + ".png"
  120. downloadAvatar(fileName: fileName, user: metadata.ownerId, dispalyName: metadata.ownerDisplayName)
  121. }
  122. }
  123. func collectionView(_ collectionView: UICollectionView, didEndDisplaying cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
  124. if !collectionView.indexPathsForVisibleItems.contains(indexPath) {
  125. guard let metadata = dataSource.cellForItemAt(indexPath: indexPath) else { return }
  126. for case let operation as NCCollectionViewDownloadThumbnail in NCNetworking.shared.downloadThumbnailQueue.operations where operation.metadata.ocId == metadata.ocId {
  127. operation.cancel()
  128. }
  129. }
  130. }
  131. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  132. var cell: NCCellProtocol & UICollectionViewCell
  133. let permissions = NCPermissions()
  134. var isShare = false
  135. var isMounted = false
  136. var a11yValues: [String] = []
  137. // LAYOUT PHOTO
  138. if layoutForView?.layout == NCGlobal.shared.layoutPhotoRatio || layoutForView?.layout == NCGlobal.shared.layoutPhotoSquare {
  139. guard let photoCell = collectionView.dequeueReusableCell(withReuseIdentifier: "photoCell", for: indexPath) as? NCPhotoCell else { return NCPhotoCell() }
  140. photoCell.photoCellDelegate = self
  141. cell = photoCell
  142. } else if layoutForView?.layout == NCGlobal.shared.layoutGrid {
  143. // LAYOUT GRID
  144. guard let gridCell = collectionView.dequeueReusableCell(withReuseIdentifier: "gridCell", for: indexPath) as? NCGridCell else { return NCGridCell() }
  145. gridCell.gridCellDelegate = self
  146. cell = gridCell
  147. } else {
  148. // LAYOUT LIST
  149. guard let listCell = collectionView.dequeueReusableCell(withReuseIdentifier: "listCell", for: indexPath) as? NCListCell else { return NCListCell() }
  150. listCell.listCellDelegate = self
  151. cell = listCell
  152. }
  153. guard let metadata = dataSource.cellForItemAt(indexPath: indexPath) else { return cell }
  154. defer {
  155. if NCGlobal.shared.disableSharesView || !metadata.isSharable() {
  156. cell.hideButtonShare(true)
  157. }
  158. }
  159. if metadataFolder != nil {
  160. isShare = metadata.permissions.contains(permissions.permissionShared) && !metadataFolder!.permissions.contains(permissions.permissionShared)
  161. isMounted = metadata.permissions.contains(permissions.permissionMounted) && !metadataFolder!.permissions.contains(permissions.permissionMounted)
  162. }
  163. cell.fileStatusImage?.image = nil
  164. cell.fileLocalImage?.image = nil
  165. cell.fileFavoriteImage?.image = nil
  166. cell.fileSharedImage?.image = nil
  167. cell.fileMoreImage?.image = nil
  168. cell.filePreviewImageView?.image = nil
  169. cell.filePreviewImageView?.backgroundColor = nil
  170. cell.fileObjectId = metadata.ocId
  171. cell.indexPath = indexPath
  172. cell.fileUser = metadata.ownerId
  173. cell.fileProgressView?.isHidden = true
  174. cell.fileProgressView?.progress = 0.0
  175. cell.hideButtonShare(false)
  176. cell.hideButtonMore(false)
  177. cell.titleInfoTrailingDefault()
  178. if isSearchingMode {
  179. cell.fileTitleLabel?.text = metadata.fileName
  180. cell.fileTitleLabel?.lineBreakMode = .byTruncatingTail
  181. if metadata.name == NCGlobal.shared.appName {
  182. cell.fileInfoLabel?.text = NSLocalizedString("_in_", comment: "") + " " + utilityFileSystem.getPath(path: metadata.path, user: metadata.user)
  183. } else {
  184. cell.fileInfoLabel?.text = metadata.subline
  185. }
  186. cell.fileSubinfoLabel?.isHidden = true
  187. } else {
  188. cell.fileSubinfoLabel?.isHidden = false
  189. cell.fileTitleLabel?.text = metadata.fileNameView
  190. cell.fileTitleLabel?.lineBreakMode = .byTruncatingMiddle
  191. cell.writeInfoDateSize(date: metadata.date, size: metadata.size)
  192. }
  193. if metadata.status == NCGlobal.shared.metadataStatusDownloading || metadata.status == NCGlobal.shared.metadataStatusUploading {
  194. cell.fileProgressView?.isHidden = false
  195. }
  196. // Accessibility [shared]
  197. if metadata.ownerId != appDelegate.userId, appDelegate.account == metadata.account {
  198. a11yValues.append(NSLocalizedString("_shared_with_you_by_", comment: "") + " " + metadata.ownerDisplayName)
  199. }
  200. if metadata.directory {
  201. let tableDirectory = NCManageDatabase.shared.getTableDirectory(ocId: metadata.ocId)
  202. if metadata.e2eEncrypted {
  203. cell.filePreviewImageView?.image = NCImageCache.images.folderEncrypted
  204. } else if isShare {
  205. cell.filePreviewImageView?.image = NCImageCache.images.folderSharedWithMe
  206. } else if !metadata.shareType.isEmpty {
  207. metadata.shareType.contains(3) ?
  208. (cell.filePreviewImageView?.image = NCImageCache.images.folderPublic) :
  209. (cell.filePreviewImageView?.image = NCImageCache.images.folderSharedWithMe)
  210. } else if !metadata.shareType.isEmpty && metadata.shareType.contains(3) {
  211. cell.filePreviewImageView?.image = NCImageCache.images.folderPublic
  212. } else if metadata.mountType == "group" {
  213. cell.filePreviewImageView?.image = NCImageCache.images.folderGroup
  214. } else if isMounted {
  215. cell.filePreviewImageView?.image = NCImageCache.images.folderExternal
  216. } else if metadata.fileName == autoUploadFileName && metadata.serverUrl == autoUploadDirectory {
  217. cell.filePreviewImageView?.image = NCImageCache.images.folderAutomaticUpload
  218. } else {
  219. cell.filePreviewImageView?.image = NCImageCache.images.folder
  220. }
  221. // Local image: offline
  222. if let tableDirectory, tableDirectory.offline {
  223. cell.fileLocalImage?.image = NCImageCache.images.offlineFlag
  224. }
  225. // color folder
  226. cell.filePreviewImageView?.image = cell.filePreviewImageView?.image?.colorizeFolder(metadata: metadata, tableDirectory: tableDirectory)
  227. } else {
  228. let tableLocalFile = NCManageDatabase.shared.getResultsTableLocalFile(predicate: NSPredicate(format: "ocId == %@", metadata.ocId))?.first
  229. // image local
  230. if let tableLocalFile, tableLocalFile.offline {
  231. a11yValues.append(NSLocalizedString("_offline_", comment: ""))
  232. cell.fileLocalImage?.image = NCImageCache.images.offlineFlag
  233. } else if utilityFileSystem.fileProviderStorageExists(metadata) {
  234. cell.fileLocalImage?.image = NCImageCache.images.local
  235. }
  236. }
  237. // image Favorite
  238. if metadata.favorite {
  239. cell.fileFavoriteImage?.image = NCImageCache.images.favorite
  240. a11yValues.append(NSLocalizedString("_favorite_short_", comment: ""))
  241. }
  242. // Share image
  243. if isShare {
  244. cell.fileSharedImage?.image = NCImageCache.images.shared
  245. } else if !metadata.shareType.isEmpty {
  246. metadata.shareType.contains(3) ?
  247. (cell.fileSharedImage?.image = NCImageCache.images.shareByLink) :
  248. (cell.fileSharedImage?.image = NCImageCache.images.shared)
  249. } else {
  250. cell.fileSharedImage?.image = NCImageCache.images.canShare
  251. }
  252. if appDelegate.account != metadata.account {
  253. cell.fileSharedImage?.image = NCImageCache.images.shared
  254. }
  255. // Button More
  256. if metadata.isInTransfer || metadata.isWaitingTransfer {
  257. cell.setButtonMore(named: NCGlobal.shared.buttonMoreStop, image: NCImageCache.images.buttonStop)
  258. } else if metadata.lock == true {
  259. cell.setButtonMore(named: NCGlobal.shared.buttonMoreLock, image: NCImageCache.images.buttonMoreLock)
  260. a11yValues.append(String(format: NSLocalizedString("_locked_by_", comment: ""), metadata.lockOwnerDisplayName))
  261. } else {
  262. cell.setButtonMore(named: NCGlobal.shared.buttonMoreMore, image: NCImageCache.images.buttonMore)
  263. }
  264. // Write status on Label Info
  265. switch metadata.status {
  266. case NCGlobal.shared.metadataStatusWaitDownload:
  267. cell.fileInfoLabel?.text = utilityFileSystem.transformedSize(metadata.size)
  268. cell.fileSubinfoLabel?.text = infoLabelsSeparator + NSLocalizedString("_status_wait_download_", comment: "")
  269. case NCGlobal.shared.metadataStatusDownloading:
  270. cell.fileInfoLabel?.text = utilityFileSystem.transformedSize(metadata.size)
  271. cell.fileSubinfoLabel?.text = infoLabelsSeparator + "↓ …"
  272. case NCGlobal.shared.metadataStatusWaitUpload:
  273. cell.fileInfoLabel?.text = utilityFileSystem.transformedSize(metadata.size)
  274. cell.fileSubinfoLabel?.text = infoLabelsSeparator + NSLocalizedString("_status_wait_upload_", comment: "")
  275. cell.fileLocalImage?.image = nil
  276. case NCGlobal.shared.metadataStatusUploading:
  277. cell.fileInfoLabel?.text = utilityFileSystem.transformedSize(metadata.size)
  278. cell.fileSubinfoLabel?.text = infoLabelsSeparator + "↑ …"
  279. cell.fileLocalImage?.image = nil
  280. case NCGlobal.shared.metadataStatusUploadError:
  281. if metadata.sessionError.isEmpty {
  282. cell.fileInfoLabel?.text = NSLocalizedString("_status_wait_upload_", comment: "")
  283. } else {
  284. cell.fileInfoLabel?.text = NSLocalizedString("_status_wait_upload_", comment: "") + " " + metadata.sessionError
  285. }
  286. default:
  287. break
  288. }
  289. // Live Photo
  290. if metadata.isLivePhoto {
  291. cell.fileStatusImage?.image = NCImageCache.images.livePhoto
  292. a11yValues.append(NSLocalizedString("_upload_mov_livephoto_", comment: ""))
  293. }
  294. // URL
  295. if metadata.classFile == NKCommon.TypeClassFile.url.rawValue {
  296. cell.fileLocalImage?.image = nil
  297. cell.hideButtonShare(true)
  298. cell.hideButtonMore(true)
  299. if let ownerId = getAvatarFromIconUrl(metadata: metadata) {
  300. cell.fileUser = ownerId
  301. }
  302. }
  303. // Separator
  304. if collectionView.numberOfItems(inSection: indexPath.section) == indexPath.row + 1 || isSearchingMode {
  305. cell.cellSeparatorView?.isHidden = true
  306. } else {
  307. cell.cellSeparatorView?.isHidden = false
  308. }
  309. // Edit mode
  310. if selectOcId.contains(metadata.ocId) {
  311. cell.selected(true, isEditMode: isEditMode)
  312. a11yValues.append(NSLocalizedString("_selected_", comment: ""))
  313. } else {
  314. cell.selected(false, isEditMode: isEditMode)
  315. }
  316. // Accessibility
  317. cell.setAccessibility(label: metadata.fileNameView + ", " + (cell.fileInfoLabel?.text ?? "") + (cell.fileSubinfoLabel?.text ?? ""), value: a11yValues.joined(separator: ", "))
  318. // Color string find in search
  319. cell.fileTitleLabel?.textColor = NCBrandColor.shared.textColor
  320. cell.fileTitleLabel?.font = .systemFont(ofSize: 15)
  321. if isSearchingMode, let literalSearch = self.literalSearch, let title = cell.fileTitleLabel?.text {
  322. let longestWordRange = (title.lowercased() as NSString).range(of: literalSearch)
  323. let attributedString = NSMutableAttributedString(string: title, attributes: [NSAttributedString.Key.font: UIFont.systemFont(ofSize: 15)])
  324. attributedString.setAttributes([NSAttributedString.Key.font: UIFont.boldSystemFont(ofSize: 15), NSAttributedString.Key.foregroundColor: UIColor.systemBlue], range: longestWordRange)
  325. cell.fileTitleLabel?.attributedText = attributedString
  326. }
  327. // Layout photo
  328. if layoutForView?.layout == NCGlobal.shared.layoutPhotoRatio || layoutForView?.layout == NCGlobal.shared.layoutPhotoSquare {
  329. if metadata.directory {
  330. cell.filePreviewImageBottom?.constant = 10
  331. cell.fileTitleLabel?.text = metadata.fileNameView
  332. } else {
  333. cell.filePreviewImageBottom?.constant = 0
  334. cell.fileTitleLabel?.text = ""
  335. }
  336. }
  337. // TAGS
  338. cell.setTags(tags: Array(metadata.tags))
  339. // Hide buttons
  340. if metadata.name != NCGlobal.shared.appName {
  341. cell.titleInfoTrailingFull()
  342. cell.hideButtonShare(true)
  343. cell.hideButtonMore(true)
  344. }
  345. cell.setIconOutlines()
  346. return cell
  347. }
  348. func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
  349. if kind == UICollectionView.elementKindSectionHeader || kind == mediaSectionHeader {
  350. if dataSource.getMetadataSourceForAllSections().isEmpty {
  351. guard let header = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "sectionFirstHeaderEmptyData", for: indexPath) as? NCSectionFirstHeaderEmptyData else { return NCSectionFirstHeaderEmptyData() }
  352. self.sectionFirstHeaderEmptyData = header
  353. header.delegate = self
  354. if !isSearchingMode, headerMenuTransferView, let ocId = NCNetworking.shared.transferInForegorund?.ocId {
  355. let text = String(format: NSLocalizedString("_upload_foreground_msg_", comment: ""), NCBrandOptions.shared.brand)
  356. header.setViewTransfer(isHidden: false, ocId: ocId, text: text, progress: NCNetworking.shared.transferInForegorund?.progress)
  357. } else {
  358. header.setViewTransfer(isHidden: true)
  359. }
  360. if isSearchingMode {
  361. header.emptyImage.image = utility.loadImage(named: "magnifyingglass", colors: [NCBrandColor.shared.brandElement])
  362. if self.dataSourceTask?.state == .running {
  363. header.emptyTitle.text = NSLocalizedString("_search_in_progress_", comment: "")
  364. } else {
  365. header.emptyTitle.text = NSLocalizedString("_search_no_record_found_", comment: "")
  366. }
  367. header.emptyDescription.text = NSLocalizedString("_search_instruction_", comment: "")
  368. } else if self.dataSourceTask?.state == .running {
  369. header.emptyImage.image = utility.loadImage(named: "wifi", colors: [NCBrandColor.shared.brandElement])
  370. header.emptyTitle.text = NSLocalizedString("_request_in_progress_", comment: "")
  371. header.emptyDescription.text = ""
  372. } else {
  373. if serverUrl.isEmpty {
  374. header.emptyImage.image = emptyImage
  375. header.emptyTitle.text = NSLocalizedString(emptyTitle, comment: "")
  376. header.emptyDescription.text = NSLocalizedString(emptyDescription, comment: "")
  377. } else {
  378. header.emptyImage.image = NCImageCache.images.folder
  379. header.emptyTitle.text = NSLocalizedString("_files_no_files_", comment: "")
  380. header.emptyDescription.text = NSLocalizedString("_no_file_pull_down_", comment: "")
  381. }
  382. }
  383. return header
  384. } else if indexPath.section == 0 {
  385. guard let header = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "sectionFirstHeader", for: indexPath) as? NCSectionFirstHeader else { return NCSectionFirstHeader() }
  386. let (_, heightHeaderRichWorkspace, heightHeaderSection) = getHeaderHeight(section: indexPath.section)
  387. self.sectionFirstHeader = header
  388. header.delegate = self
  389. if !isSearchingMode, headerMenuTransferView, let ocId = NCNetworking.shared.transferInForegorund?.ocId {
  390. let text = String(format: NSLocalizedString("_upload_foreground_msg_", comment: ""), NCBrandOptions.shared.brand)
  391. header.setViewTransfer(isHidden: false, ocId: ocId, text: text, progress: NCNetworking.shared.transferInForegorund?.progress)
  392. } else {
  393. header.setViewTransfer(isHidden: true)
  394. }
  395. header.setRichWorkspaceHeight(heightHeaderRichWorkspace)
  396. header.setRichWorkspaceText(richWorkspaceText)
  397. header.setSectionHeight(heightHeaderSection)
  398. if heightHeaderSection == 0 {
  399. header.labelSection.text = ""
  400. } else {
  401. header.labelSection.text = self.dataSource.getSectionValueLocalization(indexPath: indexPath)
  402. }
  403. header.labelSection.textColor = NCBrandColor.shared.textColor
  404. return header
  405. } else {
  406. guard let header = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "sectionHeader", for: indexPath) as? NCSectionHeader else { return NCSectionHeader() }
  407. header.labelSection.text = self.dataSource.getSectionValueLocalization(indexPath: indexPath)
  408. header.labelSection.textColor = NCBrandColor.shared.textColor
  409. return header
  410. }
  411. } else {
  412. guard let footer = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "sectionFooter", for: indexPath) as? NCSectionFooter else { return NCSectionFooter() }
  413. let sections = dataSource.numberOfSections()
  414. let section = indexPath.section
  415. let metadataForSection = self.dataSource.getMetadataForSection(indexPath.section)
  416. let isPaginated = metadataForSection?.lastSearchResult?.isPaginated ?? false
  417. let metadatasCount: Int = metadataForSection?.metadatas.count ?? 0
  418. let unifiedSearchInProgress = metadataForSection?.unifiedSearchInProgress ?? false
  419. footer.delegate = self
  420. footer.metadataForSection = metadataForSection
  421. footer.setTitleLabel("")
  422. footer.setButtonText(NSLocalizedString("_show_more_results_", comment: ""))
  423. footer.separatorIsHidden(true)
  424. footer.buttonIsHidden(true)
  425. footer.hideActivityIndicatorSection()
  426. if isSearchingMode {
  427. if sections > 1 && section != sections - 1 {
  428. footer.separatorIsHidden(false)
  429. }
  430. // If the number of entries(metadatas) is lower than the cursor, then there are no more entries.
  431. // The blind spot in this is when the number of entries is the same as the cursor. If so, we don't have a way of knowing if there are no more entries.
  432. // This is as good as it gets for determining last page without server-side flag.
  433. let isLastPage = (metadatasCount < metadataForSection?.lastSearchResult?.cursor ?? 0) || metadataForSection?.lastSearchResult?.entries.isEmpty == true
  434. if isSearchingMode && isPaginated && metadatasCount > 0 && !isLastPage {
  435. footer.buttonIsHidden(false)
  436. }
  437. if unifiedSearchInProgress {
  438. footer.showActivityIndicatorSection()
  439. }
  440. } else {
  441. if sections == 1 || section == sections - 1 {
  442. let info = dataSource.getFooterInformationAllMetadatas()
  443. footer.setTitleLabel(directories: info.directories, files: info.files, size: info.size)
  444. } else {
  445. footer.separatorIsHidden(false)
  446. }
  447. }
  448. return footer
  449. }
  450. }
  451. // MARK: -
  452. func getAvatarFromIconUrl(metadata: tableMetadata) -> String? {
  453. var ownerId: String?
  454. if metadata.iconUrl.contains("http") && metadata.iconUrl.contains("avatar") {
  455. let splitIconUrl = metadata.iconUrl.components(separatedBy: "/")
  456. var found: Bool = false
  457. for item in splitIconUrl {
  458. if found {
  459. ownerId = item
  460. break
  461. }
  462. if item == "avatar" { found = true}
  463. }
  464. }
  465. return ownerId
  466. }
  467. }