NCSelect.swift 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641
  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. serverUrl == self.serverUrl,
  148. let metadata = self.database.getMetadataFromOcId(ocId)
  149. else { return }
  150. pushMetadata(metadata)
  151. }
  152. // MARK: ACTION
  153. @IBAction func actionCancel(_ sender: UIBarButtonItem) {
  154. self.dismiss(animated: true, completion: nil)
  155. }
  156. func selectButtonPressed(_ sender: UIButton) {
  157. delegate?.dismissSelect(serverUrl: serverUrl, metadata: metadataFolder, type: type, items: items, overwrite: overwrite, copy: false, move: false, session: session)
  158. self.dismiss(animated: true, completion: nil)
  159. }
  160. func copyButtonPressed(_ sender: UIButton) {
  161. delegate?.dismissSelect(serverUrl: serverUrl, metadata: metadataFolder, type: type, items: items, overwrite: overwrite, copy: true, move: false, session: session)
  162. self.dismiss(animated: true, completion: nil)
  163. }
  164. func moveButtonPressed(_ sender: UIButton) {
  165. delegate?.dismissSelect(serverUrl: serverUrl, metadata: metadataFolder, type: type, items: items, overwrite: overwrite, copy: false, move: true, session: session)
  166. self.dismiss(animated: true, completion: nil)
  167. }
  168. func createFolderButtonPressed(_ sender: UIButton) {
  169. let alertController = UIAlertController.createFolder(serverUrl: serverUrl, account: session.account)
  170. self.present(alertController, animated: true, completion: nil)
  171. }
  172. @IBAction func valueChangedSwitchOverwrite(_ sender: UISwitch) {
  173. overwrite = sender.isOn
  174. }
  175. func tapShareListItem(with ocId: String, ocIdTransfer: String, sender: Any) { }
  176. func tapMoreListItem(with ocId: String, ocIdTransfer: String, image: UIImage?, sender: Any) { }
  177. func longPressListItem(with odId: String, ocIdTransfer: String, gestureRecognizer: UILongPressGestureRecognizer) { }
  178. func tapRichWorkspace(_ sender: Any) { }
  179. // MARK: - Push metadata
  180. func pushMetadata(_ metadata: tableMetadata) {
  181. let serverUrlPush = utilityFileSystem.stringAppendServerUrl(metadata.serverUrl, addFileName: metadata.fileName)
  182. guard let viewController = UIStoryboard(name: "NCSelect", bundle: nil).instantiateViewController(withIdentifier: "NCSelect.storyboard") as? NCSelect else { return }
  183. self.serverUrlPush = serverUrlPush
  184. viewController.delegate = delegate
  185. viewController.typeOfCommandView = typeOfCommandView
  186. viewController.includeDirectoryE2EEncryption = includeDirectoryE2EEncryption
  187. viewController.includeImages = includeImages
  188. viewController.enableSelectFile = enableSelectFile
  189. viewController.type = type
  190. viewController.overwrite = overwrite
  191. viewController.items = items
  192. viewController.titleCurrentFolder = metadata.fileNameView
  193. viewController.serverUrl = serverUrlPush
  194. viewController.session = session
  195. if let fileNameError = FileNameValidator.shared.checkFileName(metadata.fileNameView, account: session.account) {
  196. present(UIAlertController.warning(message: "\(fileNameError.errorDescription) \(NSLocalizedString("_please_rename_file_", comment: ""))"), animated: true)
  197. } else {
  198. navigationController?.pushViewController(viewController, animated: true)
  199. }
  200. }
  201. }
  202. // MARK: - Collection View
  203. extension NCSelect: UICollectionViewDelegate {
  204. func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  205. guard let metadata = self.dataSource.getMetadata(indexPath: indexPath) else { return }
  206. if metadata.directory {
  207. pushMetadata(metadata)
  208. } else {
  209. delegate?.dismissSelect(serverUrl: serverUrl, metadata: metadata, type: type, items: items, overwrite: overwrite, copy: false, move: false, session: session)
  210. self.dismiss(animated: true, completion: nil)
  211. }
  212. }
  213. }
  214. extension NCSelect: UICollectionViewDataSource {
  215. func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
  216. guard let metadata = self.dataSource.getMetadata(indexPath: indexPath) else { return }
  217. // Thumbnail
  218. if !metadata.directory {
  219. if let image = utility.getImage(ocId: metadata.ocId, etag: metadata.etag, ext: NCGlobal.shared.previewExt512) {
  220. (cell as? NCCellProtocol)?.filePreviewImageView?.image = image
  221. } else {
  222. if metadata.iconName.isEmpty {
  223. (cell as? NCCellProtocol)?.filePreviewImageView?.image = NCImageCache.shared.getImageFile()
  224. } else {
  225. (cell as? NCCellProtocol)?.filePreviewImageView?.image = utility.loadImage(named: metadata.iconName, useTypeIconFile: true, account: metadata.account)
  226. }
  227. if metadata.hasPreview,
  228. metadata.status == NCGlobal.shared.metadataStatusNormal {
  229. for case let operation as NCCollectionViewDownloadThumbnail in NCNetworking.shared.downloadThumbnailQueue.operations where operation.metadata.ocId == metadata.ocId { return }
  230. NCNetworking.shared.downloadThumbnailQueue.addOperation(NCCollectionViewDownloadThumbnail(metadata: metadata, collectionView: collectionView, ext: NCGlobal.shared.previewExt256))
  231. }
  232. }
  233. }
  234. }
  235. func collectionView(_ collectionView: UICollectionView, didEndDisplaying cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
  236. }
  237. func numberOfSections(in collectionView: UICollectionView) -> Int {
  238. return self.dataSource.numberOfSections()
  239. }
  240. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  241. return self.dataSource.numberOfItemsInSection(section)
  242. }
  243. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  244. let cell = (collectionView.dequeueReusableCell(withReuseIdentifier: "listCell", for: indexPath) as? NCListCell)!
  245. guard let metadata = self.dataSource.getMetadata(indexPath: indexPath) else { return cell }
  246. var isShare = false
  247. var isMounted = false
  248. let permissions = NCPermissions()
  249. isShare = metadata.permissions.contains(permissions.permissionShared) && !metadataFolder.permissions.contains(permissions.permissionShared)
  250. isMounted = metadata.permissions.contains(permissions.permissionMounted) && !metadataFolder.permissions.contains(permissions.permissionMounted)
  251. cell.listCellDelegate = self
  252. cell.fileOcId = metadata.ocId
  253. cell.fileOcIdTransfer = metadata.ocIdTransfer
  254. cell.fileUser = metadata.ownerId
  255. cell.labelTitle.text = metadata.fileNameView
  256. cell.labelTitle.textColor = NCBrandColor.shared.textColor
  257. cell.imageSelect.image = nil
  258. cell.imageStatus.image = nil
  259. cell.imageLocal.image = nil
  260. cell.imageFavorite.image = nil
  261. cell.imageShared.image = nil
  262. cell.imageMore.image = nil
  263. cell.imageItem.image = nil
  264. cell.imageItem.backgroundColor = nil
  265. if metadata.directory {
  266. if metadata.e2eEncrypted {
  267. cell.imageItem.image = NCImageCache.shared.getFolderEncrypted(account: metadata.account)
  268. } else if isShare {
  269. cell.imageItem.image = NCImageCache.shared.getFolderSharedWithMe(account: metadata.account)
  270. } else if !metadata.shareType.isEmpty {
  271. metadata.shareType.contains(3) ?
  272. (cell.imageItem.image = NCImageCache.shared.getFolderPublic(account: metadata.account)) :
  273. (cell.imageItem.image = NCImageCache.shared.getFolderSharedWithMe(account: metadata.account))
  274. } else if metadata.mountType == "group" {
  275. cell.imageItem.image = NCImageCache.shared.getFolderGroup(account: metadata.account)
  276. } else if isMounted {
  277. cell.imageItem.image = NCImageCache.shared.getFolderExternal(account: metadata.account)
  278. } else if metadata.fileName == autoUploadFileName && metadata.serverUrl == autoUploadDirectory {
  279. cell.imageItem.image = NCImageCache.shared.getFolderAutomaticUpload(account: metadata.account)
  280. } else {
  281. cell.imageItem.image = NCImageCache.shared.getFolder(account: metadata.account)
  282. }
  283. cell.imageItem.image = cell.imageItem.image?.colorizeFolder(metadata: metadata)
  284. cell.labelInfo.text = utility.dateDiff(metadata.date as Date)
  285. } else {
  286. cell.labelInfo.text = utility.dateDiff(metadata.date as Date) + " · " + utilityFileSystem.transformedSize(metadata.size)
  287. // image local
  288. if self.database.getTableLocalFile(ocId: metadata.ocId) != nil {
  289. cell.imageLocal.image = NCImageCache.shared.getImageOfflineFlag()
  290. } else if utilityFileSystem.fileProviderStorageExists(metadata) {
  291. cell.imageLocal.image = NCImageCache.shared.getImageLocal()
  292. }
  293. }
  294. // image Favorite
  295. if metadata.favorite {
  296. cell.imageFavorite.image = NCImageCache.shared.getImageFavorite()
  297. }
  298. cell.imageSelect.isHidden = true
  299. cell.backgroundView = nil
  300. cell.hideButtonMore(true)
  301. cell.hideButtonShare(true)
  302. cell.selected(false, isEditMode: false)
  303. // Live Photo
  304. if metadata.isLivePhoto {
  305. cell.imageStatus.image = utility.loadImage(named: "livephoto", colors: [NCBrandColor.shared.iconImageColor2])
  306. }
  307. // Remove last separator
  308. if collectionView.numberOfItems(inSection: indexPath.section) == indexPath.row + 1 {
  309. cell.separator.isHidden = true
  310. } else {
  311. cell.separator.isHidden = false
  312. }
  313. // Add TAGS
  314. cell.setTags(tags: Array(metadata.tags))
  315. return cell
  316. }
  317. func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
  318. if kind == UICollectionView.elementKindSectionHeader {
  319. if self.dataSource.isEmpty() {
  320. guard let header = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "sectionFirstHeaderEmptyData", for: indexPath) as? NCSectionFirstHeaderEmptyData else { return NCSectionFirstHeaderEmptyData() }
  321. if self.dataSourceTask?.state == .running {
  322. header.emptyImage.image = utility.loadImage(named: "wifi", colors: [NCBrandColor.shared.getElement(account: session.account)])
  323. header.emptyTitle.text = NSLocalizedString("_request_in_progress_", comment: "")
  324. header.emptyDescription.text = ""
  325. } else {
  326. header.emptyImage.image = NCImageCache.shared.getFolder(account: session.account)
  327. if includeImages {
  328. header.emptyTitle.text = NSLocalizedString("_files_no_files_", comment: "")
  329. } else {
  330. header.emptyTitle.text = NSLocalizedString("_files_no_folders_", comment: "")
  331. }
  332. header.emptyDescription.text = ""
  333. }
  334. return header
  335. } else {
  336. return UICollectionReusableView()
  337. }
  338. } else {
  339. guard let footer = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "sectionFooter", for: indexPath) as? NCSectionFooter else { return NCSectionFooter() }
  340. let sections = self.dataSource.numberOfSections()
  341. let section = indexPath.section
  342. footer.setTitleLabel("")
  343. footer.separatorIsHidden(true)
  344. if sections == 1 || section == sections - 1 {
  345. let info = self.dataSource.getFooterInformation()
  346. footer.setTitleLabel(directories: info.directories, files: info.files, size: info.size)
  347. } else {
  348. footer.separatorIsHidden(false)
  349. }
  350. return footer
  351. }
  352. }
  353. }
  354. extension NCSelect: UICollectionViewDelegateFlowLayout {
  355. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
  356. var height: CGFloat = 0
  357. if self.dataSource.isEmpty() {
  358. height = utility.getHeightHeaderEmptyData(view: view, portraitOffset: 0, landscapeOffset: -20)
  359. }
  360. return CGSize(width: collectionView.frame.width, height: height)
  361. }
  362. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize {
  363. let sections = self.dataSource.numberOfSections()
  364. if section == sections - 1 {
  365. return CGSize(width: collectionView.frame.width, height: NCGlobal.shared.endHeightFooter)
  366. } else {
  367. return CGSize(width: collectionView.frame.width, height: NCGlobal.shared.heightFooter)
  368. }
  369. }
  370. }
  371. // MARK: -
  372. extension NCSelect {
  373. func reloadDataSource() {
  374. var predicate = NSPredicate()
  375. if includeDirectoryE2EEncryption {
  376. if includeImages {
  377. predicate = NSPredicate(format: "account == %@ AND serverUrl == %@ AND (directory == true OR classFile == 'image')", session.account, serverUrl)
  378. } else {
  379. predicate = NSPredicate(format: "account == %@ AND serverUrl == %@ AND directory == true", session.account, serverUrl)
  380. }
  381. } else {
  382. if includeImages {
  383. 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)
  384. } else if enableSelectFile {
  385. predicate = NSPredicate(format: "account == %@ AND serverUrl == %@ AND e2eEncrypted == false AND NOT (status IN %@)", session.account, serverUrl, NCGlobal.shared.metadataStatusHideInView)
  386. } else {
  387. predicate = NSPredicate(format: "account == %@ AND serverUrl == %@ AND e2eEncrypted == false AND directory == true", session.account, serverUrl)
  388. }
  389. }
  390. NCNetworking.shared.readFolder(serverUrl: serverUrl,
  391. account: session.account,
  392. checkResponseDataChanged: false,
  393. queue: .main) { task in
  394. self.dataSourceTask = task
  395. if self.dataSource.isEmpty() {
  396. self.collectionView.reloadData()
  397. }
  398. } completion: { _, _, _, _, _ in
  399. let results = self.database.getResultsMetadatasPredicate(predicate, layoutForView: NCDBLayoutForView())
  400. self.dataSource = NCCollectionViewDataSource(results: results)
  401. self.collectionView.reloadData()
  402. NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterReloadDataSource, userInfo: ["serverUrl": self.serverUrl])
  403. }
  404. }
  405. }
  406. // MARK: -
  407. class NCSelectCommandView: UIView {
  408. @IBOutlet weak var separatorView: UIView!
  409. @IBOutlet weak var createFolderButton: UIButton?
  410. @IBOutlet weak var selectButton: UIButton?
  411. @IBOutlet weak var copyButton: UIButton?
  412. @IBOutlet weak var moveButton: UIButton?
  413. @IBOutlet weak var overwriteSwitch: UISwitch?
  414. @IBOutlet weak var overwriteLabel: UILabel?
  415. @IBOutlet weak var separatorHeightConstraint: NSLayoutConstraint!
  416. var selectView: NCSelect?
  417. private let gradient: CAGradientLayer = CAGradientLayer()
  418. override func awakeFromNib() {
  419. separatorHeightConstraint.constant = 0.5
  420. separatorView.backgroundColor = .separator
  421. overwriteLabel?.text = NSLocalizedString("_overwrite_", comment: "")
  422. selectButton?.layer.cornerRadius = 15
  423. selectButton?.layer.masksToBounds = true
  424. selectButton?.setTitle(NSLocalizedString("_select_", comment: ""), for: .normal)
  425. createFolderButton?.layer.cornerRadius = 15
  426. createFolderButton?.layer.masksToBounds = true
  427. createFolderButton?.setTitle(NSLocalizedString("_create_folder_", comment: ""), for: .normal)
  428. copyButton?.layer.cornerRadius = 15
  429. copyButton?.layer.masksToBounds = true
  430. copyButton?.setTitle(NSLocalizedString("_copy_", comment: ""), for: .normal)
  431. moveButton?.layer.cornerRadius = 15
  432. moveButton?.layer.masksToBounds = true
  433. moveButton?.setTitle(NSLocalizedString("_move_", comment: ""), for: .normal)
  434. }
  435. func setColor(account: String) {
  436. overwriteSwitch?.onTintColor = NCBrandColor.shared.getElement(account: account)
  437. selectButton?.backgroundColor = NCBrandColor.shared.getElement(account: account)
  438. selectButton?.setTitleColor(UIColor(white: 1, alpha: 0.3), for: .highlighted)
  439. selectButton?.setTitleColor(.white, for: .normal)
  440. createFolderButton?.backgroundColor = NCBrandColor.shared.getElement(account: account)
  441. createFolderButton?.setTitleColor(UIColor(white: 1, alpha: 0.3), for: .highlighted)
  442. createFolderButton?.setTitleColor(NCBrandColor.shared.getText(account: account), for: .normal)
  443. copyButton?.backgroundColor = NCBrandColor.shared.getElement(account: account)
  444. copyButton?.setTitleColor(UIColor(white: 1, alpha: 0.3), for: .highlighted)
  445. copyButton?.setTitleColor(NCBrandColor.shared.getText(account: account), for: .normal)
  446. moveButton?.backgroundColor = NCBrandColor.shared.getElement(account: account)
  447. moveButton?.setTitleColor(UIColor(white: 1, alpha: 0.3), for: .highlighted)
  448. moveButton?.setTitleColor(NCBrandColor.shared.getText(account: account), for: .normal)
  449. }
  450. @IBAction func createFolderButtonPressed(_ sender: UIButton) {
  451. selectView?.createFolderButtonPressed(sender)
  452. }
  453. @IBAction func selectButtonPressed(_ sender: UIButton) {
  454. selectView?.selectButtonPressed(sender)
  455. }
  456. @IBAction func copyButtonPressed(_ sender: UIButton) {
  457. selectView?.copyButtonPressed(sender)
  458. }
  459. @IBAction func moveButtonPressed(_ sender: UIButton) {
  460. selectView?.moveButtonPressed(sender)
  461. }
  462. @IBAction func valueChangedSwitchOverwrite(_ sender: UISwitch) {
  463. selectView?.valueChangedSwitchOverwrite(sender)
  464. }
  465. }
  466. // MARK: - UIViewControllerRepresentable
  467. struct NCSelectViewControllerRepresentable: UIViewControllerRepresentable {
  468. typealias UIViewControllerType = UINavigationController
  469. var delegate: NCSelectDelegate
  470. var session: NCSession.Session!
  471. func makeUIViewController(context: Context) -> UINavigationController {
  472. let storyboard = UIStoryboard(name: "NCSelect", bundle: nil)
  473. let navigationController = storyboard.instantiateInitialViewController() as? UINavigationController
  474. let viewController = navigationController?.topViewController as? NCSelect
  475. viewController?.delegate = delegate
  476. viewController?.typeOfCommandView = .selectCreateFolder
  477. viewController?.includeDirectoryE2EEncryption = true
  478. viewController?.session = session
  479. return navigationController!
  480. }
  481. func updateUIViewController(_ uiViewController: UINavigationController, context: Context) { }
  482. }
  483. struct SelectView: UIViewControllerRepresentable {
  484. @Binding var serverUrl: String
  485. var session: NCSession.Session!
  486. class Coordinator: NSObject, NCSelectDelegate {
  487. var parent: SelectView
  488. init(_ parent: SelectView) {
  489. self.parent = parent
  490. }
  491. func dismissSelect(serverUrl: String?, metadata: tableMetadata?, type: String, items: [Any], overwrite: Bool, copy: Bool, move: Bool, session: NCSession.Session) {
  492. if let serverUrl = serverUrl {
  493. self.parent.serverUrl = serverUrl
  494. }
  495. }
  496. }
  497. func makeUIViewController(context: Context) -> UINavigationController {
  498. let storyboard = UIStoryboard(name: "NCSelect", bundle: nil)
  499. let navigationController = storyboard.instantiateInitialViewController() as? UINavigationController
  500. let viewController = navigationController?.topViewController as? NCSelect
  501. viewController?.delegate = context.coordinator
  502. viewController?.typeOfCommandView = .selectCreateFolder
  503. viewController?.includeDirectoryE2EEncryption = true
  504. viewController?.session = session
  505. return navigationController!
  506. }
  507. func updateUIViewController(_ uiViewController: UINavigationController, context: Context) { }
  508. func makeCoordinator() -> Coordinator {
  509. Coordinator(self)
  510. }
  511. }