NCCollectionViewCommon+SelectTabBarDelegate.swift 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. //
  2. // NCCollectionViewCommon+SelectTabBarDelegate.swift
  3. // Nextcloud
  4. //
  5. // Created by Milen on 01.03.24.
  6. // Copyright © 2024 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 NextcloudKit
  25. extension NCCollectionViewCommon: NCCollectionViewCommonSelectTabBarDelegate {
  26. func onListSelected() {
  27. if layoutForView?.layout == NCGlobal.shared.layoutGrid {
  28. layoutForView?.layout = NCGlobal.shared.layoutList
  29. NCManageDatabase.shared.setLayoutForView(account: appDelegate.account, key: layoutKey, serverUrl: serverUrl, layout: layoutForView?.layout)
  30. self.groupByField = "name"
  31. if self.dataSource.groupByField != self.groupByField {
  32. self.dataSource.changeGroupByField(self.groupByField)
  33. }
  34. self.collectionView.reloadData()
  35. self.collectionView.collectionViewLayout.invalidateLayout()
  36. self.collectionView.setCollectionViewLayout(self.listLayout, animated: true) {_ in self.isTransitioning = false }
  37. }
  38. }
  39. func onGridSelected() {
  40. if layoutForView?.layout == NCGlobal.shared.layoutList {
  41. layoutForView?.layout = NCGlobal.shared.layoutGrid
  42. NCManageDatabase.shared.setLayoutForView(account: appDelegate.account, key: layoutKey, serverUrl: serverUrl, layout: layoutForView?.layout)
  43. if isSearchingMode {
  44. self.groupByField = "name"
  45. } else {
  46. self.groupByField = "classFile"
  47. }
  48. if self.dataSource.groupByField != self.groupByField {
  49. self.dataSource.changeGroupByField(self.groupByField)
  50. }
  51. self.collectionView.reloadData()
  52. self.collectionView.collectionViewLayout.invalidateLayout()
  53. self.collectionView.setCollectionViewLayout(self.gridLayout, animated: true) {_ in self.isTransitioning = false }
  54. }
  55. }
  56. func selectAll() {
  57. if !selectOcId.isEmpty, dataSource.getMetadataSourceForAllSections().count == selectOcId.count {
  58. selectOcId = []
  59. } else {
  60. selectOcId = dataSource.getMetadataSourceForAllSections().compactMap({ $0.ocId })
  61. }
  62. tabBarSelect.update(selectOcId: selectOcId, metadatas: getSelectedMetadatas(), userId: appDelegate.userId)
  63. collectionView.reloadData()
  64. }
  65. func delete() {
  66. var alertStyle = UIAlertController.Style.actionSheet
  67. if UIDevice.current.userInterfaceIdiom == .pad { alertStyle = .alert }
  68. let alertController = UIAlertController(title: NSLocalizedString("_confirm_delete_selected_", comment: ""), message: nil, preferredStyle: alertStyle)
  69. let metadatas = getSelectedMetadatas()
  70. let canDeleteServer = metadatas.allSatisfy { !$0.lock }
  71. if canDeleteServer {
  72. let copyMetadatas = metadatas
  73. alertController.addAction(UIAlertAction(title: NSLocalizedString("_yes_", comment: ""), style: .destructive) { _ in
  74. Task {
  75. var error = NKError()
  76. var ocId: [String] = []
  77. for metadata in copyMetadatas where error == .success {
  78. error = await NCNetworking.shared.deleteMetadata(metadata, onlyLocalCache: false)
  79. if error == .success {
  80. ocId.append(metadata.ocId)
  81. }
  82. }
  83. NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterDeleteFile, userInfo: ["ocId": ocId, "onlyLocalCache": false, "error": error])
  84. }
  85. self.setEditMode(false)
  86. })
  87. }
  88. alertController.addAction(UIAlertAction(title: NSLocalizedString("_remove_local_file_", comment: ""), style: .default) { (_: UIAlertAction) in
  89. let copyMetadatas = metadatas
  90. Task {
  91. var error = NKError()
  92. var ocId: [String] = []
  93. for metadata in copyMetadatas where error == .success {
  94. error = await NCNetworking.shared.deleteMetadata(metadata, onlyLocalCache: true)
  95. if error == .success {
  96. ocId.append(metadata.ocId)
  97. }
  98. }
  99. if error != .success {
  100. NCContentPresenter().showError(error: error)
  101. }
  102. NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterDeleteFile, userInfo: ["ocId": ocId, "onlyLocalCache": true, "error": error])
  103. self.setEditMode(false)
  104. }
  105. })
  106. alertController.addAction(UIAlertAction(title: NSLocalizedString("_cancel_", comment: ""), style: .cancel) { (_: UIAlertAction) in })
  107. self.present(alertController, animated: true, completion: nil)
  108. }
  109. func move() {
  110. let metadatas = getSelectedMetadatas()
  111. NCActionCenter.shared.openSelectView(items: metadatas, mainTabBarController: self.tabBarController as? NCMainTabBarController)
  112. setEditMode(false)
  113. }
  114. func share() {
  115. let metadatas = getSelectedMetadatas()
  116. NCActionCenter.shared.openActivityViewController(selectedMetadata: metadatas, mainTabBarController: self.tabBarController as? NCMainTabBarController)
  117. setEditMode(false)
  118. }
  119. func saveAsAvailableOffline(isAnyOffline: Bool) {
  120. let metadatas = getSelectedMetadatas()
  121. if !isAnyOffline, metadatas.count > 3 {
  122. let alert = UIAlertController(
  123. title: NSLocalizedString("_set_available_offline_", comment: ""),
  124. message: NSLocalizedString("_select_offline_warning_", comment: ""),
  125. preferredStyle: .alert)
  126. alert.addAction(UIAlertAction(title: NSLocalizedString("_continue_", comment: ""), style: .default, handler: { _ in
  127. metadatas.forEach { NCActionCenter.shared.setMetadataAvalableOffline($0, isOffline: isAnyOffline) }
  128. self.setEditMode(false)
  129. }))
  130. alert.addAction(UIAlertAction(title: NSLocalizedString("_cancel_", comment: ""), style: .cancel))
  131. self.present(alert, animated: true)
  132. } else {
  133. metadatas.forEach { NCActionCenter.shared.setMetadataAvalableOffline($0, isOffline: isAnyOffline) }
  134. setEditMode(false)
  135. }
  136. }
  137. func lock(isAnyLocked: Bool) {
  138. let metadatas = getSelectedMetadatas()
  139. for metadata in metadatas where metadata.lock == isAnyLocked {
  140. NCNetworking.shared.lockUnlockFile(metadata, shoulLock: !isAnyLocked)
  141. }
  142. setEditMode(false)
  143. }
  144. func saveLayout(_ layoutForView: NCDBLayoutForView) {
  145. NCManageDatabase.shared.setLayoutForView(layoutForView: layoutForView)
  146. NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterReloadDataSource)
  147. setNavigationRightItems(enableMenu: false)
  148. }
  149. func getSelectedMetadatas() -> [tableMetadata] {
  150. var selectedMetadatas: [tableMetadata] = []
  151. for ocId in selectOcId {
  152. guard let metadata = NCManageDatabase.shared.getMetadataFromOcId(ocId) else { continue }
  153. selectedMetadatas.append(metadata)
  154. }
  155. return selectedMetadatas
  156. }
  157. func setEditMode(_ editMode: Bool) {
  158. isEditMode = editMode
  159. selectOcId.removeAll()
  160. setNavigationRightItems(enableMenu: !editMode)
  161. collectionView.reloadData()
  162. }
  163. }