NCMedia+Menu.swift 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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. extension NCMedia {
  26. func toggleMenu() {
  27. let menuViewController = UIStoryboard.init(name: "NCMenu", bundle: nil).instantiateInitialViewController() as! NCMenu
  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: { menuAction in
  36. self.isEditMode = true
  37. }
  38. )
  39. )
  40. }
  41. actions.append(
  42. NCMenuAction(
  43. title: NSLocalizedString(filterTypeFileImage ? "_media_viewimage_show_" : "_media_viewimage_hide_", comment: ""),
  44. icon: NCUtility.shared.loadImage(named: "photo"),
  45. selected: filterTypeFileImage,
  46. on: true,
  47. action: { menuAction in
  48. self.filterTypeFileImage = !self.filterTypeFileImage
  49. self.filterTypeFileVideo = false
  50. self.reloadDataSource()
  51. }
  52. )
  53. )
  54. actions.append(
  55. NCMenuAction(
  56. title: NSLocalizedString(filterTypeFileVideo ? "_media_viewvideo_show_" : "_media_viewvideo_hide_", comment: ""),
  57. icon: NCUtility.shared.loadImage(named: "video"),
  58. selected: filterTypeFileVideo,
  59. on: true,
  60. action: { menuAction in
  61. self.filterTypeFileVideo = !self.filterTypeFileVideo
  62. self.filterTypeFileImage = false
  63. self.reloadDataSource()
  64. }
  65. )
  66. )
  67. actions.append(
  68. NCMenuAction(
  69. title: NSLocalizedString("_select_media_folder_", comment: ""),
  70. icon: NCUtility.shared.loadImage(named: "folder"),
  71. action: { menuAction 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: { menuAction in
  88. CCUtility.setMediaSortDate("date")
  89. self.reloadDataSource()
  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: { menuAction in
  100. CCUtility.setMediaSortDate("creationDate")
  101. self.reloadDataSource()
  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: { menuAction in
  112. CCUtility.setMediaSortDate("uploadDate")
  113. self.reloadDataSource()
  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: { menuAction 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: { menuAction 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: { menuAction in
  155. self.isEditMode = false
  156. for ocId in self.selectOcId {
  157. if let metadata = NCManageDatabase.shared.getMetadataFromOcId(ocId) {
  158. if metadata.typeFile == NCGlobal.shared.metadataTypeFileImage || metadata.typeFile == NCGlobal.shared.metadataTypeFileVideo {
  159. if let metadataMOV = NCManageDatabase.shared.getMetadataLivePhoto(metadata: metadata) {
  160. NCFunctionCenter.shared.saveLivePhoto(metadata: metadata, metadataMOV: metadataMOV)
  161. } else {
  162. if CCUtility.fileProviderStorageExists(metadata.ocId, fileNameView: metadata.fileNameView) {
  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: { menuAction 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: { menuAction in
  207. self.isEditMode = false
  208. self.appDelegate.pasteboardOcIds.removeAll()
  209. for ocId in self.selectOcId {
  210. self.appDelegate.pasteboardOcIds.append(ocId)
  211. }
  212. NCFunctionCenter.shared.copyPasteboard()
  213. self.selectOcId.removeAll()
  214. self.reloadDataThenPerform { }
  215. }
  216. )
  217. )
  218. //
  219. // DELETE
  220. //
  221. actions.append(
  222. NCMenuAction(
  223. title: NSLocalizedString("_delete_selected_files_", comment: ""),
  224. icon: NCUtility.shared.loadImage(named: "trash"),
  225. action: { menuAction in
  226. self.isEditMode = false
  227. for ocId in self.selectOcId {
  228. if let metadata = NCManageDatabase.shared.getMetadataFromOcId(ocId) {
  229. NCNetworking.shared.deleteMetadata(metadata, onlyLocal: false) { (errorCode, errorDescription) in
  230. if errorCode != 0 {
  231. NCContentPresenter.shared.messageNotification("_error_", description: errorDescription, delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, errorCode: errorCode)
  232. }
  233. }
  234. }
  235. }
  236. self.selectOcId.removeAll()
  237. self.reloadDataThenPerform { }
  238. }
  239. )
  240. )
  241. }
  242. menuViewController.actions = actions
  243. let menuPanelController = NCMenuPanelController()
  244. menuPanelController.parentPresenter = self
  245. menuPanelController.delegate = menuViewController
  246. menuPanelController.set(contentViewController: menuViewController)
  247. menuPanelController.track(scrollView: menuViewController.tableView)
  248. self.present(menuPanelController, animated: true, completion: nil)
  249. }
  250. }