NCViewerRichWorkspace.swift 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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 UIKit
  24. import NextcloudKit
  25. import MarkdownKit
  26. @objc class NCViewerRichWorkspace: UIViewController, UIAdaptivePresentationControllerDelegate {
  27. @IBOutlet weak var textView: UITextView!
  28. private let richWorkspaceCommon = NCRichWorkspaceCommon()
  29. private var markdownParser = MarkdownParser()
  30. private var textViewColor: UIColor?
  31. var richWorkspaceText: String = ""
  32. var serverUrl: String = ""
  33. var delegate: NCCollectionViewCommon?
  34. var session: NCSession.Session {
  35. NCSession.shared.getSession(controller: self.delegate?.tabBarController)
  36. }
  37. // MARK: - View Life Cycle
  38. override func viewDidLoad() {
  39. super.viewDidLoad()
  40. view.backgroundColor = .systemBackground
  41. navigationController?.navigationBar.tintColor = NCBrandColor.shared.iconImageColor
  42. presentationController?.delegate = self
  43. let closeItem = UIBarButtonItem(title: NSLocalizedString("_back_", comment: ""), style: .plain, target: self, action: #selector(closeItemTapped(_:)))
  44. self.navigationItem.leftBarButtonItem = closeItem
  45. let editItem = UIBarButtonItem(image: NCUtility().loadImage(named: "square.and.pencil"), style: UIBarButtonItem.Style.plain, target: self, action: #selector(editItemAction(_:)))
  46. self.navigationItem.rightBarButtonItem = editItem
  47. markdownParser = MarkdownParser(font: UIFont.systemFont(ofSize: 15), color: NCBrandColor.shared.textColor)
  48. markdownParser.header.font = UIFont.systemFont(ofSize: 25)
  49. textView.attributedText = markdownParser.parse(richWorkspaceText)
  50. textViewColor = NCBrandColor.shared.textColor
  51. }
  52. override func viewDidAppear(_ animated: Bool) {
  53. super.viewDidAppear(animated)
  54. NCNetworking.shared.readFile(serverUrlFileName: self.serverUrl, account: session.account, queue: .main) { _ in
  55. } completion: { account, metadata, error in
  56. if error == .success, let metadata {
  57. NCManageDatabase.shared.updateDirectoryRichWorkspace(metadata.richWorkspace, account: account, serverUrl: self.serverUrl)
  58. if self.richWorkspaceText != metadata.richWorkspace, metadata.richWorkspace != nil {
  59. self.delegate?.richWorkspaceText = self.richWorkspaceText
  60. self.richWorkspaceText = metadata.richWorkspace!
  61. self.textView.attributedText = self.markdownParser.parse(metadata.richWorkspace!)
  62. }
  63. }
  64. }
  65. }
  66. public func presentationControllerWillDismiss(_ presentationController: UIPresentationController) {
  67. self.viewDidAppear(true)
  68. }
  69. @objc func closeItemTapped(_ sender: UIBarButtonItem) {
  70. self.dismiss(animated: false, completion: nil)
  71. }
  72. @IBAction func editItemAction(_ sender: Any) {
  73. richWorkspaceCommon.openViewerNextcloudText(serverUrl: serverUrl, viewController: self, session: session)
  74. }
  75. }