NCSectionHeaderFooter.swift 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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 buttonMore: UIButton!
  27. @IBOutlet weak var buttonSwitch: UIButton!
  28. @IBOutlet weak var buttonOrder: UIButton!
  29. @IBOutlet weak var buttonOrderWidthConstraint: NSLayoutConstraint!
  30. @IBOutlet weak var viewRichWorkspace: UIView!
  31. @IBOutlet weak var textViewRichWorkspace: UITextView!
  32. @IBOutlet weak var separator: UIView!
  33. @IBOutlet weak var separatorHeightConstraint: NSLayoutConstraint!
  34. @IBOutlet weak var viewSection: UIView!
  35. @IBOutlet weak var labelSection: UILabel!
  36. @IBOutlet weak var viewRichWorkspaceHeightConstraint: NSLayoutConstraint!
  37. @IBOutlet weak var viewSectionHeightConstraint: NSLayoutConstraint!
  38. weak var delegate: NCSectionHeaderMenuDelegate?
  39. private var markdownParser = MarkdownParser()
  40. private var richWorkspaceText: String?
  41. private var textViewColor: UIColor?
  42. private let gradient: CAGradientLayer = CAGradientLayer()
  43. override func awakeFromNib() {
  44. super.awakeFromNib()
  45. backgroundColor = .clear
  46. buttonSwitch.setImage(UIImage(named: "switchList")!.image(color: NCBrandColor.shared.gray, size: 25), for: .normal)
  47. buttonOrder.setTitle("", for: .normal)
  48. buttonOrder.setTitleColor(.systemBlue, for: .normal)
  49. buttonMore.setImage(UIImage(named: "more")!.image(color: NCBrandColor.shared.gray, size: 25), for: .normal)
  50. // Gradient
  51. gradient.startPoint = CGPoint(x: 0, y: 0.50)
  52. gradient.endPoint = CGPoint(x: 0, y: 1)
  53. viewRichWorkspace.layer.addSublayer(gradient)
  54. setGradientColor()
  55. let tap = UITapGestureRecognizer(target: self, action: #selector(touchUpInsideViewRichWorkspace(_:)))
  56. tap.delegate = self
  57. viewRichWorkspace?.addGestureRecognizer(tap)
  58. separator.backgroundColor = NCBrandColor.shared.separator
  59. separatorHeightConstraint.constant = 0.5
  60. markdownParser = MarkdownParser(font: UIFont.systemFont(ofSize: 15), color: NCBrandColor.shared.label)
  61. markdownParser.header.font = UIFont.systemFont(ofSize: 25)
  62. if let richWorkspaceText = richWorkspaceText {
  63. textViewRichWorkspace.attributedText = markdownParser.parse(richWorkspaceText)
  64. }
  65. textViewColor = NCBrandColor.shared.label
  66. labelSection.text = ""
  67. viewSectionHeightConstraint.constant = 0
  68. }
  69. override func layoutSublayers(of layer: CALayer) {
  70. super.layoutSublayers(of: layer)
  71. gradient.frame = viewRichWorkspace.bounds
  72. }
  73. override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
  74. super.traitCollectionDidChange(previousTraitCollection)
  75. setGradientColor()
  76. }
  77. func setGradientColor() {
  78. if traitCollection.userInterfaceStyle == .dark {
  79. gradient.colors = [UIColor(white: 0, alpha: 0).cgColor, UIColor.black.cgColor]
  80. } else {
  81. gradient.colors = [UIColor(white: 1, alpha: 0).cgColor, UIColor.white.cgColor]
  82. }
  83. }
  84. func setTitleSorted(datasourceTitleButton: String) {
  85. let title = NSLocalizedString(datasourceTitleButton, comment: "")
  86. let size = title.size(withAttributes: [.font: buttonOrder.titleLabel?.font as Any])
  87. buttonOrder.setTitle(title, for: .normal)
  88. buttonOrderWidthConstraint.constant = size.width + 5
  89. }
  90. func setStatusButton(count: Int) {
  91. if count == 0 {
  92. buttonSwitch.isEnabled = false
  93. buttonOrder.isEnabled = false
  94. buttonMore.isEnabled = false
  95. } else {
  96. buttonSwitch.isEnabled = true
  97. buttonOrder.isEnabled = true
  98. buttonMore.isEnabled = true
  99. }
  100. }
  101. func setRichWorkspaceText(richWorkspaceText: String?) {
  102. guard let richWorkspaceText = richWorkspaceText else { return }
  103. if richWorkspaceText != self.richWorkspaceText {
  104. textViewRichWorkspace.attributedText = markdownParser.parse(richWorkspaceText)
  105. self.richWorkspaceText = richWorkspaceText
  106. }
  107. }
  108. func setRichWorkspaceHeight(_ size: CGFloat) {
  109. viewRichWorkspaceHeightConstraint.constant = size
  110. }
  111. func setSectionHeight(_ size:CGFloat) {
  112. viewSectionHeightConstraint.constant = size
  113. if size == 0 {
  114. viewSection.isHidden = true
  115. } else {
  116. viewSection.isHidden = false
  117. }
  118. }
  119. @IBAction func touchUpInsideMore(_ sender: Any) {
  120. delegate?.tapMoreHeader(sender: sender)
  121. }
  122. @IBAction func touchUpInsideSwitch(_ sender: Any) {
  123. delegate?.tapSwitchHeader(sender: sender)
  124. }
  125. @IBAction func touchUpInsideOrder(_ sender: Any) {
  126. delegate?.tapOrderHeader(sender: sender)
  127. }
  128. @objc func touchUpInsideViewRichWorkspace(_ sender: Any) {
  129. delegate?.tapRichWorkspace(sender: sender)
  130. }
  131. }
  132. protocol NCSectionHeaderMenuDelegate: AnyObject {
  133. func tapSwitchHeader(sender: Any)
  134. func tapMoreHeader(sender: Any)
  135. func tapOrderHeader(sender: Any)
  136. func tapRichWorkspace(sender: Any)
  137. }
  138. // optional func
  139. extension NCSectionHeaderMenuDelegate {
  140. func tapSwitchHeader(sender: Any) {}
  141. func tapMoreHeader(sender: Any) {}
  142. func tapOrderHeader(sender: Any) {}
  143. func tapRichWorkspace(sender: Any) {}
  144. }
  145. class NCSectionHeader: UICollectionReusableView {
  146. @IBOutlet weak var labelSection: UILabel!
  147. override func awakeFromNib() {
  148. super.awakeFromNib()
  149. self.backgroundColor = UIColor.clear
  150. self.labelSection.text = ""
  151. }
  152. }
  153. class NCSectionFooter: UICollectionReusableView {
  154. @IBOutlet weak var labelSection: UILabel!
  155. override func awakeFromNib() {
  156. super.awakeFromNib()
  157. self.backgroundColor = UIColor.clear
  158. labelSection.textColor = NCBrandColor.shared.gray
  159. }
  160. func setTitleLabel(directories: Int, files: Int, size: Int64) {
  161. var foldersText = ""
  162. var filesText = ""
  163. if directories > 1 {
  164. foldersText = "\(directories) " + NSLocalizedString("_folders_", comment: "")
  165. } else if directories == 1 {
  166. foldersText = "1 " + NSLocalizedString("_folder_", comment: "")
  167. }
  168. if files > 1 {
  169. filesText = "\(files) " + NSLocalizedString("_files_", comment: "") + " " + CCUtility.transformedSize(size)
  170. } else if files == 1 {
  171. filesText = "1 " + NSLocalizedString("_file_", comment: "") + " " + CCUtility.transformedSize(size)
  172. }
  173. if foldersText == "" {
  174. labelSection.text = filesText
  175. } else if filesText == "" {
  176. labelSection.text = foldersText
  177. } else {
  178. labelSection.text = foldersText + ", " + filesText
  179. }
  180. }
  181. func setTitleLabel(text: String) {
  182. labelSection.text = text
  183. }
  184. }