NCSelect.swift 35 KB

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