NCMedia+Menu.swift 10 KB

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