NCSectionHeaderMenu.swift 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  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 imageButtonTransfer: UIImageView!
  31. @IBOutlet weak var labelTransfer: UILabel!
  32. @IBOutlet weak var progressTransfer: UIProgressView!
  33. @IBOutlet weak var transferSeparatorBottom: UIView!
  34. @IBOutlet weak var textViewRichWorkspace: UITextView!
  35. @IBOutlet weak var labelSection: UILabel!
  36. @IBOutlet weak var viewTransfer: UIView!
  37. @IBOutlet weak var viewButtonsView: UIView!
  38. @IBOutlet weak var viewSeparator: UIView!
  39. @IBOutlet weak var viewRichWorkspace: UIView!
  40. @IBOutlet weak var viewSection: UIView!
  41. @IBOutlet weak var viewTransferHeightConstraint: NSLayoutConstraint!
  42. @IBOutlet weak var viewButtonsViewHeightConstraint: NSLayoutConstraint!
  43. @IBOutlet weak var viewSeparatorHeightConstraint: NSLayoutConstraint!
  44. @IBOutlet weak var viewRichWorkspaceHeightConstraint: NSLayoutConstraint!
  45. @IBOutlet weak var viewSectionHeightConstraint: NSLayoutConstraint!
  46. @IBOutlet weak var transferSeparatorBottomHeightConstraint: NSLayoutConstraint!
  47. weak var delegate: NCSectionHeaderMenuDelegate?
  48. let utility = NCUtility()
  49. private var markdownParser = MarkdownParser()
  50. private var richWorkspaceText: String?
  51. private var textViewColor: UIColor?
  52. private let gradient: CAGradientLayer = CAGradientLayer()
  53. override func awakeFromNib() {
  54. super.awakeFromNib()
  55. backgroundColor = .clear
  56. buttonSwitch.setImage(UIImage(systemName: "list.bullet")!.image(color: .systemGray, size: 25), for: .normal)
  57. buttonOrder.setTitle("", for: .normal)
  58. buttonOrder.setTitleColor(.systemBlue, for: .normal)
  59. buttonMore.setImage(UIImage(named: "more")!.image(color: .systemGray, size: 25), for: .normal)
  60. // Gradient
  61. gradient.startPoint = CGPoint(x: 0, y: 0.50)
  62. gradient.endPoint = CGPoint(x: 0, y: 1)
  63. viewRichWorkspace.layer.addSublayer(gradient)
  64. let tap = UITapGestureRecognizer(target: self, action: #selector(touchUpInsideViewRichWorkspace(_:)))
  65. tap.delegate = self
  66. viewRichWorkspace?.addGestureRecognizer(tap)
  67. viewSeparatorHeightConstraint.constant = 0.5
  68. viewSeparator.backgroundColor = .separator
  69. markdownParser = MarkdownParser(font: UIFont.systemFont(ofSize: 15), color: .label)
  70. markdownParser.header.font = UIFont.systemFont(ofSize: 25)
  71. if let richWorkspaceText = richWorkspaceText {
  72. textViewRichWorkspace.attributedText = markdownParser.parse(richWorkspaceText)
  73. }
  74. textViewColor = .label
  75. labelSection.text = ""
  76. viewSectionHeightConstraint.constant = 0
  77. buttonTransfer.backgroundColor = .clear
  78. buttonTransfer.setImage(nil, for: .normal)
  79. buttonTransfer.layer.cornerRadius = 6
  80. buttonTransfer.layer.masksToBounds = true
  81. imageButtonTransfer.image = UIImage(systemName: "stop.circle")
  82. imageButtonTransfer.tintColor = .white
  83. labelTransfer.text = ""
  84. progressTransfer.progress = 0
  85. progressTransfer.tintColor = NCBrandColor.shared.brand
  86. progressTransfer.trackTintColor = NCBrandColor.shared.brand.withAlphaComponent(0.2)
  87. transferSeparatorBottom.backgroundColor = .separator
  88. transferSeparatorBottomHeightConstraint.constant = 0.5
  89. }
  90. override func layoutSublayers(of layer: CALayer) {
  91. super.layoutSublayers(of: layer)
  92. gradient.frame = viewRichWorkspace.bounds
  93. setInterfaceColor()
  94. }
  95. override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
  96. super.traitCollectionDidChange(previousTraitCollection)
  97. setInterfaceColor()
  98. }
  99. // MARK: - View
  100. func setStatusButtonsView(enable: Bool) {
  101. buttonSwitch.isEnabled = enable
  102. buttonOrder.isEnabled = enable
  103. buttonMore.isEnabled = enable
  104. }
  105. func buttonMoreIsHidden(_ isHidden: Bool) {
  106. buttonMore.isHidden = isHidden
  107. }
  108. func setImageSwitchList() {
  109. buttonSwitch.setImage(UIImage(systemName: "list.bullet")!.image(color: .systemGray, width: 20, height: 15), for: .normal)
  110. }
  111. func setImageSwitchGrid() {
  112. buttonSwitch.setImage(UIImage(systemName: "square.grid.2x2")!.image(color: .systemGray, size: 20), for: .normal)
  113. }
  114. func setButtonsView(height: CGFloat) {
  115. viewButtonsViewHeightConstraint.constant = height
  116. if height == 0 {
  117. viewButtonsView.isHidden = true
  118. } else {
  119. viewButtonsView.isHidden = false
  120. }
  121. }
  122. func setSortedTitle(_ title: String) {
  123. let title = NSLocalizedString(title, comment: "")
  124. buttonOrder.setTitle(title, for: .normal)
  125. }
  126. // MARK: - RichWorkspace
  127. func setRichWorkspaceHeight(_ size: CGFloat) {
  128. viewRichWorkspaceHeightConstraint.constant = size
  129. if size == 0 {
  130. viewRichWorkspace.isHidden = true
  131. } else {
  132. viewRichWorkspace.isHidden = false
  133. }
  134. }
  135. func setInterfaceColor() {
  136. if traitCollection.userInterfaceStyle == .dark {
  137. gradient.colors = [UIColor(white: 0, alpha: 0).cgColor, UIColor.black.cgColor]
  138. } else {
  139. gradient.colors = [UIColor(white: 1, alpha: 0).cgColor, UIColor.white.cgColor]
  140. }
  141. }
  142. func setRichWorkspaceText(_ text: String?) {
  143. guard let text = text else { return }
  144. if text != self.richWorkspaceText {
  145. textViewRichWorkspace.attributedText = markdownParser.parse(text)
  146. self.richWorkspaceText = text
  147. }
  148. }
  149. // MARK: - Transfer
  150. func setViewTransfer(isHidden: Bool, ocId: String? = nil, text: String? = nil, progress: Float? = nil) {
  151. labelTransfer.text = text
  152. viewTransfer.isHidden = isHidden
  153. progressTransfer.progress = 0
  154. if isHidden {
  155. viewTransferHeightConstraint.constant = 0
  156. } else {
  157. var image: UIImage?
  158. if let ocId,
  159. let metadata = NCManageDatabase.shared.getMetadataFromOcId(ocId) {
  160. image = utility.createFilePreviewImage(ocId: metadata.ocId, etag: metadata.etag, fileNameView: metadata.fileNameView, classFile: metadata.classFile, status: metadata.status, createPreviewMedia: true)?.darken()
  161. if image == nil {
  162. image = UIImage(named: metadata.iconName)
  163. buttonTransfer.backgroundColor = .lightGray
  164. } else {
  165. buttonTransfer.backgroundColor = .clear
  166. }
  167. buttonTransfer.setImage(image, for: .normal)
  168. }
  169. viewTransferHeightConstraint.constant = NCGlobal.shared.heightHeaderTransfer
  170. if let progress {
  171. progressTransfer.progress = progress
  172. }
  173. }
  174. }
  175. // MARK: - Section
  176. func setSectionHeight(_ size: CGFloat) {
  177. viewSectionHeightConstraint.constant = size
  178. if size == 0 {
  179. viewSection.isHidden = true
  180. } else {
  181. viewSection.isHidden = false
  182. }
  183. }
  184. // MARK: - Action
  185. @IBAction func touchUpInsideSwitch(_ sender: Any) {
  186. delegate?.tapButtonSwitch(sender)
  187. }
  188. @IBAction func touchUpInsideOrder(_ sender: Any) {
  189. delegate?.tapButtonOrder(sender)
  190. }
  191. @IBAction func touchUpInsideMore(_ sender: Any) {
  192. delegate?.tapButtonMore(sender)
  193. }
  194. @IBAction func touchUpTransfer(_ sender: Any) {
  195. delegate?.tapButtonTransfer(sender)
  196. }
  197. @objc func touchUpInsideViewRichWorkspace(_ sender: Any) {
  198. delegate?.tapRichWorkspace(sender)
  199. }
  200. }
  201. protocol NCSectionHeaderMenuDelegate: AnyObject {
  202. func tapButtonSwitch(_ sender: Any)
  203. func tapButtonOrder(_ sender: Any)
  204. func tapButtonMore(_ sender: Any)
  205. func tapButtonTransfer(_ sender: Any)
  206. func tapRichWorkspace(_ sender: Any)
  207. }
  208. // optional func
  209. extension NCSectionHeaderMenuDelegate {
  210. func tapButtonSwitch(_ sender: Any) {}
  211. func tapButtonOrder(_ sender: Any) {}
  212. func tapButtonMore(_ sender: Any) {}
  213. func tapButtonTransfer(_ sender: Any) {}
  214. func tapRichWorkspace(_ sender: Any) {}
  215. }
  216. class NCSectionHeader: UICollectionReusableView {
  217. @IBOutlet weak var labelSection: UILabel!
  218. override func awakeFromNib() {
  219. super.awakeFromNib()
  220. self.backgroundColor = UIColor.clear
  221. self.labelSection.text = ""
  222. }
  223. }
  224. class NCSectionFooter: UICollectionReusableView, NCSectionFooterDelegate {
  225. @IBOutlet weak var buttonSection: UIButton!
  226. @IBOutlet weak var activityIndicatorSection: UIActivityIndicatorView!
  227. @IBOutlet weak var labelSection: UILabel!
  228. @IBOutlet weak var separator: UIView!
  229. @IBOutlet weak var separatorHeightConstraint: NSLayoutConstraint!
  230. @IBOutlet weak var buttonSectionHeightConstraint: NSLayoutConstraint!
  231. weak var delegate: NCSectionFooterDelegate?
  232. var metadataForSection: NCMetadataForSection?
  233. let utilityFileSystem = NCUtilityFileSystem()
  234. override func awakeFromNib() {
  235. super.awakeFromNib()
  236. self.backgroundColor = UIColor.clear
  237. labelSection.textColor = UIColor.systemGray
  238. labelSection.text = ""
  239. separator.backgroundColor = .separator
  240. separatorHeightConstraint.constant = 0.5
  241. buttonIsHidden(true)
  242. activityIndicatorSection.isHidden = true
  243. activityIndicatorSection.color = .label
  244. }
  245. func setTitleLabel(directories: Int, files: Int, size: Int64) {
  246. var foldersText = ""
  247. var filesText = ""
  248. if directories > 1 {
  249. foldersText = "\(directories) " + NSLocalizedString("_folders_", comment: "")
  250. } else if directories == 1 {
  251. foldersText = "1 " + NSLocalizedString("_folder_", comment: "")
  252. }
  253. if files > 1 {
  254. filesText = "\(files) " + NSLocalizedString("_files_", comment: "") + " • " + utilityFileSystem.transformedSize(size)
  255. } else if files == 1 {
  256. filesText = "1 " + NSLocalizedString("_file_", comment: "") + " • " + utilityFileSystem.transformedSize(size)
  257. }
  258. if foldersText.isEmpty {
  259. labelSection.text = filesText
  260. } else if filesText.isEmpty {
  261. labelSection.text = foldersText
  262. } else {
  263. labelSection.text = foldersText + " • " + filesText
  264. }
  265. }
  266. func setTitleLabel(_ text: String) {
  267. labelSection.text = text
  268. }
  269. func setButtonText(_ text: String) {
  270. buttonSection.setTitle(text, for: .normal)
  271. }
  272. func separatorIsHidden(_ isHidden: Bool) {
  273. separator.isHidden = isHidden
  274. }
  275. func buttonIsHidden(_ isHidden: Bool) {
  276. buttonSection.isHidden = isHidden
  277. if isHidden {
  278. buttonSectionHeightConstraint.constant = 0
  279. } else {
  280. buttonSectionHeightConstraint.constant = NCGlobal.shared.heightFooterButton
  281. }
  282. }
  283. func showActivityIndicatorSection() {
  284. buttonSection.isHidden = true
  285. buttonSectionHeightConstraint.constant = NCGlobal.shared.heightFooterButton
  286. activityIndicatorSection.isHidden = false
  287. activityIndicatorSection.startAnimating()
  288. }
  289. func hideActivityIndicatorSection() {
  290. activityIndicatorSection.stopAnimating()
  291. activityIndicatorSection.isHidden = true
  292. }
  293. // MARK: - Action
  294. @IBAction func touchUpInsideButton(_ sender: Any) {
  295. delegate?.tapButtonSection(sender, metadataForSection: metadataForSection)
  296. }
  297. }
  298. protocol NCSectionFooterDelegate: AnyObject {
  299. func tapButtonSection(_ sender: Any, metadataForSection: NCMetadataForSection?)
  300. }
  301. // optional func
  302. extension NCSectionFooterDelegate {
  303. func tapButtonSection(_ sender: Any, metadataForSection: NCMetadataForSection?) {}
  304. }
  305. // https://stackoverflow.com/questions/16278463/darken-an-uiimage
  306. public extension UIImage {
  307. private enum BlendMode {
  308. case multiply // This results in colors that are at least as dark as either of the two contributing sample colors
  309. case screen // This results in colors that are at least as light as either of the two contributing sample colors
  310. }
  311. // A level of zero yeilds the original image, a level of 1 results in black
  312. func darken(level: CGFloat = 0.5) -> UIImage? {
  313. return blend(mode: .multiply, level: level)
  314. }
  315. // A level of zero yeilds the original image, a level of 1 results in white
  316. func lighten(level: CGFloat = 0.5) -> UIImage? {
  317. return blend(mode: .screen, level: level)
  318. }
  319. private func blend(mode: BlendMode, level: CGFloat) -> UIImage? {
  320. let context = CIContext(options: nil)
  321. var level = level
  322. if level < 0 {
  323. level = 0
  324. } else if level > 1 {
  325. level = 1
  326. }
  327. let filterName: String
  328. switch mode {
  329. case .multiply: // As the level increases we get less white
  330. level = abs(level - 1.0)
  331. filterName = "CIMultiplyBlendMode"
  332. case .screen: // As the level increases we get more white
  333. filterName = "CIScreenBlendMode"
  334. }
  335. let blender = CIFilter(name: filterName)!
  336. let backgroundColor = CIColor(color: UIColor(white: level, alpha: 1))
  337. guard let inputImage = CIImage(image: self) else { return nil }
  338. blender.setValue(inputImage, forKey: kCIInputImageKey)
  339. guard let backgroundImageGenerator = CIFilter(name: "CIConstantColorGenerator") else { return nil }
  340. backgroundImageGenerator.setValue(backgroundColor, forKey: kCIInputColorKey)
  341. guard let backgroundImage = backgroundImageGenerator.outputImage?.cropped(to: CGRect(origin: CGPoint.zero, size: self.size)) else { return nil }
  342. blender.setValue(backgroundImage, forKey: kCIInputBackgroundImageKey)
  343. guard let blendedImage = blender.outputImage else { return nil }
  344. guard let cgImage = context.createCGImage(blendedImage, from: blendedImage.extent) else { return nil }
  345. return UIImage(cgImage: cgImage)
  346. }
  347. }