NCSelect.swift 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774
  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 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 metadataTouch: tableMetadata?
  52. private var metadataFolder = tableMetadata()
  53. private var isEditMode = false
  54. private var networkInProgress = false
  55. private var selectOcId: [String] = []
  56. private var overwrite = true
  57. private var dataSource = NCDataSource()
  58. internal var richWorkspaceText: String?
  59. private var layoutForView: NCGlobal.layoutForViewType?
  60. private var autoUploadFileName = ""
  61. private var autoUploadDirectory = ""
  62. private var listLayout: NCListLayout!
  63. private var gridLayout: NCGridLayout!
  64. private let headerHeight: CGFloat = 50
  65. private var headerRichWorkspaceHeight: CGFloat = 0
  66. private let footerHeight: CGFloat = 100
  67. private var shares: [tableShare]?
  68. private var backgroundImageView = UIImageView()
  69. private var activeAccount: tableAccount!
  70. // MARK: - View Life Cycle
  71. override func viewDidLoad() {
  72. super.viewDidLoad()
  73. self.navigationController?.navigationBar.prefersLargeTitles = true
  74. self.navigationController?.presentationController?.delegate = self
  75. view.backgroundColor = NCBrandColor.shared.systemBackground
  76. activeAccount = NCManageDatabase.shared.getActiveAccount()
  77. // Cell
  78. collectionView.register(UINib.init(nibName: "NCListCell", bundle: nil), forCellWithReuseIdentifier: "listCell")
  79. collectionView.register(UINib.init(nibName: "NCGridCell", bundle: nil), forCellWithReuseIdentifier: "gridCell")
  80. // Header
  81. collectionView.register(UINib.init(nibName: "NCSectionHeaderMenu", bundle: nil), forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "sectionHeaderMenu")
  82. // Footer
  83. collectionView.register(UINib.init(nibName: "NCSectionFooter", bundle: nil), forSupplementaryViewOfKind: UICollectionView.elementKindSectionFooter, withReuseIdentifier: "sectionFooter")
  84. collectionView.alwaysBounceVertical = true
  85. collectionView.backgroundColor = NCBrandColor.shared.systemBackground
  86. listLayout = NCListLayout()
  87. gridLayout = NCGridLayout()
  88. buttonCancel.title = NSLocalizedString("_cancel_", comment: "")
  89. // Empty
  90. emptyDataSet = NCEmptyDataSet.init(view: collectionView, offset: headerHeight, 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. }
  106. if typeOfCommandView == .copyMove {
  107. selectCommandViewSelect = Bundle.main.loadNibNamed("NCSelectCommandViewCopyMove", owner: self, options: nil)?.first as? NCSelectCommandView
  108. self.view.addSubview(selectCommandViewSelect!)
  109. selectCommandViewSelect?.selectView = self
  110. selectCommandViewSelect?.translatesAutoresizingMaskIntoConstraints = false
  111. selectCommandViewSelect?.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: 0).isActive = true
  112. selectCommandViewSelect?.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 0).isActive = true
  113. selectCommandViewSelect?.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: 0).isActive = true
  114. selectCommandViewSelect?.heightAnchor.constraint(equalToConstant: 150).isActive = true
  115. }
  116. NotificationCenter.default.addObserver(self, selector: #selector(changeTheming), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterChangeTheming), object: nil)
  117. NotificationCenter.default.addObserver(self, selector: #selector(reloadDataSource), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterReloadDataSource), 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. @objc func changeTheming() {
  147. collectionView.reloadData()
  148. selectCommandViewSelect?.separatorView.backgroundColor = NCBrandColor.shared.separator
  149. }
  150. func presentationControllerDidDismiss( _ presentationController: UIPresentationController) {
  151. // Dismission
  152. }
  153. // MARK: - Empty
  154. func emptyDataSetView(_ view: NCEmptyView) {
  155. if networkInProgress {
  156. view.emptyImage.image = UIImage.init(named: "networkInProgress")?.image(color: .gray, size: UIScreen.main.bounds.width)
  157. view.emptyTitle.text = NSLocalizedString("_request_in_progress_", comment: "")
  158. view.emptyDescription.text = ""
  159. } else {
  160. view.emptyImage.image = UIImage.init(named: "folder")?.image(color: NCBrandColor.shared.brandElement, size: UIScreen.main.bounds.width)
  161. if includeImages {
  162. view.emptyTitle.text = NSLocalizedString("_files_no_files_", comment: "")
  163. } else {
  164. view.emptyTitle.text = NSLocalizedString("_files_no_folders_", comment: "")
  165. }
  166. view.emptyDescription.text = ""
  167. }
  168. }
  169. // MARK: ACTION
  170. @IBAction func actionCancel(_ sender: UIBarButtonItem) {
  171. self.dismiss(animated: true, completion: nil)
  172. }
  173. func selectButtonPressed(_ sender: UIButton) {
  174. delegate?.dismissSelect(serverUrl: serverUrl, metadata: metadataFolder, type: type, items: items, overwrite: overwrite, copy: false, move: false)
  175. self.dismiss(animated: true, completion: nil)
  176. }
  177. func copyButtonPressed(_ sender: UIButton) {
  178. delegate?.dismissSelect(serverUrl: serverUrl, metadata: metadataFolder, type: type, items: items, overwrite: overwrite, copy: true, move: false)
  179. self.dismiss(animated: true, completion: nil)
  180. }
  181. func moveButtonPressed(_ sender: UIButton) {
  182. delegate?.dismissSelect(serverUrl: serverUrl, metadata: metadataFolder, type: type, items: items, overwrite: overwrite, copy: false, move: true)
  183. self.dismiss(animated: true, completion: nil)
  184. }
  185. func createFolderButtonPressed(_ sender: UIButton) {
  186. let alertController = UIAlertController(title: NSLocalizedString("_create_folder_", comment: ""), message:"", preferredStyle: .alert)
  187. alertController.addTextField { (textField) in
  188. textField.autocapitalizationType = UITextAutocapitalizationType.words
  189. }
  190. let actionSave = UIAlertAction(title: NSLocalizedString("_save_", comment: ""), style: .default) { (action:UIAlertAction) in
  191. if let fileName = alertController.textFields?.first?.text {
  192. self.createFolder(with: fileName)
  193. }
  194. }
  195. let actionCancel = UIAlertAction(title: NSLocalizedString("_cancel_", comment: ""), style: .cancel) { (action:UIAlertAction) in
  196. print("You've pressed cancel button")
  197. }
  198. alertController.addAction(actionSave)
  199. alertController.addAction(actionCancel)
  200. self.present(alertController, animated: true, completion:nil)
  201. }
  202. @IBAction func valueChangedSwitchOverwrite(_ sender: UISwitch) {
  203. overwrite = sender.isOn
  204. }
  205. // MARK: TAP EVENT
  206. func tapSwitchHeader(sender: Any) {
  207. if collectionView.collectionViewLayout == gridLayout {
  208. // list layout
  209. UIView.animate(withDuration: 0.0, animations: {
  210. self.collectionView.collectionViewLayout.invalidateLayout()
  211. self.collectionView.setCollectionViewLayout(self.listLayout, animated: false, completion: { (_) in
  212. self.collectionView.reloadData()
  213. })
  214. })
  215. layoutForView?.layout = NCGlobal.shared.layoutList
  216. } else {
  217. // grid layout
  218. UIView.animate(withDuration: 0.0, animations: {
  219. self.collectionView.collectionViewLayout.invalidateLayout()
  220. self.collectionView.setCollectionViewLayout(self.gridLayout, animated: false, completion: { (_) in
  221. self.collectionView.reloadData()
  222. })
  223. })
  224. layoutForView?.layout = NCGlobal.shared.layoutGrid
  225. }
  226. }
  227. func tapOrderHeader(sender: Any) {
  228. let sortMenu = NCSortMenu()
  229. sortMenu.toggleMenu(viewController: self, key: keyLayout, sortButton: sender as? UIButton, serverUrl: serverUrl)
  230. }
  231. }
  232. // MARK: - Collection View
  233. extension NCSelect: UICollectionViewDelegate {
  234. func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  235. guard let metadata = dataSource.cellForItemAt(indexPath: indexPath) else { return }
  236. if isEditMode {
  237. if let index = selectOcId.firstIndex(of: metadata.ocId) {
  238. selectOcId.remove(at: index)
  239. } else {
  240. selectOcId.append(metadata.ocId)
  241. }
  242. collectionView.reloadItems(at: [indexPath])
  243. return
  244. }
  245. if metadata.directory {
  246. guard let serverUrlPush = CCUtility.stringAppendServerUrl(metadata.serverUrl, addFileName: metadata.fileName) else { return }
  247. guard let viewController = UIStoryboard(name: "NCSelect", bundle: nil).instantiateViewController(withIdentifier: "NCSelect.storyboard") as? NCSelect else { return }
  248. self.serverUrlPush = serverUrlPush
  249. self.metadataTouch = metadata
  250. viewController.delegate = delegate
  251. viewController.typeOfCommandView = typeOfCommandView
  252. viewController.includeDirectoryE2EEncryption = includeDirectoryE2EEncryption
  253. viewController.includeImages = includeImages
  254. viewController.enableSelectFile = enableSelectFile
  255. viewController.type = type
  256. viewController.overwrite = overwrite
  257. viewController.items = items
  258. viewController.titleCurrentFolder = metadataTouch!.fileNameView
  259. viewController.serverUrl = serverUrlPush
  260. self.navigationController?.pushViewController(viewController, animated: true)
  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.init(named: "switchList")?.image(color: NCBrandColor.shared.gray, size: 25), for: .normal)
  273. } else {
  274. header.buttonSwitch.setImage(UIImage.init(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 && metadata.ownerId != activeAccount.userId && activeAccount.account == metadata.account {
  301. let fileNameLocalPath = String(CCUtility.getDirectoryUserData()) + "/" + String(CCUtility.getStringUser(metadata.user, urlBase: metadata.urlBase)) + "-" + metadata.ownerId + ".png"
  302. NCOperationQueue.shared.downloadAvatar(user: metadata.ownerId, fileNameLocalPath: fileNameLocalPath, placeholder: NCBrandColor.cacheImages.shared, cell: cell)
  303. }
  304. }
  305. func collectionView(_ collectionView: UICollectionView, didEndDisplaying cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
  306. }
  307. func numberOfSections(in collectionView: UICollectionView) -> Int {
  308. return 1
  309. }
  310. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  311. let numberOfItems = dataSource.numberOfItems()
  312. emptyDataSet?.numberOfItemsInSection(numberOfItems, section:section)
  313. return numberOfItems
  314. }
  315. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  316. guard let metadata = dataSource.cellForItemAt(indexPath: indexPath) else {
  317. if layoutForView?.layout == NCGlobal.shared.layoutList {
  318. return collectionView.dequeueReusableCell(withReuseIdentifier: "listCell", for: indexPath) as! NCListCell
  319. } else {
  320. return collectionView.dequeueReusableCell(withReuseIdentifier: "gridCell", for: indexPath) as! NCGridCell
  321. }
  322. }
  323. var tableShare: tableShare?
  324. var isShare = false
  325. var isMounted = false
  326. isShare = metadata.permissions.contains(NCGlobal.shared.permissionShared) && !metadataFolder.permissions.contains(NCGlobal.shared.permissionShared)
  327. isMounted = metadata.permissions.contains(NCGlobal.shared.permissionMounted) && !metadataFolder.permissions.contains(NCGlobal.shared.permissionMounted)
  328. if dataSource.metadataShare[metadata.ocId] != nil {
  329. tableShare = dataSource.metadataShare[metadata.ocId]
  330. }
  331. // LAYOUT LIST
  332. if layoutForView?.layout == NCGlobal.shared.layoutList {
  333. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "listCell", for: indexPath) as! NCListCell
  334. cell.delegate = self
  335. cell.fileObjectId = metadata.ocId
  336. cell.fileUser = metadata.ownerId
  337. cell.labelTitle.text = metadata.fileNameView
  338. cell.labelTitle.textColor = NCBrandColor.shared.label
  339. cell.imageSelect.image = nil
  340. cell.imageStatus.image = nil
  341. cell.imageLocal.image = nil
  342. cell.imageFavorite.image = nil
  343. cell.imageShared.image = nil
  344. cell.imageMore.image = nil
  345. cell.imageItem.image = nil
  346. cell.imageItem.backgroundColor = nil
  347. cell.progressView.progress = 0.0
  348. if metadata.directory {
  349. if metadata.e2eEncrypted {
  350. cell.imageItem.image = NCBrandColor.cacheImages.folderEncrypted
  351. } else if isShare {
  352. cell.imageItem.image = NCBrandColor.cacheImages.folderSharedWithMe
  353. } else if (tableShare != nil && tableShare?.shareType != 3) {
  354. cell.imageItem.image = NCBrandColor.cacheImages.folderSharedWithMe
  355. } else if (tableShare != nil && tableShare?.shareType == 3) {
  356. cell.imageItem.image = NCBrandColor.cacheImages.folderPublic
  357. } else if metadata.mountType == "group" {
  358. cell.imageItem.image = NCBrandColor.cacheImages.folderGroup
  359. } else if isMounted {
  360. cell.imageItem.image = NCBrandColor.cacheImages.folderExternal
  361. } else if metadata.fileName == autoUploadFileName && metadata.serverUrl == autoUploadDirectory {
  362. cell.imageItem.image = NCBrandColor.cacheImages.folderAutomaticUpload
  363. } else {
  364. cell.imageItem.image = NCBrandColor.cacheImages.folder
  365. }
  366. cell.labelInfo.text = CCUtility.dateDiff(metadata.date as Date)
  367. let lockServerUrl = CCUtility.stringAppendServerUrl(metadata.serverUrl, addFileName: metadata.fileName)!
  368. let tableDirectory = NCManageDatabase.shared.getTableDirectory(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@", activeAccount.account, lockServerUrl))
  369. // Local image: offline
  370. if tableDirectory != nil && tableDirectory!.offline {
  371. cell.imageLocal.image = NCBrandColor.cacheImages.offlineFlag
  372. }
  373. } else {
  374. cell.labelInfo.text = CCUtility.dateDiff(metadata.date as Date) + " · " + CCUtility.transformedSize(metadata.size)
  375. // image local
  376. if dataSource.metadataOffLine.contains(metadata.ocId) {
  377. cell.imageLocal.image = NCBrandColor.cacheImages.offlineFlag
  378. } else if CCUtility.fileProviderStorageExists(metadata.ocId, fileNameView: metadata.fileNameView) {
  379. cell.imageLocal.image = NCBrandColor.cacheImages.local
  380. }
  381. }
  382. // image Favorite
  383. if metadata.favorite {
  384. cell.imageFavorite.image = NCBrandColor.cacheImages.favorite
  385. }
  386. // Share image
  387. if (isShare) {
  388. cell.imageShared.image = NCBrandColor.cacheImages.shared
  389. } else if (tableShare != nil && tableShare?.shareType == 3) {
  390. cell.imageShared.image = NCBrandColor.cacheImages.shareByLink
  391. } else if (tableShare != nil && tableShare?.shareType != 3) {
  392. cell.imageShared.image = NCBrandColor.cacheImages.shared
  393. } else {
  394. cell.imageShared.image = NCBrandColor.cacheImages.canShare
  395. }
  396. cell.imageSelect.isHidden = true
  397. cell.backgroundView = nil
  398. cell.hideButtonMore(true)
  399. cell.hideButtonShare(true)
  400. cell.selectMode(false)
  401. // Live Photo
  402. if metadata.livePhoto {
  403. cell.imageStatus.image = NCBrandColor.cacheImages.livePhoto
  404. }
  405. // Remove last separator
  406. if collectionView.numberOfItems(inSection: indexPath.section) == indexPath.row + 1 {
  407. cell.separator.isHidden = true
  408. } else {
  409. cell.separator.isHidden = false
  410. }
  411. return cell
  412. }
  413. // LAYOUT GRID
  414. if layoutForView?.layout == NCGlobal.shared.layoutGrid {
  415. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "gridCell", for: indexPath) as! NCGridCell
  416. cell.delegate = self
  417. cell.fileObjectId = metadata.ocId
  418. cell.fileUser = metadata.ownerId
  419. cell.labelTitle.text = metadata.fileNameView
  420. cell.labelTitle.textColor = NCBrandColor.shared.label
  421. cell.imageSelect.image = nil
  422. cell.imageStatus.image = nil
  423. cell.imageLocal.image = nil
  424. cell.imageFavorite.image = nil
  425. cell.imageItem.image = nil
  426. cell.imageItem.backgroundColor = nil
  427. cell.progressView.progress = 0.0
  428. if metadata.directory {
  429. if metadata.e2eEncrypted {
  430. cell.imageItem.image = NCBrandColor.cacheImages.folderEncrypted
  431. } else if isShare {
  432. cell.imageItem.image = NCBrandColor.cacheImages.folderSharedWithMe
  433. } else if (tableShare != nil && tableShare!.shareType != 3) {
  434. cell.imageItem.image = NCBrandColor.cacheImages.folderSharedWithMe
  435. } else if (tableShare != nil && tableShare!.shareType == 3) {
  436. cell.imageItem.image = NCBrandColor.cacheImages.folderPublic
  437. } else if metadata.mountType == "group" {
  438. cell.imageItem.image = NCBrandColor.cacheImages.folderGroup
  439. } else if isMounted {
  440. cell.imageItem.image = NCBrandColor.cacheImages.folderExternal
  441. } else if metadata.fileName == autoUploadFileName && metadata.serverUrl == autoUploadDirectory {
  442. cell.imageItem.image = NCBrandColor.cacheImages.folderAutomaticUpload
  443. } else {
  444. cell.imageItem.image = NCBrandColor.cacheImages.folder
  445. }
  446. let lockServerUrl = CCUtility.stringAppendServerUrl(metadata.serverUrl, addFileName: metadata.fileName)!
  447. let tableDirectory = NCManageDatabase.shared.getTableDirectory(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@", activeAccount.account, lockServerUrl))
  448. // Local image: offline
  449. if tableDirectory != nil && tableDirectory!.offline {
  450. cell.imageLocal.image = NCBrandColor.cacheImages.offlineFlag
  451. }
  452. } else {
  453. // image Local
  454. if dataSource.metadataOffLine.contains(metadata.ocId) {
  455. cell.imageLocal.image = NCBrandColor.cacheImages.offlineFlag
  456. } else if CCUtility.fileProviderStorageExists(metadata.ocId, fileNameView: metadata.fileNameView) {
  457. cell.imageLocal.image = NCBrandColor.cacheImages.local
  458. }
  459. }
  460. // image Favorite
  461. if metadata.favorite {
  462. cell.imageFavorite.image = NCBrandColor.cacheImages.favorite
  463. }
  464. cell.imageSelect.isHidden = true
  465. cell.backgroundView = nil
  466. cell.hideButtonMore(true)
  467. // Live Photo
  468. if metadata.livePhoto {
  469. cell.imageStatus.image = NCBrandColor.cacheImages.livePhoto
  470. }
  471. return cell
  472. }
  473. return collectionView.dequeueReusableCell(withReuseIdentifier: "gridCell", for: indexPath) as! NCGridCell
  474. }
  475. }
  476. extension NCSelect: UICollectionViewDelegateFlowLayout {
  477. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
  478. headerRichWorkspaceHeight = 0
  479. if let richWorkspaceText = richWorkspaceText {
  480. let trimmed = richWorkspaceText.trimmingCharacters(in: .whitespaces)
  481. if trimmed.count > 0 {
  482. headerRichWorkspaceHeight = UIScreen.main.bounds.size.height / 4
  483. }
  484. }
  485. return CGSize(width: collectionView.frame.width, height: headerHeight + headerRichWorkspaceHeight)
  486. }
  487. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize {
  488. return CGSize(width: collectionView.frame.width, height: footerHeight)
  489. }
  490. }
  491. // MARK: - NC API & Algorithm
  492. extension NCSelect {
  493. @objc func reloadDataSource() {
  494. loadDatasource(withLoadFolder: false)
  495. }
  496. @objc func loadDatasource(withLoadFolder: Bool) {
  497. var predicate: NSPredicate?
  498. layoutForView = NCUtility.shared.getLayoutForView(key: keyLayout, serverUrl: serverUrl)
  499. if includeDirectoryE2EEncryption {
  500. if includeImages {
  501. predicate = NSPredicate(format: "account == %@ AND serverUrl == %@ AND (directory == true OR classFile == 'image')", activeAccount.account, serverUrl)
  502. } else {
  503. predicate = NSPredicate(format: "account == %@ AND serverUrl == %@ AND directory == true", activeAccount.account, serverUrl)
  504. }
  505. } else {
  506. if includeImages {
  507. predicate = NSPredicate(format: "account == %@ AND serverUrl == %@ AND e2eEncrypted == false AND (directory == true OR classFile == 'image')", activeAccount.account, serverUrl)
  508. } else {
  509. predicate = NSPredicate(format: "account == %@ AND serverUrl == %@ AND e2eEncrypted == false AND directory == true", activeAccount.account, serverUrl)
  510. }
  511. }
  512. let metadatasSource = NCManageDatabase.shared.getMetadatas(predicate: predicate!)
  513. self.dataSource = NCDataSource.init(metadatasSource: metadatasSource, sort: layoutForView?.sort, ascending: layoutForView?.ascending, directoryOnTop: layoutForView?.directoryOnTop, favoriteOnTop: true, filterLivePhoto: true)
  514. if withLoadFolder {
  515. loadFolder()
  516. }
  517. let directory = NCManageDatabase.shared.getTableDirectory(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@", activeAccount.account,serverUrl))
  518. richWorkspaceText = directory?.richWorkspace
  519. collectionView.reloadData()
  520. }
  521. func createFolder(with fileName: String) {
  522. NCNetworking.shared.createFolder(fileName: fileName, serverUrl: serverUrl, account: activeAccount.account, urlBase: activeAccount.urlBase) { (errorCode, errorDescription) in
  523. if errorCode == 0 {
  524. self.loadDatasource(withLoadFolder: true)
  525. } else {
  526. NCContentPresenter.shared.messageNotification("_error_", description: errorDescription, delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, errorCode: errorCode)
  527. }
  528. }
  529. }
  530. func loadFolder() {
  531. networkInProgress = true
  532. collectionView.reloadData()
  533. NCNetworking.shared.readFolder(serverUrl: serverUrl, account: activeAccount.account) { (_, _, _, _, _, _, errorCode, errorDescription) in
  534. if errorCode != 0 {
  535. NCContentPresenter.shared.messageNotification("_error_", description: errorDescription, delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, errorCode: errorCode)
  536. }
  537. self.networkInProgress = false
  538. self.loadDatasource(withLoadFolder: false)
  539. }
  540. }
  541. }
  542. class NCSelectCommandView: UIView {
  543. @IBOutlet weak var separatorView: UIView!
  544. @IBOutlet weak var createFolderButton: UIButton?
  545. @IBOutlet weak var selectButton: UIButton?
  546. @IBOutlet weak var copyButton: UIButton?
  547. @IBOutlet weak var moveButton: UIButton?
  548. @IBOutlet weak var overwriteSwitch: UISwitch?
  549. @IBOutlet weak var overwriteLabel: UILabel?
  550. @IBOutlet weak var separatorHeightConstraint: NSLayoutConstraint!
  551. var selectView: NCSelect?
  552. private let gradient: CAGradientLayer = CAGradientLayer()
  553. override func awakeFromNib() {
  554. separatorHeightConstraint.constant = 0.5
  555. separatorView.backgroundColor = NCBrandColor.shared.separator
  556. overwriteLabel?.text = NSLocalizedString("_overwrite_", comment: "")
  557. selectButton?.layer.cornerRadius = 15
  558. selectButton?.layer.masksToBounds = true
  559. selectButton?.setTitle(NSLocalizedString("_select_", comment: ""), for: .normal)
  560. createFolderButton?.layer.cornerRadius = 15
  561. createFolderButton?.layer.masksToBounds = true
  562. createFolderButton?.setTitle(NSLocalizedString("_create_folder_", comment: ""), for: .normal)
  563. copyButton?.layer.cornerRadius = 15
  564. copyButton?.layer.masksToBounds = true
  565. copyButton?.setTitle(NSLocalizedString("_copy_", comment: ""), for: .normal)
  566. moveButton?.layer.cornerRadius = 15
  567. moveButton?.layer.masksToBounds = true
  568. moveButton?.setTitle(NSLocalizedString("_move_", comment: ""), for: .normal)
  569. }
  570. @IBAction func createFolderButtonPressed(_ sender: UIButton) {
  571. selectView?.createFolderButtonPressed(sender)
  572. }
  573. @IBAction func selectButtonPressed(_ sender: UIButton) {
  574. selectView?.selectButtonPressed(sender)
  575. }
  576. @IBAction func copyButtonPressed(_ sender: UIButton) {
  577. selectView?.copyButtonPressed(sender)
  578. }
  579. @IBAction func moveButtonPressed(_ sender: UIButton) {
  580. selectView?.moveButtonPressed(sender)
  581. }
  582. @IBAction func valueChangedSwitchOverwrite(_ sender: UISwitch) {
  583. selectView?.valueChangedSwitchOverwrite(sender)
  584. }
  585. }