NCMedia+Menu.swift 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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.selectIndexPath.removeAll()
  31. self.reloadDataThenPerform { }
  32. }
  33. func toggleMenu() {
  34. var actions: [NCMenuAction] = []
  35. defer { presentMenu(with: actions) }
  36. if !isEditMode {
  37. if metadatas.count > 0 {
  38. actions.append(
  39. NCMenuAction(
  40. title: NSLocalizedString("_select_", comment: ""),
  41. icon: NCUtility.shared.loadImage(named: "checkmark.circle.fill"),
  42. action: { _ in
  43. self.isEditMode = true
  44. }
  45. )
  46. )
  47. }
  48. actions.append(
  49. NCMenuAction(
  50. title: NSLocalizedString("_media_viewimage_hide_", comment: ""),
  51. icon: NCUtility.shared.loadImage(named: "photo"),
  52. selected: filterClassTypeImage,
  53. on: true,
  54. action: { _ in
  55. self.filterClassTypeImage = !self.filterClassTypeImage
  56. self.filterClassTypeVideo = false
  57. self.reloadDataSourceWithCompletion { _ in }
  58. }
  59. )
  60. )
  61. actions.append(
  62. NCMenuAction(
  63. title: NSLocalizedString("_media_viewvideo_hide_", comment: ""),
  64. icon: NCUtility.shared.loadImage(named: "video"),
  65. selected: filterClassTypeVideo,
  66. on: true,
  67. action: { _ in
  68. self.filterClassTypeVideo = !self.filterClassTypeVideo
  69. self.filterClassTypeImage = false
  70. self.reloadDataSourceWithCompletion { _ in }
  71. }
  72. )
  73. )
  74. actions.append(
  75. NCMenuAction(
  76. title: NSLocalizedString("_select_media_folder_", comment: ""),
  77. icon: NCUtility.shared.loadImage(named: "folder"),
  78. action: { _ in
  79. let navigationController = UIStoryboard(name: "NCSelect", bundle: nil).instantiateInitialViewController() as! UINavigationController
  80. let viewController = navigationController.topViewController as! NCSelect
  81. viewController.delegate = self
  82. viewController.typeOfCommandView = .select
  83. viewController.type = "mediaFolder"
  84. viewController.selectIndexPath = self.selectIndexPath
  85. self.present(navigationController, animated: true, completion: nil)
  86. }
  87. )
  88. )
  89. actions.append(.seperator(order: 0))
  90. actions.append(
  91. NCMenuAction(
  92. title: NSLocalizedString("_play_from_files_", comment: ""),
  93. icon: NCUtility.shared.loadImage(named: "play.circle"),
  94. action: { _ in
  95. if let tabBarController = self.appDelegate.window?.rootViewController as? UITabBarController {
  96. self.documentPickerViewController = NCDocumentPickerViewController(tabBarController: tabBarController, isViewerMedia: true, allowsMultipleSelection: false, viewController: self)
  97. }
  98. }
  99. )
  100. )
  101. actions.append(
  102. NCMenuAction(
  103. title: NSLocalizedString("_play_from_url_", comment: ""),
  104. icon: NCUtility.shared.loadImage(named: "network"),
  105. action: { _ in
  106. let alert = UIAlertController(title: NSLocalizedString("_valid_video_url_", comment: ""), message: nil, preferredStyle: .alert)
  107. alert.addAction(UIAlertAction(title: NSLocalizedString("_cancel_", comment: ""), style: .cancel, handler: nil))
  108. alert.addTextField(configurationHandler: { textField in
  109. textField.placeholder = "http://myserver.com/movie.mkv"
  110. })
  111. alert.addAction(UIAlertAction(title: NSLocalizedString("_ok_", comment: ""), style: .default, handler: { _ in
  112. guard let stringUrl = alert.textFields?.first?.text, !stringUrl.isEmpty, let url = URL(string: stringUrl) else { return }
  113. let fileName = url.lastPathComponent
  114. 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: "")
  115. NCManageDatabase.shared.addMetadata(metadata)
  116. NCViewer.shared.view(viewController: self, metadata: metadata, metadatas: [metadata], imageIcon: nil)
  117. }))
  118. self.present(alert, animated: true)
  119. }
  120. )
  121. )
  122. actions.append(.seperator(order: 0))
  123. actions.append(
  124. NCMenuAction(
  125. title: NSLocalizedString("_media_by_modified_date_", comment: ""),
  126. icon: NCUtility.shared.loadImage(named: "circle.grid.cross.up.fill"),
  127. selected: CCUtility.getMediaSortDate() == "date",
  128. on: true,
  129. action: { _ in
  130. CCUtility.setMediaSortDate("date")
  131. self.reloadDataSourceWithCompletion { _ in }
  132. }
  133. )
  134. )
  135. actions.append(
  136. NCMenuAction(
  137. title: NSLocalizedString("_media_by_created_date_", comment: ""),
  138. icon: NCUtility.shared.loadImage(named: "circle.grid.cross.down.fill"),
  139. selected: CCUtility.getMediaSortDate() == "creationDate",
  140. on: true,
  141. action: { _ in
  142. CCUtility.setMediaSortDate("creationDate")
  143. self.reloadDataSourceWithCompletion { _ in }
  144. }
  145. )
  146. )
  147. actions.append(
  148. NCMenuAction(
  149. title: NSLocalizedString("_media_by_upload_date_", comment: ""),
  150. icon: NCUtility.shared.loadImage(named: "circle.grid.cross.right.fill"),
  151. selected: CCUtility.getMediaSortDate() == "uploadDate",
  152. on: true,
  153. action: { _ in
  154. CCUtility.setMediaSortDate("uploadDate")
  155. self.reloadDataSourceWithCompletion { _ in }
  156. }
  157. )
  158. )
  159. } else {
  160. //
  161. // CANCEL
  162. //
  163. actions.append(
  164. NCMenuAction(
  165. title: NSLocalizedString("_cancel_", comment: ""),
  166. icon: NCUtility.shared.loadImage(named: "xmark"),
  167. action: { _ in self.tapSelect() }
  168. )
  169. )
  170. guard !selectOcId.isEmpty else { return }
  171. let selectedMetadatas = selectOcId.compactMap(NCManageDatabase.shared.getMetadataFromOcId)
  172. //
  173. // OPEN IN
  174. //
  175. actions.append(.openInAction(selectedMetadatas: selectedMetadatas, viewController: self, completion: tapSelect))
  176. //
  177. // SAVE TO PHOTO GALLERY
  178. //
  179. actions.append(.saveMediaAction(selectedMediaMetadatas: selectedMetadatas, completion: tapSelect))
  180. //
  181. // COPY - MOVE
  182. //
  183. actions.append(.moveOrCopyAction(selectedMetadatas: selectedMetadatas, indexPath: selectIndexPath, completion: tapSelect))
  184. //
  185. // COPY
  186. //
  187. actions.append(.copyAction(selectOcId: selectOcId, completion: tapSelect))
  188. //
  189. // DELETE
  190. // can't delete from cache because is needed for NCMedia view, and if locked can't delete from server either.
  191. if !selectedMetadatas.contains(where: { $0.lock && $0.lockOwner != appDelegate.userId }) {
  192. actions.append(.deleteAction(selectedMetadatas: selectedMetadatas, indexPath: selectIndexPath, metadataFolder: nil, viewController: self, completion: tapSelect))
  193. }
  194. }
  195. }
  196. }