NCCollectionCommon.swift 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. let vc = navigationController.topViewController as! NCSelect
  89. var copyItems: [Any] = []
  90. for item in items {
  91. copyItems.append(item)
  92. }
  93. vc.delegate = self
  94. vc.hideButtonCreateFolder = false
  95. vc.selectFile = false
  96. vc.includeDirectoryE2EEncryption = false
  97. vc.includeImages = false
  98. vc.type = ""
  99. vc.titleButtonDone = NSLocalizedString("_move_", comment: "")
  100. vc.titleButtonDone1 = NSLocalizedString("_copy_",comment: "")
  101. vc.isButtonDone1Hide = false
  102. vc.isOverwriteHide = false
  103. vc.items = copyItems
  104. navigationController.modalPresentationStyle = .fullScreen
  105. viewController.present(navigationController, animated: true, completion: nil)
  106. }
  107. @objc func openFileViewInFolder(serverUrl: String, fileName: String) {
  108. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  109. let viewController = UIStoryboard(name: "NCFileViewInFolder", bundle: nil).instantiateInitialViewController() as! NCFileViewInFolder
  110. let navigationController = UINavigationController.init(rootViewController: viewController)
  111. viewController.serverUrl = serverUrl
  112. viewController.fileName = fileName
  113. navigationController.presentationController?.delegate = viewController
  114. appDelegate.window.rootViewController?.present(navigationController, animated: true, completion: nil)
  115. }
  116. }
  117. // MARK: - List Layout
  118. class NCListLayout: UICollectionViewFlowLayout {
  119. let itemHeight: CGFloat = 60
  120. override init() {
  121. super.init()
  122. sectionHeadersPinToVisibleBounds = false
  123. minimumInteritemSpacing = 0
  124. minimumLineSpacing = 1
  125. self.scrollDirection = .vertical
  126. self.sectionInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
  127. }
  128. required init?(coder aDecoder: NSCoder) {
  129. fatalError("init(coder:) has not been implemented")
  130. }
  131. override var itemSize: CGSize {
  132. get {
  133. if let collectionView = collectionView {
  134. let itemWidth: CGFloat = collectionView.frame.width
  135. return CGSize(width: itemWidth, height: self.itemHeight)
  136. }
  137. // Default fallback
  138. return CGSize(width: 100, height: 100)
  139. }
  140. set {
  141. super.itemSize = newValue
  142. }
  143. }
  144. override func targetContentOffset(forProposedContentOffset proposedContentOffset: CGPoint) -> CGPoint {
  145. return proposedContentOffset
  146. }
  147. }
  148. // MARK: - Grid Layout
  149. class NCGridLayout: UICollectionViewFlowLayout {
  150. var heightLabelPlusButton: CGFloat = 45
  151. var marginLeftRight: CGFloat = 6
  152. var itemForLine: CGFloat = 3
  153. override init() {
  154. super.init()
  155. sectionHeadersPinToVisibleBounds = false
  156. minimumInteritemSpacing = 1
  157. minimumLineSpacing = marginLeftRight
  158. self.scrollDirection = .vertical
  159. self.sectionInset = UIEdgeInsets(top: 10, left: marginLeftRight, bottom: 0, right: marginLeftRight)
  160. }
  161. required init?(coder aDecoder: NSCoder) {
  162. fatalError("init(coder:) has not been implemented")
  163. }
  164. override var itemSize: CGSize {
  165. get {
  166. if let collectionView = collectionView {
  167. let itemWidth: CGFloat = (collectionView.frame.width - marginLeftRight * 2 - marginLeftRight * (itemForLine - 1)) / itemForLine
  168. let itemHeight: CGFloat = itemWidth + heightLabelPlusButton
  169. return CGSize(width: itemWidth, height: itemHeight)
  170. }
  171. // Default fallback
  172. return CGSize(width: 100, height: 100)
  173. }
  174. set {
  175. super.itemSize = newValue
  176. }
  177. }
  178. override func targetContentOffset(forProposedContentOffset proposedContentOffset: CGPoint) -> CGPoint {
  179. return proposedContentOffset
  180. }
  181. }