NCSectionHeaderFooter.swift 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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 buttonSwitch: UIButton!
  27. @IBOutlet weak var buttonOrder: UIButton!
  28. @IBOutlet weak var buttonMore: UIButton!
  29. @IBOutlet weak var button1: UIButton!
  30. @IBOutlet weak var button2: UIButton!
  31. @IBOutlet weak var button3: UIButton!
  32. @IBOutlet weak var viewButtonsCommand: UIView!
  33. @IBOutlet weak var viewButtonsView: UIView!
  34. @IBOutlet weak var viewSeparator: UIView!
  35. @IBOutlet weak var viewRichWorkspace: UIView!
  36. @IBOutlet weak var viewSection: UIView!
  37. @IBOutlet weak var viewButtonsCommandHeightConstraint: NSLayoutConstraint!
  38. @IBOutlet weak var viewButtonsViewHeightConstraint: NSLayoutConstraint!
  39. @IBOutlet weak var viewSeparatorHeightConstraint: NSLayoutConstraint!
  40. @IBOutlet weak var viewRichWorkspaceHeightConstraint: NSLayoutConstraint!
  41. @IBOutlet weak var viewSectionHeightConstraint: NSLayoutConstraint!
  42. @IBOutlet weak var textViewRichWorkspace: UITextView!
  43. @IBOutlet weak var labelSection: UILabel!
  44. @IBOutlet weak var separatorSection: UIView!
  45. @IBOutlet weak var separatorSectionHeightConstraint: NSLayoutConstraint!
  46. weak var delegate: NCSectionHeaderMenuDelegate?
  47. private var markdownParser = MarkdownParser()
  48. private var richWorkspaceText: String?
  49. private var textViewColor: UIColor?
  50. private let gradient: CAGradientLayer = CAGradientLayer()
  51. override func awakeFromNib() {
  52. super.awakeFromNib()
  53. backgroundColor = .clear
  54. buttonSwitch.setImage(UIImage(named: "switchList")!.image(color: NCBrandColor.shared.systemGray1, size: 25), for: .normal)
  55. buttonOrder.setTitle("", for: .normal)
  56. buttonOrder.setTitleColor(.systemBlue, for: .normal)
  57. buttonMore.setImage(UIImage(named: "more")!.image(color: NCBrandColor.shared.systemGray1, size: 25), for: .normal)
  58. button1.setImage(nil, for: .normal)
  59. button1.isHidden = true
  60. button1.backgroundColor = .clear
  61. button1.setTitleColor(.systemBlue, for: .normal)
  62. button1.layer.borderColor = NCBrandColor.shared.systemGray1.cgColor
  63. button1.layer.borderWidth = 0.3
  64. button1.layer.cornerRadius = 3
  65. button2.setImage(nil, for: .normal)
  66. button2.isHidden = true
  67. button2.backgroundColor = .clear
  68. button2.setTitleColor(.systemBlue, for: .normal)
  69. button2.layer.borderColor = NCBrandColor.shared.systemGray1.cgColor
  70. button2.layer.borderWidth = 0.3
  71. button2.layer.cornerRadius = 3
  72. button3.setImage(nil, for: .normal)
  73. button3.isHidden = true
  74. button3.backgroundColor = .clear
  75. button3.setTitleColor(.systemBlue, for: .normal)
  76. button3.layer.borderColor = NCBrandColor.shared.systemGray1.cgColor
  77. button3.layer.borderWidth = 0.3
  78. button3.layer.cornerRadius = 3
  79. // Gradient
  80. gradient.startPoint = CGPoint(x: 0, y: 0.50)
  81. gradient.endPoint = CGPoint(x: 0, y: 1)
  82. viewRichWorkspace.layer.addSublayer(gradient)
  83. let tap = UITapGestureRecognizer(target: self, action: #selector(touchUpInsideViewRichWorkspace(_:)))
  84. tap.delegate = self
  85. viewRichWorkspace?.addGestureRecognizer(tap)
  86. viewSeparatorHeightConstraint.constant = 0.5
  87. viewSeparator.backgroundColor = NCBrandColor.shared.separator
  88. markdownParser = MarkdownParser(font: UIFont.systemFont(ofSize: 15), color: NCBrandColor.shared.label)
  89. markdownParser.header.font = UIFont.systemFont(ofSize: 25)
  90. if let richWorkspaceText = richWorkspaceText {
  91. textViewRichWorkspace.attributedText = markdownParser.parse(richWorkspaceText)
  92. }
  93. textViewColor = NCBrandColor.shared.label
  94. labelSection.text = ""
  95. viewSectionHeightConstraint.constant = 0
  96. separatorSection.backgroundColor = NCBrandColor.shared.separator
  97. separatorSectionHeightConstraint.constant = 0.5
  98. }
  99. override func layoutSublayers(of layer: CALayer) {
  100. super.layoutSublayers(of: layer)
  101. gradient.frame = viewRichWorkspace.bounds
  102. }
  103. override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
  104. super.traitCollectionDidChange(previousTraitCollection)
  105. setInterfaceColor()
  106. }
  107. //MARK: - Command
  108. func setStatusButtonsCommand(enable: Bool) {
  109. button1.isEnabled = enable
  110. button2.isEnabled = enable
  111. button3.isEnabled = enable
  112. }
  113. func setButtonsCommand(heigt :CGFloat, imageButton1: UIImage? = nil, titleButton1: String? = nil, imageButton2: UIImage? = nil, titleButton2: String? = nil, imageButton3: UIImage? = nil, titleButton3: String? = nil) {
  114. viewButtonsCommandHeightConstraint.constant = heigt
  115. if heigt == 0 {
  116. viewButtonsView.isHidden = true
  117. button1.isHidden = true
  118. button2.isHidden = true
  119. button3.isHidden = true
  120. } else {
  121. viewButtonsView.isHidden = false
  122. if var image = imageButton1, let title = titleButton1 {
  123. image = image.image(color: NCBrandColor.shared.systemGray1, size: 25)
  124. button1.setImage(image, for: .normal)
  125. button1.isHidden = false
  126. button1.setTitle(title.firstUppercased, for: .normal)
  127. }
  128. if var image = imageButton2, let title = titleButton2 {
  129. image = image.image(color: NCBrandColor.shared.systemGray1, size: 25)
  130. button2.setImage(image, for: .normal)
  131. button2.isHidden = false
  132. button2.setTitle(title.firstUppercased, for: .normal)
  133. }
  134. if var image = imageButton3, let title = titleButton3 {
  135. image = image.image(color: NCBrandColor.shared.systemGray1, size: 25)
  136. button3.setImage(image, for: .normal)
  137. button3.isHidden = false
  138. button3.setTitle(title.firstUppercased, for: .normal)
  139. }
  140. }
  141. }
  142. //MARK: - View
  143. func setStatusButtonsView(enable: Bool) {
  144. buttonSwitch.isEnabled = enable
  145. buttonOrder.isEnabled = enable
  146. buttonMore.isEnabled = enable
  147. }
  148. func buttonMoreIsHidden(_ isHidden: Bool) {
  149. buttonMore.isHidden = isHidden
  150. }
  151. func setImageSwitchList() {
  152. buttonSwitch.setImage(UIImage(named: "switchList")!.image(color: NCBrandColor.shared.systemGray1, size: 50), for: .normal)
  153. }
  154. func setImageSwitchGrid() {
  155. buttonSwitch.setImage(UIImage(named: "switchGrid")!.image(color: NCBrandColor.shared.systemGray1, size: 50), for: .normal)
  156. }
  157. func setButtonsView(heigt :CGFloat) {
  158. viewButtonsViewHeightConstraint.constant = heigt
  159. if heigt == 0 {
  160. viewButtonsView.isHidden = true
  161. } else {
  162. viewButtonsView.isHidden = false
  163. }
  164. }
  165. func setSortedTitle(_ title: String) {
  166. let title = NSLocalizedString(title, comment: "")
  167. //let size = title.size(withAttributes: [.font: buttonOrder.titleLabel?.font as Any])
  168. buttonOrder.setTitle(title, for: .normal)
  169. }
  170. //MARK: - RichWorkspace
  171. func setRichWorkspaceHeight(_ size: CGFloat) {
  172. viewRichWorkspaceHeightConstraint.constant = size
  173. if size == 0 {
  174. viewRichWorkspace.isHidden = true
  175. } else {
  176. viewRichWorkspace.isHidden = false
  177. }
  178. }
  179. func setInterfaceColor() {
  180. if traitCollection.userInterfaceStyle == .dark {
  181. gradient.colors = [UIColor(white: 0, alpha: 0).cgColor, UIColor.black.cgColor]
  182. //viewSeparator.backgroundColor = UIColor(red: 0.13, green: 0.13, blue: 0.13, alpha: 1.0)
  183. } else {
  184. gradient.colors = [UIColor(white: 1, alpha: 0).cgColor, UIColor.white.cgColor]
  185. //viewSeparator.backgroundColor = UIColor(red: 0.79, green: 0.79, blue: 0.79, alpha: 1.0)
  186. }
  187. }
  188. func setRichWorkspaceText(_ text: String?) {
  189. guard let text = text else { return }
  190. if text != self.richWorkspaceText {
  191. textViewRichWorkspace.attributedText = markdownParser.parse(text)
  192. self.richWorkspaceText = text
  193. }
  194. }
  195. //MARK: - Section
  196. func setSectionHeight(_ size:CGFloat) {
  197. viewSectionHeightConstraint.constant = size
  198. if size == 0 {
  199. viewSection.isHidden = true
  200. } else {
  201. viewSection.isHidden = false
  202. }
  203. }
  204. // MARK: - Action
  205. @IBAction func touchUpInsideSwitch(_ sender: Any) {
  206. delegate?.tapButtonSwitch(sender)
  207. }
  208. @IBAction func touchUpInsideOrder(_ sender: Any) {
  209. delegate?.tapButtonOrder(sender)
  210. }
  211. @IBAction func touchUpInsideMore(_ sender: Any) {
  212. delegate?.tapButtonMore(sender)
  213. }
  214. @IBAction func touchUpInsideButton1(_ sender: Any) {
  215. delegate?.tapButton1(sender)
  216. }
  217. @IBAction func touchUpInsideButton2(_ sender: Any) {
  218. delegate?.tapButton2(sender)
  219. }
  220. @IBAction func touchUpInsideButton3(_ sender: Any) {
  221. delegate?.tapButton3(sender)
  222. }
  223. @objc func touchUpInsideViewRichWorkspace(_ sender: Any) {
  224. delegate?.tapRichWorkspace(sender)
  225. }
  226. }
  227. protocol NCSectionHeaderMenuDelegate: AnyObject {
  228. func tapButtonSwitch(_ sender: Any)
  229. func tapButtonOrder(_ sender: Any)
  230. func tapButtonMore(_ sender: Any)
  231. func tapButton1(_ sender: Any)
  232. func tapButton2(_ sender: Any)
  233. func tapButton3(_ sender: Any)
  234. func tapRichWorkspace(_ sender: Any)
  235. }
  236. // optional func
  237. extension NCSectionHeaderMenuDelegate {
  238. func tapButtonSwitch(_ sender: Any) {}
  239. func tapButtonOrder(_ sender: Any) {}
  240. func tapButtonMore(_ sender: Any) {}
  241. func tapButton1(_ sender: Any) {}
  242. func tapButton2(_ sender: Any) {}
  243. func tapButton3(_ sender: Any) {}
  244. func tapRichWorkspace(_ sender: Any) {}
  245. }
  246. class NCSectionHeader: UICollectionReusableView {
  247. @IBOutlet weak var labelSection: UILabel!
  248. @IBOutlet weak var separator: UIView!
  249. @IBOutlet weak var separatorHeightConstraint: NSLayoutConstraint!
  250. override func awakeFromNib() {
  251. super.awakeFromNib()
  252. self.backgroundColor = UIColor.clear
  253. self.labelSection.text = ""
  254. separator.backgroundColor = NCBrandColor.shared.separator
  255. separatorHeightConstraint.constant = 0.5
  256. }
  257. }
  258. class NCSectionFooter: UICollectionReusableView {
  259. @IBOutlet weak var labelSection: UILabel!
  260. override func awakeFromNib() {
  261. super.awakeFromNib()
  262. self.backgroundColor = UIColor.clear
  263. labelSection.textColor = NCBrandColor.shared.gray
  264. }
  265. func setTitleLabel(directories: Int, files: Int, size: Int64) {
  266. var foldersText = ""
  267. var filesText = ""
  268. if directories > 1 {
  269. foldersText = "\(directories) " + NSLocalizedString("_folders_", comment: "")
  270. } else if directories == 1 {
  271. foldersText = "1 " + NSLocalizedString("_folder_", comment: "")
  272. }
  273. if files > 1 {
  274. filesText = "\(files) " + NSLocalizedString("_files_", comment: "") + " " + CCUtility.transformedSize(size)
  275. } else if files == 1 {
  276. filesText = "1 " + NSLocalizedString("_file_", comment: "") + " " + CCUtility.transformedSize(size)
  277. }
  278. if foldersText == "" {
  279. labelSection.text = filesText
  280. } else if filesText == "" {
  281. labelSection.text = foldersText
  282. } else {
  283. labelSection.text = foldersText + ", " + filesText
  284. }
  285. }
  286. func setTitleLabel(text: String) {
  287. labelSection.text = text
  288. }
  289. }