NCSelect.swift 35 KB

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