NCDetailNavigationController+Menu.swift 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. //
  2. // NCDetailNavigationController+Menu.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 07/02/2020.
  6. // Copyright © 2020 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 FloatingPanel
  24. import NCCommunication
  25. extension NCDetailNavigationController {
  26. private func initMoreMenu(viewController: UIViewController, metadata: tableMetadata) -> [NCMenuAction] {
  27. var actions = [NCMenuAction]()
  28. let fileNameExtension = (metadata.fileNameView as NSString).pathExtension.uppercased()
  29. let directEditingCreators = NCManageDatabase.sharedInstance.getDirectEditingCreators(account: appDelegate.activeAccount)
  30. var titleFavorite = NSLocalizedString("_add_favorites_", comment: "")
  31. if metadata.favorite { titleFavorite = NSLocalizedString("_remove_favorites_", comment: "") }
  32. actions.append(
  33. NCMenuAction(
  34. title: titleFavorite,
  35. icon: CCGraphics.changeThemingColorImage(UIImage(named: "favorite"), width: 50, height: 50, color: NCBrandColor.sharedInstance.yellowFavorite),
  36. action: { menuAction in
  37. NCNetworking.sharedInstance.favoriteMetadata(metadata, url: self.appDelegate.activeUrl) { (errorCode, errorDescription) in }
  38. }
  39. )
  40. )
  41. actions.append(
  42. NCMenuAction(
  43. title: NSLocalizedString("_details_", comment: ""),
  44. icon: CCGraphics.changeThemingColorImage(UIImage(named: "details"), width: 50, height: 50, color: NCBrandColor.sharedInstance.icon),
  45. action: { menuAction in
  46. NCMainCommon.sharedInstance.openShare(ViewController: self, metadata: metadata, indexPage: 0)
  47. }
  48. )
  49. )
  50. actions.append(
  51. NCMenuAction(title: NSLocalizedString("_open_in_", comment: ""),
  52. icon: CCGraphics.changeThemingColorImage(UIImage(named: "openFile"), width: 50, height: 50, color: NCBrandColor.sharedInstance.icon),
  53. action: { menuAction in
  54. NCMainCommon.sharedInstance.downloadOpen(metadata: metadata, selector: selectorOpenInDetail)
  55. }
  56. )
  57. )
  58. actions.append(
  59. NCMenuAction(
  60. title: NSLocalizedString("_rename_", comment: ""),
  61. icon: CCGraphics.changeThemingColorImage(UIImage(named: "rename"), width: 50, height: 50, color: NCBrandColor.sharedInstance.icon),
  62. action: { menuAction in
  63. let alertController = UIAlertController(title: NSLocalizedString("_rename_", comment: ""), message: nil, preferredStyle: .alert)
  64. alertController.addTextField { (textField) in textField.text = metadata.fileNameView }
  65. let cancelAction = UIAlertAction(title: NSLocalizedString("_cancel_", comment: ""), style: .cancel, handler: nil)
  66. let okAction = UIAlertAction(title: NSLocalizedString("_ok_", comment: ""), style: .default, handler: { action in
  67. let fileNameNew = alertController.textFields![0].text
  68. NCNetworking.sharedInstance.renameMetadata(metadata, fileNameNew: fileNameNew!, user: self.appDelegate.activeUser, userID: self.appDelegate.activeUserID, password: self.appDelegate.activePassword, url: self.appDelegate.activeUrl, viewController: self) { (errorCode, errorDescription) in }
  69. })
  70. alertController.addAction(cancelAction)
  71. alertController.addAction(okAction)
  72. self.present(alertController, animated: true, completion: nil)
  73. }
  74. )
  75. )
  76. actions.append(
  77. NCMenuAction(
  78. title: NSLocalizedString("_move_or_copy_", comment: ""),
  79. icon: CCGraphics.changeThemingColorImage(UIImage(named: "move"), width: 50, height: 50, color: NCBrandColor.sharedInstance.icon),
  80. action: { menuAction in
  81. let storyboard = UIStoryboard(name: "NCSelect", bundle: nil)
  82. let navigationController = storyboard.instantiateInitialViewController() as! UINavigationController
  83. let viewController = navigationController.topViewController as! NCSelect
  84. viewController.delegate = self.appDelegate.activeDetail
  85. viewController.hideButtonCreateFolder = false
  86. viewController.selectFile = false
  87. viewController.includeDirectoryE2EEncryption = false
  88. viewController.includeImages = false
  89. viewController.type = ""
  90. viewController.titleButtonDone = NSLocalizedString("_move_", comment: "")
  91. viewController.titleButtonDone1 = NSLocalizedString("_copy_", comment: "")
  92. viewController.isButtonDone1Hide = false
  93. viewController.isOverwriteHide = false
  94. viewController.layoutViewSelect = k_layout_view_move
  95. navigationController.modalPresentationStyle = UIModalPresentationStyle.fullScreen
  96. self.present(navigationController, animated: true, completion: nil)
  97. }
  98. )
  99. )
  100. actions.append(
  101. NCMenuAction(title: NSLocalizedString("_delete_", comment: ""),
  102. icon: CCGraphics.changeThemingColorImage(UIImage(named: "trash"), width: 50, height: 50, color: .red),
  103. action: { menuAction in
  104. let alertController = UIAlertController(title: "", message: NSLocalizedString("_want_delete_", comment: ""), preferredStyle: .alert)
  105. alertController.addAction(UIAlertAction(title: NSLocalizedString("_yes_delete_", comment: ""), style: .default) { (action:UIAlertAction) in
  106. NCNetworking.sharedInstance.deleteMetadata(metadata, user: self.appDelegate.activeUser, userID: self.appDelegate.activeUserID, password: self.appDelegate.activePassword, url: self.appDelegate.activeUrl) { (errorCode, errorDescription) in }
  107. })
  108. alertController.addAction(UIAlertAction(title: NSLocalizedString("_no_delete_", comment: ""), style: .default) { (action:UIAlertAction) in })
  109. self.present(alertController, animated: true, completion:nil)
  110. }
  111. )
  112. )
  113. // PDF
  114. if #available(iOS 11.0, *) {
  115. if (metadata.typeFile == k_metadataTypeFile_document && metadata.contentType == "application/pdf" ) {
  116. actions.append(
  117. NCMenuAction(title: NSLocalizedString("_search_", comment: ""),
  118. icon: CCGraphics.changeThemingColorImage(UIImage(named: "search"), width: 50, height: 50, color: NCBrandColor.sharedInstance.icon),
  119. action: { menuAction in
  120. NotificationCenter.default.post(name: Notification.Name.init(rawValue:
  121. k_notificationCenter_menuSearchTextPDF), object: nil)
  122. }
  123. )
  124. )
  125. }
  126. }
  127. // IMAGE - VIDEO - AUDIO
  128. if (metadata.typeFile == k_metadataTypeFile_image || metadata.typeFile == k_metadataTypeFile_video || metadata.typeFile == k_metadataTypeFile_audio) && !CCUtility.fileProviderStorageExists(appDelegate.activeDetail.metadata?.ocId, fileNameView: appDelegate.activeDetail.metadata?.fileNameView) && metadata.session == "" && metadata.typeFile == k_metadataTypeFile_image {
  129. actions.append(
  130. NCMenuAction(title: NSLocalizedString("_download_image_max_", comment: ""),
  131. icon: CCGraphics.changeThemingColorImage(UIImage(named: "downloadImageFullRes"), width: 50, height: 50, color: NCBrandColor.sharedInstance.icon),
  132. action: { menuAction in
  133. let userInfo: [String : Any] = ["metadata": metadata]
  134. NotificationCenter.default.post(name: Notification.Name.init(rawValue: k_notificationCenter_menuDownloadImage), object: nil, userInfo: userInfo)
  135. }
  136. )
  137. )
  138. }
  139. if metadata.typeFile == k_metadataTypeFile_image || metadata.typeFile == k_metadataTypeFile_video || metadata.typeFile == k_metadataTypeFile_audio {
  140. if let metadataMov = NCUtility.sharedInstance.hasMOV(metadata: metadata) {
  141. if CCUtility.fileProviderStorageSize(metadata.ocId, fileNameView: metadata.fileNameView) > 0 && CCUtility.fileProviderStorageSize(metadataMov.ocId, fileNameView: metadataMov.fileNameView) > 0 {
  142. actions.append(
  143. NCMenuAction(title: NSLocalizedString("_livephoto_save_", comment: ""),
  144. icon: CCGraphics.changeThemingColorImage(UIImage(named: "livePhoto"), width: 50, height: 50, color: NCBrandColor.sharedInstance.icon),
  145. action: { menuAction in
  146. let userInfo: [String : Any] = ["metadata": metadata, "metadataMov": metadataMov]
  147. NotificationCenter.default.post(name: Notification.Name.init(rawValue: k_notificationCenter_menuSaveLivePhoto), object: nil, userInfo: userInfo)
  148. }
  149. )
  150. )
  151. }
  152. }
  153. }
  154. if CCUtility.isDocumentModifiableExtension(fileNameExtension) && (directEditingCreators == nil || !appDelegate.reachability.isReachable()) {
  155. actions.append(
  156. NCMenuAction(title: NSLocalizedString("_internal_modify_", comment: ""),
  157. icon: CCGraphics.changeThemingColorImage(UIImage(named: "edit"), width: 50, height: 50, color: NCBrandColor.sharedInstance.icon),
  158. action: { menuAction in
  159. if let navigationController = UIStoryboard(name: "NCText", bundle: nil).instantiateViewController(withIdentifier: "NCText") as? UINavigationController {
  160. navigationController.modalPresentationStyle = .pageSheet
  161. navigationController.modalTransitionStyle = .crossDissolve
  162. if let textViewController = navigationController.topViewController as? NCText {
  163. textViewController.metadata = metadata;
  164. viewController.present(navigationController, animated: true, completion: nil)
  165. }
  166. }
  167. }
  168. )
  169. )
  170. }
  171. // CLOSE
  172. actions.append(
  173. NCMenuAction(title: NSLocalizedString("_close_", comment: ""),
  174. icon: CCGraphics.changeThemingColorImage(UIImage(named: "exit"), width: 50, height: 50, color: NCBrandColor.sharedInstance.icon),
  175. action: { menuAction in
  176. NotificationCenter.default.post(name: Notification.Name.init(rawValue: k_notificationCenter_menuDetailClose), object: nil)
  177. }
  178. )
  179. )
  180. return actions
  181. }
  182. @objc func toggleMoreMenu(viewController: UIViewController, metadata: tableMetadata) {
  183. // Refresh metadata
  184. guard let metadata = NCManageDatabase.sharedInstance.getMetadata(predicate: NSPredicate(format: "ocId == %@", metadata.ocId)) else { return }
  185. if appDelegate.activeDetail.subViewActive() != nil {
  186. let mainMenuViewController = UIStoryboard.init(name: "NCMenu", bundle: nil).instantiateViewController(withIdentifier: "NCMainMenuTableViewController") as! NCMainMenuTableViewController
  187. mainMenuViewController.actions = self.initMoreMenu(viewController: viewController, metadata: metadata)
  188. let menuPanelController = NCMenuPanelController()
  189. menuPanelController.parentPresenter = viewController
  190. menuPanelController.delegate = mainMenuViewController
  191. menuPanelController.set(contentViewController: mainMenuViewController)
  192. menuPanelController.track(scrollView: mainMenuViewController.tableView)
  193. viewController.present(menuPanelController, animated: true, completion: nil)
  194. }
  195. }
  196. }