NCSelect.swift 32 KB

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