NCSectionHeaderFooter.swift 14 KB

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