NCMedia+Menu.swift 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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(.seperator(order: 0))
  88. actions.append(
  89. NCMenuAction(
  90. title: NSLocalizedString("_play_from_files_", comment: ""),
  91. icon: NCUtility.shared.loadImage(named: "play.circle"),
  92. action: { _ in
  93. if let tabBarController = self.appDelegate.window?.rootViewController as? UITabBarController {
  94. self.documentPickerViewController = NCDocumentPickerViewController(tabBarController: tabBarController, supportedTypes: [UTType.movie], isViewerMedia: true, allowsMultipleSelection: false, viewController: self)
  95. }
  96. }
  97. )
  98. )
  99. actions.append(
  100. NCMenuAction(
  101. title: NSLocalizedString("_play_from_url_", comment: ""),
  102. icon: NCUtility.shared.loadImage(named: "network"),
  103. action: { _ in
  104. let alert = UIAlertController(title: NSLocalizedString("_valid_video_url_", comment: ""), message: nil, preferredStyle: .alert)
  105. alert.addAction(UIAlertAction(title: NSLocalizedString("_cancel_", comment: ""), style: .cancel, handler: nil))
  106. alert.addTextField(configurationHandler: { textField in
  107. textField.placeholder = "http://myserver.com/movie.mkv"
  108. })
  109. alert.addAction(UIAlertAction(title: NSLocalizedString("_ok_", comment: ""), style: .default, handler: { _ in
  110. guard let stringUrl = alert.textFields?.first?.text, !stringUrl.isEmpty, let url = URL(string: stringUrl) else { return }
  111. let fileName = url.lastPathComponent
  112. let metadata = NCManageDatabase.shared.createMetadata(account: self.appDelegate.account, user: self.appDelegate.user, userId: self.appDelegate.userId, fileName: fileName, fileNameView: fileName, ocId: NSUUID().uuidString, serverUrl: "", urlBase: self.appDelegate.urlBase, url: stringUrl, contentType: "")
  113. NCManageDatabase.shared.addMetadata(metadata)
  114. NCViewer.shared.view(viewController: self, metadata: metadata, metadatas: [metadata], imageIcon: nil)
  115. }))
  116. self.present(alert, animated: true)
  117. }
  118. )
  119. )
  120. actions.append(.seperator(order: 0))
  121. actions.append(
  122. NCMenuAction(
  123. title: NSLocalizedString("_media_by_modified_date_", comment: ""),
  124. icon: NCUtility.shared.loadImage(named: "circle.grid.cross.up.fill"),
  125. selected: CCUtility.getMediaSortDate() == "date",
  126. on: true,
  127. action: { _ in
  128. CCUtility.setMediaSortDate("date")
  129. self.reloadDataSourceWithCompletion { _ in }
  130. }
  131. )
  132. )
  133. actions.append(
  134. NCMenuAction(
  135. title: NSLocalizedString("_media_by_created_date_", comment: ""),
  136. icon: NCUtility.shared.loadImage(named: "circle.grid.cross.down.fill"),
  137. selected: CCUtility.getMediaSortDate() == "creationDate",
  138. on: true,
  139. action: { _ in
  140. CCUtility.setMediaSortDate("creationDate")
  141. self.reloadDataSourceWithCompletion { _ in }
  142. }
  143. )
  144. )
  145. actions.append(
  146. NCMenuAction(
  147. title: NSLocalizedString("_media_by_upload_date_", comment: ""),
  148. icon: NCUtility.shared.loadImage(named: "circle.grid.cross.right.fill"),
  149. selected: CCUtility.getMediaSortDate() == "uploadDate",
  150. on: true,
  151. action: { _ in
  152. CCUtility.setMediaSortDate("uploadDate")
  153. self.reloadDataSourceWithCompletion { _ in }
  154. }
  155. )
  156. )
  157. } else {
  158. //
  159. // CANCEL
  160. //
  161. actions.append(
  162. NCMenuAction(
  163. title: NSLocalizedString("_cancel_", comment: ""),
  164. icon: NCUtility.shared.loadImage(named: "xmark"),
  165. action: { _ in self.tapSelect() }
  166. )
  167. )
  168. guard !selectOcId.isEmpty else { return }
  169. let selectedMetadatas = selectOcId.compactMap(NCManageDatabase.shared.getMetadataFromOcId)
  170. //
  171. // OPEN IN
  172. //
  173. actions.append(.openInAction(selectedMetadatas: selectedMetadatas, viewController: self, completion: tapSelect))
  174. //
  175. // SAVE TO PHOTO GALLERY
  176. //
  177. actions.append(.saveMediaAction(selectedMediaMetadatas: selectedMetadatas, completion: tapSelect))
  178. //
  179. // COPY - MOVE
  180. //
  181. actions.append(.moveOrCopyAction(selectedMetadatas: selectedMetadatas, completion: tapSelect))
  182. //
  183. // COPY
  184. //
  185. actions.append(.copyAction(selectOcId: selectOcId, hudView: self.view, completion: tapSelect))
  186. //
  187. // DELETE
  188. // can't delete from cache because is needed for NCMedia view, and if locked can't delete from server either.
  189. if !selectedMetadatas.contains(where: { $0.lock && $0.lockOwner != appDelegate.userId }) {
  190. actions.append(.deleteAction(selectedMetadatas: selectedMetadatas, metadataFolder: nil, viewController: self, completion: tapSelect))
  191. }
  192. }
  193. }
  194. }