NCMedia+Menu.swift 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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 NCCommunication
  26. extension NCMedia {
  27. func toggleMenu() {
  28. var actions: [NCMenuAction] = []
  29. if !isEditMode {
  30. if metadatas.count > 0 {
  31. actions.append(
  32. NCMenuAction(
  33. title: NSLocalizedString("_select_", comment: ""),
  34. icon: NCUtility.shared.loadImage(named: "checkmark.circle.fill"),
  35. action: { _ in
  36. self.isEditMode = true
  37. }
  38. )
  39. )
  40. }
  41. actions.append(
  42. NCMenuAction(
  43. title: NSLocalizedString(filterClassTypeImage ? "_media_viewimage_show_" : "_media_viewimage_hide_", comment: ""),
  44. icon: NCUtility.shared.loadImage(named: "photo"),
  45. selected: filterClassTypeImage,
  46. on: true,
  47. action: { _ in
  48. self.filterClassTypeImage = !self.filterClassTypeImage
  49. self.filterClassTypeVideo = false
  50. self.reloadDataSourceWithCompletion { _ in }
  51. }
  52. )
  53. )
  54. actions.append(
  55. NCMenuAction(
  56. title: NSLocalizedString(filterClassTypeVideo ? "_media_viewvideo_show_" : "_media_viewvideo_hide_", comment: ""),
  57. icon: NCUtility.shared.loadImage(named: "video"),
  58. selected: filterClassTypeVideo,
  59. on: true,
  60. action: { _ in
  61. self.filterClassTypeVideo = !self.filterClassTypeVideo
  62. self.filterClassTypeImage = false
  63. self.reloadDataSourceWithCompletion { _ in }
  64. }
  65. )
  66. )
  67. actions.append(
  68. NCMenuAction(
  69. title: NSLocalizedString("_select_media_folder_", comment: ""),
  70. icon: NCUtility.shared.loadImage(named: "folder"),
  71. action: { _ in
  72. let navigationController = UIStoryboard(name: "NCSelect", bundle: nil).instantiateInitialViewController() as! UINavigationController
  73. let viewController = navigationController.topViewController as! NCSelect
  74. viewController.delegate = self
  75. viewController.typeOfCommandView = .select
  76. viewController.type = "mediaFolder"
  77. self.present(navigationController, animated: true, completion: nil)
  78. }
  79. )
  80. )
  81. actions.append(
  82. NCMenuAction(
  83. title: NSLocalizedString("_media_by_modified_date_", comment: ""),
  84. icon: NCUtility.shared.loadImage(named: "circle.grid.cross.up.fill"),
  85. selected: CCUtility.getMediaSortDate() == "date",
  86. on: true,
  87. action: { _ in
  88. CCUtility.setMediaSortDate("date")
  89. self.reloadDataSourceWithCompletion { _ in }
  90. }
  91. )
  92. )
  93. actions.append(
  94. NCMenuAction(
  95. title: NSLocalizedString("_media_by_created_date_", comment: ""),
  96. icon: NCUtility.shared.loadImage(named: "circle.grid.cross.down.fill"),
  97. selected: CCUtility.getMediaSortDate() == "creationDate",
  98. on: true,
  99. action: { _ in
  100. CCUtility.setMediaSortDate("creationDate")
  101. self.reloadDataSourceWithCompletion { _ in }
  102. }
  103. )
  104. )
  105. actions.append(
  106. NCMenuAction(
  107. title: NSLocalizedString("_media_by_upload_date_", comment: ""),
  108. icon: NCUtility.shared.loadImage(named: "circle.grid.cross.right.fill"),
  109. selected: CCUtility.getMediaSortDate() == "uploadDate",
  110. on: true,
  111. action: { _ in
  112. CCUtility.setMediaSortDate("uploadDate")
  113. self.reloadDataSourceWithCompletion { _ in }
  114. }
  115. )
  116. )
  117. } else {
  118. //
  119. // CANCEL
  120. //
  121. actions.append(
  122. NCMenuAction(
  123. title: NSLocalizedString("_cancel_", comment: ""),
  124. icon: NCUtility.shared.loadImage(named: "xmark"),
  125. action: { _ in
  126. self.isEditMode = false
  127. self.selectOcId.removeAll()
  128. self.reloadDataThenPerform { }
  129. }
  130. )
  131. )
  132. //
  133. // OPEN IN
  134. //
  135. actions.append(
  136. NCMenuAction(
  137. title: NSLocalizedString("_open_in_", comment: ""),
  138. icon: NCUtility.shared.loadImage(named: "square.and.arrow.up"),
  139. action: { _ in
  140. self.isEditMode = false
  141. NCFunctionCenter.shared.openActivityViewController(selectOcId: self.selectOcId)
  142. self.selectOcId.removeAll()
  143. self.reloadDataThenPerform { }
  144. }
  145. )
  146. )
  147. //
  148. // SAVE TO PHOTO GALLERY
  149. //
  150. actions.append(
  151. NCMenuAction(
  152. title: NSLocalizedString("_save_selected_files_", comment: ""),
  153. icon: NCUtility.shared.loadImage(named: "square.and.arrow.down"),
  154. action: { _ in
  155. self.isEditMode = false
  156. for ocId in self.selectOcId {
  157. if let metadata = NCManageDatabase.shared.getMetadataFromOcId(ocId) {
  158. if metadata.classFile == NCCommunicationCommon.typeClassFile.image.rawValue || metadata.classFile == NCCommunicationCommon.typeClassFile.video.rawValue {
  159. if let metadataMOV = NCManageDatabase.shared.getMetadataLivePhoto(metadata: metadata) {
  160. NCFunctionCenter.shared.saveLivePhoto(metadata: metadata, metadataMOV: metadataMOV)
  161. } else {
  162. if CCUtility.fileProviderStorageExists(metadata) {
  163. NCFunctionCenter.shared.saveAlbum(metadata: metadata)
  164. } else {
  165. NCOperationQueue.shared.download(metadata: metadata, selector: NCGlobal.shared.selectorSaveAlbum)
  166. }
  167. }
  168. }
  169. }
  170. }
  171. self.selectOcId.removeAll()
  172. self.reloadDataThenPerform { }
  173. }
  174. )
  175. )
  176. //
  177. // COPY - MOVE
  178. //
  179. actions.append(
  180. NCMenuAction(
  181. title: NSLocalizedString("_move_or_copy_selected_files_", comment: ""),
  182. icon: NCUtility.shared.loadImage(named: "arrow.up.right.square"),
  183. action: { _ in
  184. self.isEditMode = false
  185. var meradatasSelect = [tableMetadata]()
  186. for ocId in self.selectOcId {
  187. if let metadata = NCManageDatabase.shared.getMetadataFromOcId(ocId) {
  188. meradatasSelect.append(metadata)
  189. }
  190. }
  191. if meradatasSelect.count > 0 {
  192. NCFunctionCenter.shared.openSelectView(items: meradatasSelect, viewController: self)
  193. }
  194. self.selectOcId.removeAll()
  195. self.reloadDataThenPerform { }
  196. }
  197. )
  198. )
  199. //
  200. // COPY
  201. //
  202. actions.append(
  203. NCMenuAction(
  204. title: NSLocalizedString("_copy_file_", comment: ""),
  205. icon: NCUtility.shared.loadImage(named: "doc.on.doc"),
  206. action: { _ in
  207. self.isEditMode = false
  208. NCFunctionCenter.shared.copyPasteboard(pasteboardOcIds: self.selectOcId, hudView: self.view)
  209. self.selectOcId.removeAll()
  210. self.reloadDataThenPerform { }
  211. }
  212. )
  213. )
  214. //
  215. // DELETE
  216. //
  217. actions.append(
  218. NCMenuAction(
  219. title: NSLocalizedString("_delete_selected_files_", comment: ""),
  220. icon: NCUtility.shared.loadImage(named: "trash"),
  221. action: { _ in
  222. self.isEditMode = false
  223. for ocId in self.selectOcId {
  224. if let metadata = NCManageDatabase.shared.getMetadataFromOcId(ocId) {
  225. NCNetworking.shared.deleteMetadata(metadata, onlyLocalCache: false) { errorCode, errorDescription in
  226. if errorCode != 0 {
  227. NCContentPresenter.shared.messageNotification("_error_", description: errorDescription, delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, errorCode: errorCode)
  228. }
  229. }
  230. }
  231. }
  232. self.selectOcId.removeAll()
  233. self.reloadDataThenPerform { }
  234. }
  235. )
  236. )
  237. }
  238. presentMenu(with: actions)
  239. }
  240. }