NCMedia+Menu.swift 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. //
  2. // NCMedia+Menu.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 03/03/2021.
  6. // Copyright © 2021 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 FloatingPanel
  25. import NextcloudKit
  26. extension NCMedia {
  27. func tapSelect() {
  28. self.isEditMode = false
  29. self.selectOcId.removeAll()
  30. self.reloadDataThenPerform { }
  31. }
  32. func toggleMenu() {
  33. var actions: [NCMenuAction] = []
  34. defer { presentMenu(with: actions) }
  35. if !isEditMode {
  36. if metadatas.count > 0 {
  37. actions.append(
  38. NCMenuAction(
  39. title: NSLocalizedString("_select_", comment: ""),
  40. icon: NCUtility.shared.loadImage(named: "checkmark.circle.fill"),
  41. action: { _ in
  42. self.isEditMode = true
  43. }
  44. )
  45. )
  46. }
  47. actions.append(
  48. NCMenuAction(
  49. title: NSLocalizedString("_media_viewimage_hide_", comment: ""),
  50. icon: NCUtility.shared.loadImage(named: "photo"),
  51. selected: filterClassTypeImage,
  52. on: true,
  53. action: { _ in
  54. self.filterClassTypeImage = !self.filterClassTypeImage
  55. self.filterClassTypeVideo = false
  56. self.reloadDataSourceWithCompletion { _ in }
  57. }
  58. )
  59. )
  60. actions.append(
  61. NCMenuAction(
  62. title: NSLocalizedString("_media_viewvideo_hide_", comment: ""),
  63. icon: NCUtility.shared.loadImage(named: "video"),
  64. selected: filterClassTypeVideo,
  65. on: true,
  66. action: { _ in
  67. self.filterClassTypeVideo = !self.filterClassTypeVideo
  68. self.filterClassTypeImage = false
  69. self.reloadDataSourceWithCompletion { _ in }
  70. }
  71. )
  72. )
  73. actions.append(
  74. NCMenuAction(
  75. title: NSLocalizedString("_select_media_folder_", comment: ""),
  76. icon: NCUtility.shared.loadImage(named: "folder"),
  77. action: { _ in
  78. let navigationController = UIStoryboard(name: "NCSelect", bundle: nil).instantiateInitialViewController() as! UINavigationController
  79. let viewController = navigationController.topViewController as! NCSelect
  80. viewController.delegate = self
  81. viewController.typeOfCommandView = .select
  82. viewController.type = "mediaFolder"
  83. self.present(navigationController, animated: true, completion: nil)
  84. }
  85. )
  86. )
  87. actions.append(
  88. NCMenuAction(
  89. title: NSLocalizedString("_media_by_modified_date_", comment: ""),
  90. icon: NCUtility.shared.loadImage(named: "circle.grid.cross.up.fill"),
  91. selected: CCUtility.getMediaSortDate() == "date",
  92. on: true,
  93. action: { _ in
  94. CCUtility.setMediaSortDate("date")
  95. self.reloadDataSourceWithCompletion { _ in }
  96. }
  97. )
  98. )
  99. actions.append(
  100. NCMenuAction(
  101. title: NSLocalizedString("_media_by_created_date_", comment: ""),
  102. icon: NCUtility.shared.loadImage(named: "circle.grid.cross.down.fill"),
  103. selected: CCUtility.getMediaSortDate() == "creationDate",
  104. on: true,
  105. action: { _ in
  106. CCUtility.setMediaSortDate("creationDate")
  107. self.reloadDataSourceWithCompletion { _ in }
  108. }
  109. )
  110. )
  111. actions.append(
  112. NCMenuAction(
  113. title: NSLocalizedString("_media_by_upload_date_", comment: ""),
  114. icon: NCUtility.shared.loadImage(named: "circle.grid.cross.right.fill"),
  115. selected: CCUtility.getMediaSortDate() == "uploadDate",
  116. on: true,
  117. action: { _ in
  118. CCUtility.setMediaSortDate("uploadDate")
  119. self.reloadDataSourceWithCompletion { _ in }
  120. }
  121. )
  122. )
  123. } else {
  124. //
  125. // CANCEL
  126. //
  127. actions.append(
  128. NCMenuAction(
  129. title: NSLocalizedString("_cancel_", comment: ""),
  130. icon: NCUtility.shared.loadImage(named: "xmark"),
  131. action: { _ in self.tapSelect() }
  132. )
  133. )
  134. guard !selectOcId.isEmpty else { return }
  135. let selectedMetadatas = selectOcId.compactMap(NCManageDatabase.shared.getMetadataFromOcId)
  136. //
  137. // OPEN IN
  138. //
  139. actions.append(.openInAction(selectedMetadatas: selectedMetadatas, viewController: self, completion: tapSelect))
  140. //
  141. // SAVE TO PHOTO GALLERY
  142. //
  143. actions.append(.saveMediaAction(selectedMediaMetadatas: selectedMetadatas, completion: tapSelect))
  144. //
  145. // COPY - MOVE
  146. //
  147. actions.append(.moveOrCopyAction(selectedMetadatas: selectedMetadatas, completion: tapSelect))
  148. //
  149. // COPY
  150. //
  151. actions.append(.copyAction(selectOcId: selectOcId, hudView: self.view, completion: tapSelect))
  152. //
  153. // DELETE
  154. // can't delete from cache because is needed for NCMedia view, and if locked can't delete from server either.
  155. if !selectedMetadatas.contains(where: { $0.lock && $0.lockOwner != appDelegate.userId }) {
  156. actions.append(.deleteAction(selectedMetadatas: selectedMetadatas, metadataFolder: nil, viewController: self, completion: tapSelect))
  157. }
  158. }
  159. }
  160. }