NCViewerMedia.swift 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724
  1. //
  2. // NCViewerMedia.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 24/10/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 SVGKit
  25. import NCCommunication
  26. import MediaPlayer
  27. class NCViewerMedia: UIViewController {
  28. @IBOutlet weak var progressView: UIProgressView!
  29. enum ScreenMode {
  30. case full, normal
  31. }
  32. var currentScreenMode: ScreenMode = .normal
  33. var saveScreenModeImage: ScreenMode = .normal
  34. var pageViewController: UIPageViewController {
  35. return self.children[0] as! UIPageViewController
  36. }
  37. var currentViewController: NCViewerMediaZoom {
  38. return self.pageViewController.viewControllers![0] as! NCViewerMediaZoom
  39. }
  40. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  41. var metadatas: [tableMetadata] = []
  42. var currentIndex = 0
  43. var nextIndex: Int?
  44. var IndexInPlay: Int = -1
  45. var ncplayerLivePhoto: NCPlayer?
  46. var panGestureRecognizer: UIPanGestureRecognizer!
  47. var singleTapGestureRecognizer: UITapGestureRecognizer!
  48. var longtapGestureRecognizer: UILongPressGestureRecognizer!
  49. var textColor: UIColor = NCBrandColor.shared.label
  50. var playCommand: Any?
  51. var pauseCommand: Any?
  52. var skipForwardCommand: Any?
  53. var skipBackwardCommand: Any?
  54. var nextTrackCommand: Any?
  55. var previousTrackCommand: Any?
  56. // MARK: - View Life Cycle
  57. override func viewDidLoad() {
  58. super.viewDidLoad()
  59. navigationItem.rightBarButtonItem = UIBarButtonItem.init(image: UIImage(named: "more")!.image(color: NCBrandColor.shared.label, size: 25), style: .plain, target: self, action: #selector(self.openMenuMore))
  60. singleTapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(didSingleTapWith(gestureRecognizer:)))
  61. panGestureRecognizer = UIPanGestureRecognizer(target: self, action: #selector(didPanWith(gestureRecognizer:)))
  62. longtapGestureRecognizer = UILongPressGestureRecognizer()
  63. longtapGestureRecognizer.delaysTouchesBegan = true
  64. longtapGestureRecognizer.minimumPressDuration = 0.3
  65. longtapGestureRecognizer.delegate = self
  66. longtapGestureRecognizer.addTarget(self, action: #selector(didLongpressGestureEvent(gestureRecognizer:)))
  67. pageViewController.delegate = self
  68. pageViewController.dataSource = self
  69. pageViewController.view.addGestureRecognizer(panGestureRecognizer)
  70. pageViewController.view.addGestureRecognizer(singleTapGestureRecognizer)
  71. pageViewController.view.addGestureRecognizer(longtapGestureRecognizer)
  72. let viewerMediaZoom = getViewerMediaZoom(index: currentIndex, image: getImageMetadata(metadatas[currentIndex]), metadata: metadatas[currentIndex], direction: .forward)
  73. pageViewController.setViewControllers([viewerMediaZoom], direction: .forward, animated: true, completion: nil)
  74. NotificationCenter.default.addObserver(self, selector: #selector(changeTheming), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterChangeTheming), object: nil)
  75. NotificationCenter.default.addObserver(self, selector: #selector(viewUnload), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterMenuDetailClose), object: nil)
  76. progressView.tintColor = NCBrandColor.shared.brandElement
  77. progressView.trackTintColor = .clear
  78. progressView.progress = 0
  79. NotificationCenter.default.addObserver(self, selector: #selector(deleteFile(_:)), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterDeleteFile), object: nil)
  80. NotificationCenter.default.addObserver(self, selector: #selector(renameFile(_:)), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterRenameFile), object: nil)
  81. NotificationCenter.default.addObserver(self, selector: #selector(moveFile(_:)), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterMoveFile), object: nil)
  82. NotificationCenter.default.addObserver(self, selector: #selector(downloadedFile(_:)), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterDownloadedFile), object: nil)
  83. NotificationCenter.default.addObserver(self, selector: #selector(uploadedFile(_:)), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterUploadedFile), object: nil)
  84. NotificationCenter.default.addObserver(self, selector: #selector(triggerProgressTask(_:)), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterProgressTask), object:nil)
  85. NotificationCenter.default.addObserver(self, selector: #selector(downloadedThumbnail(_:)), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterDownloadedThumbnail), object: nil)
  86. NotificationCenter.default.addObserver(self, selector: #selector(hidePlayerToolBar(_:)), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterHidePlayerToolBar), object: nil)
  87. NotificationCenter.default.addObserver(self, selector: #selector(showPlayerToolBar(_:)), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterShowPlayerToolBar), object: nil)
  88. NotificationCenter.default.addObserver(self, selector: #selector(applicationDidBecomeActive(_:)), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterApplicationDidBecomeActive), object: nil)
  89. }
  90. override func viewDidDisappear(_ animated: Bool) {
  91. super.viewDidDisappear(animated)
  92. // Clear
  93. if let ncplayer = currentViewController.ncplayer, ncplayer.isPlay() {
  94. ncplayer.playerPause()
  95. ncplayer.saveCurrentTime()
  96. }
  97. currentViewController.playerToolBar.stopTimerAutoHide()
  98. clearCommandCenter()
  99. metadatas.removeAll()
  100. ncplayerLivePhoto = nil
  101. // Remove Observer
  102. NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterDeleteFile), object: nil)
  103. NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterRenameFile), object: nil)
  104. NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterMoveFile), object: nil)
  105. NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterDownloadedFile), object: nil)
  106. NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterUploadedFile), object: nil)
  107. NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterProgressTask), object: nil)
  108. NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterDownloadedThumbnail), object: nil)
  109. NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterHidePlayerToolBar), object: nil)
  110. NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterShowPlayerToolBar), object: nil)
  111. NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterApplicationDidBecomeActive), object: nil)
  112. }
  113. override var preferredStatusBarStyle: UIStatusBarStyle {
  114. if currentScreenMode == .normal {
  115. return .default
  116. } else {
  117. return .lightContent
  118. }
  119. }
  120. // MARK: -
  121. func getViewerMediaZoom(index: Int, image: UIImage?, metadata: tableMetadata, direction: UIPageViewController.NavigationDirection) -> NCViewerMediaZoom {
  122. let viewerMediaZoom = UIStoryboard(name: "NCViewerMedia", bundle: nil).instantiateViewController(withIdentifier: "NCViewerMediaZoom") as! NCViewerMediaZoom
  123. viewerMediaZoom.index = index
  124. viewerMediaZoom.image = image
  125. viewerMediaZoom.metadata = metadata
  126. viewerMediaZoom.viewerMedia = self
  127. singleTapGestureRecognizer.require(toFail: viewerMediaZoom.doubleTapGestureRecognizer)
  128. return viewerMediaZoom
  129. }
  130. @objc func viewUnload() {
  131. navigationController?.popViewController(animated: true)
  132. }
  133. @objc func openMenuMore() {
  134. let imageIcon = UIImage(contentsOfFile: CCUtility.getDirectoryProviderStorageIconOcId(currentViewController.metadata.ocId, etag: currentViewController.metadata.etag))
  135. NCViewer.shared.toggleMenu(viewController: self, metadata: currentViewController.metadata, webView: false, imageIcon: imageIcon)
  136. }
  137. func changeScreenMode(mode: ScreenMode, enableTimerAutoHide: Bool = false) {
  138. if mode == .normal {
  139. navigationController?.setNavigationBarHidden(false, animated: true)
  140. progressView.isHidden = false
  141. if !currentViewController.detailView.isShow() {
  142. currentViewController.playerToolBar.show(enableTimerAutoHide: enableTimerAutoHide)
  143. }
  144. NCUtility.shared.colorNavigationController(navigationController, backgroundColor: NCBrandColor.shared.systemBackground, titleColor: NCBrandColor.shared.label, tintColor: nil, withoutShadow: false)
  145. view.backgroundColor = NCBrandColor.shared.systemBackground
  146. textColor = NCBrandColor.shared.label
  147. } else {
  148. navigationController?.setNavigationBarHidden(true, animated: true)
  149. progressView.isHidden = true
  150. currentViewController.playerToolBar.hide()
  151. view.backgroundColor = .black
  152. textColor = .white
  153. }
  154. currentScreenMode = mode
  155. if currentViewController.metadata.classFile == NCCommunicationCommon.typeClassFile.image.rawValue {
  156. saveScreenModeImage = mode
  157. }
  158. setNeedsStatusBarAppearanceUpdate()
  159. currentViewController.reloadDetail()
  160. }
  161. //MARK: - NotificationCenter
  162. @objc func downloadedFile(_ notification: NSNotification) {
  163. if let userInfo = notification.userInfo as NSDictionary?, let ocId = userInfo["ocId"] as? String, let metadata = NCManageDatabase.shared.getMetadataFromOcId(ocId), let errorCode = userInfo["errorCode"] as? Int {
  164. if errorCode == 0 && currentViewController.metadata.ocId == ocId, let image = getImageMetadata(metadata) {
  165. currentViewController.reload(image: image, metadata: metadata)
  166. if self.metadatas.first(where: { $0.ocId == metadata.ocId }) != nil {
  167. progressView.progress = 0
  168. }
  169. }
  170. }
  171. }
  172. @objc func downloadedThumbnail(_ notification: NSNotification) {
  173. if let userInfo = notification.userInfo as NSDictionary?, let ocId = userInfo["ocId"] as? String, let metadata = NCManageDatabase.shared.getMetadataFromOcId(ocId) {
  174. if currentViewController.metadata.ocId == ocId, let image = getImageMetadata(metadata) {
  175. currentViewController.reload(image: image, metadata: metadata)
  176. }
  177. }
  178. }
  179. @objc func uploadedFile(_ notification: NSNotification) {
  180. if let userInfo = notification.userInfo as NSDictionary? {
  181. if let ocId = userInfo["ocId"] as? String, let metadata = NCManageDatabase.shared.getMetadataFromOcId(ocId), let errorCode = userInfo["errorCode"] as? Int {
  182. if errorCode == 0 && metadata.ocId == currentViewController.metadata.ocId {
  183. //self.reloadCurrentPage()
  184. }
  185. }
  186. }
  187. }
  188. @objc func triggerProgressTask(_ notification: NSNotification) {
  189. if let userInfo = notification.userInfo as NSDictionary? {
  190. if let serverUrl = userInfo["serverUrl"] as? String, let fileName = userInfo["fileName"] as? String, let progressNumber = userInfo["progress"] as? NSNumber {
  191. if self.metadatas.first(where: { $0.serverUrl == serverUrl && $0.fileName == fileName}) != nil {
  192. let progress = progressNumber.floatValue
  193. if progress == 1 {
  194. self.progressView.progress = 0
  195. } else {
  196. self.progressView.progress = progress
  197. }
  198. }
  199. }
  200. }
  201. }
  202. @objc func deleteFile(_ notification: NSNotification) {
  203. if let userInfo = notification.userInfo as NSDictionary? {
  204. if let ocId = userInfo["ocId"] as? String {
  205. let metadatas = self.metadatas.filter { $0.ocId != ocId }
  206. if self.metadatas.count == metadatas.count { return }
  207. self.metadatas = metadatas
  208. if ocId == currentViewController.metadata.ocId {
  209. if !shiftCurrentPage() {
  210. self.viewUnload()
  211. }
  212. }
  213. }
  214. }
  215. }
  216. @objc func renameFile(_ notification: NSNotification) {
  217. if let userInfo = notification.userInfo as NSDictionary? {
  218. if let ocId = userInfo["ocId"] as? String, let metadata = NCManageDatabase.shared.getMetadataFromOcId(ocId) {
  219. if let index = metadatas.firstIndex(where: {$0.ocId == metadata.ocId}) {
  220. metadatas[index] = metadata
  221. if index == currentIndex {
  222. navigationItem.title = metadata.fileNameView
  223. currentViewController.metadata = metadata
  224. self.currentViewController.metadata = metadata
  225. }
  226. }
  227. }
  228. }
  229. }
  230. @objc func moveFile(_ notification: NSNotification) {
  231. if let userInfo = notification.userInfo as NSDictionary? {
  232. if let ocId = userInfo["ocId"] as? String, let metadata = NCManageDatabase.shared.getMetadataFromOcId(ocId) {
  233. if metadatas.firstIndex(where: {$0.ocId == metadata.ocId}) != nil {
  234. deleteFile(notification)
  235. }
  236. }
  237. }
  238. }
  239. @objc func changeTheming() {
  240. }
  241. @objc func hidePlayerToolBar(_ notification: NSNotification) {
  242. if let userInfo = notification.userInfo as NSDictionary?, let ocId = userInfo["ocId"] as? String {
  243. if currentViewController.metadata.ocId == ocId {
  244. changeScreenMode(mode: .full)
  245. }
  246. }
  247. }
  248. @objc func showPlayerToolBar(_ notification: NSNotification) {
  249. if let userInfo = notification.userInfo as NSDictionary?, let ocId = userInfo["ocId"] as? String, let enableTimerAutoHide = userInfo["enableTimerAutoHide"] as? Bool{
  250. if currentViewController.metadata.ocId == ocId, let playerToolBar = currentViewController.playerToolBar, !playerToolBar.isPictureInPictureActive() {
  251. changeScreenMode(mode: .normal, enableTimerAutoHide: enableTimerAutoHide)
  252. }
  253. }
  254. }
  255. @objc func applicationDidBecomeActive(_ notification: NSNotification) {
  256. progressView.progress = 0
  257. }
  258. //MARK: - Image
  259. func getImageMetadata(_ metadata: tableMetadata) -> UIImage? {
  260. if let image = getImage(metadata: metadata) {
  261. return image
  262. }
  263. if metadata.classFile == NCCommunicationCommon.typeClassFile.video.rawValue && !metadata.hasPreview {
  264. NCUtility.shared.createImageFrom(fileName: metadata.fileNameView, ocId: metadata.ocId, etag: metadata.etag, classFile: metadata.classFile)
  265. }
  266. if CCUtility.fileProviderStoragePreviewIconExists(metadata.ocId, etag: metadata.etag) {
  267. if let imagePreviewPath = CCUtility.getDirectoryProviderStoragePreviewOcId(metadata.ocId, etag: metadata.etag) {
  268. return UIImage.init(contentsOfFile: imagePreviewPath)
  269. }
  270. }
  271. return nil
  272. }
  273. private func getImage(metadata: tableMetadata) -> UIImage? {
  274. let ext = CCUtility.getExtension(metadata.fileNameView)
  275. var image: UIImage?
  276. if CCUtility.fileProviderStorageExists(metadata.ocId, fileNameView: metadata.fileNameView) && metadata.classFile == NCCommunicationCommon.typeClassFile.image.rawValue {
  277. let previewPath = CCUtility.getDirectoryProviderStoragePreviewOcId(metadata.ocId, etag: metadata.etag)!
  278. let imagePath = CCUtility.getDirectoryProviderStorageOcId(metadata.ocId, fileNameView: metadata.fileNameView)!
  279. if ext == "GIF" {
  280. if !FileManager().fileExists(atPath: previewPath) {
  281. NCUtility.shared.createImageFrom(fileName: metadata.fileNameView, ocId: metadata.ocId, etag: metadata.etag, classFile: metadata.classFile)
  282. }
  283. image = UIImage.animatedImage(withAnimatedGIFURL: URL(fileURLWithPath: imagePath))
  284. } else if ext == "SVG" {
  285. if let svgImage = SVGKImage(contentsOfFile: imagePath) {
  286. svgImage.size = CGSize(width: NCGlobal.shared.sizePreview, height: NCGlobal.shared.sizePreview)
  287. if let image = svgImage.uiImage {
  288. if !FileManager().fileExists(atPath: previewPath) {
  289. do {
  290. try image.pngData()?.write(to: URL(fileURLWithPath: previewPath), options: .atomic)
  291. } catch { }
  292. }
  293. return image
  294. } else {
  295. return nil
  296. }
  297. } else {
  298. return nil
  299. }
  300. } else {
  301. NCUtility.shared.createImageFrom(fileName: metadata.fileNameView, ocId: metadata.ocId, etag: metadata.etag, classFile: metadata.classFile)
  302. image = UIImage.init(contentsOfFile: imagePath)
  303. }
  304. }
  305. return image
  306. }
  307. // MARK: - Command Center
  308. func updateCommandCenter(ncplayer: NCPlayer, metadata: tableMetadata) {
  309. var nowPlayingInfo = [String : Any]()
  310. // Clear
  311. clearCommandCenter()
  312. UIApplication.shared.beginReceivingRemoteControlEvents()
  313. // Add handler for Play Command
  314. MPRemoteCommandCenter.shared().playCommand.isEnabled = true
  315. playCommand = MPRemoteCommandCenter.shared().playCommand.addTarget { event in
  316. if !ncplayer.isPlay() {
  317. ncplayer.playerPlay()
  318. return .success
  319. }
  320. return .commandFailed
  321. }
  322. // Add handler for Pause Command
  323. MPRemoteCommandCenter.shared().pauseCommand.isEnabled = true
  324. pauseCommand = MPRemoteCommandCenter.shared().pauseCommand.addTarget { event in
  325. if ncplayer.isPlay() {
  326. ncplayer.playerPause()
  327. return .success
  328. }
  329. return .commandFailed
  330. }
  331. // VIDEO / AUDIO () ()
  332. if metadata.classFile == NCCommunicationCommon.typeClassFile.video.rawValue || metadata.classFile == NCCommunicationCommon.typeClassFile.audio.rawValue {
  333. MPRemoteCommandCenter.shared().skipForwardCommand.isEnabled = true
  334. skipForwardCommand = MPRemoteCommandCenter.shared().skipForwardCommand.addTarget { event in
  335. let seconds = Float64((event as! MPSkipIntervalCommandEvent).interval)
  336. self.currentViewController.playerToolBar.skip(seconds: seconds)
  337. return.success
  338. }
  339. MPRemoteCommandCenter.shared().skipBackwardCommand.isEnabled = true
  340. skipBackwardCommand = MPRemoteCommandCenter.shared().skipBackwardCommand.addTarget { event in
  341. let seconds = Float64((event as! MPSkipIntervalCommandEvent).interval)
  342. self.currentViewController.playerToolBar.skip(seconds: -seconds)
  343. return.success
  344. }
  345. }
  346. // AUDIO < >
  347. /*
  348. if metadata?.classFile == NCCommunicationCommon.typeClassFile.audio.rawValue {
  349. MPRemoteCommandCenter.shared().nextTrackCommand.isEnabled = true
  350. appDelegate.nextTrackCommand = MPRemoteCommandCenter.shared().nextTrackCommand.addTarget { event in
  351. self.forward()
  352. return .success
  353. }
  354. MPRemoteCommandCenter.shared().previousTrackCommand.isEnabled = true
  355. appDelegate.previousTrackCommand = MPRemoteCommandCenter.shared().previousTrackCommand.addTarget { event in
  356. self.backward()
  357. return .success
  358. }
  359. }
  360. */
  361. nowPlayingInfo[MPMediaItemPropertyTitle] = metadata.fileNameView
  362. nowPlayingInfo[MPMediaItemPropertyPlaybackDuration] = ncplayer.durationTime.seconds
  363. if let image = currentViewController.image {
  364. nowPlayingInfo[MPMediaItemPropertyArtwork] = MPMediaItemArtwork(boundsSize: image.size) { size in
  365. return image
  366. }
  367. }
  368. MPNowPlayingInfoCenter.default().nowPlayingInfo = nowPlayingInfo
  369. }
  370. func clearCommandCenter() {
  371. UIApplication.shared.endReceivingRemoteControlEvents()
  372. MPNowPlayingInfoCenter.default().nowPlayingInfo = [:]
  373. MPRemoteCommandCenter.shared().playCommand.isEnabled = false
  374. MPRemoteCommandCenter.shared().pauseCommand.isEnabled = false
  375. MPRemoteCommandCenter.shared().skipForwardCommand.isEnabled = false
  376. MPRemoteCommandCenter.shared().skipBackwardCommand.isEnabled = false
  377. MPRemoteCommandCenter.shared().nextTrackCommand.isEnabled = false
  378. MPRemoteCommandCenter.shared().previousTrackCommand.isEnabled = false
  379. if let playCommand = playCommand {
  380. MPRemoteCommandCenter.shared().playCommand.removeTarget(playCommand)
  381. self.playCommand = nil
  382. }
  383. if let pauseCommand = pauseCommand {
  384. MPRemoteCommandCenter.shared().pauseCommand.removeTarget(pauseCommand)
  385. self.pauseCommand = nil
  386. }
  387. if let skipForwardCommand = skipForwardCommand {
  388. MPRemoteCommandCenter.shared().skipForwardCommand.removeTarget(skipForwardCommand)
  389. self.skipForwardCommand = nil
  390. }
  391. if let skipBackwardCommand = skipBackwardCommand {
  392. MPRemoteCommandCenter.shared().skipBackwardCommand.removeTarget(skipBackwardCommand)
  393. self.skipBackwardCommand = nil
  394. }
  395. if let nextTrackCommand = nextTrackCommand {
  396. MPRemoteCommandCenter.shared().nextTrackCommand.removeTarget(nextTrackCommand)
  397. self.nextTrackCommand = nil
  398. }
  399. if let previousTrackCommand = previousTrackCommand {
  400. MPRemoteCommandCenter.shared().previousTrackCommand.removeTarget(previousTrackCommand)
  401. self.previousTrackCommand = nil
  402. }
  403. }
  404. }
  405. // MARK: - UIPageViewController Delegate Datasource
  406. extension NCViewerMedia: UIPageViewControllerDelegate, UIPageViewControllerDataSource {
  407. func shiftCurrentPage() -> Bool {
  408. if metadatas.count == 0 { return false }
  409. var direction: UIPageViewController.NavigationDirection = .forward
  410. if currentIndex == metadatas.count {
  411. currentIndex -= 1
  412. direction = .reverse
  413. }
  414. currentViewController.ncplayer?.deactivateObserver()
  415. let viewerMediaZoom = getViewerMediaZoom(index: currentIndex, image: getImageMetadata(metadatas[currentIndex]), metadata: metadatas[currentIndex], direction: direction)
  416. pageViewController.setViewControllers([viewerMediaZoom], direction: direction, animated: true, completion: nil)
  417. return true
  418. }
  419. func goTo(index: Int, direction: UIPageViewController.NavigationDirection, autoPlay: Bool) {
  420. currentIndex = index
  421. currentViewController.ncplayer?.deactivateObserver()
  422. let viewerMediaZoom = getViewerMediaZoom(index: currentIndex, image: getImageMetadata(metadatas[currentIndex]), metadata: metadatas[currentIndex], direction: direction)
  423. viewerMediaZoom.autoPlay = autoPlay
  424. pageViewController.setViewControllers([viewerMediaZoom], direction: direction, animated: true, completion: nil)
  425. }
  426. func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
  427. if currentIndex == 0 { return nil }
  428. let viewerMediaZoom = getViewerMediaZoom(index: currentIndex-1, image: getImageMetadata(metadatas[currentIndex-1]), metadata: metadatas[currentIndex-1], direction: .reverse)
  429. return viewerMediaZoom
  430. }
  431. func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
  432. if currentIndex == metadatas.count-1 { return nil }
  433. let viewerMediaZoom = getViewerMediaZoom(index: currentIndex+1, image: getImageMetadata(metadatas[currentIndex+1]), metadata: metadatas[currentIndex+1], direction: .forward)
  434. return viewerMediaZoom
  435. }
  436. // START TRANSITION
  437. func pageViewController(_ pageViewController: UIPageViewController, willTransitionTo pendingViewControllers: [UIViewController]) {
  438. // Save time video
  439. if let ncplayer = currentViewController.ncplayer, ncplayer.isPlay() {
  440. ncplayer.saveCurrentTime()
  441. }
  442. guard let nextViewController = pendingViewControllers.first as? NCViewerMediaZoom else { return }
  443. nextIndex = nextViewController.index
  444. }
  445. // END TRANSITION
  446. func pageViewController(_ pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) {
  447. if (completed && nextIndex != nil) {
  448. previousViewControllers.forEach { viewController in
  449. let viewerMediaZoom = viewController as! NCViewerMediaZoom
  450. viewerMediaZoom.ncplayer?.deactivateObserver()
  451. }
  452. currentIndex = nextIndex!
  453. }
  454. self.nextIndex = nil
  455. }
  456. }
  457. //MARK: - UIGestureRecognizerDelegate
  458. extension NCViewerMedia: UIGestureRecognizerDelegate {
  459. func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
  460. if let gestureRecognizer = gestureRecognizer as? UIPanGestureRecognizer {
  461. let velocity = gestureRecognizer.velocity(in: self.view)
  462. var velocityCheck : Bool = false
  463. if UIDevice.current.orientation.isLandscape {
  464. velocityCheck = velocity.x < 0
  465. }
  466. else {
  467. velocityCheck = velocity.y < 0
  468. }
  469. if velocityCheck {
  470. return false
  471. }
  472. }
  473. return true
  474. }
  475. func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
  476. if otherGestureRecognizer == currentViewController.scrollView.panGestureRecognizer {
  477. if self.currentViewController.scrollView.contentOffset.y == 0 {
  478. return true
  479. }
  480. }
  481. return false
  482. }
  483. @objc func didPanWith(gestureRecognizer: UIPanGestureRecognizer) {
  484. currentViewController.didPanWith(gestureRecognizer: gestureRecognizer)
  485. }
  486. @objc func didSingleTapWith(gestureRecognizer: UITapGestureRecognizer) {
  487. if let playerToolBar = currentViewController.playerToolBar, playerToolBar.isPictureInPictureActive() {
  488. playerToolBar.pictureInPictureController?.stopPictureInPicture()
  489. }
  490. if currentScreenMode == .full {
  491. changeScreenMode(mode: .normal, enableTimerAutoHide: true)
  492. } else {
  493. changeScreenMode(mode: .full)
  494. }
  495. }
  496. //
  497. // LIVE PHOTO
  498. //
  499. @objc func didLongpressGestureEvent(gestureRecognizer: UITapGestureRecognizer) {
  500. if !currentViewController.metadata.livePhoto { return }
  501. if gestureRecognizer.state == .began {
  502. currentViewController.updateViewConstraints()
  503. currentViewController.statusViewImage.isHidden = true
  504. currentViewController.statusLabel.isHidden = true
  505. let fileName = (currentViewController.metadata.fileNameView as NSString).deletingPathExtension + ".mov"
  506. if let metadata = NCManageDatabase.shared.getMetadata(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@ AND fileNameView LIKE[c] %@", currentViewController.metadata.account, currentViewController.metadata.serverUrl, fileName)) {
  507. if CCUtility.fileProviderStorageExists(metadata.ocId, fileNameView: metadata.fileNameView) {
  508. AudioServicesPlaySystemSound(1519) // peek feedback
  509. if let url = NCKTVHTTPCache.shared.getVideoURL(metadata: metadata) {
  510. self.ncplayerLivePhoto = NCPlayer.init(url: url, autoPlay: true, imageVideoContainer: self.currentViewController.imageVideoContainer, playerToolBar: nil, metadata: metadata, detailView: nil)
  511. }
  512. } else {
  513. let serverUrlFileName = metadata.serverUrl + "/" + metadata.fileNameView
  514. let fileNameLocalPath = CCUtility.getDirectoryProviderStorageOcId(metadata.ocId, fileNameView: metadata.fileNameView)!
  515. NCCommunication.shared.download(serverUrlFileName: serverUrlFileName, fileNameLocalPath: fileNameLocalPath, requestHandler: { (_) in
  516. }, taskHandler: { (_) in
  517. }, progressHandler: { (progress) in
  518. self.progressView.progress = Float(progress.fractionCompleted)
  519. }) { (account, etag, date, length, allHeaderFields, error, errorCode, errorDescription) in
  520. self.progressView.progress = 0
  521. if errorCode == 0 && account == metadata.account {
  522. NCManageDatabase.shared.addLocalFile(metadata: metadata)
  523. if gestureRecognizer.state == .changed || gestureRecognizer.state == .began {
  524. AudioServicesPlaySystemSound(1519) // peek feedback
  525. if let url = NCKTVHTTPCache.shared.getVideoURL(metadata: metadata) {
  526. self.ncplayerLivePhoto = NCPlayer.init(url: url, autoPlay: true, imageVideoContainer: self.currentViewController.imageVideoContainer, playerToolBar: nil, metadata: metadata, detailView: nil)
  527. }
  528. }
  529. }
  530. }
  531. }
  532. }
  533. } else if gestureRecognizer.state == .ended {
  534. currentViewController.statusViewImage.isHidden = false
  535. currentViewController.statusLabel.isHidden = false
  536. currentViewController.imageVideoContainer.image = currentViewController.image
  537. ncplayerLivePhoto?.videoLayer?.removeFromSuperlayer()
  538. ncplayerLivePhoto?.deactivateObserver()
  539. }
  540. }
  541. }