NCViewerRichWorkspace.swift 4.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. presentationController?.delegate = self
  37. let closeItem = UIBarButtonItem(title: NSLocalizedString("_back_", comment: ""), style: .plain, target: self, action: #selector(closeItemTapped(_:)))
  38. self.navigationItem.leftBarButtonItem = closeItem
  39. let editItem = UIBarButtonItem(image: UIImage(named: "actionSheetModify"), style: UIBarButtonItem.Style.plain, target: self, action: #selector(editItemAction(_:)))
  40. self.navigationItem.rightBarButtonItem = editItem
  41. NotificationCenter.default.addObserver(self, selector: #selector(changeTheming), name: NSNotification.Name(rawValue: k_notificationCenter_changeTheming), object: nil)
  42. changeTheming()
  43. }
  44. override func viewWillAppear(_ animated: Bool) {
  45. super.viewWillAppear(animated)
  46. NCNetworking.shared.readFile(serverUrlFileName: serverUrl, account: appDelegate.account) { (account, metadata, errorCode, errorDescription) in
  47. if errorCode == 0 && account == self.appDelegate.account {
  48. guard let metadata = metadata else { return }
  49. NCManageDatabase.shared.setDirectory(richWorkspace: metadata.richWorkspace, serverUrl: self.serverUrl, account: account)
  50. if self.richWorkspaceText != metadata.richWorkspace && metadata.richWorkspace != nil {
  51. self.appDelegate.activeFiles.richWorkspaceText = self.richWorkspaceText
  52. self.richWorkspaceText = metadata.richWorkspace!
  53. self.textView.attributedText = self.markdownParser.parse(metadata.richWorkspace!)
  54. }
  55. }
  56. }
  57. }
  58. public func presentationControllerWillDismiss(_ presentationController: UIPresentationController) {
  59. self.viewWillAppear(true)
  60. }
  61. @objc func changeTheming() {
  62. view.backgroundColor = NCBrandColor.shared.backgroundView
  63. if textViewColor != NCBrandColor.shared.textView {
  64. markdownParser = MarkdownParser(font: UIFont.systemFont(ofSize: 15), color: NCBrandColor.shared.textView)
  65. markdownParser.header.font = UIFont.systemFont(ofSize: 25)
  66. textView.attributedText = markdownParser.parse(richWorkspaceText)
  67. textViewColor = NCBrandColor.shared.textView
  68. }
  69. }
  70. @objc func closeItemTapped(_ sender: UIBarButtonItem) {
  71. self.dismiss(animated: false, completion: nil)
  72. }
  73. @IBAction func editItemAction(_ sender: Any) {
  74. richWorkspaceCommon.openViewerNextcloudText(serverUrl: serverUrl, viewController: self)
  75. }
  76. }