NCSelect.swift 32 KB

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