NCGroupfolders.swift 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. class NCGroupfolders: NCCollectionViewCommon {
  26. // MARK: - View Life Cycle
  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. emptyImage = UIImage(named: "folder_group")?.image(color: NCBrandColor.shared.brandElement, size: UIScreen.main.bounds.width)
  34. emptyTitle = "_files_no_files_"
  35. emptyDescription = "_tutorial_groupfolders_view_"
  36. }
  37. override func viewWillAppear(_ animated: Bool) {
  38. super.viewWillAppear(animated)
  39. if dataSource.metadatas.isEmpty {
  40. reloadDataSource()
  41. }
  42. reloadDataSourceNetwork()
  43. }
  44. // MARK: - DataSource + NC Endpoint
  45. override func queryDB() {
  46. super.queryDB()
  47. var metadatas: [tableMetadata] = []
  48. if self.serverUrl.isEmpty {
  49. metadatas = NCManageDatabase.shared.getMetadatasFromGroupfolders(account: self.appDelegate.account, urlBase: self.appDelegate.urlBase, userId: self.appDelegate.userId)
  50. } else {
  51. metadatas = NCManageDatabase.shared.getMetadatas(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@", self.appDelegate.account, self.serverUrl))
  52. }
  53. self.dataSource = NCDataSource(
  54. metadatas: metadatas,
  55. account: self.appDelegate.account,
  56. sort: self.layoutForView?.sort,
  57. ascending: self.layoutForView?.ascending,
  58. directoryOnTop: self.layoutForView?.directoryOnTop,
  59. favoriteOnTop: true,
  60. groupByField: self.groupByField,
  61. providers: self.providers,
  62. searchResults: self.searchResults)
  63. }
  64. override func reloadDataSourceNetwork() {
  65. super.reloadDataSourceNetwork()
  66. let homeServerUrl = utilityFileSystem.getHomeServer(urlBase: self.appDelegate.urlBase, userId: self.appDelegate.userId)
  67. NextcloudKit.shared.getGroupfolders(options: NKRequestOptions(queue: NextcloudKit.shared.nkCommonInstance.backgroundQueue)) { account, results, _, error in
  68. if error == .success, let groupfolders = results {
  69. NCManageDatabase.shared.addGroupfolders(account: account, groupfolders: groupfolders)
  70. Task {
  71. for groupfolder in groupfolders {
  72. let mountPoint = groupfolder.mountPoint.hasPrefix("/") ? groupfolder.mountPoint : "/" + groupfolder.mountPoint
  73. let serverUrlFileName = homeServerUrl + mountPoint
  74. if NCManageDatabase.shared.getMetadataFromDirectory(account: self.appDelegate.account, serverUrl: serverUrlFileName) == nil {
  75. let results = await NextcloudKit.shared.readFileOrFolder(serverUrlFileName: serverUrlFileName, depth: "0", showHiddenFiles: NCKeychain().showHiddenFiles)
  76. if results.error == .success, let file = results.files.first {
  77. let isDirectoryE2EE = self.utilityFileSystem.isDirectoryE2EE(file: file)
  78. let metadata = NCManageDatabase.shared.convertFileToMetadata(file, isDirectoryE2EE: isDirectoryE2EE)
  79. NCManageDatabase.shared.addMetadata(metadata)
  80. NCManageDatabase.shared.addDirectory(encrypted: isDirectoryE2EE, favorite: metadata.favorite, ocId: metadata.ocId, fileId: metadata.fileId, etag: nil, permissions: metadata.permissions, serverUrl: serverUrlFileName, account: metadata.account)
  81. }
  82. }
  83. }
  84. self.reloadDataSource()
  85. }
  86. } else {
  87. self.reloadDataSource(withQueryDB: false)
  88. }
  89. }
  90. }
  91. }