NCCollectionCommon.swift 51 KB

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