NCRichWorkspaceCommon.swift 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. @objc class NCRichWorkspaceCommon: NSObject {
  26. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  27. @objc func createViewerNextcloudText(serverUrl: String, viewController: UIViewController) {
  28. if !NextcloudKit.shared.isNetworkReachable() {
  29. let error = NKError(errorCode: NCGlobal.shared.errorInternalError, errorDescription: "_go_online_")
  30. NCContentPresenter.shared.showError(error: error)
  31. return
  32. }
  33. guard let directEditingCreator = NCManageDatabase.shared.getDirectEditingCreators(predicate: NSPredicate(format: "account == %@ AND editor == 'text'", appDelegate.account))?.first else { return }
  34. NCActivityIndicator.shared.start(backgroundView: viewController.view)
  35. let fileNamePath = CCUtility.returnFileNamePath(fromFileName: NCGlobal.shared.fileNameRichWorkspace, serverUrl: serverUrl, urlBase: appDelegate.urlBase, userId: appDelegate.userId, account: appDelegate.account)!
  36. NextcloudKit.shared.NCTextCreateFile(fileNamePath: fileNamePath, editorId: directEditingCreator.editor, creatorId: directEditingCreator.identifier, templateId: "") { account, url, _, error in
  37. NCActivityIndicator.shared.stop()
  38. if error == .success && account == self.appDelegate.account {
  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.shared.showError(error: error)
  46. }
  47. }
  48. }
  49. @objc func openViewerNextcloudText(serverUrl: String, viewController: UIViewController) {
  50. if !NextcloudKit.shared.isNetworkReachable() {
  51. let error = NKError(errorCode: NCGlobal.shared.errorInternalError, errorDescription: "_go_online_")
  52. NCContentPresenter.shared.showError(error: error)
  53. return
  54. }
  55. if let metadata = NCManageDatabase.shared.getMetadata(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@ AND fileNameView LIKE[c] %@", appDelegate.account, serverUrl, NCGlobal.shared.fileNameRichWorkspace.lowercased())) {
  56. if metadata.url == "" {
  57. NCActivityIndicator.shared.start(backgroundView: viewController.view)
  58. let fileNamePath = CCUtility.returnFileNamePath(fromFileName: metadata.fileName, serverUrl: metadata.serverUrl, urlBase: appDelegate.urlBase, userId: appDelegate.userId, account: appDelegate.account)!
  59. NextcloudKit.shared.NCTextOpenFile(fileNamePath: fileNamePath, editor: "text") { account, url, _, error in
  60. NCActivityIndicator.shared.stop()
  61. if error == .success && account == self.appDelegate.account {
  62. if let viewerRichWorkspaceWebView = UIStoryboard(name: "NCViewerRichWorkspace", bundle: nil).instantiateViewController(withIdentifier: "NCViewerRichWorkspaceWebView") as? NCViewerRichWorkspaceWebView {
  63. viewerRichWorkspaceWebView.url = url!
  64. viewerRichWorkspaceWebView.metadata = metadata
  65. viewerRichWorkspaceWebView.presentationController?.delegate = viewController as? UIAdaptivePresentationControllerDelegate
  66. viewController.present(viewerRichWorkspaceWebView, animated: true, completion: nil)
  67. }
  68. } else if error != .success {
  69. NCContentPresenter.shared.showError(error: error)
  70. }
  71. }
  72. } else {
  73. if let viewerRichWorkspaceWebView = UIStoryboard(name: "NCViewerRichWorkspace", bundle: nil).instantiateViewController(withIdentifier: "NCViewerRichWorkspaceWebView") as? NCViewerRichWorkspaceWebView {
  74. viewerRichWorkspaceWebView.url = metadata.url
  75. viewerRichWorkspaceWebView.metadata = metadata
  76. viewerRichWorkspaceWebView.presentationController?.delegate = viewController as? UIAdaptivePresentationControllerDelegate
  77. viewController.present(viewerRichWorkspaceWebView, animated: true, completion: nil)
  78. }
  79. }
  80. }
  81. }
  82. }