NCSelect.swift 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644
  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. protocol NCSelectDelegate: AnyObject {
  27. func dismissSelect(serverUrl: String?, metadata: tableMetadata?, type: String, items: [Any], overwrite: Bool, copy: Bool, move: Bool, session: NCSession.Session)
  28. }
  29. class NCSelect: UIViewController, UIGestureRecognizerDelegate, UIAdaptivePresentationControllerDelegate, NCListCellDelegate, NCSectionFirstHeaderDelegate {
  30. @IBOutlet private var collectionView: UICollectionView!
  31. @IBOutlet private var buttonCancel: UIBarButtonItem!
  32. @IBOutlet private var bottomContraint: NSLayoutConstraint?
  33. private var selectCommandViewSelect: NCSelectCommandView?
  34. let utilityFileSystem = NCUtilityFileSystem()
  35. let utility = NCUtility()
  36. let database = NCManageDatabase.shared
  37. enum selectType: Int {
  38. case select
  39. case selectCreateFolder
  40. case copyMove
  41. case nothing
  42. }
  43. // ------ external settings ------------------------------------
  44. var delegate: NCSelectDelegate?
  45. var typeOfCommandView: selectType = .select
  46. var includeDirectoryE2EEncryption = false
  47. var includeImages = false
  48. var enableSelectFile = false
  49. var type = ""
  50. var items: [tableMetadata] = []
  51. var titleCurrentFolder = NCBrandOptions.shared.brand
  52. var serverUrl = ""
  53. var session: NCSession.Session!
  54. // -------------------------------------------------------------
  55. private var dataSourceTask: URLSessionTask?
  56. private var serverUrlPush = ""
  57. private var metadataFolder = tableMetadata()
  58. private var overwrite = true
  59. private var dataSource = NCCollectionViewDataSource()
  60. private var autoUploadFileName = ""
  61. private var autoUploadDirectory = ""
  62. private var backgroundImageView = UIImageView()
  63. // MARK: - View Life Cycle
  64. override func viewDidLoad() {
  65. super.viewDidLoad()
  66. navigationController?.setNavigationBarAppearance()
  67. navigationController?.navigationBar.prefersLargeTitles = true
  68. view.backgroundColor = .systemBackground
  69. collectionView.backgroundColor = .systemBackground
  70. selectCommandViewSelect?.separatorView.backgroundColor = .separator
  71. // Cell
  72. collectionView.register(UINib(nibName: "NCListCell", bundle: nil), forCellWithReuseIdentifier: "listCell")
  73. collectionView.collectionViewLayout = NCListLayout()
  74. // Header
  75. collectionView.register(UINib(nibName: "NCSectionFirstHeaderEmptyData", bundle: nil), forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "sectionFirstHeaderEmptyData")
  76. collectionView.register(UINib(nibName: "NCSectionFirstHeader", bundle: nil), forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "sectionFirstHeader")
  77. // Footer
  78. collectionView.register(UINib(nibName: "NCSectionFooter", bundle: nil), forSupplementaryViewOfKind: UICollectionView.elementKindSectionFooter, withReuseIdentifier: "sectionFooter")
  79. collectionView.alwaysBounceVertical = true
  80. collectionView.backgroundColor = .systemBackground
  81. buttonCancel.title = NSLocalizedString("_cancel_", comment: "")
  82. bottomContraint?.constant = UIApplication.shared.firstWindow?.rootViewController?.view.safeAreaInsets.bottom ?? 0
  83. // Type of command view
  84. if typeOfCommandView == .select || typeOfCommandView == .selectCreateFolder {
  85. if typeOfCommandView == .select {
  86. selectCommandViewSelect = Bundle.main.loadNibNamed("NCSelectCommandViewSelect", owner: self, options: nil)?.first as? NCSelectCommandView
  87. } else {
  88. selectCommandViewSelect = Bundle.main.loadNibNamed("NCSelectCommandViewSelect+CreateFolder", owner: self, options: nil)?.first as? NCSelectCommandView
  89. }
  90. self.view.addSubview(selectCommandViewSelect!)
  91. selectCommandViewSelect?.setColor(account: session.account)
  92. selectCommandViewSelect?.selectView = self
  93. selectCommandViewSelect?.translatesAutoresizingMaskIntoConstraints = false
  94. selectCommandViewSelect?.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: 0).isActive = true
  95. selectCommandViewSelect?.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 0).isActive = true
  96. selectCommandViewSelect?.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: 0).isActive = true
  97. selectCommandViewSelect?.heightAnchor.constraint(equalToConstant: 80).isActive = true
  98. bottomContraint?.constant = 80
  99. }
  100. if typeOfCommandView == .copyMove {
  101. selectCommandViewSelect = Bundle.main.loadNibNamed("NCSelectCommandViewCopyMove", owner: self, options: nil)?.first as? NCSelectCommandView
  102. self.view.addSubview(selectCommandViewSelect!)
  103. selectCommandViewSelect?.setColor(account: session.account)
  104. selectCommandViewSelect?.selectView = self
  105. selectCommandViewSelect?.translatesAutoresizingMaskIntoConstraints = false
  106. if items.contains(where: { $0.lock }) {
  107. selectCommandViewSelect?.moveButton?.isEnabled = false
  108. selectCommandViewSelect?.moveButton?.titleLabel?.isEnabled = false
  109. }
  110. selectCommandViewSelect?.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: 0).isActive = true
  111. selectCommandViewSelect?.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 0).isActive = true
  112. selectCommandViewSelect?.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: 0).isActive = true
  113. selectCommandViewSelect?.heightAnchor.constraint(equalToConstant: 150).isActive = true
  114. bottomContraint?.constant = 150
  115. }
  116. NotificationCenter.default.addObserver(self, selector: #selector(createFolder(_:)), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterCreateFolder), object: nil)
  117. }
  118. override func viewDidAppear(_ animated: Bool) {
  119. super.viewDidAppear(animated)
  120. let folderPath = utilityFileSystem.getFileNamePath("", serverUrl: serverUrl, session: session)
  121. if serverUrl.isEmpty || !FileNameValidator.shared.checkFolderPath(folderPath, account: session.account) {
  122. serverUrl = utilityFileSystem.getHomeServer(session: session)
  123. titleCurrentFolder = NCBrandOptions.shared.brand
  124. }
  125. autoUploadFileName = self.database.getAccountAutoUploadFileName()
  126. autoUploadDirectory = self.database.getAccountAutoUploadDirectory(session: session)
  127. self.navigationItem.title = titleCurrentFolder
  128. reloadDataSource()
  129. }
  130. override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
  131. super.viewWillTransition(to: size, with: coordinator)
  132. coordinator.animate(alongsideTransition: { _ in
  133. let animator = UIViewPropertyAnimator(duration: 0.3, curve: .easeInOut) {
  134. self.collectionView?.collectionViewLayout.invalidateLayout()
  135. }
  136. animator.startAnimation()
  137. })
  138. }
  139. func presentationControllerDidDismiss( _ presentationController: UIPresentationController) {
  140. // Dismission
  141. }
  142. // MARK: - NotificationCenter
  143. @objc func createFolder(_ notification: NSNotification) {
  144. guard let userInfo = notification.userInfo as NSDictionary?,
  145. let ocId = userInfo["ocId"] as? String,
  146. let serverUrl = userInfo["serverUrl"] as? String,
  147. let withPush = userInfo["withPush"] as? Bool,
  148. serverUrl == self.serverUrl,
  149. let metadata = self.database.getMetadataFromOcId(ocId)
  150. else { return }
  151. if withPush {
  152. pushMetadata(metadata)
  153. }
  154. }
  155. // MARK: ACTION
  156. @IBAction func actionCancel(_ sender: UIBarButtonItem) {
  157. self.dismiss(animated: true, completion: nil)
  158. }
  159. func selectButtonPressed(_ sender: UIButton) {
  160. delegate?.dismissSelect(serverUrl: serverUrl, metadata: metadataFolder, type: type, items: items, overwrite: overwrite, copy: false, move: false, session: session)
  161. self.dismiss(animated: true, completion: nil)
  162. }
  163. func copyButtonPressed(_ sender: UIButton) {
  164. delegate?.dismissSelect(serverUrl: serverUrl, metadata: metadataFolder, type: type, items: items, overwrite: overwrite, copy: true, move: false, session: session)
  165. self.dismiss(animated: true, completion: nil)
  166. }
  167. func moveButtonPressed(_ sender: UIButton) {
  168. delegate?.dismissSelect(serverUrl: serverUrl, metadata: metadataFolder, type: type, items: items, overwrite: overwrite, copy: false, move: true, session: session)
  169. self.dismiss(animated: true, completion: nil)
  170. }
  171. func createFolderButtonPressed(_ sender: UIButton) {
  172. let alertController = UIAlertController.createFolder(serverUrl: serverUrl, session: session)
  173. self.present(alertController, animated: true, completion: nil)
  174. }
  175. @IBAction func valueChangedSwitchOverwrite(_ sender: UISwitch) {
  176. overwrite = sender.isOn
  177. }
  178. func tapShareListItem(with ocId: String, ocIdTransfer: String, sender: Any) { }
  179. func tapMoreListItem(with ocId: String, ocIdTransfer: String, image: UIImage?, sender: Any) { }
  180. func longPressListItem(with odId: String, ocIdTransfer: String, gestureRecognizer: UILongPressGestureRecognizer) { }
  181. func tapRichWorkspace(_ sender: Any) { }
  182. // MARK: - Push metadata
  183. func pushMetadata(_ metadata: tableMetadata) {
  184. let serverUrlPush = utilityFileSystem.stringAppendServerUrl(metadata.serverUrl, addFileName: metadata.fileName)
  185. guard let viewController = UIStoryboard(name: "NCSelect", bundle: nil).instantiateViewController(withIdentifier: "NCSelect.storyboard") as? NCSelect else { return }
  186. self.serverUrlPush = serverUrlPush
  187. viewController.delegate = delegate
  188. viewController.typeOfCommandView = typeOfCommandView
  189. viewController.includeDirectoryE2EEncryption = includeDirectoryE2EEncryption
  190. viewController.includeImages = includeImages
  191. viewController.enableSelectFile = enableSelectFile
  192. viewController.type = type
  193. viewController.overwrite = overwrite
  194. viewController.items = items
  195. viewController.titleCurrentFolder = metadata.fileNameView
  196. viewController.serverUrl = serverUrlPush
  197. viewController.session = session
  198. if let fileNameError = FileNameValidator.shared.checkFileName(metadata.fileNameView, account: session.account) {
  199. present(UIAlertController.warning(message: "\(fileNameError.errorDescription) \(NSLocalizedString("_please_rename_file_", comment: ""))"), animated: true)
  200. } else {
  201. navigationController?.pushViewController(viewController, animated: true)
  202. }
  203. }
  204. }
  205. // MARK: - Collection View
  206. extension NCSelect: UICollectionViewDelegate {
  207. func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  208. guard let metadata = self.dataSource.getMetadata(indexPath: indexPath) else { return }
  209. if metadata.directory {
  210. pushMetadata(metadata)
  211. } else {
  212. delegate?.dismissSelect(serverUrl: serverUrl, metadata: metadata, type: type, items: items, overwrite: overwrite, copy: false, move: false, session: session)
  213. self.dismiss(animated: true, completion: nil)
  214. }
  215. }
  216. }
  217. extension NCSelect: UICollectionViewDataSource {
  218. func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
  219. guard let metadata = self.dataSource.getMetadata(indexPath: indexPath) else { return }
  220. // Thumbnail
  221. if !metadata.directory {
  222. if let image = utility.getImage(ocId: metadata.ocId, etag: metadata.etag, ext: NCGlobal.shared.previewExt512) {
  223. (cell as? NCCellProtocol)?.filePreviewImageView?.image = image
  224. } else {
  225. if metadata.iconName.isEmpty {
  226. (cell as? NCCellProtocol)?.filePreviewImageView?.image = NCImageCache.shared.getImageFile()
  227. } else {
  228. (cell as? NCCellProtocol)?.filePreviewImageView?.image = utility.loadImage(named: metadata.iconName, useTypeIconFile: true, account: metadata.account)
  229. }
  230. if metadata.hasPreview,
  231. metadata.status == NCGlobal.shared.metadataStatusNormal {
  232. for case let operation as NCCollectionViewDownloadThumbnail in NCNetworking.shared.downloadThumbnailQueue.operations where operation.metadata.ocId == metadata.ocId { return }
  233. NCNetworking.shared.downloadThumbnailQueue.addOperation(NCCollectionViewDownloadThumbnail(metadata: metadata, collectionView: collectionView, ext: NCGlobal.shared.previewExt256))
  234. }
  235. }
  236. }
  237. }
  238. func collectionView(_ collectionView: UICollectionView, didEndDisplaying cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
  239. }
  240. func numberOfSections(in collectionView: UICollectionView) -> Int {
  241. return self.dataSource.numberOfSections()
  242. }
  243. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  244. return self.dataSource.numberOfItemsInSection(section)
  245. }
  246. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  247. let cell = (collectionView.dequeueReusableCell(withReuseIdentifier: "listCell", for: indexPath) as? NCListCell)!
  248. guard let metadata = self.dataSource.getMetadata(indexPath: indexPath) else { return cell }
  249. var isShare = false
  250. var isMounted = false
  251. let permissions = NCPermissions()
  252. isShare = metadata.permissions.contains(permissions.permissionShared) && !metadataFolder.permissions.contains(permissions.permissionShared)
  253. isMounted = metadata.permissions.contains(permissions.permissionMounted) && !metadataFolder.permissions.contains(permissions.permissionMounted)
  254. cell.listCellDelegate = self
  255. cell.fileOcId = metadata.ocId
  256. cell.fileOcIdTransfer = metadata.ocIdTransfer
  257. cell.fileUser = metadata.ownerId
  258. cell.labelTitle.text = metadata.fileNameView
  259. cell.labelTitle.textColor = NCBrandColor.shared.textColor
  260. cell.imageSelect.image = nil
  261. cell.imageStatus.image = nil
  262. cell.imageLocal.image = nil
  263. cell.imageFavorite.image = nil
  264. cell.imageShared.image = nil
  265. cell.imageMore.image = nil
  266. cell.imageItem.image = nil
  267. cell.imageItem.backgroundColor = nil
  268. if metadata.directory {
  269. if metadata.e2eEncrypted {
  270. cell.imageItem.image = NCImageCache.shared.getFolderEncrypted(account: metadata.account)
  271. } else if isShare {
  272. cell.imageItem.image = NCImageCache.shared.getFolderSharedWithMe(account: metadata.account)
  273. } else if !metadata.shareType.isEmpty {
  274. metadata.shareType.contains(3) ?
  275. (cell.imageItem.image = NCImageCache.shared.getFolderPublic(account: metadata.account)) :
  276. (cell.imageItem.image = NCImageCache.shared.getFolderSharedWithMe(account: metadata.account))
  277. } else if metadata.mountType == "group" {
  278. cell.imageItem.image = NCImageCache.shared.getFolderGroup(account: metadata.account)
  279. } else if isMounted {
  280. cell.imageItem.image = NCImageCache.shared.getFolderExternal(account: metadata.account)
  281. } else if metadata.fileName == autoUploadFileName && metadata.serverUrl == autoUploadDirectory {
  282. cell.imageItem.image = NCImageCache.shared.getFolderAutomaticUpload(account: metadata.account)
  283. } else {
  284. cell.imageItem.image = NCImageCache.shared.getFolder(account: metadata.account)
  285. }
  286. cell.imageItem.image = cell.imageItem.image?.colorizeFolder(metadata: metadata)
  287. cell.labelInfo.text = utility.dateDiff(metadata.date as Date)
  288. } else {
  289. cell.labelInfo.text = utility.dateDiff(metadata.date as Date) + " · " + utilityFileSystem.transformedSize(metadata.size)
  290. // image local
  291. if self.database.getTableLocalFile(ocId: metadata.ocId) != nil {
  292. cell.imageLocal.image = NCImageCache.shared.getImageOfflineFlag()
  293. } else if utilityFileSystem.fileProviderStorageExists(metadata) {
  294. cell.imageLocal.image = NCImageCache.shared.getImageLocal()
  295. }
  296. }
  297. // image Favorite
  298. if metadata.favorite {
  299. cell.imageFavorite.image = NCImageCache.shared.getImageFavorite()
  300. }
  301. cell.imageSelect.isHidden = true
  302. cell.backgroundView = nil
  303. cell.hideButtonMore(true)
  304. cell.hideButtonShare(true)
  305. cell.selected(false, isEditMode: false)
  306. // Live Photo
  307. if metadata.isLivePhoto {
  308. cell.imageStatus.image = utility.loadImage(named: "livephoto", colors: [NCBrandColor.shared.iconImageColor2])
  309. }
  310. // Remove last separator
  311. if collectionView.numberOfItems(inSection: indexPath.section) == indexPath.row + 1 {
  312. cell.separator.isHidden = true
  313. } else {
  314. cell.separator.isHidden = false
  315. }
  316. // Add TAGS
  317. cell.setTags(tags: Array(metadata.tags))
  318. return cell
  319. }
  320. func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
  321. if kind == UICollectionView.elementKindSectionHeader {
  322. if self.dataSource.isEmpty() {
  323. guard let header = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "sectionFirstHeaderEmptyData", for: indexPath) as? NCSectionFirstHeaderEmptyData else { return NCSectionFirstHeaderEmptyData() }
  324. if self.dataSourceTask?.state == .running {
  325. header.emptyImage.image = utility.loadImage(named: "wifi", colors: [NCBrandColor.shared.getElement(account: session.account)])
  326. header.emptyTitle.text = NSLocalizedString("_request_in_progress_", comment: "")
  327. header.emptyDescription.text = ""
  328. } else {
  329. header.emptyImage.image = NCImageCache.shared.getFolder(account: session.account)
  330. if includeImages {
  331. header.emptyTitle.text = NSLocalizedString("_files_no_files_", comment: "")
  332. } else {
  333. header.emptyTitle.text = NSLocalizedString("_files_no_folders_", comment: "")
  334. }
  335. header.emptyDescription.text = ""
  336. }
  337. return header
  338. } else {
  339. return UICollectionReusableView()
  340. }
  341. } else {
  342. guard let footer = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "sectionFooter", for: indexPath) as? NCSectionFooter else { return NCSectionFooter() }
  343. let sections = self.dataSource.numberOfSections()
  344. let section = indexPath.section
  345. footer.setTitleLabel("")
  346. footer.separatorIsHidden(true)
  347. if sections == 1 || section == sections - 1 {
  348. let info = self.dataSource.getFooterInformation()
  349. footer.setTitleLabel(directories: info.directories, files: info.files, size: info.size)
  350. } else {
  351. footer.separatorIsHidden(false)
  352. }
  353. return footer
  354. }
  355. }
  356. }
  357. extension NCSelect: UICollectionViewDelegateFlowLayout {
  358. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
  359. var height: CGFloat = 0
  360. if self.dataSource.isEmpty() {
  361. height = utility.getHeightHeaderEmptyData(view: view, portraitOffset: 0, landscapeOffset: -20)
  362. }
  363. return CGSize(width: collectionView.frame.width, height: height)
  364. }
  365. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize {
  366. let sections = self.dataSource.numberOfSections()
  367. if section == sections - 1 {
  368. return CGSize(width: collectionView.frame.width, height: NCGlobal.shared.endHeightFooter)
  369. } else {
  370. return CGSize(width: collectionView.frame.width, height: NCGlobal.shared.heightFooter)
  371. }
  372. }
  373. }
  374. // MARK: -
  375. extension NCSelect {
  376. func reloadDataSource() {
  377. var predicate = NSPredicate()
  378. if includeDirectoryE2EEncryption {
  379. if includeImages {
  380. predicate = NSPredicate(format: "account == %@ AND serverUrl == %@ AND (directory == true OR classFile == 'image') AND NOT (status IN %@)", session.account, serverUrl, NCGlobal.shared.metadataStatusHideInView)
  381. } else {
  382. predicate = NSPredicate(format: "account == %@ AND serverUrl == %@ AND directory == true AND NOT (status IN %@)", session.account, serverUrl, NCGlobal.shared.metadataStatusHideInView)
  383. }
  384. } else {
  385. if includeImages {
  386. predicate = NSPredicate(format: "account == %@ AND serverUrl == %@ AND e2eEncrypted == false AND (directory == true OR classFile == 'image') AND NOT (status IN %@)", session.account, serverUrl, NCGlobal.shared.metadataStatusHideInView)
  387. } else if enableSelectFile {
  388. predicate = NSPredicate(format: "account == %@ AND serverUrl == %@ AND e2eEncrypted == false AND NOT (status IN %@)", session.account, serverUrl, NCGlobal.shared.metadataStatusHideInView)
  389. } else {
  390. predicate = NSPredicate(format: "account == %@ AND serverUrl == %@ AND e2eEncrypted == false AND directory == true AND NOT (status IN %@)", session.account, serverUrl, NCGlobal.shared.metadataStatusHideInView)
  391. }
  392. }
  393. NCNetworking.shared.readFolder(serverUrl: serverUrl,
  394. account: session.account,
  395. checkResponseDataChanged: false,
  396. queue: .main) { task in
  397. self.dataSourceTask = task
  398. if self.dataSource.isEmpty() {
  399. self.collectionView.reloadData()
  400. }
  401. } completion: { _, _, _, _, _ in
  402. let metadatas = self.database.getResultsMetadatasPredicate(predicate, layoutForView: NCDBLayoutForView())
  403. self.dataSource = NCCollectionViewDataSource(metadatas: metadatas)
  404. self.collectionView.reloadData()
  405. NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterReloadDataSource, userInfo: ["serverUrl": self.serverUrl])
  406. }
  407. }
  408. }
  409. // MARK: -
  410. class NCSelectCommandView: UIView {
  411. @IBOutlet weak var separatorView: UIView!
  412. @IBOutlet weak var createFolderButton: UIButton?
  413. @IBOutlet weak var selectButton: UIButton?
  414. @IBOutlet weak var copyButton: UIButton?
  415. @IBOutlet weak var moveButton: UIButton?
  416. @IBOutlet weak var overwriteSwitch: UISwitch?
  417. @IBOutlet weak var overwriteLabel: UILabel?
  418. @IBOutlet weak var separatorHeightConstraint: NSLayoutConstraint!
  419. var selectView: NCSelect?
  420. private let gradient: CAGradientLayer = CAGradientLayer()
  421. override func awakeFromNib() {
  422. separatorHeightConstraint.constant = 0.5
  423. separatorView.backgroundColor = .separator
  424. overwriteLabel?.text = NSLocalizedString("_overwrite_", comment: "")
  425. selectButton?.layer.cornerRadius = 15
  426. selectButton?.layer.masksToBounds = true
  427. selectButton?.setTitle(NSLocalizedString("_select_", comment: ""), for: .normal)
  428. createFolderButton?.layer.cornerRadius = 15
  429. createFolderButton?.layer.masksToBounds = true
  430. createFolderButton?.setTitle(NSLocalizedString("_create_folder_", comment: ""), for: .normal)
  431. copyButton?.layer.cornerRadius = 15
  432. copyButton?.layer.masksToBounds = true
  433. copyButton?.setTitle(NSLocalizedString("_copy_", comment: ""), for: .normal)
  434. moveButton?.layer.cornerRadius = 15
  435. moveButton?.layer.masksToBounds = true
  436. moveButton?.setTitle(NSLocalizedString("_move_", comment: ""), for: .normal)
  437. }
  438. func setColor(account: String) {
  439. overwriteSwitch?.onTintColor = NCBrandColor.shared.getElement(account: account)
  440. selectButton?.backgroundColor = NCBrandColor.shared.getElement(account: account)
  441. selectButton?.setTitleColor(UIColor(white: 1, alpha: 0.3), for: .highlighted)
  442. selectButton?.setTitleColor(.white, for: .normal)
  443. createFolderButton?.backgroundColor = NCBrandColor.shared.getElement(account: account)
  444. createFolderButton?.setTitleColor(UIColor(white: 1, alpha: 0.3), for: .highlighted)
  445. createFolderButton?.setTitleColor(NCBrandColor.shared.getText(account: account), for: .normal)
  446. copyButton?.backgroundColor = NCBrandColor.shared.getElement(account: account)
  447. copyButton?.setTitleColor(UIColor(white: 1, alpha: 0.3), for: .highlighted)
  448. copyButton?.setTitleColor(NCBrandColor.shared.getText(account: account), for: .normal)
  449. moveButton?.backgroundColor = NCBrandColor.shared.getElement(account: account)
  450. moveButton?.setTitleColor(UIColor(white: 1, alpha: 0.3), for: .highlighted)
  451. moveButton?.setTitleColor(NCBrandColor.shared.getText(account: account), for: .normal)
  452. }
  453. @IBAction func createFolderButtonPressed(_ sender: UIButton) {
  454. selectView?.createFolderButtonPressed(sender)
  455. }
  456. @IBAction func selectButtonPressed(_ sender: UIButton) {
  457. selectView?.selectButtonPressed(sender)
  458. }
  459. @IBAction func copyButtonPressed(_ sender: UIButton) {
  460. selectView?.copyButtonPressed(sender)
  461. }
  462. @IBAction func moveButtonPressed(_ sender: UIButton) {
  463. selectView?.moveButtonPressed(sender)
  464. }
  465. @IBAction func valueChangedSwitchOverwrite(_ sender: UISwitch) {
  466. selectView?.valueChangedSwitchOverwrite(sender)
  467. }
  468. }
  469. // MARK: - UIViewControllerRepresentable
  470. struct NCSelectViewControllerRepresentable: UIViewControllerRepresentable {
  471. typealias UIViewControllerType = UINavigationController
  472. var delegate: NCSelectDelegate
  473. var session: NCSession.Session!
  474. func makeUIViewController(context: Context) -> UINavigationController {
  475. let storyboard = UIStoryboard(name: "NCSelect", bundle: nil)
  476. let navigationController = storyboard.instantiateInitialViewController() as? UINavigationController
  477. let viewController = navigationController?.topViewController as? NCSelect
  478. viewController?.delegate = delegate
  479. viewController?.typeOfCommandView = .selectCreateFolder
  480. viewController?.includeDirectoryE2EEncryption = true
  481. viewController?.session = session
  482. return navigationController!
  483. }
  484. func updateUIViewController(_ uiViewController: UINavigationController, context: Context) { }
  485. }
  486. struct SelectView: UIViewControllerRepresentable {
  487. @Binding var serverUrl: String
  488. var session: NCSession.Session!
  489. class Coordinator: NSObject, NCSelectDelegate {
  490. var parent: SelectView
  491. init(_ parent: SelectView) {
  492. self.parent = parent
  493. }
  494. func dismissSelect(serverUrl: String?, metadata: tableMetadata?, type: String, items: [Any], overwrite: Bool, copy: Bool, move: Bool, session: NCSession.Session) {
  495. if let serverUrl = serverUrl {
  496. self.parent.serverUrl = serverUrl
  497. }
  498. }
  499. }
  500. func makeUIViewController(context: Context) -> UINavigationController {
  501. let storyboard = UIStoryboard(name: "NCSelect", bundle: nil)
  502. let navigationController = storyboard.instantiateInitialViewController() as? UINavigationController
  503. let viewController = navigationController?.topViewController as? NCSelect
  504. viewController?.delegate = context.coordinator
  505. viewController?.typeOfCommandView = .selectCreateFolder
  506. viewController?.includeDirectoryE2EEncryption = true
  507. viewController?.session = session
  508. return navigationController!
  509. }
  510. func updateUIViewController(_ uiViewController: UINavigationController, context: Context) { }
  511. func makeCoordinator() -> Coordinator {
  512. Coordinator(self)
  513. }
  514. }