NCSelect.swift 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
  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 Foundation
  24. import NCCommunication
  25. @objc protocol NCSelectDelegate {
  26. @objc func dismissSelect(serverUrl: String?, metadata: tableMetadata?, type: String, array: [Any], buttonType: String, overwrite: Bool)
  27. }
  28. class NCSelect: UIViewController, UIGestureRecognizerDelegate, NCListCellDelegate, NCGridCellDelegate, NCSectionHeaderMenuDelegate, DZNEmptyDataSetSource, DZNEmptyDataSetDelegate {
  29. @IBOutlet fileprivate weak var collectionView: UICollectionView!
  30. @IBOutlet fileprivate weak var toolbar: UIView!
  31. @IBOutlet fileprivate weak var overwriteView: UIView!
  32. @IBOutlet fileprivate weak var buttonCancel: UIBarButtonItem!
  33. @IBOutlet fileprivate weak var buttonCreateFolder: UIButton!
  34. @IBOutlet fileprivate weak var buttonDone: UIButton!
  35. @IBOutlet fileprivate weak var buttonDone1: UIButton!
  36. @IBOutlet fileprivate weak var overwriteSwitch: UISwitch!
  37. @IBOutlet fileprivate weak var overwriteLabel: UILabel!
  38. @IBOutlet fileprivate weak var toolBarTop: NSLayoutConstraint!
  39. // ------ external settings ------------------------------------
  40. @objc var delegate: NCSelectDelegate?
  41. @objc var hideButtonCreateFolder = false
  42. @objc var selectFile = false
  43. @objc var includeDirectoryE2EEncryption = false
  44. @objc var includeImages = false
  45. @objc var type = ""
  46. @objc var titleButtonDone = NSLocalizedString("_move_", comment: "")
  47. @objc var titleButtonDone1 = NSLocalizedString("_copy_", comment: "")
  48. @objc var isButtonDone1Hide = true
  49. @objc var isOverwriteHide = true
  50. @objc var keyLayout = k_layout_view_move
  51. @objc var heightToolBarTop: CGFloat = 100
  52. @objc var array: [Any] = []
  53. var titleCurrentFolder = NCBrandOptions.sharedInstance.brand
  54. var serverUrl = ""
  55. // -------------------------------------------------------------
  56. private let appDelegate = UIApplication.shared.delegate as! AppDelegate
  57. private var serverUrlPush = ""
  58. private var metadataPush: tableMetadata?
  59. private var metadataFolder = tableMetadata()
  60. private var isEditMode = false
  61. private var networkInProgress = false
  62. private var selectocId: [String] = []
  63. private var overwrite = false
  64. private var dataSource: NCDataSource?
  65. private var layout = ""
  66. private var groupBy = ""
  67. private var titleButton = ""
  68. private var itemForLine = 0
  69. private var autoUploadFileName = ""
  70. private var autoUploadDirectory = ""
  71. private var listLayout: NCListLayout!
  72. private var gridLayout: NCGridLayout!
  73. private let headerMenuHeight: CGFloat = 50
  74. private let sectionHeaderHeight: CGFloat = 20
  75. private let footerHeight: CGFloat = 50
  76. private var shares: [tableShare]?
  77. private let refreshControl = UIRefreshControl()
  78. override func viewDidLoad() {
  79. super.viewDidLoad()
  80. // Cell
  81. collectionView.register(UINib.init(nibName: "NCListCell", bundle: nil), forCellWithReuseIdentifier: "listCell")
  82. collectionView.register(UINib.init(nibName: "NCGridCell", bundle: nil), forCellWithReuseIdentifier: "gridCell")
  83. // Header
  84. collectionView.register(UINib.init(nibName: "NCSectionHeaderMenu", bundle: nil), forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "sectionHeaderMenu")
  85. collectionView.register(UINib.init(nibName: "NCSectionHeader", bundle: nil), forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "sectionHeader")
  86. // Footer
  87. collectionView.register(UINib.init(nibName: "NCSectionFooter", bundle: nil), forSupplementaryViewOfKind: UICollectionView.elementKindSectionFooter, withReuseIdentifier: "sectionFooter")
  88. collectionView.alwaysBounceVertical = true
  89. collectionView.backgroundColor = NCBrandColor.sharedInstance.backgroundForm
  90. listLayout = NCListLayout()
  91. gridLayout = NCGridLayout()
  92. // Add Refresh Control
  93. collectionView.addSubview(refreshControl)
  94. // Configure Refresh Control
  95. refreshControl.tintColor = NCBrandColor.sharedInstance.brandElement
  96. refreshControl.backgroundColor = NCBrandColor.sharedInstance.backgroundView
  97. refreshControl.addTarget(self, action: #selector(loadDatasource), for: .valueChanged)
  98. // empty Data Source
  99. self.collectionView.emptyDataSetDelegate = self;
  100. self.collectionView.emptyDataSetSource = self;
  101. // title button
  102. buttonCancel.title = NSLocalizedString("_cancel_", comment: "")
  103. buttonCreateFolder.setTitle(NSLocalizedString("_create_folder_", comment: ""), for: .normal)
  104. overwriteLabel.text = NSLocalizedString("_overwrite_", comment: "")
  105. // button
  106. buttonCreateFolder.layer.cornerRadius = 15
  107. buttonCreateFolder.layer.masksToBounds = true
  108. buttonCreateFolder.layer.backgroundColor = NCBrandColor.sharedInstance.graySoft.withAlphaComponent(0.5).cgColor
  109. buttonCreateFolder.setTitleColor(.black, for: .normal)
  110. buttonDone.layer.cornerRadius = 15
  111. buttonDone.layer.masksToBounds = true
  112. buttonDone.layer.backgroundColor = NCBrandColor.sharedInstance.graySoft.withAlphaComponent(0.5).cgColor
  113. buttonDone.setTitleColor(.black, for: .normal)
  114. buttonDone1.layer.cornerRadius = 15
  115. buttonDone1.layer.masksToBounds = true
  116. buttonDone1.layer.backgroundColor = NCBrandColor.sharedInstance.graySoft.withAlphaComponent(0.5).cgColor
  117. buttonDone1.setTitleColor(.black, for: .normal)
  118. NotificationCenter.default.addObserver(self, selector: #selector(changeTheming), name: NSNotification.Name(rawValue: k_notificationCenter_changeTheming), object: nil)
  119. NotificationCenter.default.addObserver(self, selector: #selector(reloadDataSource), name: NSNotification.Name(rawValue: k_notificationCenter_reloadDataSource), object: nil)
  120. changeTheming()
  121. }
  122. override func viewWillAppear(_ animated: Bool) {
  123. super.viewWillAppear(animated)
  124. self.navigationItem.title = titleCurrentFolder
  125. toolBarTop.constant = -heightToolBarTop
  126. buttonDone.setTitle(titleButtonDone, for: .normal)
  127. buttonDone1.setTitle(titleButtonDone1, for: .normal)
  128. buttonDone1.isHidden = isButtonDone1Hide
  129. overwriteSwitch.isOn = overwrite
  130. overwriteView.isHidden = isOverwriteHide
  131. if selectFile {
  132. buttonDone.isHidden = true
  133. }
  134. if hideButtonCreateFolder {
  135. buttonCreateFolder.isHidden = true
  136. }
  137. // get auto upload folder
  138. autoUploadFileName = NCManageDatabase.sharedInstance.getAccountAutoUploadFileName()
  139. autoUploadDirectory = NCManageDatabase.sharedInstance.getAccountAutoUploadDirectory(urlBase: appDelegate.urlBase, account: appDelegate.account)
  140. (layout, _, _, groupBy, _, titleButton, itemForLine) = NCUtility.shared.getLayoutForView(key: keyLayout)
  141. gridLayout.itemForLine = CGFloat(itemForLine)
  142. if layout == k_layout_list {
  143. collectionView.collectionViewLayout = listLayout
  144. } else {
  145. collectionView.collectionViewLayout = gridLayout
  146. }
  147. loadDatasource(withLoadFolder: true)
  148. shares = NCManageDatabase.sharedInstance.getTableShares(account: appDelegate.account, serverUrl: serverUrl)
  149. }
  150. override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
  151. super.viewWillTransition(to: size, with: coordinator)
  152. coordinator.animate(alongsideTransition: nil) { _ in
  153. self.collectionView?.collectionViewLayout.invalidateLayout()
  154. }
  155. }
  156. @objc func changeTheming() {
  157. appDelegate.changeTheming(self, tableView: nil, collectionView: collectionView, form: false)
  158. refreshControl.backgroundColor = NCBrandColor.sharedInstance.backgroundView
  159. toolbar.backgroundColor = NCBrandColor.sharedInstance.tabBar
  160. //toolbar.tintColor = .gray
  161. }
  162. // MARK: DZNEmpty
  163. func backgroundColor(forEmptyDataSet scrollView: UIScrollView) -> UIColor? {
  164. return NCBrandColor.sharedInstance.backgroundView
  165. }
  166. func image(forEmptyDataSet scrollView: UIScrollView) -> UIImage? {
  167. if networkInProgress {
  168. return CCGraphics.changeThemingColorImage(UIImage.init(named: "networkInProgress"), width: 300, height: 300, color: UIColor.lightGray)
  169. } else {
  170. return CCGraphics.changeThemingColorImage(UIImage.init(named: "folder"), width: 300, height: 300, color: NCBrandColor.sharedInstance.brandElement)
  171. }
  172. }
  173. func title(forEmptyDataSet scrollView: UIScrollView) -> NSAttributedString? {
  174. let attributes = [NSAttributedString.Key.font: UIFont.boldSystemFont(ofSize: 20), NSAttributedString.Key.foregroundColor: UIColor.lightGray]
  175. if networkInProgress {
  176. return NSAttributedString.init(string: "\n"+NSLocalizedString("_request_in_progress_", comment: ""), attributes: attributes)
  177. } else if includeImages {
  178. return NSAttributedString.init(string: "\n"+NSLocalizedString("_files_no_files_", comment: ""), attributes: attributes)
  179. } else {
  180. return NSAttributedString.init(string: "\n"+NSLocalizedString("_files_no_folders_", comment: ""), attributes: attributes)
  181. }
  182. }
  183. func emptyDataSetShouldAllowScroll(_ scrollView: UIScrollView) -> Bool {
  184. return true
  185. }
  186. // MARK: ACTION
  187. @IBAction func actionCancel(_ sender: Any) {
  188. delegate?.dismissSelect(serverUrl: nil, metadata: nil, type: type, array: array, buttonType: "cancel", overwrite: overwrite)
  189. self.dismiss(animated: true, completion: nil)
  190. }
  191. @IBAction func actionDone(_ sender: Any) {
  192. delegate?.dismissSelect(serverUrl: serverUrl, metadata: metadataFolder, type: type, array: array, buttonType: "done", overwrite: overwrite)
  193. self.dismiss(animated: true, completion: nil)
  194. }
  195. @IBAction func actionDone1(_ sender: Any) {
  196. delegate?.dismissSelect(serverUrl: serverUrl, metadata: metadataFolder, type: type, array: array, buttonType: "done1", overwrite: overwrite)
  197. self.dismiss(animated: true, completion: nil)
  198. }
  199. @IBAction func actionCreateFolder(_ sender: Any) {
  200. let alertController = UIAlertController(title: NSLocalizedString("_create_folder_", comment: ""), message:"", preferredStyle: .alert)
  201. alertController.addTextField { (textField) in
  202. textField.autocapitalizationType = UITextAutocapitalizationType.words
  203. }
  204. let actionSave = UIAlertAction(title: NSLocalizedString("_save_", comment: ""), style: .default) { (action:UIAlertAction) in
  205. if let fileName = alertController.textFields?.first?.text {
  206. self.createFolder(with: fileName)
  207. }
  208. }
  209. let actionCancel = UIAlertAction(title: NSLocalizedString("_cancel_", comment: ""), style: .cancel) { (action:UIAlertAction) in
  210. print("You've pressed cancel button")
  211. }
  212. alertController.addAction(actionSave)
  213. alertController.addAction(actionCancel)
  214. self.present(alertController, animated: true, completion:nil)
  215. }
  216. @IBAction func valueChangedSwitchOverwrite(_ sender: Any) {
  217. if let viewControllers = self.navigationController?.viewControllers {
  218. for viewController in viewControllers {
  219. if viewController is NCSelect {
  220. (viewController as! NCSelect).overwrite = overwriteSwitch.isOn
  221. }
  222. }
  223. }
  224. }
  225. // MARK: TAP EVENT
  226. func tapSwitchHeader(sender: Any) {
  227. if collectionView.collectionViewLayout == gridLayout {
  228. // list layout
  229. UIView.animate(withDuration: 0.0, animations: {
  230. self.collectionView.collectionViewLayout.invalidateLayout()
  231. self.collectionView.setCollectionViewLayout(self.listLayout, animated: false, completion: { (_) in
  232. self.collectionView.reloadData()
  233. self.collectionView.setContentOffset(CGPoint(x:0,y:0), animated: false)
  234. })
  235. })
  236. layout = k_layout_list
  237. NCUtility.shared.setLayoutForView(key: keyLayout, layout: layout)
  238. } else {
  239. // grid layout
  240. UIView.animate(withDuration: 0.0, animations: {
  241. self.collectionView.collectionViewLayout.invalidateLayout()
  242. self.collectionView.setCollectionViewLayout(self.gridLayout, animated: false, completion: { (_) in
  243. self.collectionView.reloadData()
  244. self.collectionView.setContentOffset(CGPoint(x:0,y:0), animated: false)
  245. })
  246. })
  247. layout = k_layout_grid
  248. NCUtility.shared.setLayoutForView(key: keyLayout, layout: layout)
  249. }
  250. }
  251. func tapOrderHeader(sender: Any) {
  252. let sortMenu = NCSortMenu()
  253. sortMenu.toggleMenu(viewController: self, key: keyLayout, sortButton: sender as? UIButton, serverUrl: serverUrl)
  254. }
  255. func tapMoreHeader(sender: Any) {
  256. }
  257. func tapMoreListItem(with objectId: String, namedButtonMore: String, sender: Any) {
  258. }
  259. func tapMoreGridItem(with objectId: String, namedButtonMore: String, sender: Any) {
  260. }
  261. func tapShareListItem(with objectId: String, sender: Any) {
  262. }
  263. }
  264. // MARK: - Collection View
  265. extension NCSelect: UICollectionViewDelegate {
  266. func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  267. guard let metadata = dataSource?.cellForItemAt(indexPath: indexPath) else { return }
  268. if isEditMode {
  269. if let index = selectocId.firstIndex(of: metadata.ocId) {
  270. selectocId.remove(at: index)
  271. } else {
  272. selectocId.append(metadata.ocId)
  273. }
  274. collectionView.reloadItems(at: [indexPath])
  275. return
  276. }
  277. if metadata.directory {
  278. guard let serverUrlPush = CCUtility.stringAppendServerUrl(metadata.serverUrl, addFileName: metadata.fileName) else { return }
  279. guard let visualController = UIStoryboard(name: "NCSelect", bundle: nil).instantiateViewController(withIdentifier: "NCSelect.storyboard") as? NCSelect else { return }
  280. self.serverUrlPush = serverUrlPush
  281. self.metadataPush = metadata
  282. visualController.delegate = delegate
  283. visualController.hideButtonCreateFolder = hideButtonCreateFolder
  284. visualController.selectFile = selectFile
  285. visualController.includeDirectoryE2EEncryption = includeDirectoryE2EEncryption
  286. visualController.includeImages = includeImages
  287. visualController.type = type
  288. visualController.titleButtonDone = titleButtonDone
  289. visualController.titleButtonDone1 = titleButtonDone1
  290. visualController.keyLayout = keyLayout
  291. visualController.isButtonDone1Hide = isButtonDone1Hide
  292. visualController.isOverwriteHide = isOverwriteHide
  293. visualController.overwrite = overwrite
  294. visualController.heightToolBarTop = heightToolBarTop
  295. visualController.titleCurrentFolder = metadataPush!.fileNameView
  296. visualController.serverUrl = serverUrlPush
  297. self.navigationController?.pushViewController(visualController, animated: true)
  298. } else {
  299. delegate?.dismissSelect(serverUrl: serverUrl, metadata: metadata, type: type, array: array, buttonType: "select", overwrite: overwrite)
  300. self.dismiss(animated: true, completion: nil)
  301. }
  302. }
  303. }
  304. extension NCSelect: UICollectionViewDataSource {
  305. func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
  306. if (indexPath.section == 0) {
  307. if kind == UICollectionView.elementKindSectionHeader {
  308. let header = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "sectionHeaderMenu", for: indexPath) as! NCSectionHeaderMenu
  309. if collectionView.collectionViewLayout == gridLayout {
  310. header.buttonSwitch.setImage(CCGraphics.changeThemingColorImage(UIImage.init(named: "switchList"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), for: .normal)
  311. } else {
  312. header.buttonSwitch.setImage(CCGraphics.changeThemingColorImage(UIImage.init(named: "switchGrid"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), for: .normal)
  313. }
  314. header.delegate = self
  315. header.setStatusButton(count: dataSource?.metadatas.count ?? 0)
  316. header.setTitleSorted(datasourceTitleButton: titleButton)
  317. if groupBy == "none" {
  318. header.labelSection.isHidden = true
  319. header.labelSectionHeightConstraint.constant = 0
  320. } else {
  321. header.labelSection.isHidden = false
  322. header.setTitleLabel(title: "")
  323. header.labelSectionHeightConstraint.constant = sectionHeaderHeight
  324. }
  325. return header
  326. } else {
  327. let footer = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "sectionFooter", for: indexPath) as! NCSectionFooter
  328. let info = dataSource?.getFilesInformation()
  329. footer.setTitleLabel(directories: info?.directories ?? 0, files: info?.files ?? 0, size: info?.size ?? 0)
  330. return footer
  331. }
  332. } else {
  333. if kind == UICollectionView.elementKindSectionHeader {
  334. let header = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "sectionHeader", for: indexPath) as! NCSectionHeader
  335. header.setTitleLabel(title: "")
  336. return header
  337. } else {
  338. let footer = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "sectionFooter", for: indexPath) as! NCSectionFooter
  339. let info = dataSource?.getFilesInformation()
  340. footer.setTitleLabel(directories: info?.directories ?? 0, files: info?.files ?? 0, size: info?.size ?? 0)
  341. return footer
  342. }
  343. }
  344. }
  345. func numberOfSections(in collectionView: UICollectionView) -> Int {
  346. return dataSource?.sections ?? 1
  347. }
  348. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  349. return dataSource?.numberOfItemsInSection(section: section) ?? 1
  350. }
  351. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  352. let cell: UICollectionViewCell
  353. guard let metadata = dataSource?.cellForItemAt(indexPath: indexPath) else {
  354. return collectionView.dequeueReusableCell(withReuseIdentifier: "listCell", for: indexPath) as! NCListCell
  355. }
  356. if layout == k_layout_grid {
  357. cell = collectionView.dequeueReusableCell(withReuseIdentifier: "gridCell", for: indexPath) as! NCGridCell
  358. } else {
  359. cell = collectionView.dequeueReusableCell(withReuseIdentifier: "listCell", for: indexPath) as! NCListCell
  360. }
  361. NCCollectionCommon.shared.cellForItemAt(indexPath: indexPath, collectionView: collectionView, cell: cell, metadata: metadata, metadataFolder: metadataFolder, serverUrl: serverUrl, isEditMode: isEditMode, selectocId: selectocId, autoUploadFileName: autoUploadFileName, autoUploadDirectory: autoUploadDirectory ,hideButtonMore: true, downloadThumbnail: true, shares: shares, source: self)
  362. if layout == k_layout_grid {
  363. let cell = cell as! NCGridCell
  364. cell.buttonMore.isHidden = true
  365. return cell
  366. } else {
  367. let cell = cell as! NCListCell
  368. cell.imageMore.isHidden = true
  369. cell.sharedLeftConstraint.constant = 15
  370. return cell
  371. }
  372. }
  373. }
  374. extension NCSelect: UICollectionViewDelegateFlowLayout {
  375. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
  376. if section == 0 {
  377. if groupBy == "none" {
  378. return CGSize(width: collectionView.frame.width, height: headerMenuHeight)
  379. } else {
  380. return CGSize(width: collectionView.frame.width, height: headerMenuHeight + sectionHeaderHeight)
  381. }
  382. } else {
  383. return CGSize(width: collectionView.frame.width, height: sectionHeaderHeight)
  384. }
  385. }
  386. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize {
  387. let sections = dataSource?.sections ?? 1
  388. if (section == sections - 1) {
  389. return CGSize(width: collectionView.frame.width, height: footerHeight)
  390. } else {
  391. return CGSize(width: collectionView.frame.width, height: 0)
  392. }
  393. }
  394. }
  395. // MARK: - NC API & Algorithm
  396. extension NCSelect {
  397. @objc func reloadDataSource() {
  398. loadDatasource(withLoadFolder: false)
  399. }
  400. @objc func loadDatasource(withLoadFolder: Bool) {
  401. var predicate: NSPredicate?
  402. var sort: String
  403. var ascending: Bool
  404. var directoryOnTop: Bool
  405. (layout, sort, ascending, groupBy, directoryOnTop, titleButton, itemForLine) = NCUtility.shared.getLayoutForView(key: keyLayout)
  406. if serverUrl == "" {
  407. serverUrl = NCUtility.shared.getHomeServer(urlBase: appDelegate.urlBase, account: appDelegate.account)
  408. }
  409. if includeDirectoryE2EEncryption {
  410. if includeImages {
  411. predicate = NSPredicate(format: "account == %@ AND serverUrl == %@ AND (directory == true OR typeFile == 'image')", appDelegate.account, serverUrl)
  412. } else {
  413. predicate = NSPredicate(format: "account == %@ AND serverUrl == %@ AND directory == true", appDelegate.account, serverUrl)
  414. }
  415. } else {
  416. if includeImages {
  417. predicate = NSPredicate(format: "account == %@ AND serverUrl == %@ AND e2eEncrypted == false AND (directory == true OR typeFile == 'image')", appDelegate.account, serverUrl)
  418. } else {
  419. predicate = NSPredicate(format: "account == %@ AND serverUrl == %@ AND e2eEncrypted == false AND directory == true", appDelegate.account, serverUrl)
  420. }
  421. }
  422. let metadatasSource = NCManageDatabase.sharedInstance.getMetadatas(predicate: predicate!)
  423. self.dataSource = NCDataSource.init(metadatasSource: metadatasSource, sort: sort, ascending: ascending, directoryOnTop: directoryOnTop, filterLivePhoto: true)
  424. if withLoadFolder {
  425. loadFolder()
  426. } else {
  427. self.refreshControl.endRefreshing()
  428. }
  429. collectionView.reloadData()
  430. }
  431. func createFolder(with fileName: String) {
  432. NCNetworking.shared.createFolder(fileName: fileName, serverUrl: serverUrl, account: appDelegate.account, urlBase: appDelegate.urlBase) { (errorCode, errorDescription) in
  433. if errorCode == 0 {
  434. self.loadDatasource(withLoadFolder: true)
  435. } else {
  436. NCContentPresenter.shared.messageNotification("_error_", description: errorDescription, delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
  437. }
  438. }
  439. }
  440. func loadFolder() {
  441. networkInProgress = true
  442. collectionView.reloadData()
  443. NCNetworking.shared.readFolder(serverUrl: serverUrl, account: appDelegate.account) { (_, _, _, _, _, _, _) in
  444. self.networkInProgress = false
  445. self.loadDatasource(withLoadFolder: false)
  446. }
  447. }
  448. }