NCDetailNavigationController+Menu.swift 13 KB

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