NCSectionHeaderFooter.swift 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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 viewButtonsOne: UIView!
  33. @IBOutlet weak var viewButtonsTwo: UIView!
  34. @IBOutlet weak var viewSeparator: UIView!
  35. @IBOutlet weak var viewRichWorkspace: UIView!
  36. @IBOutlet weak var viewSection: UIView!
  37. @IBOutlet weak var viewButtonsOneHeightConstraint: NSLayoutConstraint!
  38. @IBOutlet weak var viewButtonsTwoHeightConstraint: 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.setTitle(NSLocalizedString("_upload_", comment: "").firstUppercased, for: .normal)
  61. button1.layer.borderColor = NCBrandColor.shared.systemGray1.cgColor
  62. button1.layer.borderWidth = 0.3
  63. button1.layer.cornerRadius = 3
  64. button2.setImage(nil, for: .normal)
  65. button2.isHidden = true
  66. button2.backgroundColor = .clear
  67. button2.setTitleColor(.systemBlue, for: .normal)
  68. button2.setTitle(NSLocalizedString("_folder_", comment: "").firstUppercased, 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.setTitle(NSLocalizedString("_scan_", comment: "").firstUppercased, for: .normal)
  77. button3.layer.borderColor = NCBrandColor.shared.systemGray1.cgColor
  78. button3.layer.borderWidth = 0.3
  79. button3.layer.cornerRadius = 3
  80. // Gradient
  81. gradient.startPoint = CGPoint(x: 0, y: 0.50)
  82. gradient.endPoint = CGPoint(x: 0, y: 1)
  83. viewRichWorkspace.layer.addSublayer(gradient)
  84. setGradientColor()
  85. let tap = UITapGestureRecognizer(target: self, action: #selector(touchUpInsideViewRichWorkspace(_:)))
  86. tap.delegate = self
  87. viewRichWorkspace?.addGestureRecognizer(tap)
  88. viewSeparatorHeightConstraint.constant = 0.5
  89. markdownParser = MarkdownParser(font: UIFont.systemFont(ofSize: 15), color: NCBrandColor.shared.label)
  90. markdownParser.header.font = UIFont.systemFont(ofSize: 25)
  91. if let richWorkspaceText = richWorkspaceText {
  92. textViewRichWorkspace.attributedText = markdownParser.parse(richWorkspaceText)
  93. }
  94. textViewColor = NCBrandColor.shared.label
  95. labelSection.text = ""
  96. viewSectionHeightConstraint.constant = 0
  97. }
  98. // 33
  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. setGradientColor()
  106. }
  107. //MARK: -
  108. func setGradientColor() {
  109. if traitCollection.userInterfaceStyle == .dark {
  110. gradient.colors = [UIColor(white: 0, alpha: 0).cgColor, UIColor.black.cgColor]
  111. viewSeparator.backgroundColor = UIColor(red: 0.13, green: 0.13, blue: 0.13, alpha: 1.0)
  112. } else {
  113. gradient.colors = [UIColor(white: 1, alpha: 0).cgColor, UIColor.white.cgColor]
  114. viewSeparator.backgroundColor = UIColor(red: 0.79, green: 0.79, blue: 0.79, alpha: 1.0)
  115. }
  116. }
  117. func setSortedTitle(_ title: String) {
  118. let title = NSLocalizedString(title, comment: "")
  119. //let size = title.size(withAttributes: [.font: buttonOrder.titleLabel?.font as Any])
  120. buttonOrder.setTitle(title, for: .normal)
  121. }
  122. func setRichWorkspaceText(_ text: String?) {
  123. guard let text = text else { return }
  124. if text != self.richWorkspaceText {
  125. textViewRichWorkspace.attributedText = markdownParser.parse(text)
  126. self.richWorkspaceText = text
  127. }
  128. }
  129. //MARK: -
  130. func setStatusButtonOne(count: Int) {
  131. if count == 0 {
  132. buttonSwitch.isEnabled = false
  133. buttonOrder.isEnabled = false
  134. buttonMore.isEnabled = false
  135. } else {
  136. buttonSwitch.isEnabled = true
  137. buttonOrder.isEnabled = true
  138. buttonMore.isEnabled = true
  139. }
  140. }
  141. func buttonMoreIsHidden(_ isHidden: Bool) {
  142. buttonMore.isHidden = isHidden
  143. }
  144. func setImageSwitchList() {
  145. buttonSwitch.setImage(UIImage(named: "switchList")!.image(color: NCBrandColor.shared.systemGray1, size: 50), for: .normal)
  146. }
  147. func setImageSwitchGrid() {
  148. buttonSwitch.setImage(UIImage(named: "switchGrid")!.image(color: NCBrandColor.shared.systemGray1, size: 50), for: .normal)
  149. }
  150. func setButtonsOneHeight(_ size:CGFloat) {
  151. viewButtonsOneHeightConstraint.constant = size
  152. if size == 0 {
  153. viewButtonsOne.isHidden = true
  154. } else {
  155. viewButtonsOne.isHidden = false
  156. }
  157. }
  158. func setButtonsTwoHeight(_ size:CGFloat, imageButton1: UIImage? = nil, imageButton2: UIImage? = nil, imageButton3: UIImage? = nil) {
  159. viewButtonsTwoHeightConstraint.constant = size
  160. if size == 0 {
  161. viewButtonsTwo.isHidden = true
  162. } else {
  163. viewButtonsTwo.isHidden = false
  164. }
  165. if var image = imageButton1 {
  166. image = image.image(color: NCBrandColor.shared.systemGray1, size: 25)
  167. button1.setImage(image, for: .normal)
  168. button1.isHidden = false
  169. }
  170. if var image = imageButton2 {
  171. image = image.image(color: NCBrandColor.shared.systemGray1, size: 25)
  172. button2.setImage(image, for: .normal)
  173. button2.isHidden = false
  174. }
  175. if var image = imageButton3 {
  176. image = image.image(color: NCBrandColor.shared.systemGray1, size: 25)
  177. button3.setImage(image, for: .normal)
  178. button3.isHidden = false
  179. }
  180. }
  181. func setRichWorkspaceHeight(_ size: CGFloat) {
  182. viewRichWorkspaceHeightConstraint.constant = size
  183. if size == 0 {
  184. viewRichWorkspace.isHidden = true
  185. } else {
  186. viewRichWorkspace.isHidden = false
  187. }
  188. }
  189. func setSectionHeight(_ size:CGFloat) {
  190. viewSectionHeightConstraint.constant = size
  191. if size == 0 {
  192. viewSection.isHidden = true
  193. } else {
  194. viewSection.isHidden = false
  195. }
  196. }
  197. @IBAction func touchUpInsideSwitch(_ sender: Any) {
  198. delegate?.tapButtonSwitch(sender)
  199. }
  200. @IBAction func touchUpInsideOrder(_ sender: Any) {
  201. delegate?.tapButtonOrder(sender)
  202. }
  203. @IBAction func touchUpInsideMore(_ sender: Any) {
  204. delegate?.tapButtonMore(sender)
  205. }
  206. @IBAction func touchUpInsideButton1(_ sender: Any) {
  207. delegate?.tapButton1(sender)
  208. }
  209. @IBAction func touchUpInsideButton2(_ sender: Any) {
  210. delegate?.tapButton2(sender)
  211. }
  212. @IBAction func touchUpInsideButton3(_ sender: Any) {
  213. delegate?.tapButton3(sender)
  214. }
  215. @objc func touchUpInsideViewRichWorkspace(_ sender: Any) {
  216. delegate?.tapRichWorkspace(sender)
  217. }
  218. }
  219. protocol NCSectionHeaderMenuDelegate: AnyObject {
  220. func tapButtonSwitch(_ sender: Any)
  221. func tapButtonOrder(_ sender: Any)
  222. func tapButtonMore(_ sender: Any)
  223. func tapButton1(_ sender: Any)
  224. func tapButton2(_ sender: Any)
  225. func tapButton3(_ sender: Any)
  226. func tapRichWorkspace(_ sender: Any)
  227. }
  228. // optional func
  229. extension NCSectionHeaderMenuDelegate {
  230. func tapButtonSwitch(_ sender: Any) {}
  231. func tapButtonOrder(_ sender: Any) {}
  232. func tapButtonMore(_ sender: Any) {}
  233. func tapButton1(_ sender: Any) {}
  234. func tapButton2(_ sender: Any) {}
  235. func tapButton3(_ sender: Any) {}
  236. func tapRichWorkspace(_ sender: Any) {}
  237. }
  238. class NCSectionHeader: UICollectionReusableView {
  239. @IBOutlet weak var labelSection: UILabel!
  240. override func awakeFromNib() {
  241. super.awakeFromNib()
  242. self.backgroundColor = UIColor.clear
  243. self.labelSection.text = ""
  244. }
  245. }
  246. class NCSectionFooter: UICollectionReusableView {
  247. @IBOutlet weak var labelSection: UILabel!
  248. override func awakeFromNib() {
  249. super.awakeFromNib()
  250. self.backgroundColor = UIColor.clear
  251. labelSection.textColor = NCBrandColor.shared.gray
  252. }
  253. func setTitleLabel(directories: Int, files: Int, size: Int64) {
  254. var foldersText = ""
  255. var filesText = ""
  256. if directories > 1 {
  257. foldersText = "\(directories) " + NSLocalizedString("_folders_", comment: "")
  258. } else if directories == 1 {
  259. foldersText = "1 " + NSLocalizedString("_folder_", comment: "")
  260. }
  261. if files > 1 {
  262. filesText = "\(files) " + NSLocalizedString("_files_", comment: "") + " " + CCUtility.transformedSize(size)
  263. } else if files == 1 {
  264. filesText = "1 " + NSLocalizedString("_file_", comment: "") + " " + CCUtility.transformedSize(size)
  265. }
  266. if foldersText == "" {
  267. labelSection.text = filesText
  268. } else if filesText == "" {
  269. labelSection.text = foldersText
  270. } else {
  271. labelSection.text = foldersText + ", " + filesText
  272. }
  273. }
  274. func setTitleLabel(text: String) {
  275. labelSection.text = text
  276. }
  277. }