NCSectionHeaderMenu.swift 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  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.50)
  51. gradient.endPoint = CGPoint(x: 0, y: 1)
  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: .label)
  57. markdownParser.header.font = UIFont.systemFont(ofSize: 25)
  58. if let richWorkspaceText = richWorkspaceText {
  59. textViewRichWorkspace.attributedText = markdownParser.parse(richWorkspaceText)
  60. }
  61. textViewColor = .label
  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 = UIImage(systemName: "stop.circle")
  69. imageButtonTransfer.tintColor = .white
  70. labelTransfer.text = ""
  71. progressTransfer.progress = 0
  72. progressTransfer.tintColor = NCBrandColor.shared.brand
  73. progressTransfer.trackTintColor = NCBrandColor.shared.brand.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 = UIImage(named: metadata.iconName)
  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 touchUpInsideSwitch(_ sender: Any) {
  146. delegate?.tapButtonSwitch(sender)
  147. }
  148. @IBAction func touchUpInsideOrder(_ sender: Any) {
  149. delegate?.tapButtonOrder(sender)
  150. }
  151. @IBAction func touchUpInsideMore(_ sender: Any) {
  152. delegate?.tapButtonMore(sender)
  153. }
  154. @IBAction func touchUpTransfer(_ sender: Any) {
  155. delegate?.tapButtonTransfer(sender)
  156. }
  157. @objc func touchUpInsideViewRichWorkspace(_ sender: Any) {
  158. delegate?.tapRichWorkspace(sender)
  159. }
  160. }
  161. protocol NCSectionHeaderMenuDelegate: AnyObject {
  162. func tapButtonSwitch(_ sender: Any)
  163. func tapButtonOrder(_ sender: Any)
  164. func tapButtonMore(_ sender: Any)
  165. func tapButtonTransfer(_ sender: Any)
  166. func tapRichWorkspace(_ sender: Any)
  167. }
  168. // optional func
  169. extension NCSectionHeaderMenuDelegate {
  170. func tapButtonSwitch(_ sender: Any) {}
  171. func tapButtonOrder(_ sender: Any) {}
  172. func tapButtonMore(_ sender: Any) {}
  173. func tapButtonTransfer(_ sender: Any) {}
  174. func tapRichWorkspace(_ sender: Any) {}
  175. }
  176. class NCSectionHeader: UICollectionReusableView {
  177. @IBOutlet weak var labelSection: UILabel!
  178. override func awakeFromNib() {
  179. super.awakeFromNib()
  180. self.backgroundColor = UIColor.clear
  181. self.labelSection.text = ""
  182. }
  183. }
  184. class NCSectionFooter: UICollectionReusableView, NCSectionFooterDelegate {
  185. @IBOutlet weak var buttonSection: UIButton!
  186. @IBOutlet weak var activityIndicatorSection: UIActivityIndicatorView!
  187. @IBOutlet weak var labelSection: UILabel!
  188. @IBOutlet weak var separator: UIView!
  189. @IBOutlet weak var separatorHeightConstraint: NSLayoutConstraint!
  190. @IBOutlet weak var buttonSectionHeightConstraint: NSLayoutConstraint!
  191. weak var delegate: NCSectionFooterDelegate?
  192. var metadataForSection: NCMetadataForSection?
  193. let utilityFileSystem = NCUtilityFileSystem()
  194. override func awakeFromNib() {
  195. super.awakeFromNib()
  196. self.backgroundColor = UIColor.clear
  197. labelSection.textColor = UIColor.systemGray
  198. labelSection.text = ""
  199. separator.backgroundColor = .separator
  200. separatorHeightConstraint.constant = 0.5
  201. buttonIsHidden(true)
  202. activityIndicatorSection.isHidden = true
  203. activityIndicatorSection.color = .label
  204. }
  205. func setTitleLabel(directories: Int, files: Int, size: Int64) {
  206. var foldersText = ""
  207. var filesText = ""
  208. if directories > 1 {
  209. foldersText = "\(directories) " + NSLocalizedString("_folders_", comment: "")
  210. } else if directories == 1 {
  211. foldersText = "1 " + NSLocalizedString("_folder_", comment: "")
  212. }
  213. if files > 1 {
  214. filesText = "\(files) " + NSLocalizedString("_files_", comment: "") + " • " + utilityFileSystem.transformedSize(size)
  215. } else if files == 1 {
  216. filesText = "1 " + NSLocalizedString("_file_", comment: "") + " • " + utilityFileSystem.transformedSize(size)
  217. }
  218. if foldersText.isEmpty {
  219. labelSection.text = filesText
  220. } else if filesText.isEmpty {
  221. labelSection.text = foldersText
  222. } else {
  223. labelSection.text = foldersText + " • " + filesText
  224. }
  225. }
  226. func setTitleLabel(_ text: String) {
  227. labelSection.text = text
  228. }
  229. func setButtonText(_ text: String) {
  230. buttonSection.setTitle(text, for: .normal)
  231. }
  232. func separatorIsHidden(_ isHidden: Bool) {
  233. separator.isHidden = isHidden
  234. }
  235. func buttonIsHidden(_ isHidden: Bool) {
  236. buttonSection.isHidden = isHidden
  237. if isHidden {
  238. buttonSectionHeightConstraint.constant = 0
  239. } else {
  240. buttonSectionHeightConstraint.constant = NCGlobal.shared.heightFooterButton
  241. }
  242. }
  243. func showActivityIndicatorSection() {
  244. buttonSection.isHidden = true
  245. buttonSectionHeightConstraint.constant = NCGlobal.shared.heightFooterButton
  246. activityIndicatorSection.isHidden = false
  247. activityIndicatorSection.startAnimating()
  248. }
  249. func hideActivityIndicatorSection() {
  250. activityIndicatorSection.stopAnimating()
  251. activityIndicatorSection.isHidden = true
  252. }
  253. // MARK: - Action
  254. @IBAction func touchUpInsideButton(_ sender: Any) {
  255. delegate?.tapButtonSection(sender, metadataForSection: metadataForSection)
  256. }
  257. }
  258. protocol NCSectionFooterDelegate: AnyObject {
  259. func tapButtonSection(_ sender: Any, metadataForSection: NCMetadataForSection?)
  260. }
  261. // optional func
  262. extension NCSectionFooterDelegate {
  263. func tapButtonSection(_ sender: Any, metadataForSection: NCMetadataForSection?) {}
  264. }
  265. // https://stackoverflow.com/questions/16278463/darken-an-uiimage
  266. public extension UIImage {
  267. private enum BlendMode {
  268. case multiply // This results in colors that are at least as dark as either of the two contributing sample colors
  269. case screen // This results in colors that are at least as light as either of the two contributing sample colors
  270. }
  271. // A level of zero yeilds the original image, a level of 1 results in black
  272. func darken(level: CGFloat = 0.5) -> UIImage? {
  273. return blend(mode: .multiply, level: level)
  274. }
  275. // A level of zero yeilds the original image, a level of 1 results in white
  276. func lighten(level: CGFloat = 0.5) -> UIImage? {
  277. return blend(mode: .screen, level: level)
  278. }
  279. private func blend(mode: BlendMode, level: CGFloat) -> UIImage? {
  280. let context = CIContext(options: nil)
  281. var level = level
  282. if level < 0 {
  283. level = 0
  284. } else if level > 1 {
  285. level = 1
  286. }
  287. let filterName: String
  288. switch mode {
  289. case .multiply: // As the level increases we get less white
  290. level = abs(level - 1.0)
  291. filterName = "CIMultiplyBlendMode"
  292. case .screen: // As the level increases we get more white
  293. filterName = "CIScreenBlendMode"
  294. }
  295. let blender = CIFilter(name: filterName)!
  296. let backgroundColor = CIColor(color: UIColor(white: level, alpha: 1))
  297. guard let inputImage = CIImage(image: self) else { return nil }
  298. blender.setValue(inputImage, forKey: kCIInputImageKey)
  299. guard let backgroundImageGenerator = CIFilter(name: "CIConstantColorGenerator") else { return nil }
  300. backgroundImageGenerator.setValue(backgroundColor, forKey: kCIInputColorKey)
  301. guard let backgroundImage = backgroundImageGenerator.outputImage?.cropped(to: CGRect(origin: CGPoint.zero, size: self.size)) else { return nil }
  302. blender.setValue(backgroundImage, forKey: kCIInputBackgroundImageKey)
  303. guard let blendedImage = blender.outputImage else { return nil }
  304. guard let cgImage = context.createCGImage(blendedImage, from: blendedImage.extent) else { return nil }
  305. return UIImage(cgImage: cgImage)
  306. }
  307. }