NCCollectionCommon.swift 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036
  1. //
  2. // NCCollectionCommon.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 08/09/2020.
  6. // Copyright © 2020 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 TLPhotoPicker
  25. import ZIPFoundation
  26. import NCCommunication
  27. class NCCollectionCommon: NSObject {
  28. @objc static let shared: NCCollectionCommon = {
  29. let instance = NCCollectionCommon()
  30. instance.createImagesThemingColor()
  31. return instance
  32. }()
  33. struct NCCollectionCommonImages {
  34. static var cellSharedImage = UIImage()
  35. static var cellCanShareImage = UIImage()
  36. static var cellShareByLinkImage = UIImage()
  37. static var cellFavouriteImage = UIImage()
  38. static var cellMoreImage = UIImage()
  39. static var cellCommentImage = UIImage()
  40. static var cellLivePhotoImage = UIImage()
  41. static var cellFolderEncryptedImage = UIImage()
  42. static var cellFolderSharedWithMeImage = UIImage()
  43. static var cellFolderPublicImage = UIImage()
  44. static var cellFolderGroupImage = UIImage()
  45. static var cellFolderExternalImage = UIImage()
  46. static var cellFolderAutomaticUploadImage = UIImage()
  47. static var cellFolderImage = UIImage()
  48. static var cellPlayImage = UIImage()
  49. }
  50. @objc func createImagesThemingColor() {
  51. NCCollectionCommonImages.cellSharedImage = CCGraphics.changeThemingColorImage(UIImage.init(named: "share"), width: 100, height: 100, color: NCBrandColor.sharedInstance.textView)
  52. NCCollectionCommonImages.cellCanShareImage = CCGraphics.changeThemingColorImage(UIImage.init(named: "share"), width: 100, height: 100, color: NCBrandColor.sharedInstance.optionItem)
  53. NCCollectionCommonImages.cellShareByLinkImage = CCGraphics.changeThemingColorImage(UIImage.init(named: "sharebylink"), width: 100, height: 100, color: NCBrandColor.sharedInstance.optionItem)
  54. NCCollectionCommonImages.cellFavouriteImage = CCGraphics.changeThemingColorImage(UIImage.init(named: "favorite"), width: 100, height: 100, color: NCBrandColor.sharedInstance.yellowFavorite)
  55. NCCollectionCommonImages.cellMoreImage = CCGraphics.changeThemingColorImage(UIImage.init(named: "more"), width: 50, height: 50, color: NCBrandColor.sharedInstance.optionItem)
  56. NCCollectionCommonImages.cellCommentImage = CCGraphics.changeThemingColorImage(UIImage.init(named: "comment"), width: 30, height: 30, color: NCBrandColor.sharedInstance.graySoft)
  57. NCCollectionCommonImages.cellLivePhotoImage = CCGraphics.changeThemingColorImage(UIImage.init(named: "livePhoto"), width: 100, height: 100, color: NCBrandColor.sharedInstance.textView)
  58. NCCollectionCommonImages.cellFolderEncryptedImage = CCGraphics.changeThemingColorImage(UIImage.init(named: "folderEncrypted"), width: 600, height: 600, color: NCBrandColor.sharedInstance.brandElement)
  59. NCCollectionCommonImages.cellFolderSharedWithMeImage = CCGraphics.changeThemingColorImage(UIImage.init(named: "folder_shared_with_me"), width: 600, height: 600, color: NCBrandColor.sharedInstance.brandElement)
  60. NCCollectionCommonImages.cellFolderPublicImage = CCGraphics.changeThemingColorImage(UIImage.init(named: "folder_public"), width: 600, height: 600, color: NCBrandColor.sharedInstance.brandElement)
  61. NCCollectionCommonImages.cellFolderGroupImage = CCGraphics.changeThemingColorImage(UIImage.init(named: "folder_group"), width: 600, height: 600, color: NCBrandColor.sharedInstance.brandElement)
  62. NCCollectionCommonImages.cellFolderExternalImage = CCGraphics.changeThemingColorImage(UIImage.init(named: "folder_external"), width: 600, height: 600, color: NCBrandColor.sharedInstance.brandElement)
  63. NCCollectionCommonImages.cellFolderAutomaticUploadImage = CCGraphics.changeThemingColorImage(UIImage.init(named: "folderAutomaticUpload"), width: 600, height: 600, color: NCBrandColor.sharedInstance.brandElement)
  64. NCCollectionCommonImages.cellFolderImage = CCGraphics.changeThemingColorImage(UIImage.init(named: "folder"), width: 600, height: 600, color: NCBrandColor.sharedInstance.brandElement)
  65. NCCollectionCommonImages.cellPlayImage = CCGraphics.changeThemingColorImage(UIImage.init(named: "play"), width: 100, height: 100, color: .white)
  66. }
  67. // MARK -
  68. func cellForItemAt(indexPath: IndexPath, collectionView: UICollectionView, cell: UICollectionViewCell, metadata: tableMetadata, metadataFolder: tableMetadata?, serverUrl: String, isEditMode: Bool, selectocId: [String], autoUploadFileName: String, autoUploadDirectory: String, hideButtonMore: Bool, downloadThumbnail: Bool, shares: [tableShare]?, source: UIViewController) {
  69. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  70. var tableShare: tableShare?
  71. // Share
  72. if shares != nil {
  73. for share in shares! {
  74. if share.fileName == metadata.fileName {
  75. tableShare = share
  76. break
  77. }
  78. }
  79. }
  80. // Download preview
  81. if downloadThumbnail {
  82. NCOperationQueue.shared.downloadThumbnail(metadata: metadata, urlBase: appDelegate.urlBase, view: collectionView, indexPath: indexPath)
  83. }
  84. var isShare = false
  85. var isMounted = false
  86. if metadataFolder != nil {
  87. isShare = metadata.permissions.contains(k_permission_shared) && !metadataFolder!.permissions.contains(k_permission_shared)
  88. isMounted = metadata.permissions.contains(k_permission_mounted) && !metadataFolder!.permissions.contains(k_permission_mounted)
  89. }
  90. if cell is NCListCell {
  91. let cell = cell as! NCListCell
  92. cell.delegate = source as? NCListCellDelegate
  93. cell.objectId = metadata.ocId
  94. cell.indexPath = indexPath
  95. cell.labelTitle.text = metadata.fileNameView
  96. cell.labelTitle.textColor = NCBrandColor.sharedInstance.textView
  97. cell.imageStatus.image = nil
  98. cell.imageLocal.image = nil
  99. cell.imageFavorite.image = nil
  100. cell.imageShared.image = nil
  101. if metadata.directory {
  102. if metadata.e2eEncrypted {
  103. cell.imageItem.image = NCCollectionCommonImages.cellFolderEncryptedImage
  104. } else if isShare {
  105. cell.imageItem.image = NCCollectionCommonImages.cellFolderSharedWithMeImage
  106. } else if (tableShare != nil && tableShare!.shareType != 3) {
  107. cell.imageItem.image = NCCollectionCommonImages.cellFolderSharedWithMeImage
  108. } else if (tableShare != nil && tableShare!.shareType == 3) {
  109. cell.imageItem.image = NCCollectionCommonImages.cellFolderPublicImage
  110. } else if metadata.mountType == "group" {
  111. cell.imageItem.image = NCCollectionCommonImages.cellFolderGroupImage
  112. } else if isMounted {
  113. cell.imageItem.image = NCCollectionCommonImages.cellFolderExternalImage
  114. } else if metadata.fileName == autoUploadFileName && serverUrl == autoUploadDirectory {
  115. cell.imageItem.image = NCCollectionCommonImages.cellFolderAutomaticUploadImage
  116. } else {
  117. cell.imageItem.image = NCCollectionCommonImages.cellFolderImage
  118. }
  119. cell.labelInfo.text = CCUtility.dateDiff(metadata.date as Date)
  120. let lockServerUrl = CCUtility.stringAppendServerUrl(serverUrl, addFileName: metadata.fileName)!
  121. let tableDirectory = NCManageDatabase.sharedInstance.getTableDirectory(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@", appDelegate.account, lockServerUrl))
  122. // Local image: offline
  123. if tableDirectory != nil && tableDirectory!.offline {
  124. cell.imageLocal.image = UIImage.init(named: "offlineFlag")
  125. }
  126. } else {
  127. if FileManager().fileExists(atPath: CCUtility.getDirectoryProviderStorageIconOcId(metadata.ocId, etag: metadata.etag)) {
  128. cell.imageItem.image = UIImage(contentsOfFile: CCUtility.getDirectoryProviderStorageIconOcId(metadata.ocId, etag: metadata.etag))
  129. } else if(!metadata.hasPreview) {
  130. if metadata.iconName.count > 0 {
  131. cell.imageItem.image = UIImage.init(named: metadata.iconName)
  132. } else {
  133. cell.imageItem.image = UIImage.init(named: "file")
  134. }
  135. }
  136. if cell.imageItem.image == nil {
  137. cell.imageItem.backgroundColor = .lightGray
  138. } else {
  139. cell.imageItem.backgroundColor = nil
  140. }
  141. cell.labelInfo.text = CCUtility.dateDiff(metadata.date as Date) + " · " + CCUtility.transformedSize(metadata.size)
  142. // image local
  143. let size = CCUtility.fileProviderStorageSize(metadata.ocId, fileNameView: metadata.fileNameView)
  144. if size > 0 {
  145. let tableLocalFile = NCManageDatabase.sharedInstance.getTableLocalFile(predicate: NSPredicate(format: "ocId == %@", metadata.ocId))
  146. if tableLocalFile == nil && size == metadata.size {
  147. NCManageDatabase.sharedInstance.addLocalFile(metadata: metadata)
  148. }
  149. if tableLocalFile?.offline ?? false {
  150. cell.imageLocal.image = UIImage.init(named: "offlineFlag")
  151. } else{
  152. cell.imageLocal.image = UIImage.init(named: "local")
  153. }
  154. }
  155. }
  156. // image Favorite
  157. if metadata.favorite {
  158. cell.imageFavorite.image = NCCollectionCommonImages.cellFavouriteImage
  159. }
  160. // Share image
  161. if (isShare) {
  162. cell.imageShared.image = NCCollectionCommonImages.cellSharedImage
  163. } else if (tableShare != nil && tableShare!.shareType == 3) {
  164. cell.imageShared.image = NCCollectionCommonImages.cellShareByLinkImage
  165. } else if (tableShare != nil && tableShare!.shareType != 3) {
  166. cell.imageShared.image = NCCollectionCommonImages.cellSharedImage
  167. } else {
  168. cell.imageShared.image = NCCollectionCommonImages.cellCanShareImage
  169. }
  170. if metadata.ownerId.count > 0 && metadata.ownerId != appDelegate.userID {
  171. // Load avatar
  172. let fileNameSource = CCUtility.getDirectoryUserData() + "/" + CCUtility.getStringUser(appDelegate.user, urlBase: appDelegate.urlBase) + "-" + metadata.ownerId + ".png"
  173. let fileNameSourceAvatar = CCUtility.getDirectoryUserData() + "/" + CCUtility.getStringUser(appDelegate.user, urlBase: appDelegate.urlBase) + "-avatar-" + metadata.ownerId + ".png"
  174. if FileManager.default.fileExists(atPath: fileNameSourceAvatar) {
  175. cell.imageShared.image = UIImage(contentsOfFile: fileNameSourceAvatar)
  176. } else if FileManager.default.fileExists(atPath: fileNameSource) {
  177. cell.imageShared.image = NCUtility.shared.createAvatar(fileNameSource: fileNameSource, fileNameSourceAvatar: fileNameSourceAvatar)
  178. } else {
  179. NCCommunication.shared.downloadAvatar(userID: metadata.ownerId, fileNameLocalPath: fileNameSource, size: Int(k_avatar_size)) { (account, data, errorCode, errorMessage) in
  180. if errorCode == 0 && account == appDelegate.account {
  181. cell.imageShared.image = NCUtility.shared.createAvatar(fileNameSource: fileNameSource, fileNameSourceAvatar: fileNameSourceAvatar)
  182. }
  183. }
  184. }
  185. }
  186. if isEditMode {
  187. cell.imageItemLeftConstraint.constant = 45
  188. cell.imageSelect.isHidden = false
  189. if selectocId.contains(metadata.ocId) {
  190. cell.imageSelect.image = CCGraphics.scale(UIImage.init(named: "checkedYes"), to: CGSize(width: 50, height: 50), isAspectRation: true)
  191. cell.backgroundView = NCUtility.shared.cellBlurEffect(with: cell.bounds)
  192. } else {
  193. cell.imageSelect.image = CCGraphics.scale(UIImage.init(named: "checkedNo"), to: CGSize(width: 50, height: 50), isAspectRation: true)
  194. cell.backgroundView = nil
  195. }
  196. } else {
  197. cell.imageItemLeftConstraint.constant = 10
  198. cell.imageSelect.isHidden = true
  199. cell.backgroundView = nil
  200. }
  201. // Transfer
  202. if metadata.status == k_metadataStatusInDownload || metadata.status == k_metadataStatusDownloading || metadata.status >= k_metadataStatusTypeUpload {
  203. cell.progressView.isHidden = false
  204. cell.setButtonMore(named: "stop")
  205. } else {
  206. cell.progressView.isHidden = true
  207. cell.progressView.progress = 0.0
  208. cell.setButtonMore(named: "more")
  209. }
  210. // Remove last separator
  211. if collectionView.numberOfItems(inSection: indexPath.section) == indexPath.row + 1 {
  212. cell.separator.isHidden = true
  213. } else {
  214. cell.separator.isHidden = false
  215. }
  216. } else if cell is NCGridCell {
  217. let cell = cell as! NCGridCell
  218. cell.delegate = source as? NCGridCellDelegate
  219. cell.objectId = metadata.ocId
  220. cell.indexPath = indexPath
  221. cell.labelTitle.text = metadata.fileNameView
  222. cell.labelTitle.textColor = NCBrandColor.sharedInstance.textView
  223. cell.imageStatus.image = nil
  224. cell.imageLocal.image = nil
  225. cell.imageFavorite.image = nil
  226. if metadata.directory {
  227. if metadata.e2eEncrypted {
  228. cell.imageItem.image = NCCollectionCommonImages.cellFolderEncryptedImage
  229. } else if isShare {
  230. cell.imageItem.image = NCCollectionCommonImages.cellFolderSharedWithMeImage
  231. } else if (tableShare != nil && tableShare!.shareType != 3) {
  232. cell.imageItem.image = NCCollectionCommonImages.cellFolderSharedWithMeImage
  233. } else if (tableShare != nil && tableShare!.shareType == 3) {
  234. cell.imageItem.image = NCCollectionCommonImages.cellFolderPublicImage
  235. } else if metadata.mountType == "group" {
  236. cell.imageItem.image = NCCollectionCommonImages.cellFolderGroupImage
  237. } else if isMounted {
  238. cell.imageItem.image = NCCollectionCommonImages.cellFolderExternalImage
  239. } else if metadata.fileName == autoUploadFileName && serverUrl == autoUploadDirectory {
  240. cell.imageItem.image = NCCollectionCommonImages.cellFolderAutomaticUploadImage
  241. } else {
  242. cell.imageItem.image = NCCollectionCommonImages.cellFolderImage
  243. }
  244. let lockServerUrl = CCUtility.stringAppendServerUrl(serverUrl, addFileName: metadata.fileName)!
  245. let tableDirectory = NCManageDatabase.sharedInstance.getTableDirectory(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@", appDelegate.account, lockServerUrl))
  246. // Local image: offline
  247. if tableDirectory != nil && tableDirectory!.offline {
  248. cell.imageLocal.image = UIImage.init(named: "offlineFlag")
  249. }
  250. } else {
  251. if FileManager().fileExists(atPath: CCUtility.getDirectoryProviderStorageIconOcId(metadata.ocId, etag: metadata.etag)) {
  252. cell.imageItem.image = UIImage(contentsOfFile: CCUtility.getDirectoryProviderStorageIconOcId(metadata.ocId, etag: metadata.etag))
  253. } else if(!metadata.hasPreview) {
  254. if metadata.iconName.count > 0 {
  255. cell.imageItem.image = UIImage.init(named: metadata.iconName)
  256. } else {
  257. cell.imageItem.image = UIImage.init(named: "file")
  258. }
  259. }
  260. if cell.imageItem.image == nil {
  261. cell.imageItem.backgroundColor = .lightGray
  262. } else {
  263. cell.imageItem.backgroundColor = nil
  264. }
  265. // image Local
  266. let tableLocalFile = NCManageDatabase.sharedInstance.getTableLocalFile(predicate: NSPredicate(format: "ocId == %@", metadata.ocId))
  267. if tableLocalFile != nil && CCUtility.fileProviderStorageExists(metadata.ocId, fileNameView: metadata.fileNameView) {
  268. if tableLocalFile!.offline { cell.imageLocal.image = UIImage.init(named: "offlineFlag") }
  269. else { cell.imageLocal.image = UIImage.init(named: "local") }
  270. }
  271. }
  272. // image Favorite
  273. if metadata.favorite {
  274. cell.imageFavorite.image = NCCollectionCommonImages.cellFavouriteImage
  275. }
  276. if isEditMode {
  277. cell.imageSelect.isHidden = false
  278. if selectocId.contains(metadata.ocId) {
  279. cell.imageSelect.image = CCGraphics.scale(UIImage.init(named: "checkedYes"), to: CGSize(width: 50, height: 50), isAspectRation: true)
  280. cell.backgroundView = NCUtility.shared.cellBlurEffect(with: cell.bounds)
  281. } else {
  282. cell.imageSelect.isHidden = true
  283. cell.backgroundView = nil
  284. }
  285. } else {
  286. cell.imageSelect.isHidden = true
  287. cell.backgroundView = nil
  288. }
  289. // Transfer
  290. if metadata.status == k_metadataStatusInDownload || metadata.status == k_metadataStatusDownloading || metadata.status >= k_metadataStatusTypeUpload {
  291. cell.progressView.isHidden = false
  292. cell.setButtonMore(named: "stop")
  293. } else {
  294. cell.progressView.isHidden = true
  295. cell.progressView.progress = 0.0
  296. cell.setButtonMore(named: "more")
  297. }
  298. }
  299. }
  300. // MARK -
  301. func notificationDeleteFile(collectionView: UICollectionView?, dataSource: NCDataSource?, metadata: tableMetadata, errorCode: Int, errorDescription: String ,onlyLocal: Bool) {
  302. if errorCode == 0 {
  303. if !onlyLocal {
  304. _ = dataSource?.deleteMetadata(ocId: metadata.ocId)
  305. }
  306. collectionView?.reloadData()
  307. } else {
  308. NCContentPresenter.shared.messageNotification("_error_", description: errorDescription, delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
  309. }
  310. }
  311. func notificationDownloadStartFile(collectionView: UICollectionView?, dataSource: NCDataSource?, metadata: tableMetadata) {
  312. dataSource?.reloadMetadata(ocId: metadata.ocId)
  313. collectionView?.reloadData()
  314. }
  315. func notificationDownloadedFile(collectionView: UICollectionView?, dataSource: NCDataSource?, metadata: tableMetadata) {
  316. dataSource?.reloadMetadata(ocId: metadata.ocId)
  317. collectionView?.reloadData()
  318. }
  319. func notificationDownloadCancelFile(collectionView: UICollectionView?, dataSource: NCDataSource?, metadata: tableMetadata) {
  320. dataSource?.reloadMetadata(ocId: metadata.ocId)
  321. collectionView?.reloadData()
  322. }
  323. func notificationUploadStartFile(collectionView: UICollectionView?, dataSource: NCDataSource?, metadata: tableMetadata, serverUrl: String, account: String) {
  324. if metadata.serverUrl == serverUrl && metadata.account == account {
  325. dataSource?.addMetadata(metadata)
  326. collectionView?.reloadData()
  327. }
  328. }
  329. func notificationUploadedFile(collectionView: UICollectionView?, dataSource: NCDataSource?, metadata: tableMetadata, ocIdTemp: String, serverUrl: String, account: String) {
  330. if metadata.serverUrl == serverUrl && metadata.account == account {
  331. dataSource?.reloadMetadata(ocId: metadata.ocId, ocIdTemp: ocIdTemp)
  332. collectionView?.reloadData()
  333. }
  334. }
  335. func notificationUploadCancelFile(collectionView: UICollectionView?, dataSource: NCDataSource?, metadata: tableMetadata, serverUrl: String, account: String) {
  336. if metadata.serverUrl == serverUrl && metadata.account == account {
  337. dataSource?.deleteMetadata(ocId: metadata.ocId)
  338. collectionView?.reloadData()
  339. }
  340. }
  341. func notificationTriggerProgressTask(collectionView: UICollectionView?, dataSource: NCDataSource?, ocId: String, progress: Float) {
  342. if let index = dataSource?.getIndexMetadata(ocId: ocId) {
  343. if let cell = collectionView?.cellForItem(at: IndexPath(row: index, section: 0)) {
  344. if cell is NCListCell {
  345. let cell = cell as! NCListCell
  346. if progress > 0 {
  347. cell.progressView?.isHidden = false
  348. cell.progressView?.progress = progress
  349. cell.setButtonMore(named: "stop")
  350. }
  351. } else if cell is NCGridCell {
  352. let cell = cell as! NCGridCell
  353. if progress > 0 {
  354. cell.progressView.isHidden = false
  355. cell.progressView.progress = progress
  356. cell.setButtonMore(named: "stop")
  357. }
  358. }
  359. }
  360. }
  361. }
  362. }
  363. // MARK: - List Layout
  364. class NCListLayout: UICollectionViewFlowLayout {
  365. let itemHeight: CGFloat = 60
  366. override init() {
  367. super.init()
  368. sectionHeadersPinToVisibleBounds = false
  369. minimumInteritemSpacing = 0
  370. minimumLineSpacing = 1
  371. self.scrollDirection = .vertical
  372. self.sectionInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
  373. }
  374. required init?(coder aDecoder: NSCoder) {
  375. fatalError("init(coder:) has not been implemented")
  376. }
  377. override var itemSize: CGSize {
  378. get {
  379. if let collectionView = collectionView {
  380. let itemWidth: CGFloat = collectionView.frame.width
  381. return CGSize(width: itemWidth, height: self.itemHeight)
  382. }
  383. // Default fallback
  384. return CGSize(width: 100, height: 100)
  385. }
  386. set {
  387. super.itemSize = newValue
  388. }
  389. }
  390. override func targetContentOffset(forProposedContentOffset proposedContentOffset: CGPoint) -> CGPoint {
  391. return proposedContentOffset
  392. }
  393. }
  394. // MARK: - Grid Layout
  395. class NCGridLayout: UICollectionViewFlowLayout {
  396. var heightLabelPlusButton: CGFloat = 45
  397. var marginLeftRight: CGFloat = 6
  398. var itemForLine: CGFloat = 3
  399. override init() {
  400. super.init()
  401. sectionHeadersPinToVisibleBounds = false
  402. minimumInteritemSpacing = 1
  403. minimumLineSpacing = marginLeftRight
  404. self.scrollDirection = .vertical
  405. self.sectionInset = UIEdgeInsets(top: 10, left: marginLeftRight, bottom: 0, right: marginLeftRight)
  406. }
  407. required init?(coder aDecoder: NSCoder) {
  408. fatalError("init(coder:) has not been implemented")
  409. }
  410. override var itemSize: CGSize {
  411. get {
  412. if let collectionView = collectionView {
  413. let itemWidth: CGFloat = (collectionView.frame.width - marginLeftRight * 2 - marginLeftRight * (itemForLine - 1)) / itemForLine
  414. let itemHeight: CGFloat = itemWidth + heightLabelPlusButton
  415. return CGSize(width: itemWidth, height: itemHeight)
  416. }
  417. // Default fallback
  418. return CGSize(width: 100, height: 100)
  419. }
  420. set {
  421. super.itemSize = newValue
  422. }
  423. }
  424. override func targetContentOffset(forProposedContentOffset proposedContentOffset: CGPoint) -> CGPoint {
  425. return proposedContentOffset
  426. }
  427. }
  428. // MARK: - NCSelect
  429. extension NCCollectionCommon: NCSelectDelegate {
  430. func dismissSelect(serverUrl: String?, metadata: tableMetadata?, type: String, array: [Any], buttonType: String, overwrite: Bool) {
  431. if (serverUrl != nil && array.count > 0) {
  432. var move = true
  433. if buttonType == "done1" { move = false }
  434. for metadata in array as! [tableMetadata] {
  435. NCOperationQueue.shared.copyMove(metadata: metadata, serverUrl: serverUrl!, overwrite: overwrite, move: move)
  436. }
  437. }
  438. }
  439. func openSelectView(viewController: UIViewController, array: [Any]) {
  440. let navigationController = UIStoryboard.init(name: "NCSelect", bundle: nil).instantiateInitialViewController() as! UINavigationController
  441. let vc = navigationController.topViewController as! NCSelect
  442. vc.delegate = self
  443. vc.hideButtonCreateFolder = false
  444. vc.selectFile = false
  445. vc.includeDirectoryE2EEncryption = false
  446. vc.includeImages = false
  447. vc.type = ""
  448. vc.titleButtonDone = NSLocalizedString("_move_", comment: "")
  449. vc.titleButtonDone1 = NSLocalizedString("_copy_",comment: "")
  450. vc.isButtonDone1Hide = false
  451. vc.isOverwriteHide = false
  452. vc.keyLayout = k_layout_view_move
  453. vc.array = array
  454. navigationController.modalPresentationStyle = .fullScreen
  455. viewController.present(navigationController, animated: true, completion: nil)
  456. }
  457. }
  458. // MARK: - Nextcloud CollectionView Common
  459. class NCCollectionViewCommon: UIViewController, UIGestureRecognizerDelegate, NCListCellDelegate, NCGridCellDelegate, NCSectionHeaderMenuDelegate, DZNEmptyDataSetSource, DZNEmptyDataSetDelegate {
  460. @IBOutlet weak var collectionView: UICollectionView!
  461. @objc var serverUrl = ""
  462. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  463. internal var metadataPush: tableMetadata?
  464. internal var isEditMode = false
  465. internal var selectOcId: [String] = []
  466. internal var dataSource: NCDataSource?
  467. internal var layout = ""
  468. internal var groupBy = ""
  469. internal var titleButton = ""
  470. internal var itemForLine = 0
  471. private var autoUploadFileName = ""
  472. private var autoUploadDirectory = ""
  473. private var listLayout: NCListLayout!
  474. private var gridLayout: NCGridLayout!
  475. private let headerMenuHeight: CGFloat = 50
  476. private let sectionHeaderHeight: CGFloat = 20
  477. private let footerHeight: CGFloat = 50
  478. internal let refreshControl = UIRefreshControl()
  479. // DECLARE
  480. internal var layoutKey = ""
  481. internal var titleCurrentFolder = ""
  482. required init?(coder aDecoder: NSCoder) {
  483. super.init(coder: aDecoder)
  484. }
  485. override func viewDidLoad() {
  486. super.viewDidLoad()
  487. // Cell
  488. collectionView.register(UINib.init(nibName: "NCListCell", bundle: nil), forCellWithReuseIdentifier: "listCell")
  489. collectionView.register(UINib.init(nibName: "NCGridCell", bundle: nil), forCellWithReuseIdentifier: "gridCell")
  490. // Header
  491. collectionView.register(UINib.init(nibName: "NCSectionHeaderMenu", bundle: nil), forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "sectionHeaderMenu")
  492. collectionView.register(UINib.init(nibName: "NCSectionHeader", bundle: nil), forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "sectionHeader")
  493. // Footer
  494. collectionView.register(UINib.init(nibName: "NCSectionFooter", bundle: nil), forSupplementaryViewOfKind: UICollectionView.elementKindSectionFooter, withReuseIdentifier: "sectionFooter")
  495. collectionView.alwaysBounceVertical = true
  496. listLayout = NCListLayout()
  497. gridLayout = NCGridLayout()
  498. // Refresh Control
  499. collectionView.addSubview(refreshControl)
  500. refreshControl.tintColor = NCBrandColor.sharedInstance.brandText
  501. refreshControl.backgroundColor = NCBrandColor.sharedInstance.brandElement
  502. refreshControl.addTarget(self, action: #selector(reloadDataSourceNetwork), for: .valueChanged)
  503. // empty Data Source
  504. self.collectionView.emptyDataSetDelegate = self
  505. self.collectionView.emptyDataSetSource = self
  506. // 3D Touch peek and pop
  507. if traitCollection.forceTouchCapability == .available {
  508. registerForPreviewing(with: self, sourceView: view)
  509. }
  510. NotificationCenter.default.addObserver(self, selector: #selector(changeTheming), name: NSNotification.Name(rawValue: k_notificationCenter_changeTheming), object: nil)
  511. NotificationCenter.default.addObserver(self, selector: #selector(reloadDataSource), name: NSNotification.Name(rawValue: k_notificationCenter_reloadDataSource), object: nil)
  512. NotificationCenter.default.addObserver(self, selector: #selector(deleteFile(_:)), name: NSNotification.Name(rawValue: k_notificationCenter_deleteFile), object: nil)
  513. NotificationCenter.default.addObserver(self, selector: #selector(downloadStartFile(_:)), name: NSNotification.Name(rawValue: k_notificationCenter_downloadStartFile), object: nil)
  514. NotificationCenter.default.addObserver(self, selector: #selector(downloadedFile(_:)), name: NSNotification.Name(rawValue: k_notificationCenter_downloadedFile), object: nil)
  515. NotificationCenter.default.addObserver(self, selector: #selector(downloadCancelFile(_:)), name: NSNotification.Name(rawValue: k_notificationCenter_downloadCancelFile), object: nil)
  516. NotificationCenter.default.addObserver(self, selector: #selector(uploadStartFile(_:)), name: NSNotification.Name(rawValue: k_notificationCenter_uploadStartFile), object: nil)
  517. NotificationCenter.default.addObserver(self, selector: #selector(uploadedFile(_:)), name: NSNotification.Name(rawValue: k_notificationCenter_uploadedFile), object: nil)
  518. NotificationCenter.default.addObserver(self, selector: #selector(uploadCancelFile(_:)), name: NSNotification.Name(rawValue: k_notificationCenter_uploadCancelFile), object: nil)
  519. NotificationCenter.default.addObserver(self, selector: #selector(triggerProgressTask(_:)), name: NSNotification.Name(rawValue: k_notificationCenter_progressTask), object:nil)
  520. changeTheming()
  521. }
  522. override func viewWillAppear(_ animated: Bool) {
  523. super.viewWillAppear(animated)
  524. self.navigationItem.title = titleCurrentFolder
  525. // get auto upload folder
  526. autoUploadFileName = NCManageDatabase.sharedInstance.getAccountAutoUploadFileName()
  527. autoUploadDirectory = NCManageDatabase.sharedInstance.getAccountAutoUploadDirectory(urlBase: appDelegate.urlBase, account: appDelegate.account)
  528. (layout, _, _, groupBy, _, titleButton, itemForLine) = NCUtility.shared.getLayoutForView(key: layoutKey)
  529. gridLayout.itemForLine = CGFloat(itemForLine)
  530. if layout == k_layout_list {
  531. collectionView?.collectionViewLayout = listLayout
  532. } else {
  533. collectionView?.collectionViewLayout = gridLayout
  534. }
  535. reloadDataSource()
  536. }
  537. override func viewDidAppear(_ animated: Bool) {
  538. super.viewDidAppear(animated)
  539. reloadDataSourceNetwork()
  540. }
  541. override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
  542. super.viewWillTransition(to: size, with: coordinator)
  543. coordinator.animate(alongsideTransition: nil) { _ in
  544. self.collectionView?.collectionViewLayout.invalidateLayout()
  545. }
  546. }
  547. // MARK: - NotificationCenter
  548. @objc func changeTheming() {
  549. appDelegate.changeTheming(self, tableView: nil, collectionView: collectionView, form: false)
  550. }
  551. @objc func deleteFile(_ notification: NSNotification) {
  552. if self.view?.window == nil { return }
  553. if let userInfo = notification.userInfo as NSDictionary? {
  554. if let metadata = userInfo["metadata"] as? tableMetadata, let onlyLocal = userInfo["onlyLocal"] as? Bool, let errorCode = userInfo["errorCode"] as? Int, let errorDescription = userInfo["errorDescription"] as? String {
  555. NCCollectionCommon.shared.notificationDeleteFile(collectionView: collectionView, dataSource: dataSource, metadata: metadata, errorCode: errorCode, errorDescription: errorDescription, onlyLocal: onlyLocal)
  556. }
  557. }
  558. }
  559. @objc func downloadStartFile(_ notification: NSNotification) {
  560. if self.view?.window == nil { return }
  561. if let userInfo = notification.userInfo as NSDictionary? {
  562. if let metadata = userInfo["metadata"] as? tableMetadata {
  563. NCCollectionCommon.shared.notificationDownloadStartFile(collectionView: collectionView, dataSource: dataSource, metadata: metadata)
  564. }
  565. }
  566. }
  567. @objc func downloadedFile(_ notification: NSNotification) {
  568. if self.view?.window == nil { return }
  569. if let userInfo = notification.userInfo as NSDictionary? {
  570. if let metadata = userInfo["metadata"] as? tableMetadata, let _ = userInfo["errorCode"] as? Int {
  571. NCCollectionCommon.shared.notificationDownloadedFile(collectionView: collectionView, dataSource: dataSource, metadata: metadata)
  572. }
  573. }
  574. }
  575. @objc func downloadCancelFile(_ notification: NSNotification) {
  576. if self.view?.window == nil { return }
  577. if let userInfo = notification.userInfo as NSDictionary? {
  578. if let metadata = userInfo["metadata"] as? tableMetadata {
  579. NCCollectionCommon.shared.notificationDownloadCancelFile(collectionView: collectionView, dataSource: dataSource, metadata: metadata)
  580. }
  581. }
  582. }
  583. @objc func uploadStartFile(_ notification: NSNotification) {
  584. if self.view?.window == nil { return }
  585. if let userInfo = notification.userInfo as NSDictionary? {
  586. if let metadata = userInfo["metadata"] as? tableMetadata {
  587. NCCollectionCommon.shared.notificationUploadStartFile(collectionView: collectionView, dataSource: dataSource, metadata: metadata, serverUrl: serverUrl, account: appDelegate.account)
  588. }
  589. }
  590. }
  591. @objc func uploadedFile(_ notification: NSNotification) {
  592. if self.view?.window == nil { return }
  593. if let userInfo = notification.userInfo as NSDictionary? {
  594. if let metadata = userInfo["metadata"] as? tableMetadata, let ocIdTemp = userInfo["ocIdTemp"] as? String, let _ = userInfo["errorCode"] as? Int {
  595. NCCollectionCommon.shared.notificationUploadedFile(collectionView: collectionView, dataSource: dataSource, metadata: metadata, ocIdTemp:ocIdTemp, serverUrl: serverUrl, account: appDelegate.account)
  596. }
  597. }
  598. }
  599. @objc func uploadCancelFile(_ notification: NSNotification) {
  600. if self.view?.window == nil { return }
  601. if let userInfo = notification.userInfo as NSDictionary? {
  602. if let metadata = userInfo["metadata"] as? tableMetadata {
  603. NCCollectionCommon.shared.notificationUploadCancelFile(collectionView: collectionView, dataSource: dataSource, metadata: metadata, serverUrl: serverUrl, account: appDelegate.account)
  604. }
  605. }
  606. }
  607. @objc func triggerProgressTask(_ notification: NSNotification) {
  608. if self.view?.window == nil { return }
  609. if let userInfo = notification.userInfo as NSDictionary? {
  610. if let ocId = userInfo["ocId"] as? String {
  611. let progressNumber = userInfo["progress"] as? NSNumber ?? 0
  612. let progress = progressNumber.floatValue
  613. NCCollectionCommon.shared.notificationTriggerProgressTask(collectionView: collectionView, dataSource: dataSource, ocId: ocId, progress: progress)
  614. }
  615. }
  616. }
  617. // MARK: DZNEmpty
  618. func backgroundColor(forEmptyDataSet scrollView: UIScrollView) -> UIColor? {
  619. return NCBrandColor.sharedInstance.backgroundView
  620. }
  621. func image(forEmptyDataSet scrollView: UIScrollView) -> UIImage? {
  622. return nil
  623. }
  624. func title(forEmptyDataSet scrollView: UIScrollView) -> NSAttributedString? {
  625. return nil
  626. }
  627. func description(forEmptyDataSet scrollView: UIScrollView!) -> NSAttributedString! {
  628. return nil
  629. }
  630. func emptyDataSetShouldAllowScroll(_ scrollView: UIScrollView) -> Bool {
  631. return true
  632. }
  633. // MARK: TAP EVENT
  634. func tapSwitchHeader(sender: Any) {
  635. if collectionView.collectionViewLayout == gridLayout {
  636. // list layout
  637. UIView.animate(withDuration: 0.0, animations: {
  638. self.collectionView.collectionViewLayout.invalidateLayout()
  639. self.collectionView.setCollectionViewLayout(self.listLayout, animated: false, completion: { (_) in
  640. self.collectionView.reloadData()
  641. self.collectionView.setContentOffset(CGPoint(x:0,y:0), animated: false)
  642. })
  643. })
  644. layout = k_layout_list
  645. NCUtility.shared.setLayoutForView(key: layoutKey, layout: layout)
  646. } else {
  647. // grid layout
  648. UIView.animate(withDuration: 0.0, animations: {
  649. self.collectionView.collectionViewLayout.invalidateLayout()
  650. self.collectionView.setCollectionViewLayout(self.gridLayout, animated: false, completion: { (_) in
  651. self.collectionView.reloadData()
  652. self.collectionView.setContentOffset(CGPoint(x:0,y:0), animated: false)
  653. })
  654. })
  655. layout = k_layout_grid
  656. NCUtility.shared.setLayoutForView(key: layoutKey, layout: layout)
  657. }
  658. }
  659. func tapOrderHeader(sender: Any) {
  660. let sortMenu = NCSortMenu()
  661. sortMenu.toggleMenu(viewController: self, key: layoutKey, sortButton: sender as? UIButton, serverUrl: serverUrl)
  662. }
  663. func tapMoreHeader(sender: Any) {
  664. }
  665. func tapMoreListItem(with objectId: String, namedButtonMore: String, sender: Any) {
  666. tapMoreGridItem(with: objectId, namedButtonMore: namedButtonMore, sender: sender)
  667. }
  668. func tapShareListItem(with objectId: String, sender: Any) {
  669. guard let metadata = NCManageDatabase.sharedInstance.getMetadata(predicate: NSPredicate(format: "ocId == %@", objectId)) else {
  670. return
  671. }
  672. NCMainCommon.shared.openShare(ViewController: self, metadata: metadata, indexPage: 2)
  673. }
  674. func tapMoreGridItem(with objectId: String, namedButtonMore: String, sender: Any) {
  675. }
  676. // MARK: SEGUE
  677. @objc func segue(metadata: tableMetadata) {
  678. self.metadataPush = metadata
  679. performSegue(withIdentifier: "segueDetail", sender: self)
  680. }
  681. override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  682. let photoDataSource: NSMutableArray = []
  683. for metadata in (dataSource?.metadatas ?? [tableMetadata]()) {
  684. if metadata.typeFile == k_metadataTypeFile_image || metadata.typeFile == k_metadataTypeFile_video {
  685. photoDataSource.add(metadata)
  686. }
  687. }
  688. if let segueNavigationController = segue.destination as? UINavigationController {
  689. if let segueViewController = segueNavigationController.topViewController as? NCDetailViewController {
  690. segueViewController.metadata = metadataPush
  691. }
  692. }
  693. }
  694. // MARK: - NC API & Algorithm
  695. @objc func reloadDataSource() { }
  696. @objc func reloadDataSourceNetwork() { }
  697. }
  698. // MARK: - 3D Touch peek and pop
  699. extension NCCollectionViewCommon: UIViewControllerPreviewingDelegate {
  700. func previewingContext(_ previewingContext: UIViewControllerPreviewing, viewControllerForLocation location: CGPoint) -> UIViewController? {
  701. guard let point = collectionView?.convert(location, from: collectionView?.superview) else { return nil }
  702. guard let indexPath = collectionView?.indexPathForItem(at: point) else { return nil }
  703. guard let metadata = dataSource?.cellForItemAt(indexPath: indexPath) else { return nil }
  704. guard let viewController = UIStoryboard(name: "CCPeekPop", bundle: nil).instantiateViewController(withIdentifier: "PeekPopImagePreview") as? CCPeekPop else { return nil }
  705. viewController.metadata = metadata
  706. if layout == k_layout_grid {
  707. guard let cell = collectionView?.cellForItem(at: indexPath) as? NCGridCell else { return nil }
  708. previewingContext.sourceRect = cell.frame
  709. viewController.imageFile = cell.imageItem.image
  710. } else {
  711. guard let cell = collectionView?.cellForItem(at: indexPath) as? NCListCell else { return nil }
  712. previewingContext.sourceRect = cell.frame
  713. viewController.imageFile = cell.imageItem.image
  714. }
  715. viewController.showOpenIn = true
  716. viewController.showOpenQuickLook = NCUtility.shared.isQuickLookDisplayable(metadata: metadata)
  717. viewController.showShare = false
  718. return viewController
  719. }
  720. func previewingContext(_ previewingContext: UIViewControllerPreviewing, commit viewControllerToCommit: UIViewController) {
  721. guard let indexPath = collectionView?.indexPathForItem(at: previewingContext.sourceRect.origin) else { return }
  722. collectionView(collectionView, didSelectItemAt: indexPath)
  723. }
  724. }
  725. // MARK: - Collection View
  726. extension NCCollectionViewCommon: UICollectionViewDelegate {
  727. func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { }
  728. }
  729. extension NCCollectionViewCommon: UICollectionViewDataSource {
  730. func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
  731. if (indexPath.section == 0) {
  732. if kind == UICollectionView.elementKindSectionHeader {
  733. let header = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "sectionHeaderMenu", for: indexPath) as! NCSectionHeaderMenu
  734. if collectionView.collectionViewLayout == gridLayout {
  735. header.buttonSwitch.setImage(CCGraphics.changeThemingColorImage(UIImage.init(named: "switchList"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), for: .normal)
  736. } else {
  737. header.buttonSwitch.setImage(CCGraphics.changeThemingColorImage(UIImage.init(named: "switchGrid"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), for: .normal)
  738. }
  739. header.delegate = self
  740. header.backgroundColor = NCBrandColor.sharedInstance.backgroundView
  741. header.separator.backgroundColor = NCBrandColor.sharedInstance.separator
  742. header.setStatusButton(count: dataSource?.metadatas.count ?? 0)
  743. header.setTitleSorted(datasourceTitleButton: titleButton)
  744. if groupBy == "none" {
  745. header.labelSection.isHidden = true
  746. header.labelSectionHeightConstraint.constant = 0
  747. } else {
  748. header.labelSection.isHidden = false
  749. header.setTitleLabel(title: "")
  750. header.labelSectionHeightConstraint.constant = sectionHeaderHeight
  751. }
  752. return header
  753. } else {
  754. let footer = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "sectionFooter", for: indexPath) as! NCSectionFooter
  755. let info = dataSource?.getFilesInformation()
  756. footer.setTitleLabel(directories: info?.directories ?? 0, files: info?.files ?? 0, size: info?.size ?? 0)
  757. return footer
  758. }
  759. } else {
  760. if kind == UICollectionView.elementKindSectionHeader {
  761. let header = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "sectionHeader", for: indexPath) as! NCSectionHeader
  762. header.setTitleLabel(title: "")
  763. return header
  764. } else {
  765. let footer = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "sectionFooter", for: indexPath) as! NCSectionFooter
  766. let info = dataSource?.getFilesInformation()
  767. footer.setTitleLabel(directories: info?.directories ?? 0, files: info?.files ?? 0, size: info?.size ?? 0)
  768. return footer
  769. }
  770. }
  771. }
  772. func numberOfSections(in collectionView: UICollectionView) -> Int {
  773. return dataSource?.sections ?? 1
  774. }
  775. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  776. return dataSource?.numberOfItemsInSection(section: section) ?? 1
  777. }
  778. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  779. let cell: UICollectionViewCell
  780. guard let metadata = dataSource?.cellForItemAt(indexPath: indexPath) else {
  781. return collectionView.dequeueReusableCell(withReuseIdentifier: "listCell", for: indexPath) as! NCListCell
  782. }
  783. if layout == k_layout_grid {
  784. cell = collectionView.dequeueReusableCell(withReuseIdentifier: "gridCell", for: indexPath) as! NCGridCell
  785. } else {
  786. cell = collectionView.dequeueReusableCell(withReuseIdentifier: "listCell", for: indexPath) as! NCListCell
  787. }
  788. let shares = NCManageDatabase.sharedInstance.getTableShares(account: metadata.account, serverUrl: metadata.serverUrl, fileName: metadata.fileName)
  789. NCCollectionCommon.shared.cellForItemAt(indexPath: indexPath, collectionView: collectionView, cell: cell, metadata: metadata, metadataFolder: nil, serverUrl: metadata.serverUrl, isEditMode: isEditMode, selectocId: selectOcId, autoUploadFileName: autoUploadFileName, autoUploadDirectory: autoUploadDirectory, hideButtonMore: false, downloadThumbnail: true, shares: shares, source: self)
  790. return cell
  791. }
  792. }
  793. extension NCCollectionViewCommon: UICollectionViewDelegateFlowLayout {
  794. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
  795. if section == 0 {
  796. if groupBy == "none" {
  797. return CGSize(width: collectionView.frame.width, height: headerMenuHeight)
  798. } else {
  799. return CGSize(width: collectionView.frame.width, height: headerMenuHeight + sectionHeaderHeight)
  800. }
  801. } else {
  802. return CGSize(width: collectionView.frame.width, height: sectionHeaderHeight)
  803. }
  804. }
  805. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize {
  806. let sections = dataSource?.sections ?? 1
  807. if (section == sections - 1) {
  808. return CGSize(width: collectionView.frame.width, height: footerHeight)
  809. } else {
  810. return CGSize(width: collectionView.frame.width, height: 0)
  811. }
  812. }
  813. }