NCSelect.swift 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704
  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 SwiftUI
  25. import NextcloudKit
  26. @objc protocol NCSelectDelegate {
  27. @objc func dismissSelect(serverUrl: String?, metadata: tableMetadata?, type: String, items: [Any], indexPath: [IndexPath], overwrite: Bool, copy: Bool, move: Bool)
  28. }
  29. class NCSelect: UIViewController, UIGestureRecognizerDelegate, UIAdaptivePresentationControllerDelegate, NCListCellDelegate, NCGridCellDelegate, NCSectionHeaderMenuDelegate, NCEmptyDataSetDelegate {
  30. @IBOutlet private var collectionView: UICollectionView!
  31. @IBOutlet private var buttonCancel: UIBarButtonItem!
  32. @IBOutlet private var bottomContraint: NSLayoutConstraint?
  33. private var selectCommandViewSelect: NCSelectCommandView?
  34. @objc enum selectType: Int {
  35. case select
  36. case selectCreateFolder
  37. case copyMove
  38. case nothing
  39. }
  40. // ------ external settings ------------------------------------
  41. @objc weak var delegate: NCSelectDelegate?
  42. @objc var typeOfCommandView: selectType = .select
  43. @objc var includeDirectoryE2EEncryption = false
  44. @objc var includeImages = false
  45. @objc var enableSelectFile = false
  46. @objc var type = ""
  47. @objc var items: [tableMetadata] = []
  48. @objc var selectIndexPath: [IndexPath] = []
  49. var titleCurrentFolder = NCBrandOptions.shared.brand
  50. var serverUrl = ""
  51. // -------------------------------------------------------------
  52. private var emptyDataSet: NCEmptyDataSet?
  53. private var serverUrlPush = ""
  54. private var metadataFolder = tableMetadata()
  55. private var networkInProgress = false
  56. private var overwrite = true
  57. private var dataSource = NCDataSource()
  58. internal var richWorkspaceText: String?
  59. internal var headerMenu: NCSectionHeaderMenu?
  60. private var autoUploadFileName = ""
  61. private var autoUploadDirectory = ""
  62. private var backgroundImageView = UIImageView()
  63. private var activeAccount: tableAccount!
  64. private let window = UIApplication.shared.connectedScenes.flatMap { ($0 as? UIWindowScene)?.windows ?? [] }.first { $0.isKeyWindow }
  65. // MARK: - View Life Cycle
  66. override func viewDidLoad() {
  67. super.viewDidLoad()
  68. self.navigationController?.navigationBar.prefersLargeTitles = true
  69. self.navigationController?.presentationController?.delegate = self
  70. view.backgroundColor = .systemBackground
  71. selectCommandViewSelect?.separatorView.backgroundColor = .separator
  72. activeAccount = NCManageDatabase.shared.getActiveAccount()
  73. // Cell
  74. collectionView.register(UINib(nibName: "NCListCell", bundle: nil), forCellWithReuseIdentifier: "listCell")
  75. collectionView.collectionViewLayout = NCListLayout()
  76. // Header
  77. collectionView.register(UINib(nibName: "NCSectionHeaderMenu", bundle: nil), forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "sectionHeaderMenu")
  78. // Footer
  79. collectionView.register(UINib(nibName: "NCSectionFooter", bundle: nil), forSupplementaryViewOfKind: UICollectionView.elementKindSectionFooter, withReuseIdentifier: "sectionFooter")
  80. collectionView.alwaysBounceVertical = true
  81. collectionView.backgroundColor = .systemBackground
  82. buttonCancel.title = NSLocalizedString("_cancel_", comment: "")
  83. bottomContraint?.constant = window?.rootViewController?.view.safeAreaInsets.bottom ?? 0
  84. // Empty
  85. emptyDataSet = NCEmptyDataSet(view: collectionView, offset: NCGlobal.shared.heightButtonsView, delegate: self)
  86. // Type of command view
  87. if typeOfCommandView == .select || typeOfCommandView == .selectCreateFolder {
  88. if typeOfCommandView == .select {
  89. selectCommandViewSelect = Bundle.main.loadNibNamed("NCSelectCommandViewSelect", owner: self, options: nil)?.first as? NCSelectCommandView
  90. } else {
  91. selectCommandViewSelect = Bundle.main.loadNibNamed("NCSelectCommandViewSelect+CreateFolder", owner: self, options: nil)?.first as? NCSelectCommandView
  92. }
  93. self.view.addSubview(selectCommandViewSelect!)
  94. selectCommandViewSelect?.selectView = self
  95. selectCommandViewSelect?.translatesAutoresizingMaskIntoConstraints = false
  96. selectCommandViewSelect?.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: 0).isActive = true
  97. selectCommandViewSelect?.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 0).isActive = true
  98. selectCommandViewSelect?.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: 0).isActive = true
  99. selectCommandViewSelect?.heightAnchor.constraint(equalToConstant: 80).isActive = true
  100. bottomContraint?.constant = 80
  101. }
  102. if typeOfCommandView == .copyMove {
  103. selectCommandViewSelect = Bundle.main.loadNibNamed("NCSelectCommandViewCopyMove", owner: self, options: nil)?.first as? NCSelectCommandView
  104. self.view.addSubview(selectCommandViewSelect!)
  105. selectCommandViewSelect?.selectView = self
  106. selectCommandViewSelect?.translatesAutoresizingMaskIntoConstraints = false
  107. if items.contains(where: { $0.lock }) {
  108. selectCommandViewSelect?.moveButton?.isEnabled = false
  109. selectCommandViewSelect?.moveButton?.titleLabel?.isEnabled = false
  110. }
  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. bottomContraint?.constant = 150
  116. }
  117. NotificationCenter.default.addObserver(self, selector: #selector(reloadDataSource), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterReloadDataSource), object: nil)
  118. NotificationCenter.default.addObserver(self, selector: #selector(createFolder(_:)), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterCreateFolder), object: nil)
  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(urlBase: activeAccount.urlBase, userId: activeAccount.userId)
  126. }
  127. // get auto upload folder
  128. autoUploadFileName = NCManageDatabase.shared.getAccountAutoUploadFileName()
  129. autoUploadDirectory = NCManageDatabase.shared.getAccountAutoUploadDirectory(urlBase: activeAccount.urlBase, userId: activeAccount.userId, account: activeAccount.account)
  130. loadDatasource(withLoadFolder: true)
  131. }
  132. override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
  133. super.viewWillTransition(to: size, with: coordinator)
  134. coordinator.animate(alongsideTransition: nil) { _ in
  135. self.collectionView?.collectionViewLayout.invalidateLayout()
  136. }
  137. }
  138. func presentationControllerDidDismiss( _ presentationController: UIPresentationController) {
  139. // Dismission
  140. }
  141. // MARK: - NotificationCenter
  142. @objc func createFolder(_ notification: NSNotification) {
  143. guard let userInfo = notification.userInfo as NSDictionary?,
  144. let ocId = userInfo["ocId"] as? String,
  145. let serverUrl = userInfo["serverUrl"] as? String,
  146. serverUrl == self.serverUrl,
  147. let metadata = NCManageDatabase.shared.getMetadataFromOcId(ocId)
  148. else { return }
  149. pushMetadata(metadata)
  150. }
  151. // MARK: - Empty
  152. func emptyDataSetView(_ view: NCEmptyView) {
  153. if networkInProgress {
  154. view.emptyImage.image = UIImage(named: "networkInProgress")?.image(color: .gray, size: UIScreen.main.bounds.width)
  155. view.emptyTitle.text = NSLocalizedString("_request_in_progress_", comment: "")
  156. view.emptyDescription.text = ""
  157. } else {
  158. view.emptyImage.image = UIImage(named: "folder")?.image(color: NCBrandColor.shared.brandElement, size: UIScreen.main.bounds.width)
  159. if includeImages {
  160. view.emptyTitle.text = NSLocalizedString("_files_no_files_", comment: "")
  161. } else {
  162. view.emptyTitle.text = NSLocalizedString("_files_no_folders_", comment: "")
  163. }
  164. view.emptyDescription.text = ""
  165. }
  166. }
  167. // MARK: ACTION
  168. @IBAction func actionCancel(_ sender: UIBarButtonItem) {
  169. self.dismiss(animated: true, completion: nil)
  170. }
  171. func selectButtonPressed(_ sender: UIButton) {
  172. delegate?.dismissSelect(serverUrl: serverUrl, metadata: metadataFolder, type: type, items: items, indexPath: selectIndexPath, overwrite: overwrite, copy: false, move: false)
  173. self.dismiss(animated: true, completion: nil)
  174. }
  175. func copyButtonPressed(_ sender: UIButton) {
  176. delegate?.dismissSelect(serverUrl: serverUrl, metadata: metadataFolder, type: type, items: items, indexPath: selectIndexPath, overwrite: overwrite, copy: true, move: false)
  177. self.dismiss(animated: true, completion: nil)
  178. }
  179. func moveButtonPressed(_ sender: UIButton) {
  180. delegate?.dismissSelect(serverUrl: serverUrl, metadata: metadataFolder, type: type, items: items, indexPath: selectIndexPath, overwrite: overwrite, copy: false, move: true)
  181. self.dismiss(animated: true, completion: nil)
  182. }
  183. func createFolderButtonPressed(_ sender: UIButton) {
  184. let alertController = UIAlertController.createFolder(serverUrl: serverUrl, urlBase: activeAccount)
  185. self.present(alertController, animated: true, completion: nil)
  186. }
  187. @IBAction func valueChangedSwitchOverwrite(_ sender: UISwitch) {
  188. overwrite = sender.isOn
  189. }
  190. // MARK: - Push metadata
  191. func pushMetadata(_ metadata: tableMetadata) {
  192. guard let serverUrlPush = CCUtility.stringAppendServerUrl(metadata.serverUrl, addFileName: metadata.fileName) else { return }
  193. guard let viewController = UIStoryboard(name: "NCSelect", bundle: nil).instantiateViewController(withIdentifier: "NCSelect.storyboard") as? NCSelect else { return }
  194. self.serverUrlPush = serverUrlPush
  195. viewController.delegate = delegate
  196. viewController.typeOfCommandView = typeOfCommandView
  197. viewController.includeDirectoryE2EEncryption = includeDirectoryE2EEncryption
  198. viewController.includeImages = includeImages
  199. viewController.enableSelectFile = enableSelectFile
  200. viewController.type = type
  201. viewController.overwrite = overwrite
  202. viewController.items = items
  203. viewController.selectIndexPath = selectIndexPath
  204. viewController.titleCurrentFolder = metadata.fileNameView
  205. viewController.serverUrl = serverUrlPush
  206. self.navigationController?.pushViewController(viewController, animated: true)
  207. }
  208. }
  209. // MARK: - Collection View
  210. extension NCSelect: UICollectionViewDelegate {
  211. func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  212. guard let metadata = dataSource.cellForItemAt(indexPath: indexPath) else { return }
  213. if metadata.directory {
  214. pushMetadata(metadata)
  215. } else {
  216. delegate?.dismissSelect(serverUrl: serverUrl, metadata: metadata, type: type, items: items, indexPath: selectIndexPath, overwrite: overwrite, copy: false, move: false)
  217. self.dismiss(animated: true, completion: nil)
  218. }
  219. }
  220. }
  221. extension NCSelect: UICollectionViewDataSource {
  222. func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
  223. guard let metadata = dataSource.cellForItemAt(indexPath: indexPath) else { return }
  224. // Thumbnail
  225. if !metadata.directory {
  226. if FileManager().fileExists(atPath: CCUtility.getDirectoryProviderStorageIconOcId(metadata.ocId, etag: metadata.etag)) {
  227. (cell as! NCCellProtocol).filePreviewImageView?.image = UIImage(contentsOfFile: CCUtility.getDirectoryProviderStorageIconOcId(metadata.ocId, etag: metadata.etag))
  228. } else {
  229. NCOperationQueue.shared.downloadThumbnail(metadata: metadata, placeholder: true, cell: cell, view: collectionView)
  230. }
  231. }
  232. // Avatar
  233. if metadata.ownerId.count > 0,
  234. metadata.ownerId != activeAccount.userId,
  235. activeAccount.account == metadata.account,
  236. let cell = cell as? NCCellProtocol {
  237. let fileName = metadata.userBaseUrl + "-" + metadata.ownerId + ".png"
  238. NCOperationQueue.shared.downloadAvatar(user: metadata.ownerId, dispalyName: metadata.ownerDisplayName, fileName: fileName, cell: cell, view: collectionView, cellImageView: cell.fileAvatarImageView)
  239. }
  240. }
  241. func collectionView(_ collectionView: UICollectionView, didEndDisplaying cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
  242. }
  243. func numberOfSections(in collectionView: UICollectionView) -> Int {
  244. return dataSource.numberOfSections()
  245. }
  246. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  247. let numberOfItems = dataSource.numberOfItemsInSection(section)
  248. emptyDataSet?.numberOfItemsInSection(numberOfItems, section: section)
  249. return numberOfItems
  250. }
  251. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  252. guard let metadata = dataSource.cellForItemAt(indexPath: indexPath) else {
  253. return collectionView.dequeueReusableCell(withReuseIdentifier: "listCell", for: indexPath) as! NCListCell
  254. }
  255. var isShare = false
  256. var isMounted = false
  257. isShare = metadata.permissions.contains(NCGlobal.shared.permissionShared) && !metadataFolder.permissions.contains(NCGlobal.shared.permissionShared)
  258. isMounted = metadata.permissions.contains(NCGlobal.shared.permissionMounted) && !metadataFolder.permissions.contains(NCGlobal.shared.permissionMounted)
  259. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "listCell", for: indexPath) as! NCListCell
  260. cell.delegate = self
  261. cell.fileObjectId = metadata.ocId
  262. cell.fileUser = metadata.ownerId
  263. cell.labelTitle.text = metadata.fileNameView
  264. cell.labelTitle.textColor = .label
  265. cell.imageSelect.image = nil
  266. cell.imageStatus.image = nil
  267. cell.imageLocal.image = nil
  268. cell.imageFavorite.image = nil
  269. cell.imageShared.image = nil
  270. cell.imageMore.image = nil
  271. cell.imageItem.image = nil
  272. cell.imageItem.backgroundColor = nil
  273. cell.progressView.progress = 0.0
  274. if metadata.directory {
  275. if metadata.e2eEncrypted {
  276. cell.imageItem.image = NCBrandColor.cacheImages.folderEncrypted
  277. } else if isShare {
  278. cell.imageItem.image = NCBrandColor.cacheImages.folderSharedWithMe
  279. } else if !metadata.shareType.isEmpty {
  280. metadata.shareType.contains(3) ?
  281. (cell.imageItem.image = NCBrandColor.cacheImages.folderPublic) :
  282. (cell.imageItem.image = NCBrandColor.cacheImages.folderSharedWithMe)
  283. } else if metadata.mountType == "group" {
  284. cell.imageItem.image = NCBrandColor.cacheImages.folderGroup
  285. } else if isMounted {
  286. cell.imageItem.image = NCBrandColor.cacheImages.folderExternal
  287. } else if metadata.fileName == autoUploadFileName && metadata.serverUrl == autoUploadDirectory {
  288. cell.imageItem.image = NCBrandColor.cacheImages.folderAutomaticUpload
  289. } else {
  290. cell.imageItem.image = NCBrandColor.cacheImages.folder
  291. }
  292. cell.imageItem.image = cell.imageItem.image?.colorizeFolder(metadata: metadata)
  293. cell.labelInfo.text = CCUtility.dateDiff(metadata.date as Date)
  294. } else {
  295. cell.labelInfo.text = CCUtility.dateDiff(metadata.date as Date) + " · " + CCUtility.transformedSize(metadata.size)
  296. // image local
  297. if NCManageDatabase.shared.getTableLocalFile(ocId: metadata.ocId) != nil {
  298. cell.imageLocal.image = NCBrandColor.cacheImages.offlineFlag
  299. } else if CCUtility.fileProviderStorageExists(metadata) {
  300. cell.imageLocal.image = NCBrandColor.cacheImages.local
  301. }
  302. }
  303. // image Favorite
  304. if metadata.favorite {
  305. cell.imageFavorite.image = NCBrandColor.cacheImages.favorite
  306. }
  307. cell.imageSelect.isHidden = true
  308. cell.backgroundView = nil
  309. cell.hideButtonMore(true)
  310. cell.hideButtonShare(true)
  311. cell.selectMode(false)
  312. // Live Photo
  313. if metadata.livePhoto {
  314. cell.imageStatus.image = NCBrandColor.cacheImages.livePhoto
  315. }
  316. // Remove last separator
  317. if collectionView.numberOfItems(inSection: indexPath.section) == indexPath.row + 1 {
  318. cell.separator.isHidden = true
  319. } else {
  320. cell.separator.isHidden = false
  321. }
  322. // Add TAGS
  323. cell.setTags(tags: Array(metadata.tags))
  324. return cell
  325. }
  326. func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
  327. if kind == UICollectionView.elementKindSectionHeader {
  328. let header = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "sectionHeaderMenu", for: indexPath) as! NCSectionHeaderMenu
  329. let (_, heightHeaderRichWorkspace, _) = getHeaderHeight(section: indexPath.section)
  330. self.headerMenu = header
  331. header.delegate = self
  332. header.setButtonsView(height: 0)
  333. header.setRichWorkspaceHeight(heightHeaderRichWorkspace)
  334. header.setRichWorkspaceText(richWorkspaceText)
  335. header.setViewTransfer(isHidden: true)
  336. return header
  337. } else {
  338. let footer = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "sectionFooter", for: indexPath) as! NCSectionFooter
  339. let sections = dataSource.numberOfSections()
  340. let section = indexPath.section
  341. footer.setTitleLabel("")
  342. footer.separatorIsHidden(true)
  343. if sections == 1 || section == sections - 1 {
  344. let info = dataSource.getFooterInformationAllMetadatas()
  345. footer.setTitleLabel(directories: info.directories, files: info.files, size: info.size)
  346. } else {
  347. footer.separatorIsHidden(false)
  348. }
  349. return footer
  350. }
  351. }
  352. }
  353. extension NCSelect: UICollectionViewDelegateFlowLayout {
  354. func getHeaderHeight(section: Int) -> (heightHeaderCommands: CGFloat, heightHeaderRichWorkspace: CGFloat, heightHeaderSection: CGFloat) {
  355. var headerRichWorkspace: CGFloat = 0
  356. if let richWorkspaceText = richWorkspaceText {
  357. let trimmed = richWorkspaceText.trimmingCharacters(in: .whitespaces)
  358. if trimmed.count > 0 {
  359. headerRichWorkspace = UIScreen.main.bounds.size.height / 6
  360. }
  361. }
  362. return (0, headerRichWorkspace, 0)
  363. }
  364. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
  365. let (heightHeaderCommands, heightHeaderRichWorkspace, heightHeaderSection) = getHeaderHeight(section: section)
  366. let heightHeader = heightHeaderCommands + heightHeaderRichWorkspace + heightHeaderSection
  367. return CGSize(width: collectionView.frame.width, height: heightHeader)
  368. }
  369. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize {
  370. let sections = dataSource.numberOfSections()
  371. if section == sections - 1 {
  372. return CGSize(width: collectionView.frame.width, height: NCGlobal.shared.endHeightFooter)
  373. } else {
  374. return CGSize(width: collectionView.frame.width, height: NCGlobal.shared.heightFooter)
  375. }
  376. }
  377. }
  378. // MARK: -
  379. extension NCSelect {
  380. @objc func reloadDataSource() {
  381. loadDatasource(withLoadFolder: false)
  382. }
  383. @objc func loadDatasource(withLoadFolder: Bool) {
  384. var predicate: NSPredicate?
  385. if includeDirectoryE2EEncryption {
  386. if includeImages {
  387. predicate = NSPredicate(format: "account == %@ AND serverUrl == %@ AND (directory == true OR classFile == 'image')", activeAccount.account, serverUrl)
  388. } else {
  389. predicate = NSPredicate(format: "account == %@ AND serverUrl == %@ AND directory == true", activeAccount.account, serverUrl)
  390. }
  391. } else {
  392. if includeImages {
  393. predicate = NSPredicate(format: "account == %@ AND serverUrl == %@ AND e2eEncrypted == false AND (directory == true OR classFile == 'image')", activeAccount.account, serverUrl)
  394. } else if enableSelectFile {
  395. predicate = NSPredicate(format: "account == %@ AND serverUrl == %@ AND e2eEncrypted == false", activeAccount.account, serverUrl)
  396. } else {
  397. predicate = NSPredicate(format: "account == %@ AND serverUrl == %@ AND e2eEncrypted == false AND directory == true", activeAccount.account, serverUrl)
  398. }
  399. }
  400. let metadatas = NCManageDatabase.shared.getMetadatas(predicate: predicate!)
  401. self.dataSource = NCDataSource(metadatas: metadatas,
  402. account: activeAccount.account,
  403. sort: "fileName",
  404. ascending: true,
  405. directoryOnTop: true,
  406. favoriteOnTop: true,
  407. filterLivePhoto: true,
  408. groupByField: "none")
  409. if withLoadFolder {
  410. loadFolder()
  411. }
  412. let directory = NCManageDatabase.shared.getTableDirectory(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@", activeAccount.account, serverUrl))
  413. richWorkspaceText = directory?.richWorkspace
  414. DispatchQueue.main.async {
  415. self.collectionView.reloadData()
  416. }
  417. }
  418. func loadFolder() {
  419. networkInProgress = true
  420. collectionView.reloadData()
  421. NCNetworking.shared.readFolder(serverUrl: serverUrl, account: activeAccount.account) { _, _, _, _, _, _, error in
  422. if error != .success {
  423. NCContentPresenter.shared.showError(error: error)
  424. }
  425. self.networkInProgress = false
  426. self.loadDatasource(withLoadFolder: false)
  427. }
  428. }
  429. }
  430. // MARK: -
  431. class NCSelectCommandView: UIView {
  432. @IBOutlet weak var separatorView: UIView!
  433. @IBOutlet weak var createFolderButton: UIButton?
  434. @IBOutlet weak var selectButton: UIButton?
  435. @IBOutlet weak var copyButton: UIButton?
  436. @IBOutlet weak var moveButton: UIButton?
  437. @IBOutlet weak var overwriteSwitch: UISwitch?
  438. @IBOutlet weak var overwriteLabel: UILabel?
  439. @IBOutlet weak var separatorHeightConstraint: NSLayoutConstraint!
  440. var selectView: NCSelect?
  441. private let gradient: CAGradientLayer = CAGradientLayer()
  442. override func awakeFromNib() {
  443. separatorHeightConstraint.constant = 0.5
  444. separatorView.backgroundColor = .separator
  445. overwriteSwitch?.onTintColor = NCBrandColor.shared.brand
  446. overwriteLabel?.text = NSLocalizedString("_overwrite_", comment: "")
  447. selectButton?.layer.cornerRadius = 15
  448. selectButton?.layer.masksToBounds = true
  449. selectButton?.setTitle(NSLocalizedString("_select_", comment: ""), for: .normal)
  450. selectButton?.backgroundColor = NCBrandColor.shared.brand
  451. selectButton?.setTitleColor(UIColor(white: 1, alpha: 0.3), for: .highlighted)
  452. selectButton?.setTitleColor(NCBrandColor.shared.brandText, for: .normal)
  453. createFolderButton?.layer.cornerRadius = 15
  454. createFolderButton?.layer.masksToBounds = true
  455. createFolderButton?.setTitle(NSLocalizedString("_create_folder_", comment: ""), for: .normal)
  456. createFolderButton?.backgroundColor = NCBrandColor.shared.brand
  457. createFolderButton?.setTitleColor(UIColor(white: 1, alpha: 0.3), for: .highlighted)
  458. createFolderButton?.setTitleColor(NCBrandColor.shared.brandText, for: .normal)
  459. copyButton?.layer.cornerRadius = 15
  460. copyButton?.layer.masksToBounds = true
  461. copyButton?.setTitle(NSLocalizedString("_copy_", comment: ""), for: .normal)
  462. copyButton?.backgroundColor = NCBrandColor.shared.brand
  463. copyButton?.setTitleColor(UIColor(white: 1, alpha: 0.3), for: .highlighted)
  464. copyButton?.setTitleColor(NCBrandColor.shared.brandText, for: .normal)
  465. moveButton?.layer.cornerRadius = 15
  466. moveButton?.layer.masksToBounds = true
  467. moveButton?.setTitle(NSLocalizedString("_move_", comment: ""), for: .normal)
  468. moveButton?.backgroundColor = NCBrandColor.shared.brand
  469. moveButton?.setTitleColor(UIColor(white: 1, alpha: 0.3), for: .highlighted)
  470. moveButton?.setTitleColor(NCBrandColor.shared.brandText, for: .normal)
  471. }
  472. @IBAction func createFolderButtonPressed(_ sender: UIButton) {
  473. selectView?.createFolderButtonPressed(sender)
  474. }
  475. @IBAction func selectButtonPressed(_ sender: UIButton) {
  476. selectView?.selectButtonPressed(sender)
  477. }
  478. @IBAction func copyButtonPressed(_ sender: UIButton) {
  479. selectView?.copyButtonPressed(sender)
  480. }
  481. @IBAction func moveButtonPressed(_ sender: UIButton) {
  482. selectView?.moveButtonPressed(sender)
  483. }
  484. @IBAction func valueChangedSwitchOverwrite(_ sender: UISwitch) {
  485. selectView?.valueChangedSwitchOverwrite(sender)
  486. }
  487. }
  488. // MARK: - UIViewControllerRepresentable
  489. struct NCSelectViewControllerRepresentable: UIViewControllerRepresentable {
  490. typealias UIViewControllerType = UINavigationController
  491. var delegate: NCSelectDelegate
  492. func makeUIViewController(context: Context) -> UINavigationController {
  493. let storyboard = UIStoryboard(name: "NCSelect", bundle: nil)
  494. let navigationController = storyboard.instantiateInitialViewController() as? UINavigationController
  495. let viewController = navigationController?.topViewController as? NCSelect
  496. viewController?.delegate = delegate
  497. viewController?.typeOfCommandView = .selectCreateFolder
  498. viewController?.includeDirectoryE2EEncryption = true
  499. return navigationController!
  500. }
  501. func updateUIViewController(_ uiViewController: UINavigationController, context: Context) { }
  502. }
  503. struct SelectView: UIViewControllerRepresentable {
  504. typealias UIViewControllerType = UINavigationController
  505. @Binding var serverUrl: String
  506. class Coordinator: NSObject, NCSelectDelegate {
  507. var parent: SelectView
  508. init(_ parent: SelectView) {
  509. self.parent = parent
  510. }
  511. func dismissSelect(serverUrl: String?, metadata: tableMetadata?, type: String, items: [Any], indexPath: [IndexPath], overwrite: Bool, copy: Bool, move: Bool) {
  512. if let serverUrl = serverUrl {
  513. self.parent.serverUrl = serverUrl
  514. }
  515. }
  516. }
  517. func makeUIViewController(context: Context) -> UINavigationController {
  518. let storyboard = UIStoryboard(name: "NCSelect", bundle: nil)
  519. let navigationController = storyboard.instantiateInitialViewController() as? UINavigationController
  520. let viewController = navigationController?.topViewController as? NCSelect
  521. viewController?.delegate = context.coordinator
  522. viewController?.typeOfCommandView = .selectCreateFolder
  523. viewController?.includeDirectoryE2EEncryption = true
  524. return navigationController!
  525. }
  526. func updateUIViewController(_ uiViewController: UINavigationController, context: Context) { }
  527. func makeCoordinator() -> Coordinator {
  528. Coordinator(self)
  529. }
  530. }