NCSelect.swift 26 KB

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