NCSelect.swift 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836
  1. //
  2. // NCSelect.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 06/11/2018.
  6. // Copyright © 2018 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 UIKit
  24. import NextcloudKit
  25. @objc protocol NCSelectDelegate {
  26. @objc func dismissSelect(serverUrl: String?, metadata: tableMetadata?, type: String, items: [Any], overwrite: Bool, copy: Bool, move: Bool)
  27. }
  28. class NCSelect: UIViewController, UIGestureRecognizerDelegate, UIAdaptivePresentationControllerDelegate, NCListCellDelegate, NCGridCellDelegate, NCSectionHeaderMenuDelegate, NCEmptyDataSetDelegate {
  29. @IBOutlet private var collectionView: UICollectionView!
  30. @IBOutlet private var buttonCancel: UIBarButtonItem!
  31. @IBOutlet private var bottomContraint: NSLayoutConstraint?
  32. private var selectCommandViewSelect: NCSelectCommandView?
  33. @objc enum selectType: Int {
  34. case select
  35. case selectCreateFolder
  36. case copyMove
  37. }
  38. // ------ external settings ------------------------------------
  39. @objc weak var delegate: NCSelectDelegate?
  40. @objc var typeOfCommandView: selectType = .select
  41. @objc var includeDirectoryE2EEncryption = false
  42. @objc var includeImages = false
  43. @objc var enableSelectFile = false
  44. @objc var type = ""
  45. @objc var items: [tableMetadata] = []
  46. var titleCurrentFolder = NCBrandOptions.shared.brand
  47. var serverUrl = ""
  48. // -------------------------------------------------------------
  49. private var emptyDataSet: NCEmptyDataSet?
  50. private let layoutKey = NCGlobal.shared.layoutViewMove
  51. private var serverUrlPush = ""
  52. private var metadataFolder = tableMetadata()
  53. private var isEditMode = false
  54. private var isSearching = false
  55. private var networkInProgress = false
  56. private var selectOcId: [String] = []
  57. private var overwrite = true
  58. private var dataSource = NCDataSource()
  59. internal var richWorkspaceText: String?
  60. private var layoutForView: NCGlobal.layoutForViewType?
  61. internal var headerMenu: NCSectionHeaderMenu?
  62. private var autoUploadFileName = ""
  63. private var autoUploadDirectory = ""
  64. private var listLayout: NCListLayout!
  65. private var gridLayout: NCGridLayout!
  66. private var backgroundImageView = UIImageView()
  67. private var activeAccount: tableAccount!
  68. // MARK: - View Life Cycle
  69. override func viewDidLoad() {
  70. super.viewDidLoad()
  71. self.navigationController?.navigationBar.prefersLargeTitles = true
  72. self.navigationController?.presentationController?.delegate = self
  73. view.backgroundColor = .systemBackground
  74. selectCommandViewSelect?.separatorView.backgroundColor = .separator
  75. activeAccount = NCManageDatabase.shared.getActiveAccount()
  76. // Cell
  77. collectionView.register(UINib(nibName: "NCListCell", bundle: nil), forCellWithReuseIdentifier: "listCell")
  78. collectionView.register(UINib(nibName: "NCGridCell", bundle: nil), forCellWithReuseIdentifier: "gridCell")
  79. // Header
  80. collectionView.register(UINib(nibName: "NCSectionHeaderMenu", bundle: nil), forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "sectionHeaderMenu")
  81. // Footer
  82. collectionView.register(UINib(nibName: "NCSectionFooter", bundle: nil), forSupplementaryViewOfKind: UICollectionView.elementKindSectionFooter, withReuseIdentifier: "sectionFooter")
  83. collectionView.alwaysBounceVertical = true
  84. collectionView.backgroundColor = .systemBackground
  85. listLayout = NCListLayout()
  86. gridLayout = NCGridLayout()
  87. buttonCancel.title = NSLocalizedString("_cancel_", comment: "")
  88. bottomContraint?.constant = UIApplication.shared.keyWindow?.rootViewController?.view.safeAreaInsets.bottom ?? 0
  89. // Empty
  90. emptyDataSet = NCEmptyDataSet(view: collectionView, offset: NCGlobal.shared.heightButtonsView, delegate: self)
  91. // Type of command view
  92. if typeOfCommandView == .select || typeOfCommandView == .selectCreateFolder {
  93. if typeOfCommandView == .select {
  94. selectCommandViewSelect = Bundle.main.loadNibNamed("NCSelectCommandViewSelect", owner: self, options: nil)?.first as? NCSelectCommandView
  95. } else {
  96. selectCommandViewSelect = Bundle.main.loadNibNamed("NCSelectCommandViewSelect+CreateFolder", owner: self, options: nil)?.first as? NCSelectCommandView
  97. }
  98. self.view.addSubview(selectCommandViewSelect!)
  99. selectCommandViewSelect?.selectView = self
  100. selectCommandViewSelect?.translatesAutoresizingMaskIntoConstraints = false
  101. selectCommandViewSelect?.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: 0).isActive = true
  102. selectCommandViewSelect?.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 0).isActive = true
  103. selectCommandViewSelect?.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: 0).isActive = true
  104. selectCommandViewSelect?.heightAnchor.constraint(equalToConstant: 80).isActive = true
  105. bottomContraint?.constant = 80
  106. }
  107. if typeOfCommandView == .copyMove {
  108. selectCommandViewSelect = Bundle.main.loadNibNamed("NCSelectCommandViewCopyMove", owner: self, options: nil)?.first as? NCSelectCommandView
  109. self.view.addSubview(selectCommandViewSelect!)
  110. selectCommandViewSelect?.selectView = self
  111. selectCommandViewSelect?.translatesAutoresizingMaskIntoConstraints = false
  112. if items.contains(where: { $0.lock }) {
  113. selectCommandViewSelect?.moveButton?.isEnabled = false
  114. selectCommandViewSelect?.moveButton?.titleLabel?.isEnabled = false
  115. }
  116. selectCommandViewSelect?.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: 0).isActive = true
  117. selectCommandViewSelect?.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 0).isActive = true
  118. selectCommandViewSelect?.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: 0).isActive = true
  119. selectCommandViewSelect?.heightAnchor.constraint(equalToConstant: 150).isActive = true
  120. bottomContraint?.constant = 150
  121. }
  122. NotificationCenter.default.addObserver(self, selector: #selector(reloadDataSource), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterReloadDataSource), object: nil)
  123. NotificationCenter.default.addObserver(self, selector: #selector(createFolder(_:)), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterCreateFolder), object: nil)
  124. }
  125. override func viewWillAppear(_ animated: Bool) {
  126. super.viewWillAppear(animated)
  127. self.navigationItem.title = titleCurrentFolder
  128. // set the serverUrl
  129. if serverUrl == "" {
  130. serverUrl = NCUtilityFileSystem.shared.getHomeServer(urlBase: activeAccount.urlBase, userId: activeAccount.userId)
  131. }
  132. // get auto upload folder
  133. autoUploadFileName = NCManageDatabase.shared.getAccountAutoUploadFileName()
  134. autoUploadDirectory = NCManageDatabase.shared.getAccountAutoUploadDirectory(urlBase: activeAccount.urlBase, userId: activeAccount.userId, account: activeAccount.account)
  135. layoutForView = NCUtility.shared.getLayoutForView(key: layoutKey, serverUrl: serverUrl)
  136. gridLayout.itemForLine = CGFloat(layoutForView?.itemForLine ?? 3)
  137. if layoutForView?.layout == NCGlobal.shared.layoutList {
  138. collectionView.collectionViewLayout = listLayout
  139. } else {
  140. collectionView.collectionViewLayout = gridLayout
  141. }
  142. loadDatasource(withLoadFolder: true)
  143. }
  144. override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
  145. super.viewWillTransition(to: size, with: coordinator)
  146. coordinator.animate(alongsideTransition: nil) { _ in
  147. self.collectionView?.collectionViewLayout.invalidateLayout()
  148. }
  149. }
  150. func presentationControllerDidDismiss( _ presentationController: UIPresentationController) {
  151. // Dismission
  152. }
  153. // MARK: - NotificationCenter
  154. @objc func createFolder(_ notification: NSNotification) {
  155. guard let userInfo = notification.userInfo as NSDictionary?,
  156. let ocId = userInfo["ocId"] as? String,
  157. let serverUrl = userInfo["serverUrl"] as? String,
  158. serverUrl == self.serverUrl,
  159. let metadata = NCManageDatabase.shared.getMetadataFromOcId(ocId)
  160. else { return }
  161. pushMetadata(metadata)
  162. }
  163. // MARK: - Empty
  164. func emptyDataSetView(_ view: NCEmptyView) {
  165. if networkInProgress {
  166. view.emptyImage.image = UIImage(named: "networkInProgress")?.image(color: .gray, size: UIScreen.main.bounds.width)
  167. view.emptyTitle.text = NSLocalizedString("_request_in_progress_", comment: "")
  168. view.emptyDescription.text = ""
  169. } else {
  170. view.emptyImage.image = UIImage(named: "folder")?.image(color: NCBrandColor.shared.brandElement, size: UIScreen.main.bounds.width)
  171. if includeImages {
  172. view.emptyTitle.text = NSLocalizedString("_files_no_files_", comment: "")
  173. } else {
  174. view.emptyTitle.text = NSLocalizedString("_files_no_folders_", comment: "")
  175. }
  176. view.emptyDescription.text = ""
  177. }
  178. }
  179. // MARK: ACTION
  180. @IBAction func actionCancel(_ sender: UIBarButtonItem) {
  181. self.dismiss(animated: true, completion: nil)
  182. }
  183. func selectButtonPressed(_ sender: UIButton) {
  184. delegate?.dismissSelect(serverUrl: serverUrl, metadata: metadataFolder, type: type, items: items, overwrite: overwrite, copy: false, move: false)
  185. self.dismiss(animated: true, completion: nil)
  186. }
  187. func copyButtonPressed(_ sender: UIButton) {
  188. delegate?.dismissSelect(serverUrl: serverUrl, metadata: metadataFolder, type: type, items: items, overwrite: overwrite, copy: true, move: false)
  189. self.dismiss(animated: true, completion: nil)
  190. }
  191. func moveButtonPressed(_ sender: UIButton) {
  192. delegate?.dismissSelect(serverUrl: serverUrl, metadata: metadataFolder, type: type, items: items, overwrite: overwrite, copy: false, move: true)
  193. self.dismiss(animated: true, completion: nil)
  194. }
  195. func createFolderButtonPressed(_ sender: UIButton) {
  196. let alertController = UIAlertController.createFolder(serverUrl: serverUrl, urlBase: activeAccount)
  197. self.present(alertController, animated: true, completion: nil)
  198. }
  199. @IBAction func valueChangedSwitchOverwrite(_ sender: UISwitch) {
  200. overwrite = sender.isOn
  201. }
  202. // MARK: TAP EVENT
  203. func tapButtonSwitch(_ sender: Any) {
  204. if layoutForView?.layout == NCGlobal.shared.layoutGrid {
  205. // list layout
  206. headerMenu?.buttonSwitch.accessibilityLabel = NSLocalizedString("_grid_view_", comment: "")
  207. layoutForView?.layout = NCGlobal.shared.layoutList
  208. NCUtility.shared.setLayoutForView(key: layoutKey, serverUrl: serverUrl, layout: layoutForView?.layout)
  209. self.collectionView.reloadData()
  210. self.collectionView.collectionViewLayout.invalidateLayout()
  211. self.collectionView.setCollectionViewLayout(self.listLayout, animated: true)
  212. } else {
  213. // grid layout
  214. headerMenu?.buttonSwitch.accessibilityLabel = NSLocalizedString("_list_view_", comment: "")
  215. layoutForView?.layout = NCGlobal.shared.layoutGrid
  216. NCUtility.shared.setLayoutForView(key: layoutKey, serverUrl: serverUrl, layout: layoutForView?.layout)
  217. self.collectionView.reloadData()
  218. self.collectionView.collectionViewLayout.invalidateLayout()
  219. self.collectionView.setCollectionViewLayout(self.gridLayout, animated: true)
  220. }
  221. }
  222. func tapButtonOrder(_ sender: Any) {
  223. let sortMenu = NCSortMenu()
  224. sortMenu.toggleMenu(viewController: self, key: layoutKey, sortButton: sender as? UIButton, serverUrl: serverUrl)
  225. }
  226. // MARK: - Push metadata
  227. func pushMetadata(_ metadata: tableMetadata) {
  228. guard let serverUrlPush = CCUtility.stringAppendServerUrl(metadata.serverUrl, addFileName: metadata.fileName) else { return }
  229. guard let viewController = UIStoryboard(name: "NCSelect", bundle: nil).instantiateViewController(withIdentifier: "NCSelect.storyboard") as? NCSelect else { return }
  230. self.serverUrlPush = serverUrlPush
  231. viewController.delegate = delegate
  232. viewController.typeOfCommandView = typeOfCommandView
  233. viewController.includeDirectoryE2EEncryption = includeDirectoryE2EEncryption
  234. viewController.includeImages = includeImages
  235. viewController.enableSelectFile = enableSelectFile
  236. viewController.type = type
  237. viewController.overwrite = overwrite
  238. viewController.items = items
  239. viewController.titleCurrentFolder = metadata.fileNameView
  240. viewController.serverUrl = serverUrlPush
  241. self.navigationController?.pushViewController(viewController, animated: true)
  242. }
  243. }
  244. // MARK: - Collection View
  245. extension NCSelect: UICollectionViewDelegate {
  246. func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  247. guard let metadata = dataSource.cellForItemAt(indexPath: indexPath) else { return }
  248. if isEditMode {
  249. if let index = selectOcId.firstIndex(of: metadata.ocId) {
  250. selectOcId.remove(at: index)
  251. } else {
  252. selectOcId.append(metadata.ocId)
  253. }
  254. collectionView.reloadItems(at: [indexPath])
  255. return
  256. }
  257. if metadata.directory {
  258. pushMetadata(metadata)
  259. } else {
  260. delegate?.dismissSelect(serverUrl: serverUrl, metadata: metadata, type: type, items: items, overwrite: overwrite, copy: false, move: false)
  261. self.dismiss(animated: true, completion: nil)
  262. }
  263. }
  264. }
  265. extension NCSelect: UICollectionViewDataSource {
  266. func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
  267. guard let metadata = dataSource.cellForItemAt(indexPath: indexPath) else { return }
  268. // Thumbnail
  269. if !metadata.directory {
  270. if FileManager().fileExists(atPath: CCUtility.getDirectoryProviderStorageIconOcId(metadata.ocId, etag: metadata.etag)) {
  271. (cell as! NCCellProtocol).filePreviewImageView?.image = UIImage(contentsOfFile: CCUtility.getDirectoryProviderStorageIconOcId(metadata.ocId, etag: metadata.etag))
  272. } else {
  273. NCOperationQueue.shared.downloadThumbnail(metadata: metadata, placeholder: true, cell: cell, view: collectionView)
  274. }
  275. }
  276. // Avatar
  277. if metadata.ownerId.count > 0,
  278. metadata.ownerId != activeAccount.userId,
  279. activeAccount.account == metadata.account,
  280. let cell = cell as? NCCellProtocol {
  281. let fileName = metadata.userBaseUrl + "-" + metadata.ownerId + ".png"
  282. NCOperationQueue.shared.downloadAvatar(user: metadata.ownerId, dispalyName: metadata.ownerDisplayName, fileName: fileName, cell: cell, view: collectionView, cellImageView: cell.fileAvatarImageView)
  283. }
  284. }
  285. func collectionView(_ collectionView: UICollectionView, didEndDisplaying cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
  286. }
  287. func numberOfSections(in collectionView: UICollectionView) -> Int {
  288. return dataSource.numberOfSections()
  289. }
  290. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  291. let numberOfItems = dataSource.numberOfItemsInSection(section)
  292. emptyDataSet?.numberOfItemsInSection(numberOfItems, section: section)
  293. return numberOfItems
  294. }
  295. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  296. guard let metadata = dataSource.cellForItemAt(indexPath: indexPath) else {
  297. if layoutForView?.layout == NCGlobal.shared.layoutList {
  298. return collectionView.dequeueReusableCell(withReuseIdentifier: "listCell", for: indexPath) as! NCListCell
  299. } else {
  300. return collectionView.dequeueReusableCell(withReuseIdentifier: "gridCell", for: indexPath) as! NCGridCell
  301. }
  302. }
  303. var isShare = false
  304. var isMounted = false
  305. isShare = metadata.permissions.contains(NCGlobal.shared.permissionShared) && !metadataFolder.permissions.contains(NCGlobal.shared.permissionShared)
  306. isMounted = metadata.permissions.contains(NCGlobal.shared.permissionMounted) && !metadataFolder.permissions.contains(NCGlobal.shared.permissionMounted)
  307. let tableShare = dataSource.metadatasForSection[indexPath.section].metadataShare[metadata.ocId]
  308. // LAYOUT LIST
  309. if layoutForView?.layout == NCGlobal.shared.layoutList {
  310. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "listCell", for: indexPath) as! NCListCell
  311. cell.delegate = self
  312. cell.fileObjectId = metadata.ocId
  313. cell.fileUser = metadata.ownerId
  314. cell.labelTitle.text = metadata.fileNameView
  315. cell.labelTitle.textColor = .label
  316. cell.imageSelect.image = nil
  317. cell.imageStatus.image = nil
  318. cell.imageLocal.image = nil
  319. cell.imageFavorite.image = nil
  320. cell.imageShared.image = nil
  321. cell.imageMore.image = nil
  322. cell.imageItem.image = nil
  323. cell.imageItem.backgroundColor = nil
  324. cell.progressView.progress = 0.0
  325. if metadata.directory {
  326. if metadata.e2eEncrypted {
  327. cell.imageItem.image = NCBrandColor.cacheImages.folderEncrypted
  328. } else if isShare {
  329. cell.imageItem.image = NCBrandColor.cacheImages.folderSharedWithMe
  330. } else if tableShare != nil && tableShare?.shareType != 3 {
  331. cell.imageItem.image = NCBrandColor.cacheImages.folderSharedWithMe
  332. } else if tableShare != nil && tableShare?.shareType == 3 {
  333. cell.imageItem.image = NCBrandColor.cacheImages.folderPublic
  334. } else if metadata.mountType == "group" {
  335. cell.imageItem.image = NCBrandColor.cacheImages.folderGroup
  336. } else if isMounted {
  337. cell.imageItem.image = NCBrandColor.cacheImages.folderExternal
  338. } else if metadata.fileName == autoUploadFileName && metadata.serverUrl == autoUploadDirectory {
  339. cell.imageItem.image = NCBrandColor.cacheImages.folderAutomaticUpload
  340. } else {
  341. cell.imageItem.image = NCBrandColor.cacheImages.folder
  342. }
  343. cell.imageItem.image = cell.imageItem.image?.colorizeFolder(metadata: metadata)
  344. cell.labelInfo.text = CCUtility.dateDiff(metadata.date as Date)
  345. } else {
  346. cell.labelInfo.text = CCUtility.dateDiff(metadata.date as Date) + " · " + CCUtility.transformedSize(metadata.size)
  347. // image local
  348. if dataSource.metadatasForSection[indexPath.section].metadataOffLine.contains(metadata.ocId) {
  349. cell.imageLocal.image = NCBrandColor.cacheImages.offlineFlag
  350. } else if CCUtility.fileProviderStorageExists(metadata) {
  351. cell.imageLocal.image = NCBrandColor.cacheImages.local
  352. }
  353. }
  354. // image Favorite
  355. if metadata.favorite {
  356. cell.imageFavorite.image = NCBrandColor.cacheImages.favorite
  357. }
  358. // Share image
  359. if isShare {
  360. cell.imageShared.image = NCBrandColor.cacheImages.shared
  361. } else if tableShare != nil && tableShare?.shareType == 3 {
  362. cell.imageShared.image = NCBrandColor.cacheImages.shareByLink
  363. } else if tableShare != nil && tableShare?.shareType != 3 {
  364. cell.imageShared.image = NCBrandColor.cacheImages.shared
  365. } else {
  366. cell.imageShared.image = NCBrandColor.cacheImages.canShare
  367. }
  368. cell.imageSelect.isHidden = true
  369. cell.backgroundView = nil
  370. cell.hideButtonMore(true)
  371. cell.hideButtonShare(true)
  372. cell.selectMode(false)
  373. // Live Photo
  374. if metadata.livePhoto {
  375. cell.imageStatus.image = NCBrandColor.cacheImages.livePhoto
  376. }
  377. // Remove last separator
  378. if collectionView.numberOfItems(inSection: indexPath.section) == indexPath.row + 1 {
  379. cell.separator.isHidden = true
  380. } else {
  381. cell.separator.isHidden = false
  382. }
  383. return cell
  384. }
  385. // LAYOUT GRID
  386. if layoutForView?.layout == NCGlobal.shared.layoutGrid {
  387. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "gridCell", for: indexPath) as! NCGridCell
  388. cell.delegate = self
  389. cell.fileObjectId = metadata.ocId
  390. cell.fileUser = metadata.ownerId
  391. cell.labelTitle.text = metadata.fileNameView
  392. cell.labelTitle.textColor = .label
  393. cell.imageSelect.image = nil
  394. cell.imageStatus.image = nil
  395. cell.imageLocal.image = nil
  396. cell.imageFavorite.image = nil
  397. cell.imageItem.image = nil
  398. cell.imageItem.backgroundColor = nil
  399. cell.progressView.progress = 0.0
  400. if metadata.directory {
  401. if metadata.e2eEncrypted {
  402. cell.imageItem.image = NCBrandColor.cacheImages.folderEncrypted
  403. } else if isShare {
  404. cell.imageItem.image = NCBrandColor.cacheImages.folderSharedWithMe
  405. } else if tableShare != nil && tableShare!.shareType != 3 {
  406. cell.imageItem.image = NCBrandColor.cacheImages.folderSharedWithMe
  407. } else if tableShare != nil && tableShare!.shareType == 3 {
  408. cell.imageItem.image = NCBrandColor.cacheImages.folderPublic
  409. } else if metadata.mountType == "group" {
  410. cell.imageItem.image = NCBrandColor.cacheImages.folderGroup
  411. } else if isMounted {
  412. cell.imageItem.image = NCBrandColor.cacheImages.folderExternal
  413. } else if metadata.fileName == autoUploadFileName && metadata.serverUrl == autoUploadDirectory {
  414. cell.imageItem.image = NCBrandColor.cacheImages.folderAutomaticUpload
  415. } else {
  416. cell.imageItem.image = NCBrandColor.cacheImages.folder
  417. }
  418. cell.imageItem.image = cell.imageItem.image?.colorizeFolder(metadata: metadata)
  419. } else {
  420. // image Local
  421. if dataSource.metadatasForSection[indexPath.section].metadataOffLine.contains(metadata.ocId) {
  422. cell.imageLocal.image = NCBrandColor.cacheImages.offlineFlag
  423. } else if CCUtility.fileProviderStorageExists(metadata) {
  424. cell.imageLocal.image = NCBrandColor.cacheImages.local
  425. }
  426. }
  427. // image Favorite
  428. if metadata.favorite {
  429. cell.imageFavorite.image = NCBrandColor.cacheImages.favorite
  430. }
  431. cell.imageSelect.isHidden = true
  432. cell.backgroundView = nil
  433. cell.hideButtonMore(true)
  434. // Live Photo
  435. if metadata.livePhoto {
  436. cell.imageStatus.image = NCBrandColor.cacheImages.livePhoto
  437. }
  438. return cell
  439. }
  440. return collectionView.dequeueReusableCell(withReuseIdentifier: "gridCell", for: indexPath) as! NCGridCell
  441. }
  442. func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
  443. if kind == UICollectionView.elementKindSectionHeader {
  444. if indexPath.section == 0 {
  445. let header = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "sectionHeaderMenu", for: indexPath) as! NCSectionHeaderMenu
  446. let (_, heightHeaderRichWorkspace, heightHeaderSection) = getHeaderHeight(section: indexPath.section)
  447. self.headerMenu = header
  448. if layoutForView?.layout == NCGlobal.shared.layoutGrid {
  449. header.setImageSwitchList()
  450. header.buttonSwitch.accessibilityLabel = NSLocalizedString("_list_view_", comment: "")
  451. } else {
  452. header.setImageSwitchGrid()
  453. header.buttonSwitch.accessibilityLabel = NSLocalizedString("_grid_view_", comment: "")
  454. }
  455. header.delegate = self
  456. header.setButtonsCommand(heigt: 0)
  457. header.setButtonsView(heigt: NCGlobal.shared.heightButtonsView)
  458. header.setStatusButtonsView(enable: !dataSource.getMetadataSourceForAllSections().isEmpty)
  459. header.setSortedTitle(layoutForView?.titleButtonHeader ?? "")
  460. header.setRichWorkspaceHeight(heightHeaderRichWorkspace)
  461. header.setRichWorkspaceText(richWorkspaceText)
  462. header.setSectionHeight(heightHeaderSection)
  463. if heightHeaderSection == 0 {
  464. header.labelSection.text = ""
  465. } else {
  466. header.labelSection.text = self.dataSource.getSectionValueLocalization(indexPath: indexPath)
  467. }
  468. header.labelSection.textColor = .label
  469. return header
  470. } else {
  471. let header = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "sectionHeader", for: indexPath) as! NCSectionHeader
  472. header.labelSection.text = self.dataSource.getSectionValueLocalization(indexPath: indexPath)
  473. header.labelSection.textColor = NCBrandColor.shared.brandElement
  474. return header
  475. }
  476. } else {
  477. let footer = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "sectionFooter", for: indexPath) as! NCSectionFooter
  478. let sections = dataSource.numberOfSections()
  479. let section = indexPath.section
  480. footer.setTitleLabel("")
  481. footer.separatorIsHidden(true)
  482. if sections == 1 || section == sections - 1 {
  483. let info = dataSource.getFooterInformationAllMetadatas()
  484. footer.setTitleLabel(directories: info.directories, files: info.files, size: info.size)
  485. } else {
  486. footer.separatorIsHidden(false)
  487. }
  488. return footer
  489. }
  490. }
  491. }
  492. extension NCSelect: UICollectionViewDelegateFlowLayout {
  493. func getHeaderHeight(section:Int) -> (heightHeaderCommands: CGFloat, heightHeaderRichWorkspace: CGFloat, heightHeaderSection: CGFloat) {
  494. var headerRichWorkspace: CGFloat = 0
  495. if let richWorkspaceText = richWorkspaceText {
  496. let trimmed = richWorkspaceText.trimmingCharacters(in: .whitespaces)
  497. if trimmed.count > 0 && !isSearching {
  498. headerRichWorkspace = UIScreen.main.bounds.size.height / 6
  499. }
  500. }
  501. if isSearching || layoutForView?.layout == NCGlobal.shared.layoutGrid || dataSource.numberOfSections() > 1 {
  502. if section == 0 {
  503. return (NCGlobal.shared.heightButtonsView, headerRichWorkspace, NCGlobal.shared.heightSection)
  504. } else {
  505. return (0, 0, NCGlobal.shared.heightSection)
  506. }
  507. } else {
  508. return (NCGlobal.shared.heightButtonsView, headerRichWorkspace, 0)
  509. }
  510. }
  511. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
  512. let (heightHeaderCommands, heightHeaderRichWorkspace, heightHeaderSection) = getHeaderHeight(section: section)
  513. let heightHeader = heightHeaderCommands + heightHeaderRichWorkspace + heightHeaderSection
  514. return CGSize(width: collectionView.frame.width, height: heightHeader)
  515. }
  516. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize {
  517. let sections = dataSource.numberOfSections()
  518. if section == sections - 1 {
  519. return CGSize(width: collectionView.frame.width, height: NCGlobal.shared.endHeightFooter)
  520. } else {
  521. return CGSize(width: collectionView.frame.width, height: NCGlobal.shared.heightFooter)
  522. }
  523. }
  524. }
  525. // MARK: -
  526. extension NCSelect {
  527. @objc func reloadDataSource() {
  528. loadDatasource(withLoadFolder: false)
  529. }
  530. @objc func loadDatasource(withLoadFolder: Bool) {
  531. var predicate: NSPredicate?
  532. var groupByField = "name"
  533. layoutForView = NCUtility.shared.getLayoutForView(key: layoutKey, serverUrl: serverUrl)
  534. // set GroupField for Grid
  535. if layoutForView?.layout == NCGlobal.shared.layoutGrid {
  536. groupByField = "classFile"
  537. }
  538. if includeDirectoryE2EEncryption {
  539. if includeImages {
  540. predicate = NSPredicate(format: "account == %@ AND serverUrl == %@ AND (directory == true OR classFile == 'image')", activeAccount.account, serverUrl)
  541. } else {
  542. predicate = NSPredicate(format: "account == %@ AND serverUrl == %@ AND directory == true", activeAccount.account, serverUrl)
  543. }
  544. } else {
  545. if includeImages {
  546. predicate = NSPredicate(format: "account == %@ AND serverUrl == %@ AND e2eEncrypted == false AND (directory == true OR classFile == 'image')", activeAccount.account, serverUrl)
  547. } else {
  548. predicate = NSPredicate(format: "account == %@ AND serverUrl == %@ AND e2eEncrypted == false AND directory == true", activeAccount.account, serverUrl)
  549. }
  550. }
  551. let metadatas = NCManageDatabase.shared.getMetadatas(predicate: predicate!)
  552. self.dataSource = NCDataSource(metadatas: metadatas,
  553. account: activeAccount.account,
  554. sort: layoutForView?.sort,
  555. ascending: layoutForView?.ascending,
  556. directoryOnTop: layoutForView?.directoryOnTop,
  557. favoriteOnTop: true,
  558. filterLivePhoto: true,
  559. groupByField: groupByField)
  560. if withLoadFolder {
  561. loadFolder()
  562. }
  563. let directory = NCManageDatabase.shared.getTableDirectory(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@", activeAccount.account, serverUrl))
  564. richWorkspaceText = directory?.richWorkspace
  565. DispatchQueue.main.async {
  566. self.collectionView.reloadData()
  567. }
  568. }
  569. func loadFolder() {
  570. networkInProgress = true
  571. collectionView.reloadData()
  572. NCNetworking.shared.readFolder(serverUrl: serverUrl, account: activeAccount.account) { _, _, _, _, _, _, error in
  573. if error != .success {
  574. NCContentPresenter.shared.showError(error: error)
  575. }
  576. self.networkInProgress = false
  577. self.loadDatasource(withLoadFolder: false)
  578. }
  579. }
  580. }
  581. // MARK: -
  582. class NCSelectCommandView: UIView {
  583. @IBOutlet weak var separatorView: UIView!
  584. @IBOutlet weak var createFolderButton: UIButton?
  585. @IBOutlet weak var selectButton: UIButton?
  586. @IBOutlet weak var copyButton: UIButton?
  587. @IBOutlet weak var moveButton: UIButton?
  588. @IBOutlet weak var overwriteSwitch: UISwitch?
  589. @IBOutlet weak var overwriteLabel: UILabel?
  590. @IBOutlet weak var separatorHeightConstraint: NSLayoutConstraint!
  591. var selectView: NCSelect?
  592. private let gradient: CAGradientLayer = CAGradientLayer()
  593. override func awakeFromNib() {
  594. separatorHeightConstraint.constant = 0.5
  595. separatorView.backgroundColor = .separator
  596. overwriteLabel?.text = NSLocalizedString("_overwrite_", comment: "")
  597. selectButton?.layer.cornerRadius = 15
  598. selectButton?.layer.masksToBounds = true
  599. selectButton?.setTitle(NSLocalizedString("_select_", comment: ""), for: .normal)
  600. selectButton?.backgroundColor = NCBrandColor.shared.brand
  601. selectButton?.setTitleColor(UIColor(white: 1, alpha: 0.3), for: .highlighted)
  602. selectButton?.setTitleColor(NCBrandColor.shared.brandText, for: .normal)
  603. createFolderButton?.layer.cornerRadius = 15
  604. createFolderButton?.layer.masksToBounds = true
  605. createFolderButton?.setTitle(NSLocalizedString("_create_folder_", comment: ""), for: .normal)
  606. createFolderButton?.backgroundColor = NCBrandColor.shared.brand
  607. createFolderButton?.setTitleColor(UIColor(white: 1, alpha: 0.3), for: .highlighted)
  608. createFolderButton?.setTitleColor(NCBrandColor.shared.brandText, for: .normal)
  609. copyButton?.layer.cornerRadius = 15
  610. copyButton?.layer.masksToBounds = true
  611. copyButton?.setTitle(NSLocalizedString("_copy_", comment: ""), for: .normal)
  612. copyButton?.backgroundColor = NCBrandColor.shared.brand
  613. copyButton?.setTitleColor(UIColor(white: 1, alpha: 0.3), for: .highlighted)
  614. copyButton?.setTitleColor(NCBrandColor.shared.brandText, for: .normal)
  615. moveButton?.layer.cornerRadius = 15
  616. moveButton?.layer.masksToBounds = true
  617. moveButton?.setTitle(NSLocalizedString("_move_", comment: ""), for: .normal)
  618. moveButton?.backgroundColor = NCBrandColor.shared.brand
  619. moveButton?.setTitleColor(UIColor(white: 1, alpha: 0.3), for: .highlighted)
  620. moveButton?.setTitleColor(NCBrandColor.shared.brandText, for: .normal)
  621. }
  622. @IBAction func createFolderButtonPressed(_ sender: UIButton) {
  623. selectView?.createFolderButtonPressed(sender)
  624. }
  625. @IBAction func selectButtonPressed(_ sender: UIButton) {
  626. selectView?.selectButtonPressed(sender)
  627. }
  628. @IBAction func copyButtonPressed(_ sender: UIButton) {
  629. selectView?.copyButtonPressed(sender)
  630. }
  631. @IBAction func moveButtonPressed(_ sender: UIButton) {
  632. selectView?.moveButtonPressed(sender)
  633. }
  634. @IBAction func valueChangedSwitchOverwrite(_ sender: UISwitch) {
  635. selectView?.valueChangedSwitchOverwrite(sender)
  636. }
  637. }