NCRichWorkspaceCommon.swift 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. //
  2. // NCRichWorkspaceCommon.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 17/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. class NCRichWorkspaceCommon: NSObject {
  26. let utilityFileSystem = NCUtilityFileSystem()
  27. func createViewerNextcloudText(serverUrl: String, viewController: UIViewController, session: NCSession.Session) {
  28. if !NextcloudKit.shared.isNetworkReachable() {
  29. let error = NKError(errorCode: NCGlobal.shared.errorInternalError, errorDescription: "_go_online_")
  30. NCContentPresenter().showError(error: error)
  31. return
  32. }
  33. guard let directEditingCreator = NCManageDatabase.shared.getDirectEditingCreators(predicate: NSPredicate(format: "account == %@ AND editor == 'text'", session.account))?.first else { return }
  34. NCActivityIndicator.shared.start(backgroundView: viewController.view)
  35. let fileNamePath = utilityFileSystem.getFileNamePath(NCGlobal.shared.fileNameRichWorkspace, serverUrl: serverUrl, session: session)
  36. NextcloudKit.shared.NCTextCreateFile(fileNamePath: fileNamePath, editorId: directEditingCreator.editor, creatorId: directEditingCreator.identifier, templateId: "", account: session.account) { _, url, _, error in
  37. NCActivityIndicator.shared.stop()
  38. if error == .success {
  39. if let viewerRichWorkspaceWebView = UIStoryboard(name: "NCViewerRichWorkspace", bundle: nil).instantiateViewController(withIdentifier: "NCViewerRichWorkspaceWebView") as? NCViewerRichWorkspaceWebView {
  40. viewerRichWorkspaceWebView.url = url!
  41. viewerRichWorkspaceWebView.presentationController?.delegate = viewController as? UIAdaptivePresentationControllerDelegate
  42. viewController.present(viewerRichWorkspaceWebView, animated: true, completion: nil)
  43. }
  44. } else if error != .success {
  45. NCContentPresenter().showError(error: error)
  46. }
  47. }
  48. }
  49. func openViewerNextcloudText(serverUrl: String, viewController: UIViewController, session: NCSession.Session) {
  50. if !NextcloudKit.shared.isNetworkReachable() {
  51. let error = NKError(errorCode: NCGlobal.shared.errorInternalError, errorDescription: "_go_online_")
  52. return NCContentPresenter().showError(error: error)
  53. }
  54. if let metadata = NCManageDatabase.shared.getMetadata(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@ AND fileNameView LIKE[c] %@",
  55. session.account,
  56. serverUrl,
  57. NCGlobal.shared.fileNameRichWorkspace.lowercased())) {
  58. if metadata.url.isEmpty {
  59. NCActivityIndicator.shared.start(backgroundView: viewController.view)
  60. let fileNamePath = utilityFileSystem.getFileNamePath(metadata.fileName, serverUrl: metadata.serverUrl, session: session)
  61. NextcloudKit.shared.NCTextOpenFile(fileNamePath: fileNamePath, editor: "text", account: metadata.account) { _, url, _, error in
  62. NCActivityIndicator.shared.stop()
  63. if error == .success {
  64. if let viewerRichWorkspaceWebView = UIStoryboard(name: "NCViewerRichWorkspace", bundle: nil).instantiateViewController(withIdentifier: "NCViewerRichWorkspaceWebView") as? NCViewerRichWorkspaceWebView {
  65. viewerRichWorkspaceWebView.url = url!
  66. viewerRichWorkspaceWebView.metadata = metadata
  67. viewerRichWorkspaceWebView.presentationController?.delegate = viewController as? UIAdaptivePresentationControllerDelegate
  68. viewController.present(viewerRichWorkspaceWebView, animated: true, completion: nil)
  69. }
  70. } else if error != .success {
  71. NCContentPresenter().showError(error: error)
  72. }
  73. }
  74. } else {
  75. if let viewerRichWorkspaceWebView = UIStoryboard(name: "NCViewerRichWorkspace", bundle: nil).instantiateViewController(withIdentifier: "NCViewerRichWorkspaceWebView") as? NCViewerRichWorkspaceWebView {
  76. viewerRichWorkspaceWebView.url = metadata.url
  77. viewerRichWorkspaceWebView.metadata = metadata
  78. viewerRichWorkspaceWebView.presentationController?.delegate = viewController as? UIAdaptivePresentationControllerDelegate
  79. viewController.present(viewerRichWorkspaceWebView, animated: true, completion: nil)
  80. }
  81. }
  82. }
  83. }
  84. }