NCViewerMedia.swift 32 KB

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