NCTrash+Menu.swift 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. //
  2. // NCTrash+Menu.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 03/03/2021.
  6. // Copyright © 2021 Marino Faggiana. All rights reserved.
  7. // Copyright © 2022 Henrik Storch. All rights reserved.
  8. //
  9. // Author Marino Faggiana <marino.faggiana@nextcloud.com>
  10. // Author Henrik Storch <henrik.storch@nextcloud.com>
  11. //
  12. // This program is free software: you can redistribute it and/or modify
  13. // it under the terms of the GNU General Public License as published by
  14. // the Free Software Foundation, either version 3 of the License, or
  15. // (at your option) any later version.
  16. //
  17. // This program is distributed in the hope that it will be useful,
  18. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. // GNU General Public License for more details.
  21. //
  22. // You should have received a copy of the GNU General Public License
  23. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  24. //
  25. import UIKit
  26. import FloatingPanel
  27. import NextcloudKit
  28. extension NCTrash {
  29. var selectActions: [NCMenuAction] {
  30. [
  31. NCMenuAction(
  32. title: NSLocalizedString("_cancel_", comment: ""),
  33. icon: NCUtility.shared.loadImage(named: "xmark"),
  34. action: { _ in
  35. self.tapSelect()
  36. }
  37. ),
  38. NCMenuAction(
  39. title: NSLocalizedString("_select_all_", comment: ""),
  40. icon: NCUtility.shared.loadImage(named: "checkmark.circle.fill"),
  41. action: { _ in
  42. self.selectOcId = self.datasource.map { $0.fileId }
  43. self.collectionView.reloadData()
  44. }
  45. ),
  46. NCMenuAction.seperator(),
  47. NCMenuAction(
  48. title: NSLocalizedString("_trash_restore_selected_", comment: ""),
  49. icon: NCUtility.shared.loadImage(named: "restore"),
  50. action: { _ in
  51. self.selectOcId.forEach(self.restoreItem)
  52. self.tapSelect()
  53. }
  54. ),
  55. NCMenuAction(
  56. title: NSLocalizedString("_trash_delete_selected_", comment: ""),
  57. icon: NCUtility.shared.loadImage(named: "trash"),
  58. action: { _ in
  59. let alert = UIAlertController(title: NSLocalizedString("_trash_delete_selected_", comment: ""), message: "", preferredStyle: .alert)
  60. alert.addAction(UIAlertAction(title: NSLocalizedString("_delete_", comment: ""), style: .destructive, handler: { _ in
  61. self.selectOcId.forEach(self.deleteItem)
  62. self.tapSelect()
  63. }))
  64. alert.addAction(UIAlertAction(title: NSLocalizedString("_cancel_", comment: ""), style: .cancel, handler: { _ in }))
  65. self.present(alert, animated: true, completion: nil)
  66. }
  67. )
  68. ]
  69. }
  70. func toggleMenuMoreHeader() {
  71. var actions: [NCMenuAction] = []
  72. actions.append(
  73. NCMenuAction(
  74. title: NSLocalizedString("_trash_restore_all_", comment: ""),
  75. icon: NCUtility.shared.loadImage(named: "restore"),
  76. action: { _ in
  77. self.datasource.forEach({ self.restoreItem(with: $0.fileId) })
  78. }
  79. )
  80. )
  81. actions.append(
  82. NCMenuAction(
  83. title: NSLocalizedString("_trash_delete_all_", comment: ""),
  84. icon: NCUtility.shared.loadImage(named: "trash"),
  85. action: { _ in
  86. let alert = UIAlertController(title: NSLocalizedString("_trash_delete_all_description_", comment: ""), message: "", preferredStyle: .alert)
  87. alert.addAction(UIAlertAction(title: NSLocalizedString("_trash_delete_all_", comment: ""), style: .destructive, handler: { _ in
  88. self.emptyTrash()
  89. }))
  90. alert.addAction(UIAlertAction(title: NSLocalizedString("_cancel_", comment: ""), style: .cancel))
  91. self.present(alert, animated: true, completion: nil)
  92. }
  93. )
  94. )
  95. presentMenu(with: actions)
  96. }
  97. func toggleMenuMore(with objectId: String, image: UIImage?, isGridCell: Bool) {
  98. guard let tableTrash = NCManageDatabase.shared.getTrashItem(fileId: objectId, account: appDelegate.account) else {
  99. return
  100. }
  101. guard isGridCell else {
  102. let alert = UIAlertController(title: NSLocalizedString("_want_delete_", comment: ""), message: tableTrash.trashbinFileName, preferredStyle: .alert)
  103. alert.addAction(UIAlertAction(title: NSLocalizedString("_delete_", comment: ""), style: .destructive, handler: { _ in
  104. self.deleteItem(with: objectId)
  105. }))
  106. alert.addAction(UIAlertAction(title: NSLocalizedString("_cancel_", comment: ""), style: .cancel))
  107. self.present(alert, animated: true, completion: nil)
  108. return
  109. }
  110. var actions: [NCMenuAction] = []
  111. var iconHeader: UIImage!
  112. if let icon = UIImage(contentsOfFile: CCUtility.getDirectoryProviderStorageIconOcId(tableTrash.fileId, etag: tableTrash.fileName)) {
  113. iconHeader = icon
  114. } else {
  115. if tableTrash.directory {
  116. iconHeader = UIImage(named: "folder")!.image(color: UIColor.systemGray, size: 50)
  117. } else {
  118. iconHeader = UIImage(named: tableTrash.iconName)
  119. }
  120. }
  121. actions.append(
  122. NCMenuAction(
  123. title: tableTrash.trashbinFileName,
  124. icon: iconHeader,
  125. action: nil
  126. )
  127. )
  128. actions.append(
  129. NCMenuAction(
  130. title: NSLocalizedString("_restore_", comment: ""),
  131. icon: UIImage(named: "restore")!.image(color: UIColor.systemGray, size: 50),
  132. action: { _ in
  133. self.restoreItem(with: objectId)
  134. }
  135. )
  136. )
  137. actions.append(
  138. NCMenuAction(
  139. title: NSLocalizedString("_delete_", comment: ""),
  140. icon: NCUtility.shared.loadImage(named: "trash"),
  141. action: { _ in
  142. self.deleteItem(with: objectId)
  143. }
  144. )
  145. )
  146. presentMenu(with: actions)
  147. }
  148. }