NCDetailNavigationController.swift 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. //
  2. // NCDetailNavigationController.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 07/02/2020.
  6. // Copyright © 2020 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. class NCDetailNavigationController: UINavigationController {
  25. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  26. var progressView: UIProgressView?
  27. let progressHeight: CGFloat = 1
  28. override func viewDidLoad() {
  29. super.viewDidLoad()
  30. NotificationCenter.default.addObserver(self, selector: #selector(self.changeTheming), name: NSNotification.Name(rawValue: k_notificationCenter_changeTheming), object: nil)
  31. NotificationCenter.default.addObserver(self, selector: #selector(self.triggerProgressTask(_:)), name: NSNotification.Name(rawValue: k_notificationCenter_progressTask), object:nil)
  32. NotificationCenter.default.addObserver(self, selector: #selector(self.downloadFile(_:)), name: NSNotification.Name(rawValue: k_notificationCenter_downloadFile), object: nil)
  33. changeTheming()
  34. }
  35. override func viewWillAppear(_ animated: Bool) {
  36. super.viewWillAppear(animated)
  37. let buttonMore = UIBarButtonItem.init(image: CCGraphics.changeThemingColorImage(UIImage(named: "more"), width: 50, height: 50, color: NCBrandColor.sharedInstance.textView), style: .plain, target: self, action: #selector(self.openMenuMore))
  38. topViewController?.navigationItem.rightBarButtonItem = buttonMore
  39. topViewController?.navigationItem.leftBarButtonItem = nil
  40. if let splitViewController = self.splitViewController {
  41. if !splitViewController.isCollapsed {
  42. topViewController?.navigationItem.leftBarButtonItem = splitViewController.displayModeButtonItem
  43. }
  44. }
  45. setProgressBar()
  46. }
  47. //MARK: - NotificationCenter
  48. @objc func changeTheming() {
  49. navigationBar.barTintColor = NCBrandColor.sharedInstance.brand
  50. navigationBar.tintColor = NCBrandColor.sharedInstance.brandText
  51. navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor:NCBrandColor.sharedInstance.brandText]
  52. }
  53. @objc func triggerProgressTask(_ notification: NSNotification) {
  54. guard let metadata = appDelegate.activeDetail.metadata else {
  55. setProgressBar()
  56. return
  57. }
  58. if let userInfo = notification.userInfo as NSDictionary? {
  59. if let account = userInfo["account"] as? String, let serverUrl = userInfo["serverUrl"] as? String, let progress = userInfo["progress"] as? Float {
  60. if account == metadata.account && serverUrl == metadata.serverUrl {
  61. self.progress(progress)
  62. }
  63. }
  64. }
  65. }
  66. @objc func downloadFile(_ notification: NSNotification) {
  67. guard let metadataDetail = appDelegate.activeDetail.metadata else { return }
  68. if let userInfo = notification.userInfo as NSDictionary? {
  69. if let metadata = userInfo["metadata"] as? tableMetadata {
  70. if metadataDetail.account == metadata.account && metadataDetail.serverUrl == metadata.serverUrl {
  71. setProgressBar()
  72. }
  73. }
  74. }
  75. }
  76. //MARK: - Button
  77. @objc func openMenuMore() {
  78. if let metadata = appDelegate.activeDetail?.metadata {
  79. self.toggleMoreMenu(viewController: self, metadata: metadata)
  80. }
  81. }
  82. //MARK: - ProgressBar
  83. @objc func setProgressBar() {
  84. if progressView != nil {
  85. progressView?.removeFromSuperview()
  86. }
  87. progressView = UIProgressView.init(progressViewStyle: .bar)
  88. progressView!.frame = CGRect(x: 0, y: navigationBar.frame.height-progressHeight, width: navigationBar.frame.width, height: progressHeight)
  89. progressView!.setProgress(0, animated: false)
  90. progressView!.tintColor = NCBrandColor.sharedInstance.icon
  91. progressView!.trackTintColor = .clear
  92. navigationBar.addSubview(progressView!)
  93. NotificationCenter.default.addObserver(self, selector: #selector(self.orientationDidChange), name: UIDevice.orientationDidChangeNotification, object: nil)
  94. }
  95. @objc func progress(_ progress: Float) {
  96. guard let progressView = self.progressView else { return }
  97. progressView.progress = progress
  98. }
  99. @objc func orientationDidChange() {
  100. guard let progressView = self.progressView else { return }
  101. progressView.frame = CGRect(x: 0, y: navigationBar.frame.height-progressHeight, width: navigationBar.frame.width, height: progressHeight)
  102. }
  103. }