NCSectionHeaderFooter.swift 6.9 KB

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