NCViewerRichWorkspace.swift 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. //
  2. // NCViewerRichWorkspace.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 14/01/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. import NCCommunication
  25. import MarkdownKit
  26. @objc class NCViewerRichWorkspace: UIViewController, UIAdaptivePresentationControllerDelegate {
  27. @IBOutlet weak var textView: UITextView!
  28. private let appDelegate = UIApplication.shared.delegate as! AppDelegate
  29. private let richWorkspaceCommon = NCRichWorkspaceCommon()
  30. private var markdownParser = MarkdownParser()
  31. private var textViewColor: UIColor?
  32. @objc public var richWorkspaceText: String = ""
  33. @objc public var serverUrl: String = ""
  34. override func viewDidLoad() {
  35. super.viewDidLoad()
  36. view.backgroundColor = NCBrandColor.shared.systemBackground
  37. presentationController?.delegate = self
  38. let closeItem = UIBarButtonItem(title: NSLocalizedString("_back_", comment: ""), style: .plain, target: self, action: #selector(closeItemTapped(_:)))
  39. self.navigationItem.leftBarButtonItem = closeItem
  40. let editItem = UIBarButtonItem(image: UIImage(named: "actionSheetModify"), style: UIBarButtonItem.Style.plain, target: self, action: #selector(editItemAction(_:)))
  41. self.navigationItem.rightBarButtonItem = editItem
  42. NotificationCenter.default.addObserver(self, selector: #selector(changeTheming), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterChangeTheming), object: nil)
  43. changeTheming()
  44. }
  45. override func viewWillAppear(_ animated: Bool) {
  46. super.viewWillAppear(animated)
  47. NCNetworking.shared.readFile(serverUrlFileName: serverUrl, account: appDelegate.account) { (account, metadata, errorCode, errorDescription) in
  48. if errorCode == 0 && account == self.appDelegate.account {
  49. guard let metadata = metadata else { return }
  50. NCManageDatabase.shared.setDirectory(richWorkspace: metadata.richWorkspace, serverUrl: self.serverUrl, account: account)
  51. if self.richWorkspaceText != metadata.richWorkspace && metadata.richWorkspace != nil {
  52. self.appDelegate.activeFiles?.richWorkspaceText = self.richWorkspaceText
  53. self.richWorkspaceText = metadata.richWorkspace!
  54. self.textView.attributedText = self.markdownParser.parse(metadata.richWorkspace!)
  55. }
  56. }
  57. }
  58. }
  59. override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
  60. super.traitCollectionDidChange(previousTraitCollection)
  61. if traitCollection.userInterfaceStyle == .dark {
  62. appDelegate.darkMode = true
  63. } else {
  64. appDelegate.darkMode = false
  65. }
  66. }
  67. public func presentationControllerWillDismiss(_ presentationController: UIPresentationController) {
  68. self.viewWillAppear(true)
  69. }
  70. @objc func changeTheming() {
  71. if textViewColor != NCBrandColor.shared.label {
  72. markdownParser = MarkdownParser(font: UIFont.systemFont(ofSize: 15), color: NCBrandColor.shared.label)
  73. markdownParser.header.font = UIFont.systemFont(ofSize: 25)
  74. textView.attributedText = markdownParser.parse(richWorkspaceText)
  75. textViewColor = NCBrandColor.shared.label
  76. }
  77. }
  78. @objc func closeItemTapped(_ sender: UIBarButtonItem) {
  79. self.dismiss(animated: false, completion: nil)
  80. }
  81. @IBAction func editItemAction(_ sender: Any) {
  82. richWorkspaceCommon.openViewerNextcloudText(serverUrl: serverUrl, viewController: self)
  83. }
  84. }