NCCollectionCommon.swift 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. //
  2. // NCCollectionCommon.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 08/09/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 Foundation
  24. import NCCommunication
  25. class NCCollectionCommon: NSObject, NCSelectDelegate {
  26. @objc static let shared: NCCollectionCommon = {
  27. let instance = NCCollectionCommon()
  28. return instance
  29. }()
  30. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  31. // MARK: - NCSelect + Delegate
  32. func dismissSelect(serverUrl: String?, metadata: tableMetadata?, type: String, items: [Any], buttonType: String, overwrite: Bool) {
  33. if (serverUrl != nil && items.count > 0) {
  34. var move = true
  35. if buttonType == "done1" { move = false }
  36. for metadata in items as! [tableMetadata] {
  37. NCOperationQueue.shared.copyMove(metadata: metadata, serverUrl: serverUrl!, overwrite: overwrite, move: move)
  38. }
  39. }
  40. }
  41. func openSelectView(items: [Any], viewController: UIViewController) {
  42. let navigationController = UIStoryboard.init(name: "NCSelect", bundle: nil).instantiateInitialViewController() as! UINavigationController
  43. let topViewController = navigationController.topViewController as! NCSelect
  44. var listViewController = [NCSelect]()
  45. var copyItems: [Any] = []
  46. for item in items {
  47. copyItems.append(item)
  48. }
  49. let homeUrl = NCUtilityFileSystem.shared.getHomeServer(urlBase: appDelegate.urlBase, account: appDelegate.account)
  50. var serverUrl = (copyItems[0] as! Nextcloud.tableMetadata).serverUrl
  51. // Setup view controllers such that the current view is of the same directory the items to be copied are in
  52. while true {
  53. // If not in the topmost directory, create a new view controller and set correct title.
  54. // If in the topmost directory, use the default view controller as the base.
  55. var viewController: NCSelect?
  56. if serverUrl != homeUrl {
  57. viewController = UIStoryboard(name: "NCSelect", bundle: nil).instantiateViewController(withIdentifier: "NCSelect.storyboard") as? NCSelect
  58. if viewController == nil {
  59. return
  60. }
  61. viewController!.titleCurrentFolder = (serverUrl as NSString).lastPathComponent
  62. } else {
  63. viewController = topViewController
  64. }
  65. guard let vc = viewController else { return }
  66. vc.delegate = self
  67. vc.hideButtonCreateFolder = false
  68. vc.selectFile = false
  69. vc.includeDirectoryE2EEncryption = false
  70. vc.includeImages = false
  71. vc.type = ""
  72. vc.titleButtonDone = NSLocalizedString("_move_", comment: "")
  73. vc.titleButtonDone1 = NSLocalizedString("_copy_",comment: "")
  74. vc.isButtonDone1Hide = false
  75. vc.isOverwriteHide = false
  76. vc.items = copyItems
  77. vc.serverUrl = serverUrl
  78. vc.navigationItem.backButtonTitle = vc.titleCurrentFolder
  79. listViewController.insert(vc, at: 0)
  80. if serverUrl != homeUrl {
  81. serverUrl = NCUtilityFileSystem.shared.deletingLastPathComponent(serverUrl: serverUrl, urlBase: appDelegate.urlBase, account: appDelegate.account)
  82. } else {
  83. break
  84. }
  85. }
  86. navigationController.setViewControllers(listViewController, animated: false)
  87. navigationController.modalPresentationStyle = .formSheet
  88. viewController.present(navigationController, animated: true, completion: nil)
  89. }
  90. // MARK: - Context Menu Configuration
  91. @available(iOS 13.0, *)
  92. func contextMenuConfiguration(metadata: tableMetadata, viewController: UIViewController, enableDeleteLocal: Bool, enableViewInFolder: Bool) -> UIMenu {
  93. var titleDeleteConfirmFile = NSLocalizedString("_delete_file_", comment: "")
  94. if metadata.directory { titleDeleteConfirmFile = NSLocalizedString("_delete_folder_", comment: "") }
  95. var titleSave: String = NSLocalizedString("_save_selected_files_", comment: "")
  96. let metadataMOV = NCManageDatabase.shared.getMetadataLivePhoto(metadata: metadata)
  97. if metadataMOV != nil {
  98. titleSave = NSLocalizedString("_livephoto_save_", comment: "")
  99. }
  100. let copy = UIAction(title: NSLocalizedString("_copy_file_", comment: ""), image: UIImage(systemName: "doc.on.doc") ) { action in
  101. self.appDelegate.pasteboardOcIds = [metadata.ocId]
  102. NCFunctionCenter.shared.copyPasteboard()
  103. }
  104. let detail = UIAction(title: NSLocalizedString("_details_", comment: ""), image: UIImage(systemName: "info") ) { action in
  105. NCFunctionCenter.shared.openShare(ViewController: viewController, metadata: metadata, indexPage: 0)
  106. }
  107. let save = UIAction(title: titleSave, image: UIImage(systemName: "square.and.arrow.down")) { action in
  108. if metadataMOV != nil {
  109. NCFunctionCenter.shared.saveLivePhoto(metadata: metadata, metadataMOV: metadataMOV!)
  110. } else {
  111. if CCUtility.fileProviderStorageExists(metadata.ocId, fileNameView: metadata.fileNameView) {
  112. NCFunctionCenter.shared.saveAlbum(metadata: metadata)
  113. } else {
  114. NCOperationQueue.shared.download(metadata: metadata, selector: NCGlobal.shared.selectorSaveAlbum)
  115. }
  116. }
  117. }
  118. let viewInFolder = UIAction(title: NSLocalizedString("_view_in_folder_", comment: ""), image: UIImage(systemName: "arrow.forward.square")) { action in
  119. NCFunctionCenter.shared.openFileViewInFolder(serverUrl: metadata.serverUrl, fileName: metadata.fileName)
  120. }
  121. let openIn = UIAction(title: NSLocalizedString("_open_in_", comment: ""), image: UIImage(systemName: "square.and.arrow.up") ) { action in
  122. NCFunctionCenter.shared.downloadOpen(metadata: metadata, selector: NCGlobal.shared.selectorOpenIn)
  123. }
  124. let openQuickLook = UIAction(title: NSLocalizedString("_open_quicklook_", comment: ""), image: UIImage(systemName: "eye")) { action in
  125. NCFunctionCenter.shared.downloadOpen(metadata: metadata, selector: NCGlobal.shared.selectorLoadFileQuickLook)
  126. }
  127. let open = UIMenu(title: NSLocalizedString("_open_", comment: ""), image: UIImage(systemName: "square.and.arrow.up"), children: [openIn, openQuickLook])
  128. let moveCopy = UIAction(title: NSLocalizedString("_move_or_copy_", comment: ""), image: UIImage(systemName: "arrow.up.right.square")) { action in
  129. NCCollectionCommon.shared.openSelectView(items: [metadata], viewController: viewController)
  130. }
  131. let deleteConfirmFile = UIAction(title: titleDeleteConfirmFile, image: UIImage(systemName: "trash"), attributes: .destructive) { action in
  132. NCNetworking.shared.deleteMetadata(metadata, account: self.appDelegate.account, urlBase: self.appDelegate.urlBase, onlyLocal: false) { (errorCode, errorDescription) in
  133. if errorCode != 0 {
  134. NCContentPresenter.shared.messageNotification("_error_", description: errorDescription, delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, errorCode: errorCode)
  135. }
  136. }
  137. }
  138. let deleteConfirmLocal = UIAction(title: NSLocalizedString("_remove_local_file_", comment: ""), image: UIImage(systemName: "trash"), attributes: .destructive) { action in
  139. NCNetworking.shared.deleteMetadata(metadata, account: self.appDelegate.account, urlBase: self.appDelegate.urlBase, onlyLocal: true) { (errorCode, errorDescription) in
  140. }
  141. }
  142. var delete = UIMenu(title: NSLocalizedString("_delete_file_", comment: ""), image: UIImage(systemName: "trash"), options: .destructive, children: [deleteConfirmLocal, deleteConfirmFile])
  143. if !enableDeleteLocal {
  144. delete = UIMenu(title: NSLocalizedString("_delete_file_", comment: ""), image: UIImage(systemName: "trash"), options: .destructive, children: [deleteConfirmFile])
  145. }
  146. if metadata.directory {
  147. delete = UIMenu(title: NSLocalizedString("_delete_folder_", comment: ""), image: UIImage(systemName: "trash"), options: .destructive, children: [deleteConfirmFile])
  148. }
  149. // ------ MENU -----
  150. if metadata.directory {
  151. return UIMenu(title: "", children: [detail, moveCopy, delete])
  152. }
  153. var children: [UIMenuElement] = [detail, open, moveCopy, copy, delete]
  154. if metadata.typeFile == NCGlobal.shared.metadataTypeFileImage || metadata.typeFile == NCGlobal.shared.metadataTypeFileVideo {
  155. children.insert(save, at: 2)
  156. }
  157. if enableViewInFolder {
  158. children.insert(viewInFolder, at: 5)
  159. }
  160. return UIMenu(title: "", image: nil, identifier: nil, children: children)
  161. }
  162. }
  163. // MARK: - List Layout
  164. class NCListLayout: UICollectionViewFlowLayout {
  165. var itemHeight: CGFloat = 60
  166. override init() {
  167. super.init()
  168. sectionHeadersPinToVisibleBounds = false
  169. minimumInteritemSpacing = 0
  170. minimumLineSpacing = 1
  171. self.scrollDirection = .vertical
  172. self.sectionInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
  173. }
  174. required init?(coder aDecoder: NSCoder) {
  175. fatalError("init(coder:) has not been implemented")
  176. }
  177. override var itemSize: CGSize {
  178. get {
  179. if let collectionView = collectionView {
  180. let itemWidth: CGFloat = collectionView.frame.width
  181. return CGSize(width: itemWidth, height: self.itemHeight)
  182. }
  183. // Default fallback
  184. return CGSize(width: 100, height: 100)
  185. }
  186. set {
  187. super.itemSize = newValue
  188. }
  189. }
  190. override func targetContentOffset(forProposedContentOffset proposedContentOffset: CGPoint) -> CGPoint {
  191. return proposedContentOffset
  192. }
  193. }
  194. // MARK: - Grid Layout
  195. class NCGridLayout: UICollectionViewFlowLayout {
  196. var heightLabelPlusButton: CGFloat = 45
  197. var marginLeftRight: CGFloat = 6
  198. var itemForLine: CGFloat = 3
  199. var itemWidthDefault: CGFloat = 120
  200. override init() {
  201. super.init()
  202. sectionHeadersPinToVisibleBounds = false
  203. minimumInteritemSpacing = 1
  204. minimumLineSpacing = marginLeftRight
  205. self.scrollDirection = .vertical
  206. self.sectionInset = UIEdgeInsets(top: 10, left: marginLeftRight, bottom: 0, right: marginLeftRight)
  207. }
  208. required init?(coder aDecoder: NSCoder) {
  209. fatalError("init(coder:) has not been implemented")
  210. }
  211. override var itemSize: CGSize {
  212. get {
  213. if let collectionView = collectionView {
  214. if collectionView.frame.width < 400 {
  215. itemForLine = 3
  216. } else {
  217. itemForLine = collectionView.frame.width / itemWidthDefault
  218. }
  219. let itemWidth: CGFloat = (collectionView.frame.width - marginLeftRight * 2 - marginLeftRight * (itemForLine - 1)) / itemForLine
  220. let itemHeight: CGFloat = itemWidth + heightLabelPlusButton
  221. return CGSize(width: itemWidth, height: itemHeight)
  222. }
  223. // Default fallback
  224. return CGSize(width: itemWidthDefault, height: itemWidthDefault)
  225. }
  226. set {
  227. super.itemSize = newValue
  228. }
  229. }
  230. override func targetContentOffset(forProposedContentOffset proposedContentOffset: CGPoint) -> CGPoint {
  231. return proposedContentOffset
  232. }
  233. }