NCViewerProviderContextMenu.swift 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. //
  2. // NCViewerProviderContextMenu.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 12/01/21.
  6. // Copyright © 2021 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 SVGKit
  26. import MobileVLCKit
  27. class NCViewerProviderContextMenu: UIViewController {
  28. private let imageView = UIImageView()
  29. private var metadata: tableMetadata?
  30. private var metadataLivePhoto: tableMetadata?
  31. private var image: UIImage?
  32. private let player = VLCMediaPlayer()
  33. private let sizeIcon: CGFloat = 150
  34. // MARK: - View Life Cycle
  35. required init?(coder: NSCoder) {
  36. fatalError("init(coder:) has not been implemented")
  37. }
  38. init(metadata: tableMetadata, image: UIImage?) {
  39. super.init(nibName: nil, bundle: nil)
  40. self.metadata = metadata
  41. self.metadataLivePhoto = NCManageDatabase.shared.getMetadataLivePhoto(metadata: metadata)
  42. self.image = image
  43. if metadata.directory {
  44. var imageFolder = UIImage(named: "folder")!.image(color: NCBrandColor.shared.brandElement, size: sizeIcon * 2)
  45. if let image = self.image {
  46. imageFolder = image.image(color: NCBrandColor.shared.brandElement, size: sizeIcon * 2)
  47. }
  48. imageView.image = imageFolder.colorizeFolder(metadata: metadata)
  49. imageView.frame = resize(CGSize(width: sizeIcon, height: sizeIcon))
  50. } else {
  51. // ICON
  52. if let image = UIImage(named: metadata.iconName)?.resizeImage(size: CGSize(width: sizeIcon * 2, height: sizeIcon * 2)) {
  53. imageView.image = image
  54. imageView.frame = resize(CGSize(width: sizeIcon, height: sizeIcon))
  55. }
  56. // PREVIEW
  57. if NCUtilityFileSystem.shared.fileProviderStoragePreviewIconExists(metadata.ocId, etag: metadata.etag) {
  58. if let image = UIImage(contentsOfFile: NCUtilityFileSystem.shared.getDirectoryProviderStoragePreviewOcId(metadata.ocId, etag: metadata.etag)) {
  59. imageView.image = image
  60. imageView.frame = resize(image.size)
  61. }
  62. }
  63. // VIEW IMAGE
  64. if metadata.isImage && NCUtilityFileSystem.shared.fileProviderStorageExists(metadata) {
  65. viewImage(metadata: metadata)
  66. }
  67. // VIEW LIVE PHOTO
  68. if let metadataLivePhoto = metadataLivePhoto, NCUtilityFileSystem.shared.fileProviderStorageExists(metadataLivePhoto) {
  69. viewVideo(metadata: metadataLivePhoto)
  70. }
  71. // VIEW VIDEO
  72. if metadata.isVideo {
  73. if !NCUtilityFileSystem.shared.fileProviderStoragePreviewIconExists(metadata.ocId, etag: metadata.etag) {
  74. let newSize = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height)
  75. imageView.image = nil
  76. imageView.frame = newSize
  77. preferredContentSize = newSize.size
  78. }
  79. viewVideo(metadata: metadata)
  80. }
  81. // PLAY AUDIO
  82. if metadata.isAudio {
  83. var maxDownload: UInt64 = 0
  84. if NCUtilityFileSystem.shared.fileProviderStorageExists(metadata) {
  85. viewVideo(metadata: metadata)
  86. } else {
  87. if NCNetworking.shared.networkReachability == NKCommon.TypeReachability.reachableCellular {
  88. maxDownload = NCGlobal.shared.maxAutoDownloadCellular
  89. } else {
  90. maxDownload = NCGlobal.shared.maxAutoDownload
  91. }
  92. if metadata.size <= maxDownload,
  93. let appDelegate = (UIApplication.shared.delegate as? AppDelegate),
  94. appDelegate.downloadQueue.operations.filter({ ($0 as? NCOperationDownload)?.metadata.ocId == metadata.ocId }).isEmpty {
  95. appDelegate.downloadQueue.addOperation(NCOperationDownload(metadata: metadata, selector: ""))
  96. }
  97. }
  98. }
  99. // AUTO DOWNLOAD IMAGE GIF
  100. if !NCUtilityFileSystem.shared.fileProviderStorageExists(metadata),
  101. metadata.contentType == "image/gif",
  102. let appDelegate = (UIApplication.shared.delegate as? AppDelegate),
  103. appDelegate.downloadQueue.operations.filter({ ($0 as? NCOperationDownload)?.metadata.ocId == metadata.ocId }).isEmpty {
  104. appDelegate.downloadQueue.addOperation(NCOperationDownload(metadata: metadata, selector: ""))
  105. }
  106. // AUTO DOWNLOAD IMAGE SVG
  107. if !NCUtilityFileSystem.shared.fileProviderStorageExists(metadata),
  108. metadata.contentType == "image/svg+xml",
  109. let appDelegate = (UIApplication.shared.delegate as? AppDelegate),
  110. appDelegate.downloadQueue.operations.filter({ ($0 as? NCOperationDownload)?.metadata.ocId == metadata.ocId }).isEmpty {
  111. appDelegate.downloadQueue.addOperation(NCOperationDownload(metadata: metadata, selector: ""))
  112. }
  113. // AUTO DOWNLOAD LIVE PHOTO
  114. if let metadataLivePhoto = self.metadataLivePhoto,
  115. !NCUtilityFileSystem.shared.fileProviderStorageExists(metadataLivePhoto),
  116. let appDelegate = (UIApplication.shared.delegate as? AppDelegate),
  117. appDelegate.downloadQueue.operations.filter({ ($0 as? NCOperationDownload)?.metadata.ocId == metadata.ocId }).isEmpty {
  118. appDelegate.downloadQueue.addOperation(NCOperationDownload(metadata: metadataLivePhoto, selector: ""))
  119. }
  120. }
  121. }
  122. override func loadView() {
  123. view = imageView
  124. imageView.contentMode = .scaleAspectFill
  125. }
  126. override func viewWillAppear(_ animated: Bool) {
  127. super.viewWillAppear(animated)
  128. NotificationCenter.default.addObserver(self, selector: #selector(downloadStartFile(_:)), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterDownloadStartFile), object: nil)
  129. NotificationCenter.default.addObserver(self, selector: #selector(downloadedFile(_:)), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterDownloadedFile), object: nil)
  130. NotificationCenter.default.addObserver(self, selector: #selector(downloadCancelFile(_:)), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterDownloadCancelFile), object: nil)
  131. }
  132. override func viewWillDisappear(_ animated: Bool) {
  133. super.viewWillDisappear(animated)
  134. player.stop()
  135. NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterDownloadStartFile), object: nil)
  136. NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterDownloadedFile), object: nil)
  137. NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterDownloadCancelFile), object: nil)
  138. }
  139. // MARK: - NotificationCenter
  140. @objc func downloadStartFile(_ notification: NSNotification) {
  141. guard let userInfo = notification.userInfo as NSDictionary?,
  142. let ocId = userInfo["ocId"] as? String
  143. else { return }
  144. if ocId == self.metadata?.ocId || ocId == self.metadataLivePhoto?.ocId {
  145. NCActivityIndicator.shared.start(backgroundView: self.view)
  146. }
  147. }
  148. @objc func downloadedFile(_ notification: NSNotification) {
  149. guard let userInfo = notification.userInfo as NSDictionary?,
  150. let ocId = userInfo["ocId"] as? String,
  151. let error = userInfo["error"] as? NKError,
  152. let metadata = NCManageDatabase.shared.getMetadataFromOcId(ocId)
  153. else { return }
  154. if error == .success && metadata.ocId == self.metadata?.ocId {
  155. if metadata.isImage {
  156. viewImage(metadata: metadata)
  157. } else if metadata.isVideo {
  158. viewVideo(metadata: metadata)
  159. } else if metadata.isAudio {
  160. viewVideo(metadata: metadata)
  161. }
  162. }
  163. if error == .success && metadata.ocId == self.metadataLivePhoto?.ocId {
  164. viewVideo(metadata: metadata)
  165. }
  166. if ocId == self.metadata?.ocId || ocId == self.metadataLivePhoto?.ocId {
  167. NCActivityIndicator.shared.stop()
  168. }
  169. }
  170. @objc func downloadCancelFile(_ notification: NSNotification) {
  171. guard let userInfo = notification.userInfo as NSDictionary?,
  172. let ocId = userInfo["ocId"] as? String
  173. else { return }
  174. if ocId == self.metadata?.ocId || ocId == self.metadataLivePhoto?.ocId {
  175. NCActivityIndicator.shared.stop()
  176. }
  177. }
  178. // MARK: - Viewer
  179. private func viewImage(metadata: tableMetadata) {
  180. var image: UIImage?
  181. let filePath = NCUtilityFileSystem.shared.getDirectoryProviderStorageOcId(metadata.ocId, fileNameView: metadata.fileNameView)
  182. if metadata.contentType == "image/gif" {
  183. image = UIImage.animatedImage(withAnimatedGIFURL: URL(fileURLWithPath: filePath))
  184. } else if metadata.contentType == "image/svg+xml" {
  185. let imagePath = NCUtilityFileSystem.shared.getDirectoryProviderStorageOcId(metadata.ocId, fileNameView: metadata.fileNameView)
  186. if let svgImage = SVGKImage(contentsOfFile: imagePath) {
  187. svgImage.size = CGSize(width: NCGlobal.shared.sizePreview, height: NCGlobal.shared.sizePreview)
  188. image = svgImage.uiImage
  189. }
  190. } else {
  191. image = UIImage(contentsOfFile: filePath)
  192. }
  193. imageView.image = image
  194. imageView.frame = resize(image?.size)
  195. }
  196. private func viewVideo(metadata: tableMetadata) {
  197. NCNetworking.shared.getVideoUrl(metadata: metadata) { url, _, _ in
  198. if let url = url {
  199. self.player.media = VLCMedia(url: url)
  200. self.player.delegate = self
  201. self.player.media?.addOption(":http-user-agent=\(userAgent)")
  202. self.player.drawable = self.imageView
  203. self.player.play()
  204. }
  205. }
  206. }
  207. private func resize(_ size: CGSize?) -> CGRect {
  208. var frame = CGRect.zero
  209. guard let size = size else {
  210. preferredContentSize = frame.size
  211. return frame
  212. }
  213. if size.width <= UIScreen.main.bounds.width {
  214. frame = CGRect(x: 0, y: 0, width: size.width, height: size.height)
  215. preferredContentSize = frame.size
  216. return frame
  217. }
  218. let originRatio = size.width / size.height
  219. let newRatio = UIScreen.main.bounds.width / UIScreen.main.bounds.height
  220. var newSize = CGSize.zero
  221. if originRatio < newRatio {
  222. newSize.height = UIScreen.main.bounds.height
  223. newSize.width = UIScreen.main.bounds.height * originRatio
  224. } else {
  225. newSize.width = UIScreen.main.bounds.width
  226. newSize.height = UIScreen.main.bounds.width / originRatio
  227. }
  228. frame = CGRect(x: 0, y: 0, width: newSize.width, height: newSize.height)
  229. preferredContentSize = frame.size
  230. return frame
  231. }
  232. }
  233. extension NCViewerProviderContextMenu: VLCMediaPlayerDelegate {
  234. func mediaPlayerStateChanged(_ aNotification: Notification) {
  235. switch player.state {
  236. case .stopped:
  237. print("Played mode: STOPPED")
  238. case .opening:
  239. NCActivityIndicator.shared.start(backgroundView: self.view)
  240. print("Played mode: OPENING")
  241. case .buffering:
  242. print("Played mode: BUFFERING")
  243. case .ended:
  244. print("Played mode: ENDED")
  245. case .error:
  246. NCActivityIndicator.shared.stop()
  247. let error = NKError(errorCode: NCGlobal.shared.errorInternalError, errorDescription: "_error_something_wrong_")
  248. NCContentPresenter.shared.showError(error: error, priority: .max)
  249. print("Played mode: ERROR")
  250. case .playing:
  251. NCActivityIndicator.shared.stop()
  252. print("Played mode: PLAYING")
  253. case .paused:
  254. print("Played mode: PAUSED")
  255. default: break
  256. }
  257. }
  258. func mediaPlayerTimeChanged(_ aNotification: Notification) {
  259. // Handle other states...
  260. }
  261. func mediaPlayerTitleChanged(_ aNotification: Notification) {
  262. // Handle other states...
  263. }
  264. func mediaPlayerChapterChanged(_ aNotification: Notification) {
  265. // Handle other states...
  266. }
  267. func mediaPlayerLoudnessChanged(_ aNotification: Notification) {
  268. // Handle other states...
  269. }
  270. func mediaPlayerSnapshot(_ aNotification: Notification) {
  271. // Handle other states...
  272. }
  273. func mediaPlayerStartedRecording(_ player: VLCMediaPlayer) {
  274. // Handle other states...
  275. }
  276. func mediaPlayer(_ player: VLCMediaPlayer, recordingStoppedAtPath path: String) {
  277. // Handle other states...
  278. }
  279. }