NCSelect.swift 36 KB

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