NCSectionHeaderFooter.swift 6.9 KB

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