NCMedia+Menu.swift 12 KB

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