NCTrash+Menu.swift 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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("_trash_restore_selected_", comment: ""),
  33. icon: NCUtility.shared.loadImage(named: "restore"),
  34. action: { _ in
  35. self.selectOcId.forEach(self.restoreItem)
  36. self.tapSelect()
  37. }
  38. ),
  39. NCMenuAction(
  40. title: NSLocalizedString("_trash_delete_selected_", comment: ""),
  41. icon: NCUtility.shared.loadImage(named: "trash"),
  42. action: { _ in
  43. let alert = UIAlertController(title: NSLocalizedString("_trash_delete_selected_", comment: ""), message: "", preferredStyle: .alert)
  44. alert.addAction(UIAlertAction(title: NSLocalizedString("_delete_", comment: ""), style: .destructive, handler: { _ in
  45. self.selectOcId.forEach(self.deleteItem)
  46. self.tapSelect()
  47. }))
  48. alert.addAction(UIAlertAction(title: NSLocalizedString("_cancel_", comment: ""), style: .cancel, handler: { _ in }))
  49. self.present(alert, animated: true, completion: nil)
  50. }
  51. )
  52. ]
  53. }
  54. func toggleMenuMoreHeader() {
  55. var actions: [NCMenuAction] = []
  56. actions.append(
  57. NCMenuAction(
  58. title: NSLocalizedString("_trash_restore_all_", comment: ""),
  59. icon: NCUtility.shared.loadImage(named: "restore"),
  60. action: { _ in
  61. self.datasource.forEach({ self.restoreItem(with: $0.fileId) })
  62. }
  63. )
  64. )
  65. actions.append(
  66. NCMenuAction(
  67. title: NSLocalizedString("_trash_delete_all_", comment: ""),
  68. icon: NCUtility.shared.loadImage(named: "trash"),
  69. action: { _ in
  70. let alert = UIAlertController(title: NSLocalizedString("_trash_delete_all_description_", comment: ""), message: "", preferredStyle: .alert)
  71. alert.addAction(UIAlertAction(title: NSLocalizedString("_trash_delete_all_", comment: ""), style: .destructive, handler: { _ in
  72. self.emptyTrash()
  73. }))
  74. alert.addAction(UIAlertAction(title: NSLocalizedString("_cancel_", comment: ""), style: .cancel))
  75. self.present(alert, animated: true, completion: nil)
  76. }
  77. )
  78. )
  79. presentMenu(with: actions)
  80. }
  81. func toggleMenuMore(with objectId: String, image: UIImage?, isGridCell: Bool) {
  82. guard let tableTrash = NCManageDatabase.shared.getTrashItem(fileId: objectId, account: appDelegate.account) else {
  83. return
  84. }
  85. guard isGridCell else {
  86. let alert = UIAlertController(title: NSLocalizedString("_want_delete_", comment: ""), message: tableTrash.trashbinFileName, preferredStyle: .alert)
  87. alert.addAction(UIAlertAction(title: NSLocalizedString("_delete_", comment: ""), style: .destructive, handler: { _ in
  88. self.deleteItem(with: objectId)
  89. }))
  90. alert.addAction(UIAlertAction(title: NSLocalizedString("_cancel_", comment: ""), style: .cancel))
  91. self.present(alert, animated: true, completion: nil)
  92. return
  93. }
  94. var actions: [NCMenuAction] = []
  95. var iconHeader: UIImage!
  96. if let icon = UIImage(contentsOfFile: CCUtility.getDirectoryProviderStorageIconOcId(tableTrash.fileId, etag: tableTrash.fileName)) {
  97. iconHeader = icon
  98. } else {
  99. if tableTrash.directory {
  100. iconHeader = UIImage(named: "folder")!.image(color: UIColor.systemGray, size: 50)
  101. } else {
  102. iconHeader = UIImage(named: tableTrash.iconName)
  103. }
  104. }
  105. actions.append(
  106. NCMenuAction(
  107. title: tableTrash.trashbinFileName,
  108. icon: iconHeader,
  109. action: nil
  110. )
  111. )
  112. actions.append(
  113. NCMenuAction(
  114. title: NSLocalizedString("_restore_", comment: ""),
  115. icon: UIImage(named: "restore")!.image(color: UIColor.systemGray, size: 50),
  116. action: { _ in
  117. self.restoreItem(with: objectId)
  118. }
  119. )
  120. )
  121. actions.append(
  122. NCMenuAction(
  123. title: NSLocalizedString("_delete_", comment: ""),
  124. icon: NCUtility.shared.loadImage(named: "trash"),
  125. action: { _ in
  126. self.deleteItem(with: objectId)
  127. }
  128. )
  129. )
  130. presentMenu(with: actions)
  131. }
  132. }