NCCollectionCommon.swift 51 KB

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