NCCollectionCommon.swift 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  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 NCCommunication
  25. class NCCollectionCommon: NSObject {
  26. @objc static let shared: NCCollectionCommon = {
  27. let instance = NCCollectionCommon()
  28. instance.createImagesThemingColor()
  29. return instance
  30. }()
  31. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  32. struct NCCollectionCommonImages {
  33. static var cellSharedImage = UIImage()
  34. static var cellCanShareImage = UIImage()
  35. static var cellShareByLinkImage = UIImage()
  36. static var cellFavouriteImage = UIImage()
  37. static var cellMoreImage = UIImage()
  38. static var cellCommentImage = UIImage()
  39. static var cellLivePhotoImage = UIImage()
  40. static var cellFolderEncryptedImage = UIImage()
  41. static var cellFolderSharedWithMeImage = UIImage()
  42. static var cellFolderPublicImage = UIImage()
  43. static var cellFolderGroupImage = UIImage()
  44. static var cellFolderExternalImage = UIImage()
  45. static var cellFolderAutomaticUploadImage = UIImage()
  46. static var cellFolderImage = UIImage()
  47. static var cellCheckedYes = UIImage()
  48. static var cellCheckedNo = UIImage()
  49. static var cellPlayImage = UIImage()
  50. }
  51. // MARK: -
  52. @objc func createImagesThemingColor() {
  53. NCCollectionCommonImages.cellSharedImage = CCGraphics.changeThemingColorImage(UIImage.init(named: "share"), width: 100, height: 100, color: NCBrandColor.sharedInstance.textView)
  54. NCCollectionCommonImages.cellCanShareImage = CCGraphics.changeThemingColorImage(UIImage.init(named: "share"), width: 100, height: 100, color: NCBrandColor.sharedInstance.optionItem)
  55. NCCollectionCommonImages.cellShareByLinkImage = CCGraphics.changeThemingColorImage(UIImage.init(named: "sharebylink"), width: 100, height: 100, color: NCBrandColor.sharedInstance.optionItem)
  56. NCCollectionCommonImages.cellFavouriteImage = CCGraphics.changeThemingColorImage(UIImage.init(named: "favorite"), width: 100, height: 100, color: NCBrandColor.sharedInstance.yellowFavorite)
  57. NCCollectionCommonImages.cellMoreImage = CCGraphics.changeThemingColorImage(UIImage.init(named: "more"), width: 50, height: 50, color: NCBrandColor.sharedInstance.optionItem)
  58. NCCollectionCommonImages.cellCommentImage = CCGraphics.changeThemingColorImage(UIImage.init(named: "comment"), width: 30, height: 30, color: NCBrandColor.sharedInstance.graySoft)
  59. NCCollectionCommonImages.cellLivePhotoImage = CCGraphics.changeThemingColorImage(UIImage.init(named: "livePhoto"), width: 100, height: 100, color: NCBrandColor.sharedInstance.textView)
  60. NCCollectionCommonImages.cellFolderEncryptedImage = CCGraphics.changeThemingColorImage(UIImage.init(named: "folderEncrypted"), width: 600, height: 600, color: NCBrandColor.sharedInstance.brandElement)
  61. NCCollectionCommonImages.cellFolderSharedWithMeImage = CCGraphics.changeThemingColorImage(UIImage.init(named: "folder_shared_with_me"), width: 600, height: 600, color: NCBrandColor.sharedInstance.brandElement)
  62. NCCollectionCommonImages.cellFolderPublicImage = CCGraphics.changeThemingColorImage(UIImage.init(named: "folder_public"), width: 600, height: 600, color: NCBrandColor.sharedInstance.brandElement)
  63. NCCollectionCommonImages.cellFolderGroupImage = CCGraphics.changeThemingColorImage(UIImage.init(named: "folder_group"), width: 600, height: 600, color: NCBrandColor.sharedInstance.brandElement)
  64. NCCollectionCommonImages.cellFolderExternalImage = CCGraphics.changeThemingColorImage(UIImage.init(named: "folder_external"), width: 600, height: 600, color: NCBrandColor.sharedInstance.brandElement)
  65. NCCollectionCommonImages.cellFolderAutomaticUploadImage = CCGraphics.changeThemingColorImage(UIImage.init(named: "folderAutomaticUpload"), width: 600, height: 600, color: NCBrandColor.sharedInstance.brandElement)
  66. NCCollectionCommonImages.cellFolderImage = CCGraphics.changeThemingColorImage(UIImage.init(named: "folder"), width: 600, height: 600, color: NCBrandColor.sharedInstance.brandElement)
  67. NCCollectionCommonImages.cellCheckedYes = CCGraphics.changeThemingColorImage(UIImage.init(named: "checkedYes"), width: 100, height: 100, color: NCBrandColor.sharedInstance.brandElement)
  68. NCCollectionCommonImages.cellCheckedNo = CCGraphics.changeThemingColorImage(UIImage.init(named: "checkedNo"), width: 100, height: 100, color: NCBrandColor.sharedInstance.graySoft)
  69. NCCollectionCommonImages.cellPlayImage = CCGraphics.changeThemingColorImage(UIImage.init(named: "play"), width: 100, height: 100, color: .white)
  70. }
  71. // MARK: -
  72. func cellForItemAt(indexPath: IndexPath, collectionView: UICollectionView, cell: UICollectionViewCell, metadata: tableMetadata, metadataFolder: tableMetadata?, serverUrl: String, isEditMode: Bool, isEncryptedFolder: Bool, selectocId: [String], autoUploadFileName: String, autoUploadDirectory: String, hideButtonMore: Bool, downloadThumbnail: Bool, shares: [tableShare]?, source: UIViewController, dataSource: NCDataSource?) {
  73. var tableShare: tableShare?
  74. // Share
  75. if shares != nil {
  76. for share in shares! {
  77. if share.fileName == metadata.fileName {
  78. tableShare = share
  79. break
  80. }
  81. }
  82. }
  83. // Download preview
  84. if downloadThumbnail {
  85. NCOperationQueue.shared.downloadThumbnail(metadata: metadata, urlBase: appDelegate.urlBase, view: collectionView, indexPath: indexPath)
  86. }
  87. var isShare = false
  88. var isMounted = false
  89. if metadataFolder != nil {
  90. isShare = metadata.permissions.contains(k_permission_shared) && !metadataFolder!.permissions.contains(k_permission_shared)
  91. isMounted = metadata.permissions.contains(k_permission_mounted) && !metadataFolder!.permissions.contains(k_permission_mounted)
  92. }
  93. if cell is NCListCell {
  94. let cell = cell as! NCListCell
  95. cell.delegate = source as? NCListCellDelegate
  96. cell.objectId = metadata.ocId
  97. cell.indexPath = indexPath
  98. cell.labelTitle.text = metadata.fileNameView
  99. cell.labelTitle.textColor = NCBrandColor.sharedInstance.textView
  100. cell.separator.backgroundColor = NCBrandColor.sharedInstance.separator
  101. cell.imageSelect.image = nil
  102. cell.imageStatus.image = nil
  103. cell.imageLocal.image = nil
  104. cell.imageFavorite.image = nil
  105. cell.imageShared.image = nil
  106. cell.imageMore.image = nil
  107. cell.imageItem.image = nil
  108. cell.imageItem.backgroundColor = nil
  109. cell.progressView.progress = 0.0
  110. if metadata.directory {
  111. if metadata.e2eEncrypted {
  112. cell.imageItem.image = NCCollectionCommonImages.cellFolderEncryptedImage
  113. } else if isShare {
  114. cell.imageItem.image = NCCollectionCommonImages.cellFolderSharedWithMeImage
  115. } else if (tableShare != nil && tableShare!.shareType != 3) {
  116. cell.imageItem.image = NCCollectionCommonImages.cellFolderSharedWithMeImage
  117. } else if (tableShare != nil && tableShare!.shareType == 3) {
  118. cell.imageItem.image = NCCollectionCommonImages.cellFolderPublicImage
  119. } else if metadata.mountType == "group" {
  120. cell.imageItem.image = NCCollectionCommonImages.cellFolderGroupImage
  121. } else if isMounted {
  122. cell.imageItem.image = NCCollectionCommonImages.cellFolderExternalImage
  123. } else if metadata.fileName == autoUploadFileName && serverUrl == autoUploadDirectory {
  124. cell.imageItem.image = NCCollectionCommonImages.cellFolderAutomaticUploadImage
  125. } else {
  126. cell.imageItem.image = NCCollectionCommonImages.cellFolderImage
  127. }
  128. cell.labelInfo.text = CCUtility.dateDiff(metadata.date as Date)
  129. let lockServerUrl = CCUtility.stringAppendServerUrl(serverUrl, addFileName: metadata.fileName)!
  130. let tableDirectory = NCManageDatabase.sharedInstance.getTableDirectory(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@", appDelegate.account, lockServerUrl))
  131. // Local image: offline
  132. if tableDirectory != nil && tableDirectory!.offline {
  133. cell.imageLocal.image = UIImage.init(named: "offlineFlag")
  134. }
  135. } else {
  136. if FileManager().fileExists(atPath: CCUtility.getDirectoryProviderStorageIconOcId(metadata.ocId, etag: metadata.etag)) {
  137. cell.imageItem.image = UIImage(contentsOfFile: CCUtility.getDirectoryProviderStorageIconOcId(metadata.ocId, etag: metadata.etag))
  138. } else {
  139. if metadata.hasPreview {
  140. cell.imageItem.backgroundColor = .lightGray
  141. } else {
  142. if metadata.iconName.count > 0 {
  143. cell.imageItem.image = UIImage.init(named: metadata.iconName)
  144. } else {
  145. cell.imageItem.image = UIImage.init(named: "file")
  146. }
  147. }
  148. }
  149. cell.labelInfo.text = CCUtility.dateDiff(metadata.date as Date) + " · " + CCUtility.transformedSize(metadata.size)
  150. // image local
  151. let size = CCUtility.fileProviderStorageSize(metadata.ocId, fileNameView: metadata.fileNameView)
  152. if size > 0 {
  153. let tableLocalFile = NCManageDatabase.sharedInstance.getTableLocalFile(predicate: NSPredicate(format: "ocId == %@", metadata.ocId))
  154. if tableLocalFile == nil && size == metadata.size {
  155. NCManageDatabase.sharedInstance.addLocalFile(metadata: metadata)
  156. }
  157. if tableLocalFile?.offline ?? false {
  158. cell.imageLocal.image = UIImage.init(named: "offlineFlag")
  159. } else{
  160. cell.imageLocal.image = UIImage.init(named: "local")
  161. }
  162. }
  163. }
  164. // image Favorite
  165. if metadata.favorite {
  166. cell.imageFavorite.image = NCCollectionCommonImages.cellFavouriteImage
  167. }
  168. // Share image
  169. if (isShare) {
  170. cell.imageShared.image = NCCollectionCommonImages.cellSharedImage
  171. } else if (tableShare != nil && tableShare!.shareType == 3) {
  172. cell.imageShared.image = NCCollectionCommonImages.cellShareByLinkImage
  173. } else if (tableShare != nil && tableShare!.shareType != 3) {
  174. cell.imageShared.image = NCCollectionCommonImages.cellSharedImage
  175. } else {
  176. cell.imageShared.image = NCCollectionCommonImages.cellCanShareImage
  177. }
  178. if metadata.ownerId.count > 0 && metadata.ownerId != appDelegate.userID {
  179. // Load avatar
  180. let fileNameSource = CCUtility.getDirectoryUserData() + "/" + CCUtility.getStringUser(appDelegate.user, urlBase: appDelegate.urlBase) + "-" + metadata.ownerId + ".png"
  181. let fileNameSourceAvatar = CCUtility.getDirectoryUserData() + "/" + CCUtility.getStringUser(appDelegate.user, urlBase: appDelegate.urlBase) + "-avatar-" + metadata.ownerId + ".png"
  182. if FileManager.default.fileExists(atPath: fileNameSourceAvatar) {
  183. cell.imageShared.image = UIImage(contentsOfFile: fileNameSourceAvatar)
  184. } else if FileManager.default.fileExists(atPath: fileNameSource) {
  185. cell.imageShared.image = NCUtility.shared.createAvatar(fileNameSource: fileNameSource, fileNameSourceAvatar: fileNameSourceAvatar)
  186. } else {
  187. NCCommunication.shared.downloadAvatar(userID: metadata.ownerId, fileNameLocalPath: fileNameSource, size: Int(k_avatar_size)) { (account, data, errorCode, errorMessage) in
  188. if errorCode == 0 && account == self.appDelegate.account {
  189. cell.imageShared.image = NCUtility.shared.createAvatar(fileNameSource: fileNameSource, fileNameSourceAvatar: fileNameSourceAvatar)
  190. }
  191. }
  192. }
  193. }
  194. if isEditMode {
  195. cell.imageItemLeftConstraint.constant = 45
  196. cell.imageSelect.isHidden = false
  197. if selectocId.contains(metadata.ocId) {
  198. cell.imageSelect.image = NCCollectionCommonImages.cellCheckedYes
  199. cell.backgroundView = NCUtility.shared.cellBlurEffect(with: cell.bounds)
  200. } else {
  201. cell.imageSelect.image = NCCollectionCommonImages.cellCheckedNo
  202. cell.backgroundView = nil
  203. }
  204. } else {
  205. cell.imageItemLeftConstraint.constant = 10
  206. cell.imageSelect.isHidden = true
  207. cell.backgroundView = nil
  208. }
  209. // Transfer
  210. var progress: Float = 0.0
  211. var totalBytes: Double = 0.0
  212. let progressArray = appDelegate.listProgressMetadata.object(forKey: metadata.ocId) as? NSArray
  213. if progressArray != nil && progressArray?.count == 3 {
  214. progress = progressArray?.object(at: 0) as? Float ?? 0
  215. totalBytes = progressArray?.object(at: 1) as? Double ?? 0
  216. }
  217. if metadata.status == k_metadataStatusInDownload || metadata.status == k_metadataStatusDownloading || metadata.status >= k_metadataStatusTypeUpload {
  218. cell.progressView.isHidden = false
  219. cell.setButtonMore(named: "stop")
  220. } else {
  221. cell.progressView.isHidden = true
  222. cell.progressView.progress = progress
  223. cell.setButtonMore(named: "more")
  224. }
  225. // Write status on Label Info
  226. switch metadata.status {
  227. case Int(k_metadataStatusWaitDownload):
  228. cell.labelInfo.text = CCUtility.transformedSize(metadata.size) + " - " + NSLocalizedString("_status_wait_download_", comment: "")
  229. break
  230. case Int(k_metadataStatusInDownload):
  231. cell.labelInfo.text = CCUtility.transformedSize(metadata.size) + " - " + NSLocalizedString("_status_in_download_", comment: "")
  232. break
  233. case Int(k_metadataStatusDownloading):
  234. cell.labelInfo.text = CCUtility.transformedSize(metadata.size) + " - ↓ " + CCUtility.transformedSize(totalBytes)
  235. break
  236. case Int(k_metadataStatusWaitUpload):
  237. cell.labelInfo.text = CCUtility.transformedSize(metadata.size) + " - " + NSLocalizedString("_status_wait_upload_", comment: "")
  238. break
  239. case Int(k_metadataStatusInUpload):
  240. cell.labelInfo.text = CCUtility.transformedSize(metadata.size) + " - " + NSLocalizedString("_status_in_upload_", comment: "")
  241. break
  242. case Int(k_metadataStatusUploading):
  243. cell.labelInfo.text = CCUtility.transformedSize(metadata.size) + " - ↑ " + CCUtility.transformedSize(totalBytes)
  244. break
  245. default:
  246. break
  247. }
  248. // Live Photo
  249. if metadata.livePhoto {
  250. cell.imageStatus.image = NCCollectionCommonImages.cellLivePhotoImage
  251. }
  252. // E2EE
  253. if metadata.e2eEncrypted || isEncryptedFolder {
  254. cell.hideButtonShare(true)
  255. } else {
  256. cell.hideButtonShare(false)
  257. }
  258. // Remove last separator
  259. if collectionView.numberOfItems(inSection: indexPath.section) == indexPath.row + 1 {
  260. cell.separator.isHidden = true
  261. } else {
  262. cell.separator.isHidden = false
  263. }
  264. } else if cell is NCGridCell {
  265. let cell = cell as! NCGridCell
  266. cell.delegate = source as? NCGridCellDelegate
  267. cell.objectId = metadata.ocId
  268. cell.indexPath = indexPath
  269. cell.labelTitle.text = metadata.fileNameView
  270. cell.labelTitle.textColor = NCBrandColor.sharedInstance.textView
  271. cell.imageSelect.image = nil
  272. cell.imageStatus.image = nil
  273. cell.imageLocal.image = nil
  274. cell.imageFavorite.image = nil
  275. cell.imageItem.image = nil
  276. cell.imageItem.backgroundColor = nil
  277. cell.progressView.progress = 0.0
  278. if metadata.directory {
  279. if metadata.e2eEncrypted {
  280. cell.imageItem.image = NCCollectionCommonImages.cellFolderEncryptedImage
  281. } else if isShare {
  282. cell.imageItem.image = NCCollectionCommonImages.cellFolderSharedWithMeImage
  283. } else if (tableShare != nil && tableShare!.shareType != 3) {
  284. cell.imageItem.image = NCCollectionCommonImages.cellFolderSharedWithMeImage
  285. } else if (tableShare != nil && tableShare!.shareType == 3) {
  286. cell.imageItem.image = NCCollectionCommonImages.cellFolderPublicImage
  287. } else if metadata.mountType == "group" {
  288. cell.imageItem.image = NCCollectionCommonImages.cellFolderGroupImage
  289. } else if isMounted {
  290. cell.imageItem.image = NCCollectionCommonImages.cellFolderExternalImage
  291. } else if metadata.fileName == autoUploadFileName && serverUrl == autoUploadDirectory {
  292. cell.imageItem.image = NCCollectionCommonImages.cellFolderAutomaticUploadImage
  293. } else {
  294. cell.imageItem.image = NCCollectionCommonImages.cellFolderImage
  295. }
  296. let lockServerUrl = CCUtility.stringAppendServerUrl(serverUrl, addFileName: metadata.fileName)!
  297. let tableDirectory = NCManageDatabase.sharedInstance.getTableDirectory(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@", appDelegate.account, lockServerUrl))
  298. // Local image: offline
  299. if tableDirectory != nil && tableDirectory!.offline {
  300. cell.imageLocal.image = UIImage.init(named: "offlineFlag")
  301. }
  302. } else {
  303. if FileManager().fileExists(atPath: CCUtility.getDirectoryProviderStorageIconOcId(metadata.ocId, etag: metadata.etag)) {
  304. cell.imageItem.image = UIImage(contentsOfFile: CCUtility.getDirectoryProviderStorageIconOcId(metadata.ocId, etag: metadata.etag))
  305. } else {
  306. if metadata.hasPreview {
  307. cell.imageItem.backgroundColor = .lightGray
  308. } else {
  309. if metadata.iconName.count > 0 {
  310. cell.imageItem.image = UIImage.init(named: metadata.iconName)
  311. } else {
  312. cell.imageItem.image = UIImage.init(named: "file")
  313. }
  314. }
  315. }
  316. // image Local
  317. let tableLocalFile = NCManageDatabase.sharedInstance.getTableLocalFile(predicate: NSPredicate(format: "ocId == %@", metadata.ocId))
  318. if tableLocalFile != nil && CCUtility.fileProviderStorageExists(metadata.ocId, fileNameView: metadata.fileNameView) {
  319. if tableLocalFile!.offline { cell.imageLocal.image = UIImage.init(named: "offlineFlag") }
  320. else { cell.imageLocal.image = UIImage.init(named: "local") }
  321. }
  322. }
  323. // image Favorite
  324. if metadata.favorite {
  325. cell.imageFavorite.image = NCCollectionCommonImages.cellFavouriteImage
  326. }
  327. if isEditMode {
  328. cell.imageSelect.isHidden = false
  329. if selectocId.contains(metadata.ocId) {
  330. cell.imageSelect.image = NCCollectionCommonImages.cellCheckedYes
  331. cell.backgroundView = NCUtility.shared.cellBlurEffect(with: cell.bounds)
  332. } else {
  333. cell.imageSelect.image = NCCollectionCommonImages.cellCheckedNo
  334. cell.backgroundView = nil
  335. }
  336. } else {
  337. cell.imageSelect.isHidden = true
  338. cell.backgroundView = nil
  339. }
  340. // Transfer
  341. if metadata.status == k_metadataStatusInDownload || metadata.status == k_metadataStatusDownloading || metadata.status >= k_metadataStatusTypeUpload {
  342. cell.progressView.isHidden = false
  343. cell.setButtonMore(named: "stop")
  344. } else {
  345. cell.progressView.isHidden = true
  346. cell.progressView.progress = 0.0
  347. cell.setButtonMore(named: "more")
  348. }
  349. // Live Photo
  350. if metadata.livePhoto {
  351. cell.imageStatus.image = NCCollectionCommonImages.cellLivePhotoImage
  352. }
  353. }
  354. }
  355. }
  356. // MARK: - List Layout
  357. class NCListLayout: UICollectionViewFlowLayout {
  358. let itemHeight: CGFloat = 60
  359. override init() {
  360. super.init()
  361. sectionHeadersPinToVisibleBounds = false
  362. minimumInteritemSpacing = 0
  363. minimumLineSpacing = 1
  364. self.scrollDirection = .vertical
  365. self.sectionInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
  366. }
  367. required init?(coder aDecoder: NSCoder) {
  368. fatalError("init(coder:) has not been implemented")
  369. }
  370. override var itemSize: CGSize {
  371. get {
  372. if let collectionView = collectionView {
  373. let itemWidth: CGFloat = collectionView.frame.width
  374. return CGSize(width: itemWidth, height: self.itemHeight)
  375. }
  376. // Default fallback
  377. return CGSize(width: 100, height: 100)
  378. }
  379. set {
  380. super.itemSize = newValue
  381. }
  382. }
  383. override func targetContentOffset(forProposedContentOffset proposedContentOffset: CGPoint) -> CGPoint {
  384. return proposedContentOffset
  385. }
  386. }
  387. // MARK: - Grid Layout
  388. class NCGridLayout: UICollectionViewFlowLayout {
  389. var heightLabelPlusButton: CGFloat = 45
  390. var marginLeftRight: CGFloat = 6
  391. var itemForLine: CGFloat = 3
  392. override init() {
  393. super.init()
  394. sectionHeadersPinToVisibleBounds = false
  395. minimumInteritemSpacing = 1
  396. minimumLineSpacing = marginLeftRight
  397. self.scrollDirection = .vertical
  398. self.sectionInset = UIEdgeInsets(top: 10, left: marginLeftRight, bottom: 0, right: marginLeftRight)
  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 - marginLeftRight * 2 - marginLeftRight * (itemForLine - 1)) / itemForLine
  407. let itemHeight: CGFloat = itemWidth + heightLabelPlusButton
  408. return CGSize(width: itemWidth, height: itemHeight)
  409. }
  410. // Default fallback
  411. return CGSize(width: 100, height: 100)
  412. }
  413. set {
  414. super.itemSize = newValue
  415. }
  416. }
  417. override func targetContentOffset(forProposedContentOffset proposedContentOffset: CGPoint) -> CGPoint {
  418. return proposedContentOffset
  419. }
  420. }
  421. // MARK: - NCSelect + Delegate
  422. extension NCCollectionCommon: NCSelectDelegate {
  423. func dismissSelect(serverUrl: String?, metadata: tableMetadata?, type: String, array: [Any], buttonType: String, overwrite: Bool) {
  424. if (serverUrl != nil && array.count > 0) {
  425. var move = true
  426. if buttonType == "done1" { move = false }
  427. for metadata in array as! [tableMetadata] {
  428. NCOperationQueue.shared.copyMove(metadata: metadata, serverUrl: serverUrl!, overwrite: overwrite, move: move)
  429. }
  430. }
  431. }
  432. func openSelectView(viewController: UIViewController, array: [Any]) {
  433. let navigationController = UIStoryboard.init(name: "NCSelect", bundle: nil).instantiateInitialViewController() as! UINavigationController
  434. let vc = navigationController.topViewController as! NCSelect
  435. vc.delegate = self
  436. vc.hideButtonCreateFolder = false
  437. vc.selectFile = false
  438. vc.includeDirectoryE2EEncryption = false
  439. vc.includeImages = false
  440. vc.type = ""
  441. vc.titleButtonDone = NSLocalizedString("_move_", comment: "")
  442. vc.titleButtonDone1 = NSLocalizedString("_copy_",comment: "")
  443. vc.isButtonDone1Hide = false
  444. vc.isOverwriteHide = false
  445. vc.keyLayout = k_layout_view_move
  446. vc.array = array
  447. navigationController.modalPresentationStyle = .fullScreen
  448. viewController.present(navigationController, animated: true, completion: nil)
  449. }
  450. }