NCSectionHeaderMenu.swift 11 KB

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