NCDetailViewController.swift 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. //
  2. // NCDetailViewController.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 07/02/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 WebKit
  25. import NCCommunication
  26. class NCDetailViewController: UIViewController {
  27. @IBOutlet weak var backgroundView: UIImageView!
  28. @objc var metadata: tableMetadata?
  29. @objc var selector: String?
  30. private let appDelegate = UIApplication.shared.delegate as! AppDelegate
  31. let imageNames = ["image1", "image2", "image3"]
  32. let imageTitles = ["Image 1", "Image 2", "Image 3"]
  33. var index: Int = 0
  34. required init?(coder: NSCoder) {
  35. super.init(coder: coder)
  36. appDelegate.activeDetail = self
  37. }
  38. override func viewDidLoad() {
  39. super.viewDidLoad()
  40. NotificationCenter.default.addObserver(self, selector: #selector(self.changeTheming), name: NSNotification.Name(rawValue: "changeTheming"), object: nil)
  41. changeTheming()
  42. if metadata != nil {
  43. viewFile(metadata: metadata!, selector: selector)
  44. }
  45. }
  46. override func viewDidDisappear(_ animated: Bool) {
  47. super.viewDidDisappear(animated)
  48. if appDelegate.player != nil && appDelegate.player.rate != 0 {
  49. appDelegate.player.pause()
  50. }
  51. if appDelegate.isMediaObserver {
  52. appDelegate.isMediaObserver = false
  53. NCViewerMedia.sharedInstance.removeObserver()
  54. }
  55. }
  56. func viewUnload() {
  57. if let splitViewController = self.splitViewController as? NCSplitViewController {
  58. if splitViewController.isCollapsed {
  59. if let navigationController = splitViewController.viewControllers.last as? UINavigationController {
  60. navigationController.popToRootViewController(animated: true)
  61. }
  62. } else {
  63. for view in backgroundView.subviews {
  64. view.removeFromSuperview()
  65. }
  66. self.navigationController?.navigationBar.topItem?.title = ""
  67. }
  68. }
  69. }
  70. @objc func changeTheming() {
  71. backgroundView.image = CCGraphics.changeThemingColorImage(UIImage.init(named: "logo"), multiplier: 2, color: NCBrandColor.sharedInstance.brand.withAlphaComponent(0.4))
  72. view.backgroundColor = NCBrandColor.sharedInstance.backgroundView
  73. }
  74. func subViewActive() -> UIView? {
  75. return backgroundView.subviews.first
  76. }
  77. @objc func viewFile(metadata: tableMetadata, selector: String?) {
  78. self.metadata = metadata
  79. self.selector = selector
  80. self.navigationController?.navigationBar.topItem?.title = metadata.fileNameView
  81. if FileManager().fileExists(atPath: CCUtility.getDirectoryProviderStorageIconOcId(metadata.ocId, fileNameView: metadata.fileNameView)) == false {
  82. CCGraphics.createNewImage(from: metadata.fileNameView, ocId: metadata.ocId, extension: (metadata.fileNameView as NSString).pathExtension, filterGrayScale: false, typeFile: metadata.typeFile, writeImage: true)
  83. }
  84. if appDelegate.isMediaObserver {
  85. appDelegate.isMediaObserver = false
  86. NCViewerMedia.sharedInstance.removeObserver()
  87. }
  88. // IMAGE
  89. if metadata.typeFile == k_metadataTypeFile_image {
  90. // let viewerPhotoViewController = NCViewerPhotoViewController()
  91. // self.addChild(viewerPhotoViewController)
  92. // self.backgroundView.addSubview(viewerPhotoViewController.view)
  93. // viewerPhotoViewController.didMove(toParent: self)
  94. return
  95. }
  96. // AUDIO VIDEO
  97. if metadata.typeFile == k_metadataTypeFile_audio || metadata.typeFile == k_metadataTypeFile_video {
  98. NCViewerMedia.sharedInstance.viewMedia(metadata, view: backgroundView)
  99. return
  100. }
  101. // DOCUMENT - INTERNAL VIEWER
  102. if metadata.typeFile == k_metadataTypeFile_document && selector != nil && selector == selectorLoadFileInternalView {
  103. NCViewerDocumentWeb.sharedInstance.viewDocumentWebAt(metadata, view: backgroundView)
  104. return
  105. }
  106. // DOCUMENT
  107. if metadata.typeFile == k_metadataTypeFile_document {
  108. // PDF
  109. if metadata.contentType == "application/pdf" {
  110. if #available(iOS 11.0, *) {
  111. let viewerPDF = NCViewerPDF.init(frame: backgroundView.frame)
  112. let filePath = CCUtility.getDirectoryProviderStorageOcId(metadata.ocId, fileNameView: metadata.fileNameView)!
  113. if CCUtility.fileProviderStorageExists(metadata.ocId, fileNameView: metadata.fileNameView) == false {
  114. return
  115. }
  116. viewerPDF.setupPdfView(filePath: URL(fileURLWithPath: filePath), view: backgroundView)
  117. }
  118. return
  119. }
  120. // DirectEditinf: Nextcloud Text - OnlyOffice
  121. if NCUtility.sharedInstance.isDirectEditing(metadata) != nil && appDelegate.reachability.isReachable() {
  122. let editor = NCUtility.sharedInstance.isDirectEditing(metadata)!
  123. if editor == k_editor_text || editor == k_editor_onlyoffice {
  124. NCUtility.sharedInstance.startActivityIndicator(view: backgroundView, bottom: 0)
  125. if metadata.url == "" {
  126. var customUserAgent: String?
  127. let fileNamePath = CCUtility.returnFileNamePath(fromFileName: metadata.fileName, serverUrl: metadata.serverUrl, activeUrl: appDelegate.activeUrl)!
  128. if editor == k_editor_onlyoffice {
  129. customUserAgent = NCUtility.sharedInstance.getCustomUserAgentOnlyOffice()
  130. }
  131. NCCommunication.sharedInstance.NCTextOpenFile(urlString: appDelegate.activeUrl, fileNamePath: fileNamePath, editor: editor, customUserAgent: customUserAgent, account: appDelegate.activeAccount) { (account, url, errorCode, errorMessage) in
  132. if errorCode == 0 && account == self.appDelegate.activeAccount && url != nil {
  133. let nextcloudText = NCViewerNextcloudText.init(frame: self.backgroundView.frame, configuration: WKWebViewConfiguration())
  134. nextcloudText.viewerAt(url!, metadata: metadata, editor: editor, view: self.backgroundView, viewController: self)
  135. if editor == k_editor_text && self.splitViewController!.isCollapsed {
  136. self.navigationController?.navigationItem.hidesBackButton = true
  137. }
  138. } else if errorCode != 0 {
  139. NCContentPresenter.shared.messageNotification("_error_", description: errorMessage, delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
  140. self.navigationController?.popViewController(animated: true)
  141. } else {
  142. self.navigationController?.popViewController(animated: true)
  143. }
  144. }
  145. } else {
  146. let nextcloudText = NCViewerNextcloudText.init(frame: backgroundView.frame, configuration: WKWebViewConfiguration())
  147. nextcloudText.viewerAt(metadata.url, metadata: metadata, editor: editor, view: backgroundView, viewController: self)
  148. if editor == k_editor_text && self.splitViewController!.isCollapsed {
  149. self.navigationController?.navigationItem.hidesBackButton = true
  150. }
  151. }
  152. }
  153. return
  154. }
  155. // RichDocument: Collabora
  156. if NCUtility.sharedInstance.isRichDocument(metadata) && appDelegate.reachability.isReachable() {
  157. NCUtility.sharedInstance.startActivityIndicator(view: backgroundView, bottom: 0)
  158. if metadata.url == "" {
  159. OCNetworking.sharedManager()?.createLinkRichdocuments(withAccount: appDelegate.activeAccount, fileId: metadata.fileId, completion: { (account, url, errorMessage, errorCode) in
  160. if errorCode == 0 && account == self.appDelegate.activeAccount && url != nil {
  161. let richDocument = NCViewerRichdocument.init(frame: self.backgroundView.frame, configuration: WKWebViewConfiguration())
  162. richDocument.viewRichDocumentAt(url!, metadata: metadata, view: self.backgroundView, viewController: self)
  163. if self.splitViewController != nil && self.splitViewController!.isCollapsed {
  164. self.navigationController?.navigationItem.hidesBackButton = true
  165. }
  166. } else if errorCode != 0 {
  167. NCContentPresenter.shared.messageNotification("_error_", description: errorMessage, delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
  168. self.navigationController?.popViewController(animated: true)
  169. } else {
  170. self.navigationController?.popViewController(animated: true)
  171. }
  172. })
  173. } else {
  174. let richDocument = NCViewerRichdocument.init(frame: backgroundView.frame, configuration: WKWebViewConfiguration())
  175. richDocument.viewRichDocumentAt(metadata.url, metadata: metadata, view: backgroundView, viewController: self)
  176. if self.splitViewController != nil && self.splitViewController!.isCollapsed {
  177. self.navigationController?.navigationItem.hidesBackButton = true
  178. }
  179. }
  180. }
  181. }
  182. // OTHER
  183. NCViewerDocumentWeb.sharedInstance.viewDocumentWebAt(metadata, view: backgroundView)
  184. }
  185. }
  186. // MARK: SwiftPhotoGalleryDataSource Methods
  187. extension NCDetailViewController: NCPhotoGalleryDataSource {
  188. func numberOfImagesInGallery(gallery: NCPhotoGallery) -> Int {
  189. let i = imageNames.count
  190. return imageNames.count
  191. }
  192. func imageInGallery(gallery: NCPhotoGallery, forIndex: Int) -> UIImage? {
  193. if forIndex < 0 { return nil }
  194. let image = UIImage(named: imageNames[forIndex])
  195. return image
  196. }
  197. }
  198. // MARK: SwiftPhotoGalleryDelegate Methods
  199. extension NCDetailViewController: NCPhotoGalleryDelegate {
  200. func galleryDidTapToClose(gallery: NCPhotoGallery) {
  201. self.index = gallery.currentPage
  202. dismiss(animated: true, completion: nil)
  203. }
  204. }