NCSelect.swift 37 KB

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