NCCollectionCommon.swift 50 KB

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