NCDetailNavigationController+Menu.swift 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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. let mainMenuViewController = UIStoryboard.init(name: "NCMenu", bundle: nil).instantiateViewController(withIdentifier: "NCMainMenuTableViewController") as! NCMainMenuTableViewController
  31. if let metadata = NCManageDatabase.sharedInstance.getMetadataFromOcId(metadata.ocId) {
  32. mainMenuViewController.actions = self.initMoreMenu(viewController: viewController, metadata: metadata)
  33. } else {
  34. mainMenuViewController.actions = self.initMoreMenuClose(viewController: viewController)
  35. }
  36. let menuPanelController = NCMenuPanelController()
  37. menuPanelController.parentPresenter = viewController
  38. menuPanelController.delegate = mainMenuViewController
  39. menuPanelController.set(contentViewController: mainMenuViewController)
  40. menuPanelController.track(scrollView: mainMenuViewController.tableView)
  41. viewController.present(menuPanelController, animated: true, completion: nil)
  42. }
  43. private func initMoreMenuClose(viewController: UIViewController) -> [NCMenuAction] {
  44. var actions = [NCMenuAction]()
  45. actions.append(
  46. NCMenuAction(title: NSLocalizedString("_close_", comment: ""),
  47. icon: CCGraphics.changeThemingColorImage(UIImage(named: "exit"), width: 50, height: 50, color: NCBrandColor.sharedInstance.icon),
  48. action: { menuAction in
  49. NotificationCenter.default.postOnMainThread(name: k_notificationCenter_menuDetailClose)
  50. }
  51. )
  52. )
  53. return actions
  54. }
  55. private func initMoreMenu(viewController: UIViewController, metadata: tableMetadata) -> [NCMenuAction] {
  56. var actions = [NCMenuAction]()
  57. var titleFavorite = NSLocalizedString("_add_favorites_", comment: "")
  58. if metadata.favorite { titleFavorite = NSLocalizedString("_remove_favorites_", comment: "") }
  59. let localFile = NCManageDatabase.sharedInstance.getTableLocalFile(predicate: NSPredicate(format: "ocId == %@", metadata.ocId))
  60. var titleOffline = ""
  61. if (localFile == nil || localFile!.offline == false) {
  62. titleOffline = NSLocalizedString("_set_available_offline_", comment: "")
  63. } else {
  64. titleOffline = NSLocalizedString("_remove_available_offline_", comment: "")
  65. }
  66. //
  67. // FAVORITE
  68. //
  69. actions.append(
  70. NCMenuAction(
  71. title: titleFavorite,
  72. icon: CCGraphics.changeThemingColorImage(UIImage(named: "favorite"), width: 50, height: 50, color: NCBrandColor.sharedInstance.yellowFavorite),
  73. action: { menuAction in
  74. NCNetworking.shared.favoriteMetadata(metadata, urlBase: self.appDelegate.urlBase) { (errorCode, errorDescription) in
  75. if errorCode != 0 {
  76. NCContentPresenter.shared.messageNotification("_error_", description: errorDescription, delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
  77. }
  78. }
  79. }
  80. )
  81. )
  82. //
  83. // DETAIL
  84. //
  85. actions.append(
  86. NCMenuAction(
  87. title: NSLocalizedString("_details_", comment: ""),
  88. icon: CCGraphics.changeThemingColorImage(UIImage(named: "details"), width: 50, height: 50, color: NCBrandColor.sharedInstance.icon),
  89. action: { menuAction in
  90. NCNetworkingNotificationCenter.shared.openShare(ViewController: self, metadata: metadata, indexPage: 0)
  91. }
  92. )
  93. )
  94. //
  95. // OPEN IN
  96. //
  97. if metadata.session == "" {
  98. actions.append(
  99. NCMenuAction(title: NSLocalizedString("_open_in_", comment: ""),
  100. icon: CCGraphics.changeThemingColorImage(UIImage(named: "openFile"), width: 50, height: 50, color: NCBrandColor.sharedInstance.icon),
  101. action: { menuAction in
  102. NCNetworkingNotificationCenter.shared.downloadOpen(metadata: metadata, selector: selectorOpenInDetail)
  103. }
  104. )
  105. )
  106. }
  107. //
  108. // RENAME
  109. //
  110. actions.append(
  111. NCMenuAction(
  112. title: NSLocalizedString("_rename_", comment: ""),
  113. icon: CCGraphics.changeThemingColorImage(UIImage(named: "rename"), width: 50, height: 50, color: NCBrandColor.sharedInstance.icon),
  114. action: { menuAction in
  115. let alertController = UIAlertController(title: NSLocalizedString("_rename_", comment: ""), message: nil, preferredStyle: .alert)
  116. alertController.addTextField { (textField) in textField.text = metadata.fileNameView }
  117. let cancelAction = UIAlertAction(title: NSLocalizedString("_cancel_", comment: ""), style: .cancel, handler: nil)
  118. let okAction = UIAlertAction(title: NSLocalizedString("_ok_", comment: ""), style: .default, handler: { action in
  119. let fileNameNew = alertController.textFields![0].text
  120. NCNetworking.shared.renameMetadata(metadata, fileNameNew: fileNameNew!, urlBase: self.appDelegate.urlBase, viewController: self) { (errorCode, errorDescription) in
  121. if errorCode != 0 {
  122. NCContentPresenter.shared.messageNotification("_error_", description: errorDescription, delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
  123. }
  124. }
  125. })
  126. alertController.addAction(cancelAction)
  127. alertController.addAction(okAction)
  128. self.present(alertController, animated: true, completion: nil)
  129. }
  130. )
  131. )
  132. //
  133. // COPY - MOVE
  134. //
  135. actions.append(
  136. NCMenuAction(
  137. title: NSLocalizedString("_move_or_copy_", comment: ""),
  138. icon: CCGraphics.changeThemingColorImage(UIImage(named: "move"), width: 50, height: 50, color: NCBrandColor.sharedInstance.icon),
  139. action: { menuAction in
  140. let storyboard = UIStoryboard(name: "NCSelect", bundle: nil)
  141. let navigationController = storyboard.instantiateInitialViewController() as! UINavigationController
  142. let viewController = navigationController.topViewController as! NCSelect
  143. viewController.delegate = self.appDelegate.activeDetail
  144. viewController.hideButtonCreateFolder = false
  145. viewController.selectFile = false
  146. viewController.includeDirectoryE2EEncryption = false
  147. viewController.includeImages = false
  148. viewController.type = ""
  149. viewController.titleButtonDone = NSLocalizedString("_move_", comment: "")
  150. viewController.titleButtonDone1 = NSLocalizedString("_copy_", comment: "")
  151. viewController.isButtonDone1Hide = false
  152. viewController.isOverwriteHide = false
  153. navigationController.modalPresentationStyle = UIModalPresentationStyle.fullScreen
  154. self.present(navigationController, animated: true, completion: nil)
  155. }
  156. )
  157. )
  158. //
  159. // OFFLINE
  160. //
  161. if metadata.session == "" {
  162. actions.append(
  163. NCMenuAction(
  164. title: titleOffline,
  165. icon: CCGraphics.changeThemingColorImage(UIImage(named: "offline"), width: 50, height: 50, color: NCBrandColor.sharedInstance.icon),
  166. action: { menuAction in
  167. if ((localFile == nil || !CCUtility.fileProviderStorageExists(metadata.ocId, fileNameView: metadata.fileNameView)) && metadata.session == "") {
  168. NCNetworking.shared.download(metadata: metadata, selector: selectorLoadOffline) { (_) in }
  169. } else {
  170. NCManageDatabase.sharedInstance.setLocalFile(ocId: metadata.ocId, offline: !localFile!.offline)
  171. }
  172. }
  173. )
  174. )
  175. }
  176. //
  177. // VIEW IN FOLDER
  178. //
  179. if appDelegate.activeFileViewInFolder == nil {
  180. actions.append(
  181. NCMenuAction(
  182. title: NSLocalizedString("_view_in_folder_", comment: ""),
  183. icon: CCGraphics.changeThemingColorImage(UIImage(named: "viewInFolder"), width: 50, height: 50, color: NCBrandColor.sharedInstance.icon),
  184. action: { menuAction in
  185. NCCollectionCommon.shared.openFileViewInFolder(serverUrl: metadata.serverUrl, fileName: metadata.fileName)
  186. }
  187. )
  188. )
  189. }
  190. //
  191. // DELETE
  192. //
  193. actions.append(
  194. NCMenuAction(title: NSLocalizedString("_delete_", comment: ""),
  195. icon: CCGraphics.changeThemingColorImage(UIImage(named: "trash"), width: 50, height: 50, color: .red),
  196. action: { menuAction in
  197. let alertController = UIAlertController(title: "", message: NSLocalizedString("_want_delete_", comment: ""), preferredStyle: .alert)
  198. alertController.addAction(UIAlertAction(title: NSLocalizedString("_yes_delete_", comment: ""), style: .default) { (action:UIAlertAction) in
  199. NCNetworking.shared.deleteMetadata(metadata, account: self.appDelegate.account, urlBase: self.appDelegate.urlBase, onlyLocal: false) { (errorCode, errorDescription) in
  200. if errorCode != 0 {
  201. NCContentPresenter.shared.messageNotification("_error_", description: errorDescription, delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
  202. }
  203. }
  204. })
  205. alertController.addAction(UIAlertAction(title: NSLocalizedString("_no_delete_", comment: ""), style: .default) { (action:UIAlertAction) in })
  206. self.present(alertController, animated: true, completion:nil)
  207. }
  208. )
  209. )
  210. //
  211. // PDF
  212. //
  213. if (metadata.typeFile == k_metadataTypeFile_document && metadata.contentType == "application/pdf" ) {
  214. actions.append(
  215. NCMenuAction(title: NSLocalizedString("_search_", comment: ""),
  216. icon: CCGraphics.changeThemingColorImage(UIImage(named: "search"), width: 50, height: 50, color: NCBrandColor.sharedInstance.icon),
  217. action: { menuAction in
  218. NotificationCenter.default.postOnMainThread(name: k_notificationCenter_menuSearchTextPDF)
  219. }
  220. )
  221. )
  222. }
  223. //
  224. // IMAGE - VIDEO - AUDIO
  225. //
  226. if metadata.session == "" {
  227. 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 {
  228. actions.append(
  229. NCMenuAction(title: NSLocalizedString("_download_image_max_", comment: ""),
  230. icon: CCGraphics.changeThemingColorImage(UIImage(named: "downloadImageFullRes"), width: 50, height: 50, color: NCBrandColor.sharedInstance.icon),
  231. action: { menuAction in
  232. NotificationCenter.default.postOnMainThread(name: k_notificationCenter_menuDownloadImage, userInfo: ["metadata": metadata])
  233. }
  234. )
  235. )
  236. }
  237. }
  238. if metadata.typeFile == k_metadataTypeFile_image || metadata.typeFile == k_metadataTypeFile_video || metadata.typeFile == k_metadataTypeFile_audio {
  239. if let metadataLive = NCManageDatabase.sharedInstance.isLivePhoto(metadata: metadata) {
  240. if CCUtility.fileProviderStorageSize(metadata.ocId, fileNameView: metadata.fileNameView) > 0 && CCUtility.fileProviderStorageSize(metadataLive.ocId, fileNameView: metadataLive.fileNameView) > 0 {
  241. actions.append(
  242. NCMenuAction(title: NSLocalizedString("_livephoto_save_", comment: ""),
  243. icon: CCGraphics.changeThemingColorImage(UIImage(named: "livePhoto"), width: 50, height: 50, color: NCBrandColor.sharedInstance.icon),
  244. action: { menuAction in
  245. NotificationCenter.default.postOnMainThread(name: k_notificationCenter_menuSaveLivePhoto, userInfo: ["metadata": metadata, "metadataMov": metadataLive])
  246. }
  247. )
  248. )
  249. }
  250. }
  251. }
  252. //
  253. // CLOSE
  254. //
  255. actions.append(
  256. NCMenuAction(title: NSLocalizedString("_close_", comment: ""),
  257. icon: CCGraphics.changeThemingColorImage(UIImage(named: "exit"), width: 50, height: 50, color: NCBrandColor.sharedInstance.icon),
  258. action: { menuAction in
  259. NotificationCenter.default.postOnMainThread(name: k_notificationCenter_menuDetailClose)
  260. }
  261. )
  262. )
  263. return actions
  264. }
  265. }