NCSectionFirstHeader.swift 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. //
  2. // NCSectionFirstHeader.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. protocol NCSectionFirstHeaderDelegate: AnyObject {
  26. func tapRichWorkspace(_ sender: Any)
  27. }
  28. class NCSectionFirstHeader: UICollectionReusableView, UIGestureRecognizerDelegate {
  29. @IBOutlet weak var buttonTransfer: UIButton!
  30. @IBOutlet weak var imageTransfer: UIImageView!
  31. @IBOutlet weak var labelTransfer: UILabel!
  32. @IBOutlet weak var progressTransfer: UIProgressView!
  33. @IBOutlet weak var transferSeparatorBottom: UIView!
  34. @IBOutlet weak var textViewRichWorkspace: UITextView!
  35. @IBOutlet weak var labelSection: UILabel!
  36. @IBOutlet weak var viewTransfer: UIView!
  37. @IBOutlet weak var viewRichWorkspace: UIView!
  38. @IBOutlet weak var viewSection: UIView!
  39. @IBOutlet weak var viewTransferHeightConstraint: NSLayoutConstraint!
  40. @IBOutlet weak var viewRichWorkspaceHeightConstraint: NSLayoutConstraint!
  41. @IBOutlet weak var viewSectionHeightConstraint: NSLayoutConstraint!
  42. @IBOutlet weak var transferSeparatorBottomHeightConstraint: NSLayoutConstraint!
  43. weak var delegate: NCSectionFirstHeaderDelegate?
  44. let utility = NCUtility()
  45. private var markdownParser = MarkdownParser()
  46. private var richWorkspaceText: String?
  47. private var textViewColor: UIColor?
  48. private let gradient: CAGradientLayer = CAGradientLayer()
  49. override func awakeFromNib() {
  50. super.awakeFromNib()
  51. backgroundColor = .clear
  52. // Gradient
  53. gradient.startPoint = CGPoint(x: 0, y: 0.8)
  54. gradient.endPoint = CGPoint(x: 0, y: 0.9)
  55. viewRichWorkspace.layer.addSublayer(gradient)
  56. let tap = UITapGestureRecognizer(target: self, action: #selector(touchUpInsideViewRichWorkspace(_:)))
  57. tap.delegate = self
  58. viewRichWorkspace?.addGestureRecognizer(tap)
  59. markdownParser = MarkdownParser(font: UIFont.systemFont(ofSize: 15), color: NCBrandColor.shared.textColor)
  60. markdownParser.header.font = UIFont.systemFont(ofSize: 25)
  61. if let richWorkspaceText = richWorkspaceText {
  62. textViewRichWorkspace.attributedText = markdownParser.parse(richWorkspaceText)
  63. }
  64. textViewColor = NCBrandColor.shared.textColor
  65. labelSection.text = ""
  66. viewSectionHeightConstraint.constant = 0
  67. imageTransfer.tintColor = NCBrandColor.shared.iconImageColor
  68. imageTransfer.image = NCUtility().loadImage(named: "icloud.and.arrow.up")
  69. progressTransfer.progress = 0
  70. progressTransfer.tintColor = NCBrandColor.shared.iconImageColor
  71. progressTransfer.trackTintColor = NCBrandColor.shared.customer.withAlphaComponent(0.2)
  72. transferSeparatorBottom.backgroundColor = .separator
  73. transferSeparatorBottomHeightConstraint.constant = 0.5
  74. }
  75. override func layoutSublayers(of layer: CALayer) {
  76. super.layoutSublayers(of: layer)
  77. gradient.frame = viewRichWorkspace.bounds
  78. setInterfaceColor()
  79. }
  80. override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
  81. super.traitCollectionDidChange(previousTraitCollection)
  82. setInterfaceColor()
  83. }
  84. // MARK: - RichWorkspace
  85. func setRichWorkspaceHeight(_ size: CGFloat) {
  86. viewRichWorkspaceHeightConstraint.constant = size
  87. if size == 0 {
  88. viewRichWorkspace.isHidden = true
  89. } else {
  90. viewRichWorkspace.isHidden = false
  91. }
  92. }
  93. func setInterfaceColor() {
  94. if traitCollection.userInterfaceStyle == .dark {
  95. gradient.colors = [UIColor(white: 0, alpha: 0).cgColor, UIColor.black.cgColor]
  96. } else {
  97. gradient.colors = [UIColor(white: 1, alpha: 0).cgColor, UIColor.white.cgColor]
  98. }
  99. }
  100. func setRichWorkspaceText(_ text: String?) {
  101. guard let text = text else { return }
  102. if text != self.richWorkspaceText {
  103. textViewRichWorkspace.attributedText = markdownParser.parse(text)
  104. self.richWorkspaceText = text
  105. }
  106. }
  107. // MARK: - Transfer
  108. func setViewTransfer(isHidden: Bool, progress: Float? = nil) {
  109. viewTransfer.isHidden = isHidden
  110. if isHidden {
  111. viewTransferHeightConstraint.constant = 0
  112. progressTransfer.progress = 0
  113. } else {
  114. viewTransferHeightConstraint.constant = NCGlobal.shared.heightHeaderTransfer
  115. if NCTransferProgress.shared.haveUploadInForeground() {
  116. labelTransfer.text = String(format: NSLocalizedString("_upload_foreground_msg_", comment: ""), NCBrandOptions.shared.brand)
  117. if let progress {
  118. progressTransfer.progress = progress
  119. } else if let progress = NCTransferProgress.shared.getLastTransferProgressInForeground() {
  120. progressTransfer.progress = progress
  121. } else {
  122. progressTransfer.progress = 0.0
  123. }
  124. } else {
  125. labelTransfer.text = NSLocalizedString("_upload_background_msg_", comment: "")
  126. progressTransfer.progress = 0.0
  127. }
  128. }
  129. }
  130. // MARK: - Section
  131. func setSectionHeight(_ size: CGFloat) {
  132. viewSectionHeightConstraint.constant = size
  133. if size == 0 {
  134. viewSection.isHidden = true
  135. } else {
  136. viewSection.isHidden = false
  137. }
  138. }
  139. // MARK: - Action
  140. @objc func touchUpInsideViewRichWorkspace(_ sender: Any) {
  141. delegate?.tapRichWorkspace(sender)
  142. }
  143. }
  144. // https://stackoverflow.com/questions/16278463/darken-an-uiimage
  145. public extension UIImage {
  146. private enum BlendMode {
  147. case multiply // This results in colors that are at least as dark as either of the two contributing sample colors
  148. case screen // This results in colors that are at least as light as either of the two contributing sample colors
  149. }
  150. // A level of zero yeilds the original image, a level of 1 results in black
  151. func darken(level: CGFloat = 0.5) -> UIImage? {
  152. return blend(mode: .multiply, level: level)
  153. }
  154. // A level of zero yeilds the original image, a level of 1 results in white
  155. func lighten(level: CGFloat = 0.5) -> UIImage? {
  156. return blend(mode: .screen, level: level)
  157. }
  158. private func blend(mode: BlendMode, level: CGFloat) -> UIImage? {
  159. let context = CIContext(options: nil)
  160. var level = level
  161. if level < 0 {
  162. level = 0
  163. } else if level > 1 {
  164. level = 1
  165. }
  166. let filterName: String
  167. switch mode {
  168. case .multiply: // As the level increases we get less white
  169. level = abs(level - 1.0)
  170. filterName = "CIMultiplyBlendMode"
  171. case .screen: // As the level increases we get more white
  172. filterName = "CIScreenBlendMode"
  173. }
  174. let blender = CIFilter(name: filterName)!
  175. let backgroundColor = CIColor(color: UIColor(white: level, alpha: 1))
  176. guard let inputImage = CIImage(image: self) else { return nil }
  177. blender.setValue(inputImage, forKey: kCIInputImageKey)
  178. guard let backgroundImageGenerator = CIFilter(name: "CIConstantColorGenerator") else { return nil }
  179. backgroundImageGenerator.setValue(backgroundColor, forKey: kCIInputColorKey)
  180. guard let backgroundImage = backgroundImageGenerator.outputImage?.cropped(to: CGRect(origin: CGPoint.zero, size: self.size)) else { return nil }
  181. blender.setValue(backgroundImage, forKey: kCIInputBackgroundImageKey)
  182. guard let blendedImage = blender.outputImage else { return nil }
  183. guard let cgImage = context.createCGImage(blendedImage, from: blendedImage.extent) else { return nil }
  184. return UIImage(cgImage: cgImage)
  185. }
  186. }