NCGroupfolders.swift 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. //
  2. // NCGroupfolders.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 14/04/2023.
  6. // Copyright © 2023 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 NextcloudKit
  25. import RealmSwift
  26. class NCGroupfolders: NCCollectionViewCommon {
  27. required init?(coder aDecoder: NSCoder) {
  28. super.init(coder: aDecoder)
  29. titleCurrentFolder = NSLocalizedString("_group_folders_", comment: "")
  30. layoutKey = NCGlobal.shared.layoutViewGroupfolders
  31. enableSearchBar = false
  32. headerRichWorkspaceDisable = true
  33. emptyImageName = "folder_group"
  34. emptyTitle = "_files_no_files_"
  35. emptyDescription = "_tutorial_groupfolders_view_"
  36. }
  37. // MARK: - View Life Cycle
  38. override func viewWillAppear(_ animated: Bool) {
  39. super.viewWillAppear(animated)
  40. reloadDataSource()
  41. }
  42. override func viewDidAppear(_ animated: Bool) {
  43. super.viewDidAppear(animated)
  44. getServerData()
  45. }
  46. // MARK: - DataSource
  47. override func reloadDataSource() {
  48. var metadatas: [tableMetadata] = []
  49. if self.serverUrl.isEmpty {
  50. if let results = database.getResultsMetadatasFromGroupfolders(session: session) {
  51. metadatas = Array(results.freeze())
  52. }
  53. } else {
  54. metadatas = self.database.getResultsMetadatasPredicate(self.defaultPredicate, layoutForView: layoutForView)
  55. }
  56. self.dataSource = NCCollectionViewDataSource(metadatas: metadatas, layoutForView: layoutForView)
  57. super.reloadDataSource()
  58. }
  59. override func getServerData() {
  60. let homeServerUrl = utilityFileSystem.getHomeServer(session: session)
  61. NextcloudKit.shared.getGroupfolders(account: session.account) { task in
  62. self.dataSourceTask = task
  63. if self.dataSource.isEmpty() {
  64. self.collectionView.reloadData()
  65. }
  66. } completion: { account, results, _, error in
  67. if error == .success, let groupfolders = results {
  68. self.database.addGroupfolders(account: account, groupfolders: groupfolders)
  69. Task {
  70. for groupfolder in groupfolders {
  71. let mountPoint = groupfolder.mountPoint.hasPrefix("/") ? groupfolder.mountPoint : "/" + groupfolder.mountPoint
  72. let serverUrlFileName = homeServerUrl + mountPoint
  73. let results = await NCNetworking.shared.readFileOrFolder(serverUrlFileName: serverUrlFileName, depth: "0", showHiddenFiles: NCKeychain().showHiddenFiles, account: account)
  74. if results.error == .success, let file = results.files?.first {
  75. let isDirectoryE2EE = self.utilityFileSystem.isDirectoryE2EE(file: file)
  76. let metadata = self.database.convertFileToMetadata(file, isDirectoryE2EE: isDirectoryE2EE)
  77. self.database.addMetadata(metadata)
  78. self.database.addDirectory(e2eEncrypted: isDirectoryE2EE, favorite: metadata.favorite, ocId: metadata.ocId, fileId: metadata.fileId, permissions: metadata.permissions, serverUrl: serverUrlFileName, account: metadata.account)
  79. }
  80. }
  81. self.reloadDataSource()
  82. }
  83. }
  84. self.refreshControl.endRefreshing()
  85. }
  86. }
  87. }