NCCollectionViewCommon+Menu.swift 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. //
  2. // NCCollectionViewCommon+Menu.swift
  3. // Nextcloud
  4. //
  5. // Created by Philippe Weidmann on 24.01.20.
  6. // Copyright © 2020 Philippe Weidmann. All rights reserved.
  7. // Copyright © 2020 Marino Faggiana All rights reserved.
  8. //
  9. // Author Philippe Weidmann
  10. // Author Marino Faggiana <marino.faggiana@nextcloud.com>
  11. //
  12. // This program is free software: you can redistribute it and/or modify
  13. // it under the terms of the GNU General Public License as published by
  14. // the Free Software Foundation, either version 3 of the License, or
  15. // (at your option) any later version.
  16. //
  17. // This program is distributed in the hope that it will be useful,
  18. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. // GNU General Public License for more details.
  21. //
  22. // You should have received a copy of the GNU General Public License
  23. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  24. //
  25. import FloatingPanel
  26. extension NCCollectionViewCommon {
  27. func toggleMoreMenu(viewController: UIViewController, metadata: tableMetadata) {
  28. if let metadata = NCManageDatabase.sharedInstance.getMetadataFromOcId(metadata.ocId) {
  29. let mainMenuViewController = UIStoryboard.init(name: "NCMenu", bundle: nil).instantiateViewController(withIdentifier: "NCMainMenuTableViewController") as! NCMainMenuTableViewController
  30. mainMenuViewController.actions = self.initMenuMore(viewController: viewController, metadata: metadata)
  31. let menuPanelController = NCMenuPanelController()
  32. menuPanelController.parentPresenter = viewController
  33. menuPanelController.delegate = mainMenuViewController
  34. menuPanelController.set(contentViewController: mainMenuViewController)
  35. menuPanelController.track(scrollView: mainMenuViewController.tableView)
  36. viewController.present(menuPanelController, animated: true, completion: nil)
  37. }
  38. }
  39. private func initMenuMore(viewController: UIViewController, metadata: tableMetadata) -> [NCMenuAction] {
  40. var actions = [NCMenuAction]()
  41. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  42. let isFolderEncrypted = CCUtility.isFolderEncrypted(metadata.serverUrl+"/"+metadata.fileName, e2eEncrypted: metadata.e2eEncrypted, account: metadata.account, urlBase: metadata.urlBase)
  43. var isOffline = false
  44. if metadata.directory {
  45. if let directory = NCManageDatabase.sharedInstance.getTableDirectory(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@", appDelegate.account, CCUtility.stringAppendServerUrl(metadata.serverUrl, addFileName: metadata.fileName)!)) {
  46. isOffline = directory.offline
  47. }
  48. } else {
  49. if let localFile = NCManageDatabase.sharedInstance.getTableLocalFile(predicate: NSPredicate(format: "ocId == %@", metadata.ocId)) {
  50. isOffline = localFile.offline
  51. }
  52. }
  53. var iconHeader: UIImage!
  54. if let icon = UIImage(contentsOfFile: CCUtility.getDirectoryProviderStorageIconOcId(metadata.ocId, etag: metadata.etag)) {
  55. iconHeader = icon
  56. } else {
  57. if metadata.directory {
  58. iconHeader = CCGraphics.changeThemingColorImage(UIImage(named: "folder"), width: 50, height: 50, color: NCBrandColor.sharedInstance.icon)
  59. } else {
  60. iconHeader = UIImage(named: metadata.iconName)
  61. }
  62. }
  63. actions.append(
  64. NCMenuAction(
  65. title: metadata.fileNameView,
  66. icon: iconHeader,
  67. action: nil
  68. )
  69. )
  70. // Favorite
  71. if (layoutKey == k_layout_view_favorite && serverUrl == "") || (layoutKey != k_layout_view_favorite) {
  72. actions.append(
  73. NCMenuAction(
  74. title: metadata.favorite ? NSLocalizedString("_remove_favorites_", comment: "") : NSLocalizedString("_add_favorites_", comment: ""),
  75. icon: CCGraphics.changeThemingColorImage(UIImage(named: "favorite"), width: 50, height: 50, color: NCBrandColor.sharedInstance.yellowFavorite),
  76. action: { menuAction in
  77. NCNetworking.shared.favoriteMetadata(metadata, urlBase: appDelegate.urlBase) { (errorCode, errorDescription) in
  78. if errorCode != 0 {
  79. NCContentPresenter.shared.messageNotification("_error_", description: errorDescription, delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
  80. }
  81. }
  82. }
  83. )
  84. )
  85. }
  86. // Offline
  87. if !isFolderEncrypted && (layoutKey == k_layout_view_offline && serverUrl == "" || (layoutKey != k_layout_view_offline)) {
  88. actions.append(
  89. NCMenuAction(
  90. title: isOffline ? NSLocalizedString("_remove_available_offline_", comment: "") : NSLocalizedString("_set_available_offline_", comment: ""),
  91. icon: CCGraphics.changeThemingColorImage(UIImage(named: "offline"), width: 50, height: 50, color: NCBrandColor.sharedInstance.icon),
  92. action: { menuAction in
  93. if isOffline {
  94. if metadata.directory {
  95. NCManageDatabase.sharedInstance.setDirectory(serverUrl: CCUtility.stringAppendServerUrl(metadata.serverUrl, addFileName: metadata.fileName)!, offline: false, account: self.appDelegate.account)
  96. } else {
  97. NCManageDatabase.sharedInstance.setLocalFile(ocId: metadata.ocId, offline: false)
  98. }
  99. } else {
  100. if metadata.directory {
  101. NCManageDatabase.sharedInstance.setDirectory(serverUrl: CCUtility.stringAppendServerUrl(metadata.serverUrl, addFileName: metadata.fileName)!, offline: true, account: self.appDelegate.account)
  102. NCOperationQueue.shared.synchronizationMetadata(metadata, selector: selectorDownloadAllFile)
  103. } else {
  104. NCNetworking.shared.download(metadata: metadata, selector: selectorLoadOffline) { (_) in }
  105. if let metadataLivePhoto = NCManageDatabase.sharedInstance.isLivePhoto(metadata: metadata) {
  106. NCNetworking.shared.download(metadata: metadataLivePhoto, selector: selectorLoadOffline) { (_) in }
  107. }
  108. }
  109. }
  110. self.reloadDataSource()
  111. }
  112. )
  113. )
  114. }
  115. // All
  116. actions.append(
  117. NCMenuAction(
  118. title: NSLocalizedString("_details_", comment: ""),
  119. icon: CCGraphics.changeThemingColorImage(UIImage(named: "details"), width: 50, height: 50, color: NCBrandColor.sharedInstance.icon),
  120. action: { menuAction in
  121. NCMainCommon.shared.openShare(ViewController: self, metadata: metadata, indexPage: 0)
  122. }
  123. )
  124. )
  125. if !metadata.directory && !NCBrandOptions.sharedInstance.disable_openin_file {
  126. actions.append(
  127. NCMenuAction(
  128. title: NSLocalizedString("_open_in_", comment: ""),
  129. icon: CCGraphics.changeThemingColorImage(UIImage(named: "openFile"), width: 50, height: 50, color: NCBrandColor.sharedInstance.icon),
  130. action: { menuAction in
  131. NCMainCommon.shared.downloadOpen(metadata: metadata, selector: selectorOpenIn)
  132. }
  133. )
  134. )
  135. }
  136. actions.append(
  137. NCMenuAction(
  138. title: NSLocalizedString("_rename_", comment: ""),
  139. icon: CCGraphics.changeThemingColorImage(UIImage(named: "rename"), width: 50, height: 50, color: NCBrandColor.sharedInstance.icon),
  140. action: { menuAction in
  141. let alertController = UIAlertController(title: NSLocalizedString("_rename_", comment: ""), message: nil, preferredStyle: .alert)
  142. alertController.addTextField { (textField) in
  143. textField.text = metadata.fileNameView
  144. }
  145. let cancelAction = UIAlertAction(title: NSLocalizedString("_cancel_", comment: ""), style: .cancel, handler: nil)
  146. let okAction = UIAlertAction(title: NSLocalizedString("_ok_", comment: ""), style: .default, handler: { action in
  147. if let fileNameNew = alertController.textFields?.first?.text {
  148. NCNetworking.shared.renameMetadata(metadata, fileNameNew: fileNameNew, urlBase: appDelegate.urlBase, viewController: self) { (errorCode, errorDescription) in
  149. if errorCode != 0 {
  150. NCContentPresenter.shared.messageNotification("_error_", description: errorDescription, delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
  151. }
  152. }
  153. }
  154. })
  155. alertController.addAction(cancelAction)
  156. alertController.addAction(okAction)
  157. self.present(alertController, animated: true, completion: nil)
  158. }
  159. )
  160. )
  161. if !isFolderEncrypted && serverUrl != "" {
  162. actions.append(
  163. NCMenuAction(
  164. title: NSLocalizedString("_move_or_copy_", comment: ""),
  165. icon: CCGraphics.changeThemingColorImage(UIImage(named: "move"), width: 50, height: 50, color: NCBrandColor.sharedInstance.icon),
  166. action: { menuAction in
  167. NCCollectionCommon.shared.openSelectView(viewController: viewController, array: [metadata])
  168. }
  169. )
  170. )
  171. }
  172. actions.append(
  173. NCMenuAction(
  174. title: NSLocalizedString("_delete_", comment: ""),
  175. icon: CCGraphics.changeThemingColorImage(UIImage(named: "trash"), width: 50, height: 50, color: .red),
  176. action: { menuAction in
  177. let alertController = UIAlertController(title: "", message: NSLocalizedString("_want_delete_", comment: ""), preferredStyle: .alert)
  178. alertController.addAction(UIAlertAction(title: NSLocalizedString("_yes_delete_", comment: ""), style: .default) { (action:UIAlertAction) in
  179. NCOperationQueue.shared.delete(metadata: metadata, onlyLocal: false)
  180. })
  181. alertController.addAction(UIAlertAction(title: NSLocalizedString("_remove_local_file_", comment: ""), style: .default) { (action:UIAlertAction) in
  182. NCOperationQueue.shared.delete(metadata: metadata, onlyLocal: true)
  183. })
  184. alertController.addAction(UIAlertAction(title: NSLocalizedString("_no_delete_", comment: ""), style: .default) { (action:UIAlertAction) in })
  185. self.present(alertController, animated: true, completion:nil)
  186. }
  187. )
  188. )
  189. return actions
  190. }
  191. func toggleMoreSelect(viewController: UIViewController, selectOcId: [String]) {
  192. let mainMenuViewController = UIStoryboard.init(name: "NCMenu", bundle: nil).instantiateViewController(withIdentifier: "NCMainMenuTableViewController") as! NCMainMenuTableViewController
  193. mainMenuViewController.actions = self.initMenuSelect(viewController: viewController, selectOcId: selectOcId)
  194. let menuPanelController = NCMenuPanelController()
  195. menuPanelController.parentPresenter = viewController
  196. menuPanelController.delegate = mainMenuViewController
  197. menuPanelController.set(contentViewController: mainMenuViewController)
  198. menuPanelController.track(scrollView: mainMenuViewController.tableView)
  199. viewController.present(menuPanelController, animated: true, completion: nil)
  200. }
  201. private func initMenuSelect(viewController: UIViewController, selectOcId: [String]) -> [NCMenuAction] {
  202. var actions = [NCMenuAction]()
  203. actions.append(
  204. NCMenuAction(
  205. title: NSLocalizedString("_select_all_", comment: ""),
  206. icon: CCGraphics.changeThemingColorImage(UIImage(named: "selectFull"), width: 50, height: 50, color: NCBrandColor.sharedInstance.icon),
  207. action: { menuAction in
  208. self.collectionViewSelectAll()
  209. }
  210. )
  211. )
  212. actions.append(
  213. NCMenuAction(
  214. title: NSLocalizedString("_move_or_copy_selected_files_", comment: ""),
  215. icon: CCGraphics.changeThemingColorImage(UIImage(named: "move"), width: 50, height: 50, color: NCBrandColor.sharedInstance.icon),
  216. action: { menuAction in
  217. var meradatasSelect = [tableMetadata]()
  218. for ocId in selectOcId {
  219. if let metadata = NCManageDatabase.sharedInstance.getMetadataFromOcId(ocId) {
  220. meradatasSelect.append(metadata)
  221. }
  222. }
  223. if meradatasSelect.count > 0 {
  224. NCCollectionCommon.shared.openSelectView(viewController: viewController, array: meradatasSelect)
  225. }
  226. self.tapSelect(sender: self)
  227. }
  228. )
  229. )
  230. actions.append(
  231. NCMenuAction(
  232. title: NSLocalizedString("_delete_selected_files_", comment: ""),
  233. icon: CCGraphics.changeThemingColorImage(UIImage(named: "trash"), width: 50, height: 50, color: .red),
  234. action: { menuAction in
  235. let alertController = UIAlertController(title: "", message: NSLocalizedString("_want_delete_", comment: ""), preferredStyle: .alert)
  236. alertController.addAction(UIAlertAction(title: NSLocalizedString("_yes_delete_", comment: ""), style: .default) { (action:UIAlertAction) in
  237. for ocId in selectOcId {
  238. if let metadata = NCManageDatabase.sharedInstance.getMetadataFromOcId(ocId) {
  239. NCOperationQueue.shared.delete(metadata: metadata, onlyLocal: false)
  240. }
  241. }
  242. self.tapSelect(sender: self)
  243. })
  244. alertController.addAction(UIAlertAction(title: NSLocalizedString("_remove_local_file_", comment: ""), style: .default) { (action:UIAlertAction) in
  245. for ocId in selectOcId {
  246. if let metadata = NCManageDatabase.sharedInstance.getMetadataFromOcId(ocId) {
  247. NCOperationQueue.shared.delete(metadata: metadata, onlyLocal: true)
  248. }
  249. }
  250. self.tapSelect(sender: self)
  251. })
  252. alertController.addAction(UIAlertAction(title: NSLocalizedString("_no_delete_", comment: ""), style: .default) { (action:UIAlertAction) in })
  253. self.present(alertController, animated: true, completion:nil)
  254. }
  255. )
  256. )
  257. actions.append(
  258. NCMenuAction(
  259. title: NSLocalizedString("_save_selected_files_", comment: ""),
  260. icon: CCGraphics.changeThemingColorImage(UIImage(named: "saveSelectedFiles"), width: 50, height: 50, color: NCBrandColor.sharedInstance.icon),
  261. action: { menuAction in
  262. for ocId in selectOcId {
  263. if let metadata = NCManageDatabase.sharedInstance.getMetadataFromOcId(ocId) {
  264. if metadata.typeFile == k_metadataTypeFile_image || metadata.typeFile == k_metadataTypeFile_video {
  265. NCOperationQueue.shared.download(metadata: metadata, selector: selectorSaveAlbum, setFavorite: false)
  266. }
  267. }
  268. }
  269. self.tapSelect(sender: self)
  270. }
  271. )
  272. )
  273. return actions
  274. }
  275. }