NCCollectionViewCommon+SelectTabBar.swift 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. //
  2. // NCCollectionViewCommon+SelectTabBar.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 selectAll() {
  28. if !selectOcId.isEmpty, dataSource.getMetadataSourceForAllSections().count == selectOcId.count {
  29. selectOcId = []
  30. } else {
  31. selectOcId = dataSource.getMetadataSourceForAllSections().compactMap({ $0.ocId })
  32. }
  33. tabBarSelect.update(selectOcId: selectOcId, metadatas: getSelectedMetadatas(), userId: appDelegate.userId)
  34. collectionView.reloadData()
  35. }
  36. func delete() {
  37. var alertStyle = UIAlertController.Style.actionSheet
  38. if UIDevice.current.userInterfaceIdiom == .pad { alertStyle = .alert }
  39. let alertController = UIAlertController(title: NSLocalizedString("_confirm_delete_selected_", comment: ""), message: nil, preferredStyle: alertStyle)
  40. let metadatas = getSelectedMetadatas()
  41. let canDeleteServer = metadatas.allSatisfy { !$0.lock }
  42. if canDeleteServer {
  43. let copyMetadatas = metadatas
  44. alertController.addAction(UIAlertAction(title: NSLocalizedString("_yes_", comment: ""), style: .destructive) { _ in
  45. Task {
  46. var error = NKError()
  47. var ocId: [String] = []
  48. for metadata in copyMetadatas where error == .success {
  49. error = await NCNetworking.shared.deleteMetadata(metadata, onlyLocalCache: false)
  50. if error == .success {
  51. ocId.append(metadata.ocId)
  52. }
  53. }
  54. NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterDeleteFile, userInfo: ["ocId": ocId, "onlyLocalCache": false, "error": error])
  55. }
  56. self.setEditMode(false)
  57. })
  58. }
  59. alertController.addAction(UIAlertAction(title: NSLocalizedString("_remove_local_file_", comment: ""), style: .default) { (_: UIAlertAction) in
  60. let copyMetadatas = metadatas
  61. Task {
  62. var error = NKError()
  63. var ocId: [String] = []
  64. for metadata in copyMetadatas where error == .success {
  65. error = await NCNetworking.shared.deleteMetadata(metadata, onlyLocalCache: true)
  66. if error == .success {
  67. ocId.append(metadata.ocId)
  68. }
  69. }
  70. if error != .success {
  71. NCContentPresenter().showError(error: error)
  72. }
  73. NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterDeleteFile, userInfo: ["ocId": ocId, "onlyLocalCache": true, "error": error])
  74. self.setEditMode(false)
  75. }
  76. })
  77. alertController.addAction(UIAlertAction(title: NSLocalizedString("_cancel_", comment: ""), style: .cancel) { (_: UIAlertAction) in })
  78. self.present(alertController, animated: true, completion: nil)
  79. }
  80. func move() {
  81. let metadatas = getSelectedMetadatas()
  82. NCActionCenter.shared.openSelectView(items: metadatas, controller: self.tabBarController as? NCMainTabBarController)
  83. setEditMode(false)
  84. }
  85. func share() {
  86. let metadatas = getSelectedMetadatas()
  87. NCActionCenter.shared.openActivityViewController(selectedMetadata: metadatas, controller: self.tabBarController as? NCMainTabBarController)
  88. setEditMode(false)
  89. }
  90. func saveAsAvailableOffline(isAnyOffline: Bool) {
  91. let metadatas = getSelectedMetadatas()
  92. if !isAnyOffline, metadatas.count > 3 {
  93. let alert = UIAlertController(
  94. title: NSLocalizedString("_set_available_offline_", comment: ""),
  95. message: NSLocalizedString("_select_offline_warning_", comment: ""),
  96. preferredStyle: .alert)
  97. alert.addAction(UIAlertAction(title: NSLocalizedString("_continue_", comment: ""), style: .default, handler: { _ in
  98. metadatas.forEach { NCActionCenter.shared.setMetadataAvalableOffline($0, isOffline: isAnyOffline) }
  99. self.setEditMode(false)
  100. }))
  101. alert.addAction(UIAlertAction(title: NSLocalizedString("_cancel_", comment: ""), style: .cancel))
  102. self.present(alert, animated: true)
  103. } else {
  104. metadatas.forEach { NCActionCenter.shared.setMetadataAvalableOffline($0, isOffline: isAnyOffline) }
  105. setEditMode(false)
  106. }
  107. }
  108. func lock(isAnyLocked: Bool) {
  109. let metadatas = getSelectedMetadatas()
  110. for metadata in metadatas where metadata.lock == isAnyLocked {
  111. NCNetworking.shared.lockUnlockFile(metadata, shoulLock: !isAnyLocked)
  112. }
  113. setEditMode(false)
  114. }
  115. func getSelectedMetadatas() -> [tableMetadata] {
  116. var selectedMetadatas: [tableMetadata] = []
  117. for ocId in selectOcId {
  118. guard let metadata = NCManageDatabase.shared.getMetadataFromOcId(ocId) else { continue }
  119. selectedMetadatas.append(metadata)
  120. }
  121. return selectedMetadatas
  122. }
  123. func setEditMode(_ editMode: Bool) {
  124. isEditMode = editMode
  125. selectOcId.removeAll()
  126. if editMode {
  127. navigationItem.leftBarButtonItems = nil
  128. } else {
  129. setNavigationLeftItems()
  130. }
  131. setNavigationRightItems()
  132. navigationController?.interactivePopGestureRecognizer?.isEnabled = !editMode
  133. navigationItem.hidesBackButton = editMode
  134. searchController(enabled: !editMode)
  135. collectionView.reloadData()
  136. }
  137. }