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. instance.createImagesThemingColor()
  29. return instance
  30. }()
  31. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  32. struct images {
  33. static var cellFileImage = UIImage()
  34. static var cellSharedImage = UIImage()
  35. static var cellCanShareImage = UIImage()
  36. static var cellShareByLinkImage = UIImage()
  37. static var cellFavouriteImage = UIImage()
  38. static var cellCommentImage = UIImage()
  39. static var cellLivePhotoImage = UIImage()
  40. static var cellOfflineFlag = UIImage()
  41. static var cellLocal = UIImage()
  42. static var cellFolderEncryptedImage = UIImage()
  43. static var cellFolderSharedWithMeImage = UIImage()
  44. static var cellFolderPublicImage = UIImage()
  45. static var cellFolderGroupImage = UIImage()
  46. static var cellFolderExternalImage = UIImage()
  47. static var cellFolderAutomaticUploadImage = UIImage()
  48. static var cellFolderImage = UIImage()
  49. static var cellCheckedYes = UIImage()
  50. static var cellCheckedNo = UIImage()
  51. static var cellButtonMore = UIImage()
  52. static var cellButtonStop = UIImage()
  53. }
  54. // MARK: -
  55. @objc func createImagesThemingColor() {
  56. images.cellFileImage = UIImage.init(named: "file")!
  57. images.cellSharedImage = CCGraphics.changeThemingColorImage(UIImage.init(named: "share"), width: 50, height: 50, color: NCBrandColor.shared.graySoft)
  58. images.cellCanShareImage = CCGraphics.changeThemingColorImage(UIImage.init(named: "share"), width: 50, height: 50, color: NCBrandColor.shared.graySoft)
  59. images.cellShareByLinkImage = CCGraphics.changeThemingColorImage(UIImage.init(named: "sharebylink"), width: 50, height: 50, color: NCBrandColor.shared.graySoft)
  60. images.cellFavouriteImage = CCGraphics.changeThemingColorImage(UIImage.init(named: "favorite"), width: 50, height: 50, color: NCBrandColor.shared.yellowFavorite)
  61. images.cellCommentImage = CCGraphics.changeThemingColorImage(UIImage.init(named: "comment"), width: 50, height: 50, color: NCBrandColor.shared.graySoft)
  62. images.cellLivePhotoImage = CCGraphics.changeThemingColorImage(UIImage.init(named: "livePhoto"), width: 50, height: 50, color: NCBrandColor.shared.textView)
  63. images.cellOfflineFlag = UIImage.init(named: "offlineFlag")!
  64. images.cellLocal = UIImage.init(named: "local")!
  65. images.cellFolderEncryptedImage = CCGraphics.changeThemingColorImage(UIImage.init(named: "folderEncrypted"), width: 600, height: 600, color: NCBrandColor.shared.brandElement)
  66. images.cellFolderSharedWithMeImage = CCGraphics.changeThemingColorImage(UIImage.init(named: "folder_shared_with_me"), width: 600, height: 600, color: NCBrandColor.shared.brandElement)
  67. images.cellFolderPublicImage = CCGraphics.changeThemingColorImage(UIImage.init(named: "folder_public"), width: 600, height: 600, color: NCBrandColor.shared.brandElement)
  68. images.cellFolderGroupImage = CCGraphics.changeThemingColorImage(UIImage.init(named: "folder_group"), width: 600, height: 600, color: NCBrandColor.shared.brandElement)
  69. images.cellFolderExternalImage = CCGraphics.changeThemingColorImage(UIImage.init(named: "folder_external"), width: 600, height: 600, color: NCBrandColor.shared.brandElement)
  70. images.cellFolderAutomaticUploadImage = CCGraphics.changeThemingColorImage(UIImage.init(named: "folderAutomaticUpload"), width: 600, height: 600, color: NCBrandColor.shared.brandElement)
  71. images.cellFolderImage = CCGraphics.changeThemingColorImage(UIImage.init(named: "folder"), width: 600, height: 600, color: NCBrandColor.shared.brandElement)
  72. images.cellCheckedYes = CCGraphics.changeThemingColorImage(UIImage.init(named: "checkedYes"), width: 50, height: 50, color: .darkGray)
  73. images.cellCheckedNo = CCGraphics.changeThemingColorImage(UIImage.init(named: "checkedNo"), width: 50, height: 50, color: NCBrandColor.shared.graySoft)
  74. images.cellButtonMore = CCGraphics.changeThemingColorImage(UIImage.init(named: "more"), width: 50, height: 50, color: NCBrandColor.shared.graySoft)
  75. images.cellButtonStop = CCGraphics.changeThemingColorImage(UIImage.init(named: "stop"), width: 50, height: 50, color: NCBrandColor.shared.graySoft)
  76. }
  77. // MARK: - NCSelect + Delegate
  78. func dismissSelect(serverUrl: String?, metadata: tableMetadata?, type: String, items: [Any], buttonType: String, overwrite: Bool) {
  79. if (serverUrl != nil && items.count > 0) {
  80. var move = true
  81. if buttonType == "done1" { move = false }
  82. for metadata in items as! [tableMetadata] {
  83. NCOperationQueue.shared.copyMove(metadata: metadata, serverUrl: serverUrl!, overwrite: overwrite, move: move)
  84. }
  85. }
  86. }
  87. func openSelectView(items: [Any]) {
  88. let navigationController = UIStoryboard.init(name: "NCSelect", bundle: nil).instantiateInitialViewController() as! UINavigationController
  89. let topViewController = navigationController.topViewController as! NCSelect
  90. var listViewController = [NCSelect]()
  91. var copyItems: [Any] = []
  92. for item in items {
  93. copyItems.append(item)
  94. }
  95. let homeUrl = NCUtility.shared.getHomeServer(urlBase: appDelegate.urlBase, account: appDelegate.account)
  96. var serverUrl = (copyItems[0] as! Nextcloud.tableMetadata).serverUrl
  97. // Setup view controllers such that the current view is of the same directory the items to be copied are in
  98. while true {
  99. // If not in the topmost directory, create a new view controller and set correct title.
  100. // If in the topmost directory, use the default view controller as the base.
  101. var viewController: NCSelect?
  102. if serverUrl != homeUrl {
  103. viewController = UIStoryboard(name: "NCSelect", bundle: nil).instantiateViewController(withIdentifier: "NCSelect.storyboard") as? NCSelect
  104. if viewController == nil {
  105. return
  106. }
  107. viewController!.titleCurrentFolder = (serverUrl as NSString).lastPathComponent
  108. } else {
  109. viewController = topViewController
  110. }
  111. guard let vc = viewController else { return }
  112. vc.delegate = self
  113. vc.hideButtonCreateFolder = false
  114. vc.selectFile = false
  115. vc.includeDirectoryE2EEncryption = false
  116. vc.includeImages = false
  117. vc.type = ""
  118. vc.titleButtonDone = NSLocalizedString("_move_", comment: "")
  119. vc.titleButtonDone1 = NSLocalizedString("_copy_",comment: "")
  120. vc.isButtonDone1Hide = false
  121. vc.isOverwriteHide = false
  122. vc.items = copyItems
  123. vc.serverUrl = serverUrl
  124. vc.navigationItem.backButtonTitle = vc.titleCurrentFolder
  125. listViewController.insert(vc, at: 0)
  126. if serverUrl != homeUrl {
  127. serverUrl = NCUtility.shared.deletingLastPathComponent(serverUrl: serverUrl, urlBase: appDelegate.urlBase, account: appDelegate.account)
  128. } else {
  129. break
  130. }
  131. }
  132. navigationController.setViewControllers(listViewController, animated: false)
  133. navigationController.modalPresentationStyle = .formSheet
  134. appDelegate.window.rootViewController?.present(navigationController, animated: true, completion: nil)
  135. }
  136. @objc func openFileViewInFolder(serverUrl: String, fileName: String) {
  137. let viewController = UIStoryboard(name: "NCFileViewInFolder", bundle: nil).instantiateInitialViewController() as! NCFileViewInFolder
  138. let navigationController = UINavigationController.init(rootViewController: viewController)
  139. let topViewController = viewController
  140. var listViewController = [NCFileViewInFolder]()
  141. var serverUrl = serverUrl
  142. let homeUrl = NCUtility.shared.getHomeServer(urlBase: appDelegate.urlBase, account: appDelegate.account)
  143. while true {
  144. var viewController: NCFileViewInFolder?
  145. if serverUrl != homeUrl {
  146. viewController = UIStoryboard(name: "NCFileViewInFolder", bundle: nil).instantiateInitialViewController() as? NCFileViewInFolder
  147. if viewController == nil {
  148. return
  149. }
  150. viewController!.titleCurrentFolder = (serverUrl as NSString).lastPathComponent
  151. } else {
  152. viewController = topViewController
  153. }
  154. guard let vc = viewController else { return }
  155. vc.serverUrl = serverUrl
  156. vc.fileName = fileName
  157. vc.navigationItem.backButtonTitle = vc.titleCurrentFolder
  158. listViewController.insert(vc, at: 0)
  159. if serverUrl != homeUrl {
  160. serverUrl = NCUtility.shared.deletingLastPathComponent(serverUrl: serverUrl, urlBase: appDelegate.urlBase, account: appDelegate.account)
  161. } else {
  162. break
  163. }
  164. }
  165. navigationController.setViewControllers(listViewController, animated: false)
  166. navigationController.modalPresentationStyle = .formSheet
  167. appDelegate.window.rootViewController?.present(navigationController, animated: true, completion: nil)
  168. }
  169. }
  170. // MARK: - List Layout
  171. class NCListLayout: UICollectionViewFlowLayout {
  172. var itemHeight: CGFloat = 60
  173. override init() {
  174. super.init()
  175. sectionHeadersPinToVisibleBounds = false
  176. minimumInteritemSpacing = 0
  177. minimumLineSpacing = 1
  178. self.scrollDirection = .vertical
  179. self.sectionInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
  180. }
  181. required init?(coder aDecoder: NSCoder) {
  182. fatalError("init(coder:) has not been implemented")
  183. }
  184. override var itemSize: CGSize {
  185. get {
  186. if let collectionView = collectionView {
  187. let itemWidth: CGFloat = collectionView.frame.width
  188. return CGSize(width: itemWidth, height: self.itemHeight)
  189. }
  190. // Default fallback
  191. return CGSize(width: 100, height: 100)
  192. }
  193. set {
  194. super.itemSize = newValue
  195. }
  196. }
  197. override func targetContentOffset(forProposedContentOffset proposedContentOffset: CGPoint) -> CGPoint {
  198. return proposedContentOffset
  199. }
  200. }
  201. // MARK: - Grid Layout
  202. class NCGridLayout: UICollectionViewFlowLayout {
  203. var heightLabelPlusButton: CGFloat = 45
  204. var marginLeftRight: CGFloat = 6
  205. var itemForLine: CGFloat = 3
  206. override init() {
  207. super.init()
  208. sectionHeadersPinToVisibleBounds = false
  209. minimumInteritemSpacing = 1
  210. minimumLineSpacing = marginLeftRight
  211. self.scrollDirection = .vertical
  212. self.sectionInset = UIEdgeInsets(top: 10, left: marginLeftRight, bottom: 0, right: marginLeftRight)
  213. }
  214. required init?(coder aDecoder: NSCoder) {
  215. fatalError("init(coder:) has not been implemented")
  216. }
  217. override var itemSize: CGSize {
  218. get {
  219. if let collectionView = collectionView {
  220. let itemWidth: CGFloat = (collectionView.frame.width - marginLeftRight * 2 - marginLeftRight * (itemForLine - 1)) / itemForLine
  221. let itemHeight: CGFloat = itemWidth + heightLabelPlusButton
  222. return CGSize(width: itemWidth, height: itemHeight)
  223. }
  224. // Default fallback
  225. return CGSize(width: 100, height: 100)
  226. }
  227. set {
  228. super.itemSize = newValue
  229. }
  230. }
  231. override func targetContentOffset(forProposedContentOffset proposedContentOffset: CGPoint) -> CGPoint {
  232. return proposedContentOffset
  233. }
  234. }