NCGroupfolders.swift 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. headerMenuButtonsCommand = false
  33. headerMenuButtonsView = true
  34. headerRichWorkspaceDisable = true
  35. emptyImage = UIImage(named: "folder_group")?.image(color: NCBrandColor.shared.brandElement, size: UIScreen.main.bounds.width)
  36. emptyTitle = "_files_no_files_"
  37. emptyDescription = "_tutorial_groupfolders_view_"
  38. }
  39. override func viewWillAppear(_ animated: Bool) {
  40. super.viewWillAppear(animated)
  41. navigationController?.setFileAppreance()
  42. NotificationCenter.default.addObserver(self, selector: #selector(readFile(_:)), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterOperationReadFile), object: nil)
  43. }
  44. override func viewWillDisappear(_ animated: Bool) {
  45. super.viewWillDisappear(animated)
  46. NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterOperationReadFile), object: nil)
  47. }
  48. // MARK: - NotificationCenter
  49. @objc func readFile(_ notification: NSNotification) {
  50. guard let userInfo = notification.userInfo as NSDictionary?,
  51. let ocId = userInfo["ocId"] as? String,
  52. let metadata = NCManageDatabase.shared.getMetadataFromOcId(ocId)
  53. else {
  54. return
  55. }
  56. dataSource.addMetadata(metadata)
  57. self.collectionView?.reloadData()
  58. }
  59. // MARK: - DataSource + NC Endpoint
  60. override func reloadDataSource(forced: Bool = true) {
  61. super.reloadDataSource()
  62. DispatchQueue.global().async {
  63. var metadatas: [tableMetadata] = []
  64. if self.serverUrl.isEmpty {
  65. metadatas = NCManageDatabase.shared.getMetadatasFromGroupfolders(account: self.appDelegate.account, urlBase: self.appDelegate.urlBase, userId: self.appDelegate.userId)
  66. } else {
  67. metadatas = NCManageDatabase.shared.getMetadatas(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@", self.appDelegate.account, self.serverUrl))
  68. }
  69. self.dataSource = NCDataSource(
  70. metadatas: metadatas,
  71. account: self.appDelegate.account,
  72. sort: self.layoutForView?.sort,
  73. ascending: self.layoutForView?.ascending,
  74. directoryOnTop: self.layoutForView?.directoryOnTop,
  75. favoriteOnTop: true,
  76. filterLivePhoto: true,
  77. groupByField: self.groupByField,
  78. providers: self.providers,
  79. searchResults: self.searchResults)
  80. DispatchQueue.main.async {
  81. self.refreshControl.endRefreshing()
  82. self.collectionView.reloadData()
  83. }
  84. }
  85. }
  86. override func reloadDataSourceNetwork(forced: Bool = false) {
  87. super.reloadDataSourceNetwork(forced: forced)
  88. isReloadDataSourceNetworkInProgress = true
  89. collectionView?.reloadData()
  90. if serverUrl.isEmpty {
  91. let homeServerUrl = NCUtilityFileSystem.shared.getHomeServer(urlBase: self.appDelegate.urlBase, userId: self.appDelegate.userId)
  92. let options = NKRequestOptions(queue: NextcloudKit.shared.nkCommonInstance.backgroundQueue)
  93. NextcloudKit.shared.getGroupfolders(options: options) { account, results, data, error in
  94. DispatchQueue.main.async {
  95. self.refreshControl.endRefreshing()
  96. self.isReloadDataSourceNetworkInProgress = false
  97. }
  98. if error == .success, let groupfolders = results {
  99. NCManageDatabase.shared.addGroupfolders(account: account, groupfolders: groupfolders)
  100. for groupfolder in groupfolders {
  101. let serverUrlFileName = homeServerUrl + "/" + groupfolder.mountPoint
  102. if NCManageDatabase.shared.getMetadataFromDirectory(account: self.appDelegate.account, serverUrl: serverUrlFileName) == nil {
  103. NCOperationQueue.shared.readFile(serverUrlFileName: serverUrlFileName)
  104. }
  105. }
  106. } else if error != .success {
  107. NCContentPresenter.shared.showError(error: error, data: data)
  108. }
  109. self.reloadDataSource()
  110. }
  111. } else {
  112. networkReadFolder(forced: forced) { _, _, metadatasUpdate, metadatasDelete, _ in
  113. DispatchQueue.main.async {
  114. self.refreshControl.endRefreshing()
  115. self.isReloadDataSourceNetworkInProgress = false
  116. if !(metadatasUpdate?.isEmpty ?? true) || !(metadatasDelete?.isEmpty ?? true) || forced {
  117. self.reloadDataSource()
  118. } else {
  119. self.collectionView?.reloadData()
  120. }
  121. }
  122. }
  123. }
  124. }
  125. }