NCCollectionViewCommon+SelectTabBar.swift 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 !fileSelect.isEmpty, self.dataSource.getMetadatas().count == fileSelect.count {
  29. fileSelect = []
  30. } else {
  31. fileSelect = self.dataSource.getMetadatas().compactMap({ $0.ocId })
  32. }
  33. tabBarSelect.update(fileSelect: fileSelect, metadatas: getSelectedMetadatas(), userId: session.userId)
  34. self.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. alertController.addAction(UIAlertAction(title: NSLocalizedString("_yes_", comment: ""), style: .destructive) { _ in
  44. NCNetworking.shared.deleteMetadatas(metadatas, sceneIdentifier: self.controller?.sceneIdentifier)
  45. NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterReloadDataSource)
  46. self.setEditMode(false)
  47. })
  48. }
  49. alertController.addAction(UIAlertAction(title: NSLocalizedString("_remove_local_file_", comment: ""), style: .default) { (_: UIAlertAction) in
  50. let copyMetadatas = metadatas
  51. Task {
  52. var error = NKError()
  53. var ocId: [String] = []
  54. for metadata in copyMetadatas where error == .success {
  55. error = await NCNetworking.shared.deleteCache(metadata, sceneIdentifier: self.controller?.sceneIdentifier)
  56. if error == .success {
  57. ocId.append(metadata.ocId)
  58. }
  59. }
  60. NotificationCenter.default.postOnMainThread(name: self.global.notificationCenterDeleteFile, userInfo: ["ocId": ocId, "error": error])
  61. }
  62. self.setEditMode(false)
  63. })
  64. alertController.addAction(UIAlertAction(title: NSLocalizedString("_cancel_", comment: ""), style: .cancel) { (_: UIAlertAction) in })
  65. self.present(alertController, animated: true, completion: nil)
  66. }
  67. func move() {
  68. let metadatas = getSelectedMetadatas()
  69. NCActionCenter.shared.openSelectView(items: metadatas, controller: self.controller)
  70. setEditMode(false)
  71. }
  72. func share() {
  73. let metadatas = getSelectedMetadatas()
  74. NCActionCenter.shared.openActivityViewController(selectedMetadata: metadatas, controller: self.controller)
  75. setEditMode(false)
  76. }
  77. func saveAsAvailableOffline(isAnyOffline: Bool) {
  78. let metadatas = getSelectedMetadatas()
  79. if !isAnyOffline, metadatas.count > 3 {
  80. let alert = UIAlertController(
  81. title: NSLocalizedString("_set_available_offline_", comment: ""),
  82. message: NSLocalizedString("_select_offline_warning_", comment: ""),
  83. preferredStyle: .alert)
  84. alert.addAction(UIAlertAction(title: NSLocalizedString("_continue_", comment: ""), style: .default, handler: { _ in
  85. metadatas.forEach { NCActionCenter.shared.setMetadataAvalableOffline($0, isOffline: isAnyOffline) }
  86. self.setEditMode(false)
  87. }))
  88. alert.addAction(UIAlertAction(title: NSLocalizedString("_cancel_", comment: ""), style: .cancel))
  89. self.present(alert, animated: true)
  90. } else {
  91. metadatas.forEach { NCActionCenter.shared.setMetadataAvalableOffline($0, isOffline: isAnyOffline) }
  92. setEditMode(false)
  93. }
  94. }
  95. func lock(isAnyLocked: Bool) {
  96. let metadatas = getSelectedMetadatas()
  97. for metadata in metadatas where metadata.lock == isAnyLocked {
  98. NCNetworking.shared.lockUnlockFile(metadata, shoulLock: !isAnyLocked)
  99. }
  100. setEditMode(false)
  101. }
  102. func getSelectedMetadatas() -> [tableMetadata] {
  103. var selectedMetadatas: [tableMetadata] = []
  104. for ocId in fileSelect {
  105. guard let metadata = database.getMetadataFromOcId(ocId) else { continue }
  106. selectedMetadatas.append(metadata)
  107. }
  108. return selectedMetadatas
  109. }
  110. func setEditMode(_ editMode: Bool) {
  111. isEditMode = editMode
  112. fileSelect.removeAll()
  113. if editMode {
  114. navigationItem.leftBarButtonItems = nil
  115. } else {
  116. setNavigationLeftItems()
  117. }
  118. setNavigationRightItems()
  119. navigationController?.interactivePopGestureRecognizer?.isEnabled = !editMode
  120. navigationItem.hidesBackButton = editMode
  121. searchController(enabled: !editMode)
  122. self.collectionView.reloadData()
  123. }
  124. }