NCSectionHeaderFooter.swift 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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 Foundation
  24. import MarkdownKit
  25. class NCSectionHeaderMenu: UICollectionReusableView, UIGestureRecognizerDelegate {
  26. @IBOutlet weak var buttonMore: UIButton!
  27. @IBOutlet weak var buttonSwitch: UIButton!
  28. @IBOutlet weak var buttonOrder: UIButton!
  29. @IBOutlet weak var buttonOrderWidthConstraint: NSLayoutConstraint!
  30. @IBOutlet weak var viewRichWorkspace: UIView!
  31. @IBOutlet weak var viewRichWorkspaceHeightConstraint: NSLayoutConstraint!
  32. @IBOutlet weak var textViewRichWorkspace: UITextView!
  33. @IBOutlet weak var separator: UIView!
  34. var delegate: NCSectionHeaderMenuDelegate?
  35. private var markdownParser = MarkdownParser()
  36. private var richWorkspaceText: String?
  37. private var textViewColor: UIColor?
  38. private let gradient : CAGradientLayer = CAGradientLayer()
  39. override func awakeFromNib() {
  40. super.awakeFromNib()
  41. buttonSwitch.setImage(CCGraphics.changeThemingColorImage(UIImage.init(named: "switchList"), width: 50, height: 50, color: NCBrandColor.sharedInstance.icon), for: .normal)
  42. buttonOrder.setTitle("", for: .normal)
  43. buttonOrder.setTitleColor(NCBrandColor.sharedInstance.brandElement, for: .normal)
  44. buttonMore.setImage(CCGraphics.changeThemingColorImage(UIImage.init(named: "more"), width: 50, height: 50, color: NCBrandColor.sharedInstance.icon), for: .normal)
  45. separator.backgroundColor = NCBrandColor.sharedInstance.separator
  46. self.backgroundColor = NCBrandColor.sharedInstance.backgroundView
  47. // Gradient
  48. gradient.startPoint = CGPoint(x: 0, y: 0.60)
  49. gradient.endPoint = CGPoint(x: 0, y: 1)
  50. viewRichWorkspace.layer.addSublayer(gradient)
  51. let tap = UITapGestureRecognizer(target: self, action: #selector(touchUpInsideViewRichWorkspace(_:)))
  52. tap.delegate = self
  53. viewRichWorkspace?.addGestureRecognizer(tap)
  54. NotificationCenter.default.addObserver(self, selector: #selector(changeTheming), name: NSNotification.Name(rawValue: k_notificationCenter_changeTheming), object: nil)
  55. changeTheming()
  56. }
  57. @objc func changeTheming() {
  58. if textViewColor != NCBrandColor.sharedInstance.textView {
  59. markdownParser = MarkdownParser(font: UIFont.systemFont(ofSize: 15), color: NCBrandColor.sharedInstance.textView)
  60. markdownParser.header.font = UIFont.systemFont(ofSize: 25)
  61. if let richWorkspaceText = richWorkspaceText {
  62. textViewRichWorkspace.attributedText = markdownParser.parse(richWorkspaceText)
  63. }
  64. textViewColor = NCBrandColor.sharedInstance.textView
  65. if CCUtility.getDarkMode() {
  66. gradient.colors = [UIColor.init(white: 0, alpha: 0).cgColor, UIColor.black.cgColor]
  67. } else {
  68. gradient.colors = [UIColor.init(white: 1, alpha: 0).cgColor, UIColor.white.cgColor]
  69. }
  70. }
  71. }
  72. func setTitleSorted(datasourceTitleButton: String) {
  73. let title = NSLocalizedString(datasourceTitleButton, comment: "")
  74. let size = title.size(withAttributes:[.font: buttonOrder.titleLabel?.font as Any])
  75. buttonOrder.setTitle(title, for: .normal)
  76. buttonOrderWidthConstraint.constant = size.width + 5
  77. }
  78. func setStatusButton(count: Int) {
  79. if count == 0 {
  80. buttonSwitch.isEnabled = false
  81. buttonOrder.isEnabled = false
  82. buttonMore.isEnabled = false
  83. } else {
  84. buttonSwitch.isEnabled = true
  85. buttonOrder.isEnabled = true
  86. buttonMore.isEnabled = true
  87. }
  88. }
  89. func setRichWorkspaceText(richWorkspaceText: String?) {
  90. guard let richWorkspaceText = richWorkspaceText else { return }
  91. if richWorkspaceText != self.richWorkspaceText {
  92. textViewRichWorkspace.attributedText = markdownParser.parse(richWorkspaceText)
  93. self.richWorkspaceText = richWorkspaceText
  94. }
  95. }
  96. @IBAction func touchUpInsideMore(_ sender: Any) {
  97. delegate?.tapMoreHeader(sender: sender)
  98. }
  99. @IBAction func touchUpInsideSwitch(_ sender: Any) {
  100. delegate?.tapSwitchHeader(sender: sender)
  101. }
  102. @IBAction func touchUpInsideOrder(_ sender: Any) {
  103. delegate?.tapOrderHeader(sender: sender)
  104. }
  105. @objc func touchUpInsideViewRichWorkspace(_ sender: Any) {
  106. delegate?.tapRichWorkspace(sender: sender)
  107. }
  108. }
  109. protocol NCSectionHeaderMenuDelegate {
  110. func tapSwitchHeader(sender: Any)
  111. func tapMoreHeader(sender: Any)
  112. func tapOrderHeader(sender: Any)
  113. func tapRichWorkspace(sender: Any)
  114. }
  115. class NCSectionHeader: UICollectionReusableView {
  116. @IBOutlet weak var labelSection: UILabel!
  117. override func awakeFromNib() {
  118. super.awakeFromNib()
  119. self.backgroundColor = NCBrandColor.sharedInstance.select
  120. }
  121. func setTitleLabel(title: String) {
  122. /*
  123. var title = ""
  124. if sectionDatasource.sections.object(at: section) is String {
  125. title = sectionDatasource.sections.object(at: section) as! String
  126. }
  127. if sectionDatasource.sections.object(at: section) is Date {
  128. let titleDate = sectionDatasource.sections.object(at: section) as! Date
  129. title = CCUtility.getTitleSectionDate(titleDate)
  130. }
  131. */
  132. if title.contains("download") {
  133. labelSection.text = NSLocalizedString("_title_section_download_", comment: "")
  134. } else if title.contains("upload") {
  135. labelSection.text = NSLocalizedString("_title_section_upload_", comment: "")
  136. } else {
  137. labelSection.text = NSLocalizedString(title, comment: "")
  138. }
  139. }
  140. }
  141. class NCSectionFooter: UICollectionReusableView {
  142. @IBOutlet weak var labelSection: UILabel!
  143. override func awakeFromNib() {
  144. super.awakeFromNib()
  145. self.backgroundColor = UIColor.clear
  146. labelSection.textColor = NCBrandColor.sharedInstance.icon
  147. }
  148. func setTitleLabel(directories: Int, files: Int, size: Double) {
  149. var foldersText = ""
  150. var filesText = ""
  151. if directories > 1 {
  152. foldersText = "\(directories) " + NSLocalizedString("_folders_", comment: "")
  153. } else if directories == 1 {
  154. foldersText = "1 " + NSLocalizedString("_folder_", comment: "")
  155. }
  156. if files > 1 {
  157. filesText = "\(files) " + NSLocalizedString("_files_", comment: "") + " " + CCUtility.transformedSize(size)
  158. } else if files == 1 {
  159. filesText = "1 " + NSLocalizedString("_file_", comment: "") + " " + CCUtility.transformedSize(size)
  160. }
  161. if foldersText == "" {
  162. labelSection.text = filesText
  163. } else if filesText == "" {
  164. labelSection.text = foldersText
  165. } else {
  166. labelSection.text = foldersText + ", " + filesText
  167. }
  168. }
  169. }