NCRichWorkspaceCommon.swift 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. // swiftlint:disable force_cast
  27. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  28. // swiftlint:enable force_cast
  29. @objc func createViewerNextcloudText(serverUrl: String, viewController: UIViewController) {
  30. if !NextcloudKit.shared.isNetworkReachable() {
  31. let error = NKError(errorCode: NCGlobal.shared.errorInternalError, errorDescription: "_go_online_")
  32. NCContentPresenter.shared.showError(error: error)
  33. return
  34. }
  35. guard let directEditingCreator = NCManageDatabase.shared.getDirectEditingCreators(predicate: NSPredicate(format: "account == %@ AND editor == 'text'", appDelegate.account))?.first else { return }
  36. NCActivityIndicator.shared.start(backgroundView: viewController.view)
  37. let fileNamePath = CCUtility.returnFileNamePath(fromFileName: NCGlobal.shared.fileNameRichWorkspace, serverUrl: serverUrl, urlBase: appDelegate.urlBase, userId: appDelegate.userId, account: appDelegate.account)!
  38. NextcloudKit.shared.NCTextCreateFile(fileNamePath: fileNamePath, editorId: directEditingCreator.editor, creatorId: directEditingCreator.identifier, templateId: "") { account, url, _, error in
  39. NCActivityIndicator.shared.stop()
  40. if error == .success && account == self.appDelegate.account {
  41. if let viewerRichWorkspaceWebView = UIStoryboard(name: "NCViewerRichWorkspace", bundle: nil).instantiateViewController(withIdentifier: "NCViewerRichWorkspaceWebView") as? NCViewerRichWorkspaceWebView {
  42. viewerRichWorkspaceWebView.url = url!
  43. viewerRichWorkspaceWebView.presentationController?.delegate = viewController as? UIAdaptivePresentationControllerDelegate
  44. viewController.present(viewerRichWorkspaceWebView, animated: true, completion: nil)
  45. }
  46. } else if error != .success {
  47. NCContentPresenter.shared.showError(error: error)
  48. }
  49. }
  50. }
  51. @objc func openViewerNextcloudText(serverUrl: String, viewController: UIViewController) {
  52. if !NextcloudKit.shared.isNetworkReachable() {
  53. let error = NKError(errorCode: NCGlobal.shared.errorInternalError, errorDescription: "_go_online_")
  54. NCContentPresenter.shared.showError(error: error)
  55. return
  56. }
  57. if let metadata = NCManageDatabase.shared.getMetadata(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@ AND fileNameView LIKE[c] %@", appDelegate.account, serverUrl, NCGlobal.shared.fileNameRichWorkspace.lowercased())) {
  58. if metadata.url.isEmpty {
  59. NCActivityIndicator.shared.start(backgroundView: viewController.view)
  60. let fileNamePath = CCUtility.returnFileNamePath(fromFileName: metadata.fileName, serverUrl: metadata.serverUrl, urlBase: appDelegate.urlBase, userId: appDelegate.userId, account: appDelegate.account)!
  61. NextcloudKit.shared.NCTextOpenFile(fileNamePath: fileNamePath, editor: "text") { account, url, _, error in
  62. NCActivityIndicator.shared.stop()
  63. if error == .success && account == self.appDelegate.account {
  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.shared.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. }