NCSectionHeaderMenu.swift 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. //
  2. // NCSectionHeaderFooter.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 09/10/2018.
  6. // Copyright © 2018 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 UIKit
  24. import MarkdownKit
  25. class NCSectionHeaderMenu: UICollectionReusableView, UIGestureRecognizerDelegate {
  26. @IBOutlet weak var buttonTransfer: UIButton!
  27. @IBOutlet weak var imageButtonTransfer: UIImageView!
  28. @IBOutlet weak var labelTransfer: UILabel!
  29. @IBOutlet weak var progressTransfer: UIProgressView!
  30. @IBOutlet weak var transferSeparatorBottom: UIView!
  31. @IBOutlet weak var textViewRichWorkspace: UITextView!
  32. @IBOutlet weak var labelSection: UILabel!
  33. @IBOutlet weak var viewTransfer: UIView!
  34. @IBOutlet weak var viewRichWorkspace: UIView!
  35. @IBOutlet weak var viewSection: UIView!
  36. @IBOutlet weak var viewTransferHeightConstraint: NSLayoutConstraint!
  37. @IBOutlet weak var viewRichWorkspaceHeightConstraint: NSLayoutConstraint!
  38. @IBOutlet weak var viewSectionHeightConstraint: NSLayoutConstraint!
  39. @IBOutlet weak var transferSeparatorBottomHeightConstraint: NSLayoutConstraint!
  40. weak var delegate: NCSectionHeaderMenuDelegate?
  41. let utility = NCUtility()
  42. private var markdownParser = MarkdownParser()
  43. private var richWorkspaceText: String?
  44. private var textViewColor: UIColor?
  45. private let gradient: CAGradientLayer = CAGradientLayer()
  46. override func awakeFromNib() {
  47. super.awakeFromNib()
  48. backgroundColor = .clear
  49. // Gradient
  50. gradient.startPoint = CGPoint(x: 0, y: 0.8)
  51. gradient.endPoint = CGPoint(x: 0, y: 0.9)
  52. viewRichWorkspace.layer.addSublayer(gradient)
  53. let tap = UITapGestureRecognizer(target: self, action: #selector(touchUpInsideViewRichWorkspace(_:)))
  54. tap.delegate = self
  55. viewRichWorkspace?.addGestureRecognizer(tap)
  56. markdownParser = MarkdownParser(font: UIFont.systemFont(ofSize: 15), color: NCBrandColor.shared.textColor)
  57. markdownParser.header.font = UIFont.systemFont(ofSize: 25)
  58. if let richWorkspaceText = richWorkspaceText {
  59. textViewRichWorkspace.attributedText = markdownParser.parse(richWorkspaceText)
  60. }
  61. textViewColor = NCBrandColor.shared.textColor
  62. labelSection.text = ""
  63. viewSectionHeightConstraint.constant = 0
  64. buttonTransfer.backgroundColor = .clear
  65. buttonTransfer.setImage(nil, for: .normal)
  66. buttonTransfer.layer.cornerRadius = 6
  67. buttonTransfer.layer.masksToBounds = true
  68. imageButtonTransfer.image = NCUtility().loadImage(named: "stop.circle")
  69. imageButtonTransfer.tintColor = .white
  70. labelTransfer.text = ""
  71. progressTransfer.progress = 0
  72. progressTransfer.tintColor = NCBrandColor.shared.brandElement
  73. progressTransfer.trackTintColor = NCBrandColor.shared.brandElement.withAlphaComponent(0.2)
  74. transferSeparatorBottom.backgroundColor = .separator
  75. transferSeparatorBottomHeightConstraint.constant = 0.5
  76. }
  77. override func layoutSublayers(of layer: CALayer) {
  78. super.layoutSublayers(of: layer)
  79. gradient.frame = viewRichWorkspace.bounds
  80. setInterfaceColor()
  81. }
  82. override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
  83. super.traitCollectionDidChange(previousTraitCollection)
  84. setInterfaceColor()
  85. }
  86. // MARK: - RichWorkspace
  87. func setRichWorkspaceHeight(_ size: CGFloat) {
  88. viewRichWorkspaceHeightConstraint.constant = size
  89. if size == 0 {
  90. viewRichWorkspace.isHidden = true
  91. } else {
  92. viewRichWorkspace.isHidden = false
  93. }
  94. }
  95. func setInterfaceColor() {
  96. if traitCollection.userInterfaceStyle == .dark {
  97. gradient.colors = [UIColor(white: 0, alpha: 0).cgColor, UIColor.black.cgColor]
  98. } else {
  99. gradient.colors = [UIColor(white: 1, alpha: 0).cgColor, UIColor.white.cgColor]
  100. }
  101. }
  102. func setRichWorkspaceText(_ text: String?) {
  103. guard let text = text else { return }
  104. if text != self.richWorkspaceText {
  105. textViewRichWorkspace.attributedText = markdownParser.parse(text)
  106. self.richWorkspaceText = text
  107. }
  108. }
  109. // MARK: - Transfer
  110. func setViewTransfer(isHidden: Bool, ocId: String? = nil, text: String? = nil, progress: Float? = nil) {
  111. labelTransfer.text = text
  112. viewTransfer.isHidden = isHidden
  113. progressTransfer.progress = 0
  114. if isHidden {
  115. viewTransferHeightConstraint.constant = 0
  116. } else {
  117. var image: UIImage?
  118. if let ocId,
  119. let metadata = NCManageDatabase.shared.getMetadataFromOcId(ocId) {
  120. image = utility.createFilePreviewImage(ocId: metadata.ocId, etag: metadata.etag, fileNameView: metadata.fileNameView, classFile: metadata.classFile, status: metadata.status, createPreviewMedia: true)?.darken()
  121. if image == nil {
  122. image = utility.loadImage(named: metadata.iconName, useTypeIconFile: true)
  123. buttonTransfer.backgroundColor = .lightGray
  124. } else {
  125. buttonTransfer.backgroundColor = .clear
  126. }
  127. buttonTransfer.setImage(image, for: .normal)
  128. }
  129. viewTransferHeightConstraint.constant = NCGlobal.shared.heightHeaderTransfer
  130. if let progress {
  131. progressTransfer.progress = progress
  132. }
  133. }
  134. }
  135. // MARK: - Section
  136. func setSectionHeight(_ size: CGFloat) {
  137. viewSectionHeightConstraint.constant = size
  138. if size == 0 {
  139. viewSection.isHidden = true
  140. } else {
  141. viewSection.isHidden = false
  142. }
  143. }
  144. // MARK: - Action
  145. @IBAction func touchUpTransfer(_ sender: Any) {
  146. delegate?.tapButtonTransfer(sender)
  147. }
  148. @objc func touchUpInsideViewRichWorkspace(_ sender: Any) {
  149. delegate?.tapRichWorkspace(sender)
  150. }
  151. }
  152. protocol NCSectionHeaderMenuDelegate: AnyObject {
  153. func tapButtonTransfer(_ sender: Any)
  154. func tapRichWorkspace(_ sender: Any)
  155. }
  156. // optional func
  157. extension NCSectionHeaderMenuDelegate {
  158. func tapButtonTransfer(_ sender: Any) {}
  159. func tapRichWorkspace(_ sender: Any) {}
  160. }
  161. class NCSectionHeader: UICollectionReusableView {
  162. @IBOutlet weak var labelSection: UILabel!
  163. override func awakeFromNib() {
  164. super.awakeFromNib()
  165. self.backgroundColor = UIColor.clear
  166. self.labelSection.text = ""
  167. }
  168. }
  169. class NCSectionFooter: UICollectionReusableView, NCSectionFooterDelegate {
  170. @IBOutlet weak var buttonSection: UIButton!
  171. @IBOutlet weak var activityIndicatorSection: UIActivityIndicatorView!
  172. @IBOutlet weak var labelSection: UILabel!
  173. @IBOutlet weak var separator: UIView!
  174. @IBOutlet weak var separatorHeightConstraint: NSLayoutConstraint!
  175. @IBOutlet weak var buttonSectionHeightConstraint: NSLayoutConstraint!
  176. weak var delegate: NCSectionFooterDelegate?
  177. var metadataForSection: NCMetadataForSection?
  178. let utilityFileSystem = NCUtilityFileSystem()
  179. override func awakeFromNib() {
  180. super.awakeFromNib()
  181. self.backgroundColor = .clear
  182. labelSection.textColor = NCBrandColor.shared.textColor2
  183. labelSection.text = ""
  184. separator.backgroundColor = .separator
  185. separatorHeightConstraint.constant = 0.5
  186. buttonIsHidden(true)
  187. activityIndicatorSection.isHidden = true
  188. activityIndicatorSection.color = NCBrandColor.shared.textColor
  189. }
  190. func setTitleLabel(directories: Int, files: Int, size: Int64) {
  191. var foldersText = ""
  192. var filesText = ""
  193. if directories > 1 {
  194. foldersText = "\(directories) " + NSLocalizedString("_folders_", comment: "")
  195. } else if directories == 1 {
  196. foldersText = "1 " + NSLocalizedString("_folder_", comment: "")
  197. }
  198. if files > 1 {
  199. filesText = "\(files) " + NSLocalizedString("_files_", comment: "") + " • " + utilityFileSystem.transformedSize(size)
  200. } else if files == 1 {
  201. filesText = "1 " + NSLocalizedString("_file_", comment: "") + " • " + utilityFileSystem.transformedSize(size)
  202. }
  203. if foldersText.isEmpty {
  204. labelSection.text = filesText
  205. } else if filesText.isEmpty {
  206. labelSection.text = foldersText
  207. } else {
  208. labelSection.text = foldersText + " • " + filesText
  209. }
  210. }
  211. func setTitleLabel(_ text: String) {
  212. labelSection.text = text
  213. }
  214. func setButtonText(_ text: String) {
  215. buttonSection.setTitle(text, for: .normal)
  216. }
  217. func separatorIsHidden(_ isHidden: Bool) {
  218. separator.isHidden = isHidden
  219. }
  220. func buttonIsHidden(_ isHidden: Bool) {
  221. buttonSection.isHidden = isHidden
  222. if isHidden {
  223. buttonSectionHeightConstraint.constant = 0
  224. } else {
  225. buttonSectionHeightConstraint.constant = NCGlobal.shared.heightFooterButton
  226. }
  227. }
  228. func showActivityIndicatorSection() {
  229. buttonSection.isHidden = true
  230. buttonSectionHeightConstraint.constant = NCGlobal.shared.heightFooterButton
  231. activityIndicatorSection.isHidden = false
  232. activityIndicatorSection.startAnimating()
  233. }
  234. func hideActivityIndicatorSection() {
  235. activityIndicatorSection.stopAnimating()
  236. activityIndicatorSection.isHidden = true
  237. }
  238. // MARK: - Action
  239. @IBAction func touchUpInsideButton(_ sender: Any) {
  240. delegate?.tapButtonSection(sender, metadataForSection: metadataForSection)
  241. }
  242. }
  243. protocol NCSectionFooterDelegate: AnyObject {
  244. func tapButtonSection(_ sender: Any, metadataForSection: NCMetadataForSection?)
  245. }
  246. // optional func
  247. extension NCSectionFooterDelegate {
  248. func tapButtonSection(_ sender: Any, metadataForSection: NCMetadataForSection?) {}
  249. }
  250. // https://stackoverflow.com/questions/16278463/darken-an-uiimage
  251. public extension UIImage {
  252. private enum BlendMode {
  253. case multiply // This results in colors that are at least as dark as either of the two contributing sample colors
  254. case screen // This results in colors that are at least as light as either of the two contributing sample colors
  255. }
  256. // A level of zero yeilds the original image, a level of 1 results in black
  257. func darken(level: CGFloat = 0.5) -> UIImage? {
  258. return blend(mode: .multiply, level: level)
  259. }
  260. // A level of zero yeilds the original image, a level of 1 results in white
  261. func lighten(level: CGFloat = 0.5) -> UIImage? {
  262. return blend(mode: .screen, level: level)
  263. }
  264. private func blend(mode: BlendMode, level: CGFloat) -> UIImage? {
  265. let context = CIContext(options: nil)
  266. var level = level
  267. if level < 0 {
  268. level = 0
  269. } else if level > 1 {
  270. level = 1
  271. }
  272. let filterName: String
  273. switch mode {
  274. case .multiply: // As the level increases we get less white
  275. level = abs(level - 1.0)
  276. filterName = "CIMultiplyBlendMode"
  277. case .screen: // As the level increases we get more white
  278. filterName = "CIScreenBlendMode"
  279. }
  280. let blender = CIFilter(name: filterName)!
  281. let backgroundColor = CIColor(color: UIColor(white: level, alpha: 1))
  282. guard let inputImage = CIImage(image: self) else { return nil }
  283. blender.setValue(inputImage, forKey: kCIInputImageKey)
  284. guard let backgroundImageGenerator = CIFilter(name: "CIConstantColorGenerator") else { return nil }
  285. backgroundImageGenerator.setValue(backgroundColor, forKey: kCIInputColorKey)
  286. guard let backgroundImage = backgroundImageGenerator.outputImage?.cropped(to: CGRect(origin: CGPoint.zero, size: self.size)) else { return nil }
  287. blender.setValue(backgroundImage, forKey: kCIInputBackgroundImageKey)
  288. guard let blendedImage = blender.outputImage else { return nil }
  289. guard let cgImage = context.createCGImage(blendedImage, from: blendedImage.extent) else { return nil }
  290. return UIImage(cgImage: cgImage)
  291. }
  292. }