NCSelect.swift 33 KB

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