NCCollectionCommon.swift 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077
  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) {
  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. }
  309. } else {
  310. if let row = dataSource?.deleteMetadata(ocId: metadata.ocId) {
  311. let indexPath = IndexPath(row: row, section: 0)
  312. collectionView?.performBatchUpdates({
  313. collectionView?.deleteItems(at: [indexPath])
  314. }, completion: { (_) in
  315. collectionView?.reloadData()
  316. })
  317. }
  318. }
  319. } else {
  320. NCContentPresenter.shared.messageNotification("_error_", description: errorDescription, delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
  321. }
  322. }
  323. func notificationDownloadStartFile(collectionView: UICollectionView?, dataSource: NCDataSource?, metadata: tableMetadata) {
  324. if let row = dataSource?.reloadMetadata(ocId: metadata.ocId) {
  325. let indexPath = IndexPath(row: row, section: 0)
  326. collectionView?.reloadItems(at: [indexPath])
  327. }
  328. }
  329. func notificationDownloadedFile(collectionView: UICollectionView?, dataSource: NCDataSource?, metadata: tableMetadata) {
  330. if let row = dataSource?.reloadMetadata(ocId: metadata.ocId) {
  331. let indexPath = IndexPath(row: row, section: 0)
  332. collectionView?.reloadItems(at: [indexPath])
  333. }
  334. }
  335. func notificationDownloadCancelFile(collectionView: UICollectionView?, dataSource: NCDataSource?, metadata: tableMetadata) {
  336. if let row = dataSource?.reloadMetadata(ocId: metadata.ocId) {
  337. let indexPath = IndexPath(row: row, section: 0)
  338. collectionView?.reloadItems(at: [indexPath])
  339. }
  340. }
  341. func notificationUploadStartFile(collectionView: UICollectionView?, dataSource: NCDataSource?, metadata: tableMetadata) {
  342. if let row = dataSource?.addMetadata(metadata) {
  343. let indexPath = IndexPath(row: row, section: 0)
  344. collectionView?.performBatchUpdates({
  345. collectionView?.insertItems(at: [indexPath])
  346. }, completion: { (_) in
  347. collectionView?.reloadData()
  348. })
  349. }
  350. }
  351. func notificationUploadedFile(collectionView: UICollectionView?, dataSource: NCDataSource?, metadata: tableMetadata, ocIdTemp: String) -> Bool {
  352. if let row = dataSource?.reloadMetadata(ocId: metadata.ocId, ocIdTemp: ocIdTemp) {
  353. let indexPath = IndexPath(row: row, section: 0)
  354. collectionView?.reloadItems(at: [indexPath])
  355. return true
  356. }
  357. return false
  358. }
  359. func notificationUploadCancelFile(collectionView: UICollectionView?, dataSource: NCDataSource?, metadata: tableMetadata) -> Bool {
  360. if let row = dataSource?.deleteMetadata(ocId: metadata.ocId) {
  361. let indexPath = IndexPath(row: row, section: 0)
  362. collectionView?.performBatchUpdates({
  363. collectionView?.deleteItems(at: [indexPath])
  364. }, completion: { (_) in
  365. collectionView?.reloadData()
  366. })
  367. return true
  368. }
  369. return false
  370. }
  371. func notificationTriggerProgressTask(collectionView: UICollectionView?, dataSource: NCDataSource?, ocId: String, progress: Float) {
  372. if let index = dataSource?.getIndexMetadata(ocId: ocId) {
  373. if let cell = collectionView?.cellForItem(at: IndexPath(row: index, section: 0)) {
  374. if cell is NCListCell {
  375. let cell = cell as! NCListCell
  376. if progress > 0 {
  377. cell.progressView?.isHidden = false
  378. cell.progressView?.progress = progress
  379. cell.setButtonMore(named: "stop")
  380. }
  381. } else if cell is NCGridCell {
  382. let cell = cell as! NCGridCell
  383. if progress > 0 {
  384. cell.progressView.isHidden = false
  385. cell.progressView.progress = progress
  386. cell.setButtonMore(named: "stop")
  387. }
  388. }
  389. }
  390. }
  391. }
  392. }
  393. // MARK: - List Layout
  394. class NCListLayout: UICollectionViewFlowLayout {
  395. let itemHeight: CGFloat = 60
  396. override init() {
  397. super.init()
  398. sectionHeadersPinToVisibleBounds = false
  399. minimumInteritemSpacing = 0
  400. minimumLineSpacing = 1
  401. self.scrollDirection = .vertical
  402. self.sectionInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
  403. }
  404. required init?(coder aDecoder: NSCoder) {
  405. fatalError("init(coder:) has not been implemented")
  406. }
  407. override var itemSize: CGSize {
  408. get {
  409. if let collectionView = collectionView {
  410. let itemWidth: CGFloat = collectionView.frame.width
  411. return CGSize(width: itemWidth, height: self.itemHeight)
  412. }
  413. // Default fallback
  414. return CGSize(width: 100, height: 100)
  415. }
  416. set {
  417. super.itemSize = newValue
  418. }
  419. }
  420. override func targetContentOffset(forProposedContentOffset proposedContentOffset: CGPoint) -> CGPoint {
  421. return proposedContentOffset
  422. }
  423. }
  424. // MARK: - Grid Layout
  425. class NCGridLayout: UICollectionViewFlowLayout {
  426. var heightLabelPlusButton: CGFloat = 45
  427. var marginLeftRight: CGFloat = 6
  428. var itemForLine: CGFloat = 3
  429. override init() {
  430. super.init()
  431. sectionHeadersPinToVisibleBounds = false
  432. minimumInteritemSpacing = 1
  433. minimumLineSpacing = marginLeftRight
  434. self.scrollDirection = .vertical
  435. self.sectionInset = UIEdgeInsets(top: 10, left: marginLeftRight, bottom: 0, right: marginLeftRight)
  436. }
  437. required init?(coder aDecoder: NSCoder) {
  438. fatalError("init(coder:) has not been implemented")
  439. }
  440. override var itemSize: CGSize {
  441. get {
  442. if let collectionView = collectionView {
  443. let itemWidth: CGFloat = (collectionView.frame.width - marginLeftRight * 2 - marginLeftRight * (itemForLine - 1)) / itemForLine
  444. let itemHeight: CGFloat = itemWidth + heightLabelPlusButton
  445. return CGSize(width: itemWidth, height: itemHeight)
  446. }
  447. // Default fallback
  448. return CGSize(width: 100, height: 100)
  449. }
  450. set {
  451. super.itemSize = newValue
  452. }
  453. }
  454. override func targetContentOffset(forProposedContentOffset proposedContentOffset: CGPoint) -> CGPoint {
  455. return proposedContentOffset
  456. }
  457. }
  458. // MARK: - NCSelect + Delegate
  459. extension NCCollectionCommon: NCSelectDelegate {
  460. func dismissSelect(serverUrl: String?, metadata: tableMetadata?, type: String, array: [Any], buttonType: String, overwrite: Bool) {
  461. if (serverUrl != nil && array.count > 0) {
  462. var move = true
  463. if buttonType == "done1" { move = false }
  464. for metadata in array as! [tableMetadata] {
  465. NCOperationQueue.shared.copyMove(metadata: metadata, serverUrl: serverUrl!, overwrite: overwrite, move: move)
  466. }
  467. }
  468. }
  469. func openSelectView(viewController: UIViewController, array: [Any]) {
  470. let navigationController = UIStoryboard.init(name: "NCSelect", bundle: nil).instantiateInitialViewController() as! UINavigationController
  471. let vc = navigationController.topViewController as! NCSelect
  472. vc.delegate = self
  473. vc.hideButtonCreateFolder = false
  474. vc.selectFile = false
  475. vc.includeDirectoryE2EEncryption = false
  476. vc.includeImages = false
  477. vc.type = ""
  478. vc.titleButtonDone = NSLocalizedString("_move_", comment: "")
  479. vc.titleButtonDone1 = NSLocalizedString("_copy_",comment: "")
  480. vc.isButtonDone1Hide = false
  481. vc.isOverwriteHide = false
  482. vc.keyLayout = k_layout_view_move
  483. vc.array = array
  484. navigationController.modalPresentationStyle = .fullScreen
  485. viewController.present(navigationController, animated: true, completion: nil)
  486. }
  487. }
  488. // MARK: - Nextcloud CollectionView Common
  489. class NCCollectionViewCommon: UIViewController, UIGestureRecognizerDelegate, NCListCellDelegate, NCGridCellDelegate, NCSectionHeaderMenuDelegate, DZNEmptyDataSetSource, DZNEmptyDataSetDelegate {
  490. @IBOutlet weak var collectionView: UICollectionView!
  491. @objc var serverUrl = ""
  492. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  493. internal var metadataPush: tableMetadata?
  494. internal var isEditMode = false
  495. internal var selectOcId: [String] = []
  496. internal var dataSource: NCDataSource?
  497. internal var layout = ""
  498. internal var groupBy = ""
  499. internal var titleButton = ""
  500. internal var itemForLine = 0
  501. private var autoUploadFileName = ""
  502. private var autoUploadDirectory = ""
  503. private var listLayout: NCListLayout!
  504. private var gridLayout: NCGridLayout!
  505. private let headerMenuHeight: CGFloat = 50
  506. private let sectionHeaderHeight: CGFloat = 20
  507. private let footerHeight: CGFloat = 50
  508. internal let refreshControl = UIRefreshControl()
  509. // DECLARE
  510. internal var layoutKey = ""
  511. internal var titleCurrentFolder = ""
  512. required init?(coder aDecoder: NSCoder) {
  513. super.init(coder: aDecoder)
  514. }
  515. override func viewDidLoad() {
  516. super.viewDidLoad()
  517. // Cell
  518. collectionView.register(UINib.init(nibName: "NCListCell", bundle: nil), forCellWithReuseIdentifier: "listCell")
  519. collectionView.register(UINib.init(nibName: "NCGridCell", bundle: nil), forCellWithReuseIdentifier: "gridCell")
  520. // Header
  521. collectionView.register(UINib.init(nibName: "NCSectionHeaderMenu", bundle: nil), forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "sectionHeaderMenu")
  522. collectionView.register(UINib.init(nibName: "NCSectionHeader", bundle: nil), forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "sectionHeader")
  523. // Footer
  524. collectionView.register(UINib.init(nibName: "NCSectionFooter", bundle: nil), forSupplementaryViewOfKind: UICollectionView.elementKindSectionFooter, withReuseIdentifier: "sectionFooter")
  525. collectionView.alwaysBounceVertical = true
  526. listLayout = NCListLayout()
  527. gridLayout = NCGridLayout()
  528. // Refresh Control
  529. collectionView.addSubview(refreshControl)
  530. refreshControl.tintColor = NCBrandColor.sharedInstance.brandText
  531. refreshControl.backgroundColor = NCBrandColor.sharedInstance.brandElement
  532. refreshControl.addTarget(self, action: #selector(reloadDataSourceNetwork), for: .valueChanged)
  533. // empty Data Source
  534. self.collectionView.emptyDataSetDelegate = self
  535. self.collectionView.emptyDataSetSource = self
  536. // 3D Touch peek and pop
  537. if traitCollection.forceTouchCapability == .available {
  538. registerForPreviewing(with: self, sourceView: view)
  539. }
  540. NotificationCenter.default.addObserver(self, selector: #selector(changeTheming), name: NSNotification.Name(rawValue: k_notificationCenter_changeTheming), object: nil)
  541. NotificationCenter.default.addObserver(self, selector: #selector(reloadDataSource), name: NSNotification.Name(rawValue: k_notificationCenter_reloadDataSource), object: nil)
  542. NotificationCenter.default.addObserver(self, selector: #selector(deleteFile(_:)), name: NSNotification.Name(rawValue: k_notificationCenter_deleteFile), object: nil)
  543. NotificationCenter.default.addObserver(self, selector: #selector(downloadStartFile(_:)), name: NSNotification.Name(rawValue: k_notificationCenter_downloadStartFile), object: nil)
  544. NotificationCenter.default.addObserver(self, selector: #selector(downloadedFile(_:)), name: NSNotification.Name(rawValue: k_notificationCenter_downloadedFile), object: nil)
  545. NotificationCenter.default.addObserver(self, selector: #selector(downloadCancelFile(_:)), name: NSNotification.Name(rawValue: k_notificationCenter_downloadCancelFile), object: nil)
  546. NotificationCenter.default.addObserver(self, selector: #selector(uploadStartFile(_:)), name: NSNotification.Name(rawValue: k_notificationCenter_uploadStartFile), object: nil)
  547. NotificationCenter.default.addObserver(self, selector: #selector(uploadedFile(_:)), name: NSNotification.Name(rawValue: k_notificationCenter_uploadedFile), object: nil)
  548. NotificationCenter.default.addObserver(self, selector: #selector(uploadCancelFile(_:)), name: NSNotification.Name(rawValue: k_notificationCenter_uploadCancelFile), object: nil)
  549. NotificationCenter.default.addObserver(self, selector: #selector(triggerProgressTask(_:)), name: NSNotification.Name(rawValue: k_notificationCenter_progressTask), object:nil)
  550. changeTheming()
  551. }
  552. override func viewWillAppear(_ animated: Bool) {
  553. super.viewWillAppear(animated)
  554. self.navigationItem.title = titleCurrentFolder
  555. // get auto upload folder
  556. autoUploadFileName = NCManageDatabase.sharedInstance.getAccountAutoUploadFileName()
  557. autoUploadDirectory = NCManageDatabase.sharedInstance.getAccountAutoUploadDirectory(urlBase: appDelegate.urlBase, account: appDelegate.account)
  558. (layout, _, _, groupBy, _, titleButton, itemForLine) = NCUtility.shared.getLayoutForView(key: layoutKey)
  559. gridLayout.itemForLine = CGFloat(itemForLine)
  560. if layout == k_layout_list {
  561. collectionView?.collectionViewLayout = listLayout
  562. } else {
  563. collectionView?.collectionViewLayout = gridLayout
  564. }
  565. reloadDataSource()
  566. }
  567. override func viewDidAppear(_ animated: Bool) {
  568. super.viewDidAppear(animated)
  569. reloadDataSourceNetwork()
  570. }
  571. override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
  572. super.viewWillTransition(to: size, with: coordinator)
  573. coordinator.animate(alongsideTransition: nil) { _ in
  574. self.collectionView?.collectionViewLayout.invalidateLayout()
  575. }
  576. }
  577. // MARK: - NotificationCenter
  578. @objc func changeTheming() {
  579. appDelegate.changeTheming(self, tableView: nil, collectionView: collectionView, form: false)
  580. }
  581. @objc func deleteFile(_ notification: NSNotification) {
  582. if self.view?.window == nil { return }
  583. if let userInfo = notification.userInfo as NSDictionary? {
  584. 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 {
  585. NCCollectionCommon.shared.notificationDeleteFile(collectionView: collectionView, dataSource: dataSource, metadata: metadata, errorCode: errorCode, errorDescription: errorDescription, onlyLocal: onlyLocal)
  586. }
  587. }
  588. }
  589. @objc func downloadStartFile(_ notification: NSNotification) {
  590. if self.view?.window == nil { return }
  591. if let userInfo = notification.userInfo as NSDictionary? {
  592. if let metadata = userInfo["metadata"] as? tableMetadata {
  593. NCCollectionCommon.shared.notificationDownloadStartFile(collectionView: collectionView, dataSource: dataSource, metadata: metadata)
  594. }
  595. }
  596. }
  597. @objc func downloadedFile(_ notification: NSNotification) {
  598. if self.view?.window == nil { return }
  599. if let userInfo = notification.userInfo as NSDictionary? {
  600. if let metadata = userInfo["metadata"] as? tableMetadata, let _ = userInfo["errorCode"] as? Int {
  601. NCCollectionCommon.shared.notificationDownloadedFile(collectionView: collectionView, dataSource: dataSource, metadata: metadata)
  602. }
  603. }
  604. }
  605. @objc func downloadCancelFile(_ notification: NSNotification) {
  606. if self.view?.window == nil { return }
  607. if let userInfo = notification.userInfo as NSDictionary? {
  608. if let metadata = userInfo["metadata"] as? tableMetadata {
  609. NCCollectionCommon.shared.notificationDownloadCancelFile(collectionView: collectionView, dataSource: dataSource, metadata: metadata)
  610. }
  611. }
  612. }
  613. @objc func uploadStartFile(_ 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 {
  617. if metadata.serverUrl == serverUrl && metadata.account == appDelegate.account {
  618. NCCollectionCommon.shared.notificationUploadStartFile(collectionView: collectionView, dataSource: dataSource, metadata: metadata)
  619. }
  620. }
  621. }
  622. }
  623. @objc func uploadedFile(_ 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, let ocIdTemp = userInfo["ocIdTemp"] as? String, let _ = userInfo["errorCode"] as? Int {
  627. if metadata.serverUrl == serverUrl && metadata.account == appDelegate.account {
  628. if !NCCollectionCommon.shared.notificationUploadedFile(collectionView: collectionView, dataSource: dataSource, metadata: metadata, ocIdTemp:ocIdTemp) {
  629. self.reloadDataSource()
  630. }
  631. }
  632. }
  633. }
  634. }
  635. @objc func uploadCancelFile(_ notification: NSNotification) {
  636. if self.view?.window == nil { return }
  637. if let userInfo = notification.userInfo as NSDictionary? {
  638. if let metadata = userInfo["metadata"] as? tableMetadata {
  639. if metadata.serverUrl == serverUrl && metadata.account == appDelegate.account {
  640. if !NCCollectionCommon.shared.notificationUploadCancelFile(collectionView: collectionView, dataSource: dataSource, metadata: metadata) {
  641. self.reloadDataSource()
  642. }
  643. }
  644. }
  645. }
  646. }
  647. @objc func triggerProgressTask(_ notification: NSNotification) {
  648. if self.view?.window == nil { return }
  649. if let userInfo = notification.userInfo as NSDictionary? {
  650. if let ocId = userInfo["ocId"] as? String {
  651. let progressNumber = userInfo["progress"] as? NSNumber ?? 0
  652. let progress = progressNumber.floatValue
  653. NCCollectionCommon.shared.notificationTriggerProgressTask(collectionView: collectionView, dataSource: dataSource, ocId: ocId, progress: progress)
  654. }
  655. }
  656. }
  657. // MARK: DZNEmpty
  658. func backgroundColor(forEmptyDataSet scrollView: UIScrollView) -> UIColor? {
  659. return NCBrandColor.sharedInstance.backgroundView
  660. }
  661. func image(forEmptyDataSet scrollView: UIScrollView) -> UIImage? {
  662. return nil
  663. }
  664. func title(forEmptyDataSet scrollView: UIScrollView) -> NSAttributedString? {
  665. return nil
  666. }
  667. func description(forEmptyDataSet scrollView: UIScrollView!) -> NSAttributedString! {
  668. return nil
  669. }
  670. func emptyDataSetShouldAllowScroll(_ scrollView: UIScrollView) -> Bool {
  671. return true
  672. }
  673. // MARK: TAP EVENT
  674. func tapSwitchHeader(sender: Any) {
  675. if collectionView.collectionViewLayout == gridLayout {
  676. // list layout
  677. UIView.animate(withDuration: 0.0, animations: {
  678. self.collectionView.collectionViewLayout.invalidateLayout()
  679. self.collectionView.setCollectionViewLayout(self.listLayout, animated: false, completion: { (_) in
  680. self.collectionView.reloadData()
  681. self.collectionView.setContentOffset(CGPoint(x:0,y:0), animated: false)
  682. })
  683. })
  684. layout = k_layout_list
  685. NCUtility.shared.setLayoutForView(key: layoutKey, layout: layout)
  686. } else {
  687. // grid layout
  688. UIView.animate(withDuration: 0.0, animations: {
  689. self.collectionView.collectionViewLayout.invalidateLayout()
  690. self.collectionView.setCollectionViewLayout(self.gridLayout, animated: false, completion: { (_) in
  691. self.collectionView.reloadData()
  692. self.collectionView.setContentOffset(CGPoint(x:0,y:0), animated: false)
  693. })
  694. })
  695. layout = k_layout_grid
  696. NCUtility.shared.setLayoutForView(key: layoutKey, layout: layout)
  697. }
  698. }
  699. func tapOrderHeader(sender: Any) {
  700. let sortMenu = NCSortMenu()
  701. sortMenu.toggleMenu(viewController: self, key: layoutKey, sortButton: sender as? UIButton, serverUrl: serverUrl)
  702. }
  703. func tapMoreHeader(sender: Any) {
  704. }
  705. func tapMoreListItem(with objectId: String, namedButtonMore: String, sender: Any) {
  706. tapMoreGridItem(with: objectId, namedButtonMore: namedButtonMore, sender: sender)
  707. }
  708. func tapShareListItem(with objectId: String, sender: Any) {
  709. guard let metadata = NCManageDatabase.sharedInstance.getMetadata(predicate: NSPredicate(format: "ocId == %@", objectId)) else {
  710. return
  711. }
  712. NCMainCommon.shared.openShare(ViewController: self, metadata: metadata, indexPage: 2)
  713. }
  714. func tapMoreGridItem(with objectId: String, namedButtonMore: String, sender: Any) {
  715. }
  716. // MARK: SEGUE
  717. @objc func segue(metadata: tableMetadata) {
  718. self.metadataPush = metadata
  719. performSegue(withIdentifier: "segueDetail", sender: self)
  720. }
  721. override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  722. let photoDataSource: NSMutableArray = []
  723. for metadata in (dataSource?.metadatas ?? [tableMetadata]()) {
  724. if metadata.typeFile == k_metadataTypeFile_image || metadata.typeFile == k_metadataTypeFile_video {
  725. photoDataSource.add(metadata)
  726. }
  727. }
  728. if let segueNavigationController = segue.destination as? UINavigationController {
  729. if let segueViewController = segueNavigationController.topViewController as? NCDetailViewController {
  730. segueViewController.metadata = metadataPush
  731. }
  732. }
  733. }
  734. // MARK: - NC API & Algorithm
  735. @objc func reloadDataSource() { }
  736. @objc func reloadDataSourceNetwork() { }
  737. }
  738. // MARK: - 3D Touch peek and pop
  739. extension NCCollectionViewCommon: UIViewControllerPreviewingDelegate {
  740. func previewingContext(_ previewingContext: UIViewControllerPreviewing, viewControllerForLocation location: CGPoint) -> UIViewController? {
  741. guard let point = collectionView?.convert(location, from: collectionView?.superview) else { return nil }
  742. guard let indexPath = collectionView?.indexPathForItem(at: point) else { return nil }
  743. guard let metadata = dataSource?.cellForItemAt(indexPath: indexPath) else { return nil }
  744. guard let viewController = UIStoryboard(name: "CCPeekPop", bundle: nil).instantiateViewController(withIdentifier: "PeekPopImagePreview") as? CCPeekPop else { return nil }
  745. viewController.metadata = metadata
  746. if layout == k_layout_grid {
  747. guard let cell = collectionView?.cellForItem(at: indexPath) as? NCGridCell else { return nil }
  748. previewingContext.sourceRect = cell.frame
  749. viewController.imageFile = cell.imageItem.image
  750. } else {
  751. guard let cell = collectionView?.cellForItem(at: indexPath) as? NCListCell else { return nil }
  752. previewingContext.sourceRect = cell.frame
  753. viewController.imageFile = cell.imageItem.image
  754. }
  755. viewController.showOpenIn = true
  756. viewController.showOpenQuickLook = NCUtility.shared.isQuickLookDisplayable(metadata: metadata)
  757. viewController.showShare = false
  758. return viewController
  759. }
  760. func previewingContext(_ previewingContext: UIViewControllerPreviewing, commit viewControllerToCommit: UIViewController) {
  761. guard let indexPath = collectionView?.indexPathForItem(at: previewingContext.sourceRect.origin) else { return }
  762. collectionView(collectionView, didSelectItemAt: indexPath)
  763. }
  764. }
  765. // MARK: - Collection View
  766. extension NCCollectionViewCommon: UICollectionViewDelegate {
  767. func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { }
  768. }
  769. extension NCCollectionViewCommon: UICollectionViewDataSource {
  770. func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
  771. if (indexPath.section == 0) {
  772. if kind == UICollectionView.elementKindSectionHeader {
  773. let header = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "sectionHeaderMenu", for: indexPath) as! NCSectionHeaderMenu
  774. if collectionView.collectionViewLayout == gridLayout {
  775. header.buttonSwitch.setImage(CCGraphics.changeThemingColorImage(UIImage.init(named: "switchList"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), for: .normal)
  776. } else {
  777. header.buttonSwitch.setImage(CCGraphics.changeThemingColorImage(UIImage.init(named: "switchGrid"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), for: .normal)
  778. }
  779. header.delegate = self
  780. header.backgroundColor = NCBrandColor.sharedInstance.backgroundView
  781. header.separator.backgroundColor = NCBrandColor.sharedInstance.separator
  782. header.setStatusButton(count: dataSource?.metadatas.count ?? 0)
  783. header.setTitleSorted(datasourceTitleButton: titleButton)
  784. if groupBy == "none" {
  785. header.labelSection.isHidden = true
  786. header.labelSectionHeightConstraint.constant = 0
  787. } else {
  788. header.labelSection.isHidden = false
  789. header.setTitleLabel(title: "")
  790. header.labelSectionHeightConstraint.constant = sectionHeaderHeight
  791. }
  792. return header
  793. } else {
  794. let footer = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "sectionFooter", for: indexPath) as! NCSectionFooter
  795. let info = dataSource?.getFilesInformation()
  796. footer.setTitleLabel(directories: info?.directories ?? 0, files: info?.files ?? 0, size: info?.size ?? 0)
  797. return footer
  798. }
  799. } else {
  800. if kind == UICollectionView.elementKindSectionHeader {
  801. let header = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "sectionHeader", for: indexPath) as! NCSectionHeader
  802. header.setTitleLabel(title: "")
  803. return header
  804. } else {
  805. let footer = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "sectionFooter", for: indexPath) as! NCSectionFooter
  806. let info = dataSource?.getFilesInformation()
  807. footer.setTitleLabel(directories: info?.directories ?? 0, files: info?.files ?? 0, size: info?.size ?? 0)
  808. return footer
  809. }
  810. }
  811. }
  812. func numberOfSections(in collectionView: UICollectionView) -> Int {
  813. return dataSource?.sections ?? 1
  814. }
  815. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  816. return dataSource?.numberOfItemsInSection(section: section) ?? 1
  817. }
  818. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  819. let cell: UICollectionViewCell
  820. guard let metadata = dataSource?.cellForItemAt(indexPath: indexPath) else {
  821. return collectionView.dequeueReusableCell(withReuseIdentifier: "listCell", for: indexPath) as! NCListCell
  822. }
  823. if layout == k_layout_grid {
  824. cell = collectionView.dequeueReusableCell(withReuseIdentifier: "gridCell", for: indexPath) as! NCGridCell
  825. } else {
  826. cell = collectionView.dequeueReusableCell(withReuseIdentifier: "listCell", for: indexPath) as! NCListCell
  827. }
  828. let shares = NCManageDatabase.sharedInstance.getTableShares(account: metadata.account, serverUrl: metadata.serverUrl, fileName: metadata.fileName)
  829. 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)
  830. return cell
  831. }
  832. }
  833. extension NCCollectionViewCommon: UICollectionViewDelegateFlowLayout {
  834. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
  835. if section == 0 {
  836. if groupBy == "none" {
  837. return CGSize(width: collectionView.frame.width, height: headerMenuHeight)
  838. } else {
  839. return CGSize(width: collectionView.frame.width, height: headerMenuHeight + sectionHeaderHeight)
  840. }
  841. } else {
  842. return CGSize(width: collectionView.frame.width, height: sectionHeaderHeight)
  843. }
  844. }
  845. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize {
  846. let sections = dataSource?.sections ?? 1
  847. if (section == sections - 1) {
  848. return CGSize(width: collectionView.frame.width, height: footerHeight)
  849. } else {
  850. return CGSize(width: collectionView.frame.width, height: 0)
  851. }
  852. }
  853. }