NCDetailNavigationController+Menu.swift 13 KB

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