NCCollectionCommon.swift 26 KB

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