NCCollectionCommon.swift 11 KB

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