NCSectionHeaderMenu.swift 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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 viewButtonsView: UIView!
  30. @IBOutlet weak var viewSeparator: UIView!
  31. @IBOutlet weak var viewRichWorkspace: UIView!
  32. @IBOutlet weak var viewSection: UIView!
  33. @IBOutlet weak var viewButtonsViewHeightConstraint: NSLayoutConstraint!
  34. @IBOutlet weak var viewSeparatorHeightConstraint: NSLayoutConstraint!
  35. @IBOutlet weak var viewRichWorkspaceHeightConstraint: NSLayoutConstraint!
  36. @IBOutlet weak var viewSectionHeightConstraint: NSLayoutConstraint!
  37. @IBOutlet weak var textViewRichWorkspace: UITextView!
  38. @IBOutlet weak var labelSection: UILabel!
  39. weak var delegate: NCSectionHeaderMenuDelegate?
  40. private var markdownParser = MarkdownParser()
  41. private var richWorkspaceText: String?
  42. private var textViewColor: UIColor?
  43. private let gradient: CAGradientLayer = CAGradientLayer()
  44. override func awakeFromNib() {
  45. super.awakeFromNib()
  46. backgroundColor = .clear
  47. buttonSwitch.setImage(UIImage(systemName: "list.bullet")!.image(color: .systemGray, size: 25), for: .normal)
  48. buttonOrder.setTitle("", for: .normal)
  49. buttonOrder.setTitleColor(.systemBlue, for: .normal)
  50. buttonMore.setImage(UIImage(named: "more")!.image(color: .systemGray, size: 25), for: .normal)
  51. // Gradient
  52. gradient.startPoint = CGPoint(x: 0, y: 0.50)
  53. gradient.endPoint = CGPoint(x: 0, y: 1)
  54. viewRichWorkspace.layer.addSublayer(gradient)
  55. let tap = UITapGestureRecognizer(target: self, action: #selector(touchUpInsideViewRichWorkspace(_:)))
  56. tap.delegate = self
  57. viewRichWorkspace?.addGestureRecognizer(tap)
  58. viewSeparatorHeightConstraint.constant = 0.5
  59. viewSeparator.backgroundColor = .separator
  60. markdownParser = MarkdownParser(font: UIFont.systemFont(ofSize: 15), color: .label)
  61. markdownParser.header.font = UIFont.systemFont(ofSize: 25)
  62. if let richWorkspaceText = richWorkspaceText {
  63. textViewRichWorkspace.attributedText = markdownParser.parse(richWorkspaceText)
  64. }
  65. textViewColor = .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. setInterfaceColor()
  73. }
  74. override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
  75. super.traitCollectionDidChange(previousTraitCollection)
  76. setInterfaceColor()
  77. }
  78. // MARK: - View
  79. func setStatusButtonsView(enable: Bool) {
  80. buttonSwitch.isEnabled = enable
  81. buttonOrder.isEnabled = enable
  82. buttonMore.isEnabled = enable
  83. }
  84. func buttonMoreIsHidden(_ isHidden: Bool) {
  85. buttonMore.isHidden = isHidden
  86. }
  87. func setImageSwitchList() {
  88. buttonSwitch.setImage(UIImage(systemName: "list.bullet")!.image(color: .systemGray, width: 20, height: 15), for: .normal)
  89. }
  90. func setImageSwitchGrid() {
  91. buttonSwitch.setImage(UIImage(systemName: "square.grid.2x2")!.image(color: .systemGray, size: 20), for: .normal)
  92. }
  93. func setButtonsView(height: CGFloat) {
  94. viewButtonsViewHeightConstraint.constant = height
  95. if height == 0 {
  96. viewButtonsView.isHidden = true
  97. } else {
  98. viewButtonsView.isHidden = false
  99. }
  100. }
  101. func setSortedTitle(_ title: String) {
  102. let title = NSLocalizedString(title, comment: "")
  103. buttonOrder.setTitle(title, for: .normal)
  104. }
  105. // MARK: - RichWorkspace
  106. func setRichWorkspaceHeight(_ size: CGFloat) {
  107. viewRichWorkspaceHeightConstraint.constant = size
  108. if size == 0 {
  109. viewRichWorkspace.isHidden = true
  110. } else {
  111. viewRichWorkspace.isHidden = false
  112. }
  113. }
  114. func setInterfaceColor() {
  115. if traitCollection.userInterfaceStyle == .dark {
  116. gradient.colors = [UIColor(white: 0, alpha: 0).cgColor, UIColor.black.cgColor]
  117. } else {
  118. gradient.colors = [UIColor(white: 1, alpha: 0).cgColor, UIColor.white.cgColor]
  119. }
  120. }
  121. func setRichWorkspaceText(_ text: String?) {
  122. guard let text = text else { return }
  123. if text != self.richWorkspaceText {
  124. textViewRichWorkspace.attributedText = markdownParser.parse(text)
  125. self.richWorkspaceText = text
  126. }
  127. }
  128. // MARK: - Section
  129. func setSectionHeight(_ size: CGFloat) {
  130. viewSectionHeightConstraint.constant = size
  131. if size == 0 {
  132. viewSection.isHidden = true
  133. } else {
  134. viewSection.isHidden = false
  135. }
  136. }
  137. // MARK: - Action
  138. @IBAction func touchUpInsideSwitch(_ sender: Any) {
  139. delegate?.tapButtonSwitch(sender)
  140. }
  141. @IBAction func touchUpInsideOrder(_ sender: Any) {
  142. delegate?.tapButtonOrder(sender)
  143. }
  144. @IBAction func touchUpInsideMore(_ sender: Any) {
  145. delegate?.tapButtonMore(sender)
  146. }
  147. @IBAction func touchUpInsideButton1(_ sender: Any) {
  148. delegate?.tapButton1(sender)
  149. }
  150. @IBAction func touchUpInsideButton2(_ sender: Any) {
  151. delegate?.tapButton2(sender)
  152. }
  153. @IBAction func touchUpInsideButton3(_ sender: Any) {
  154. delegate?.tapButton3(sender)
  155. }
  156. @objc func touchUpInsideViewRichWorkspace(_ sender: Any) {
  157. delegate?.tapRichWorkspace(sender)
  158. }
  159. }
  160. protocol NCSectionHeaderMenuDelegate: AnyObject {
  161. func tapButtonSwitch(_ sender: Any)
  162. func tapButtonOrder(_ sender: Any)
  163. func tapButtonMore(_ sender: Any)
  164. func tapButton1(_ sender: Any)
  165. func tapButton2(_ sender: Any)
  166. func tapButton3(_ sender: Any)
  167. func tapRichWorkspace(_ sender: Any)
  168. }
  169. // optional func
  170. extension NCSectionHeaderMenuDelegate {
  171. func tapButtonSwitch(_ sender: Any) {}
  172. func tapButtonOrder(_ sender: Any) {}
  173. func tapButtonMore(_ sender: Any) {}
  174. func tapButton1(_ sender: Any) {}
  175. func tapButton2(_ sender: Any) {}
  176. func tapButton3(_ sender: Any) {}
  177. func tapRichWorkspace(_ sender: Any) {}
  178. }
  179. class NCSectionHeader: UICollectionReusableView {
  180. @IBOutlet weak var labelSection: UILabel!
  181. override func awakeFromNib() {
  182. super.awakeFromNib()
  183. self.backgroundColor = UIColor.clear
  184. self.labelSection.text = ""
  185. }
  186. }
  187. class NCSectionFooter: UICollectionReusableView, NCSectionFooterDelegate {
  188. @IBOutlet weak var buttonSection: UIButton!
  189. @IBOutlet weak var activityIndicatorSection: UIActivityIndicatorView!
  190. @IBOutlet weak var labelSection: UILabel!
  191. @IBOutlet weak var separator: UIView!
  192. @IBOutlet weak var separatorHeightConstraint: NSLayoutConstraint!
  193. @IBOutlet weak var buttonSectionHeightConstraint: NSLayoutConstraint!
  194. weak var delegate: NCSectionFooterDelegate?
  195. var metadataForSection: NCMetadataForSection?
  196. override func awakeFromNib() {
  197. super.awakeFromNib()
  198. self.backgroundColor = UIColor.clear
  199. labelSection.textColor = UIColor.systemGray
  200. labelSection.text = ""
  201. separator.backgroundColor = .separator
  202. separatorHeightConstraint.constant = 0.5
  203. buttonIsHidden(true)
  204. activityIndicatorSection.isHidden = true
  205. activityIndicatorSection.color = .label
  206. }
  207. func setTitleLabel(directories: Int, files: Int, size: Int64) {
  208. var foldersText = ""
  209. var filesText = ""
  210. if directories > 1 {
  211. foldersText = "\(directories) " + NSLocalizedString("_folders_", comment: "")
  212. } else if directories == 1 {
  213. foldersText = "1 " + NSLocalizedString("_folder_", comment: "")
  214. }
  215. if files > 1 {
  216. filesText = "\(files) " + NSLocalizedString("_files_", comment: "") + " " + CCUtility.transformedSize(size)
  217. } else if files == 1 {
  218. filesText = "1 " + NSLocalizedString("_file_", comment: "") + " " + CCUtility.transformedSize(size)
  219. }
  220. if foldersText.isEmpty {
  221. labelSection.text = filesText
  222. } else if filesText.isEmpty {
  223. labelSection.text = foldersText
  224. } else {
  225. labelSection.text = foldersText + ", " + filesText
  226. }
  227. }
  228. func setTitleLabel(_ text: String) {
  229. labelSection.text = text
  230. }
  231. func setButtonText(_ text: String) {
  232. buttonSection.setTitle(text, for: .normal)
  233. }
  234. func separatorIsHidden(_ isHidden: Bool) {
  235. separator.isHidden = isHidden
  236. }
  237. func buttonIsHidden(_ isHidden: Bool) {
  238. buttonSection.isHidden = isHidden
  239. if isHidden {
  240. buttonSectionHeightConstraint.constant = 0
  241. } else {
  242. buttonSectionHeightConstraint.constant = NCGlobal.shared.heightFooterButton
  243. }
  244. }
  245. func showActivityIndicatorSection() {
  246. buttonSection.isHidden = true
  247. buttonSectionHeightConstraint.constant = NCGlobal.shared.heightFooterButton
  248. activityIndicatorSection.isHidden = false
  249. activityIndicatorSection.startAnimating()
  250. }
  251. func hideActivityIndicatorSection() {
  252. activityIndicatorSection.stopAnimating()
  253. activityIndicatorSection.isHidden = true
  254. }
  255. // MARK: - Action
  256. @IBAction func touchUpInsideButton(_ sender: Any) {
  257. delegate?.tapButtonSection(sender, metadataForSection: metadataForSection)
  258. }
  259. }
  260. protocol NCSectionFooterDelegate: AnyObject {
  261. func tapButtonSection(_ sender: Any, metadataForSection: NCMetadataForSection?)
  262. }
  263. // optional func
  264. extension NCSectionFooterDelegate {
  265. func tapButtonSection(_ sender: Any, metadataForSection: NCMetadataForSection?) {}
  266. }