NCViewerMediaPage.swift 29 KB

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