NCCollectionViewCommon+SelectTabBarDelegate.swift 8.3 KB

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