NCViewerMediaPage.swift 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628
  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 NextcloudKit
  25. import MediaPlayer
  26. import JGProgressHUD
  27. import Alamofire
  28. class NCViewerMediaPage: UIViewController {
  29. @IBOutlet weak var progressView: UIProgressView!
  30. enum ScreenMode {
  31. case full, normal
  32. }
  33. var currentScreenMode: ScreenMode = .normal
  34. var saveScreenModeImage: ScreenMode = .normal
  35. var pageViewController: UIPageViewController {
  36. return self.children[0] as! UIPageViewController
  37. }
  38. var currentViewController: NCViewerMedia {
  39. return self.pageViewController.viewControllers![0] as! NCViewerMedia
  40. }
  41. private var hideStatusBar: Bool = false {
  42. didSet {
  43. setNeedsStatusBarAppearanceUpdate()
  44. }
  45. }
  46. var metadatas: [tableMetadata] = []
  47. var modifiedOcId: [String] = []
  48. var currentIndex = 0
  49. var nextIndex: Int?
  50. var panGestureRecognizer: UIPanGestureRecognizer!
  51. var singleTapGestureRecognizer: UITapGestureRecognizer!
  52. var longtapGestureRecognizer: UILongPressGestureRecognizer!
  53. var textColor: UIColor = .label
  54. var playCommand: Any?
  55. var pauseCommand: Any?
  56. var skipForwardCommand: Any?
  57. var skipBackwardCommand: Any?
  58. var nextTrackCommand: Any?
  59. var previousTrackCommand: Any?
  60. // MARK: - View Life Cycle
  61. override func viewDidLoad() {
  62. super.viewDidLoad()
  63. navigationItem.rightBarButtonItem = UIBarButtonItem(image: UIImage(named: "more")!.image(color: .label, size: 25), style: .plain, target: self, action: #selector(self.openMenuMore))
  64. singleTapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(didSingleTapWith(gestureRecognizer:)))
  65. panGestureRecognizer = UIPanGestureRecognizer(target: self, action: #selector(didPanWith(gestureRecognizer:)))
  66. longtapGestureRecognizer = UILongPressGestureRecognizer()
  67. longtapGestureRecognizer.delaysTouchesBegan = true
  68. longtapGestureRecognizer.minimumPressDuration = 0.3
  69. longtapGestureRecognizer.delegate = self
  70. longtapGestureRecognizer.addTarget(self, action: #selector(didLongpressGestureEvent(gestureRecognizer:)))
  71. pageViewController.delegate = self
  72. pageViewController.dataSource = self
  73. pageViewController.view.addGestureRecognizer(panGestureRecognizer)
  74. pageViewController.view.addGestureRecognizer(singleTapGestureRecognizer)
  75. pageViewController.view.addGestureRecognizer(longtapGestureRecognizer)
  76. let viewerMedia = getViewerMedia(index: currentIndex, metadata: metadatas[currentIndex])
  77. pageViewController.setViewControllers([viewerMedia], direction: .forward, animated: true, completion: nil)
  78. NotificationCenter.default.addObserver(self, selector: #selector(viewUnload), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterMenuDetailClose), object: nil)
  79. progressView.tintColor = NCBrandColor.shared.brandElement
  80. progressView.trackTintColor = .clear
  81. progressView.progress = 0
  82. NotificationCenter.default.addObserver(self, selector: #selector(deleteFile(_:)), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterDeleteFile), object: nil)
  83. NotificationCenter.default.addObserver(self, selector: #selector(renameFile(_:)), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterRenameFile), object: nil)
  84. NotificationCenter.default.addObserver(self, selector: #selector(moveFile(_:)), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterMoveFile), object: nil)
  85. NotificationCenter.default.addObserver(self, selector: #selector(downloadedFile(_:)), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterDownloadedFile), object: nil)
  86. NotificationCenter.default.addObserver(self, selector: #selector(triggerProgressTask(_:)), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterProgressTask), object: nil)
  87. NotificationCenter.default.addObserver(self, selector: #selector(uploadStartFile(_:)), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterUploadStartFile), object: nil)
  88. NotificationCenter.default.addObserver(self, selector: #selector(uploadedFile(_:)), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterUploadedFile), object: nil)
  89. NotificationCenter.default.addObserver(self, selector: #selector(hidePlayerToolBar(_:)), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterHidePlayerToolBar), object: nil)
  90. NotificationCenter.default.addObserver(self, selector: #selector(showPlayerToolBar(_:)), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterShowPlayerToolBar), object: nil)
  91. NotificationCenter.default.addObserver(self, selector: #selector(reloadMediaPage(_:)), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterReloadMediaPage), object: nil)
  92. NotificationCenter.default.addObserver(self, selector: #selector(applicationDidBecomeActive(_:)), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterApplicationDidBecomeActive), object: nil)
  93. }
  94. deinit {
  95. NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterDeleteFile), object: nil)
  96. NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterRenameFile), object: nil)
  97. NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterMoveFile), object: nil)
  98. NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterDownloadedFile), object: nil)
  99. NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterProgressTask), object: nil)
  100. NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterUploadStartFile), object: nil)
  101. NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterUploadedFile), object: nil)
  102. NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterHidePlayerToolBar), object: nil)
  103. NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterShowPlayerToolBar), object: nil)
  104. NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterReloadMediaPage), object: nil)
  105. NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterApplicationDidBecomeActive), object: nil)
  106. }
  107. override func viewDidDisappear(_ animated: Bool) {
  108. super.viewDidDisappear(animated)
  109. if let ncplayer = currentViewController.ncplayer, ncplayer.isPlay() {
  110. ncplayer.playerPause()
  111. }
  112. currentViewController.playerToolBar?.stopTimerAutoHide()
  113. clearCommandCenter()
  114. }
  115. override var preferredStatusBarStyle: UIStatusBarStyle {
  116. if currentScreenMode == .normal {
  117. return .default
  118. } else {
  119. return .lightContent
  120. }
  121. }
  122. override var prefersHomeIndicatorAutoHidden: Bool {
  123. return currentScreenMode == .full
  124. }
  125. override var prefersStatusBarHidden: Bool {
  126. return hideStatusBar
  127. }
  128. // MARK: -
  129. func getViewerMedia(index: Int, metadata: tableMetadata) -> NCViewerMedia {
  130. let viewerMedia = UIStoryboard(name: "NCViewerMediaPage", bundle: nil).instantiateViewController(withIdentifier: "NCViewerMedia") as! NCViewerMedia
  131. viewerMedia.index = index
  132. viewerMedia.metadata = metadata
  133. viewerMedia.viewerMediaPage = self
  134. singleTapGestureRecognizer.require(toFail: viewerMedia.doubleTapGestureRecognizer)
  135. return viewerMedia
  136. }
  137. @objc func viewUnload() {
  138. navigationController?.popViewController(animated: true)
  139. }
  140. @objc func openMenuMore() {
  141. let imageIcon = UIImage(contentsOfFile: CCUtility.getDirectoryProviderStorageIconOcId(currentViewController.metadata.ocId, etag: currentViewController.metadata.etag))
  142. NCViewer.shared.toggleMenu(viewController: self, metadata: currentViewController.metadata, webView: false, imageIcon: imageIcon)
  143. }
  144. func changeScreenMode(mode: ScreenMode, enableTimerAutoHide: Bool = false) {
  145. if mode == .normal {
  146. navigationController?.setNavigationBarHidden(false, animated: true)
  147. hideStatusBar = false
  148. progressView.isHidden = false
  149. if !currentViewController.detailView.isShow() {
  150. currentViewController.playerToolBar?.show(enableTimerAutoHide: enableTimerAutoHide)
  151. }
  152. NCUtility.shared.colorNavigationController(navigationController, backgroundColor: .systemBackground, titleColor: .label, tintColor: nil, withoutShadow: false)
  153. view.backgroundColor = .systemBackground
  154. textColor = .label
  155. } else {
  156. navigationController?.setNavigationBarHidden(true, animated: true)
  157. hideStatusBar = true
  158. progressView.isHidden = true
  159. currentViewController.playerToolBar?.hide()
  160. view.backgroundColor = .black
  161. textColor = .white
  162. }
  163. currentScreenMode = mode
  164. if currentViewController.metadata.classFile == NKCommon.TypeClassFile.image.rawValue {
  165. saveScreenModeImage = mode
  166. }
  167. setNeedsStatusBarAppearanceUpdate()
  168. setNeedsUpdateOfHomeIndicatorAutoHidden()
  169. currentViewController.reloadDetail()
  170. }
  171. // MARK: - NotificationCenter
  172. @objc func downloadedFile(_ notification: NSNotification) {
  173. guard let userInfo = notification.userInfo as NSDictionary?,
  174. let ocId = userInfo["ocId"] as? String
  175. else {
  176. return
  177. }
  178. progressView.progress = 0
  179. let metadata = metadatas[currentIndex]
  180. if metadata.ocId == ocId,
  181. (metadata.classFile == NKCommon.TypeClassFile.video.rawValue || metadata.classFile == NKCommon.TypeClassFile.audio.rawValue),
  182. CCUtility.fileProviderStorageExists(metadata),
  183. let ncplayer = currentViewController.ncplayer {
  184. let url = URL(fileURLWithPath: CCUtility.getDirectoryProviderStorageOcId(metadata.ocId, fileNameView: metadata.fileNameView)!)
  185. if ncplayer.isPlay() {
  186. ncplayer.playerPause(withSnapshot: false)
  187. DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {
  188. ncplayer.openAVPlayer(url: url)
  189. ncplayer.playerPlay()
  190. }
  191. } else {
  192. ncplayer.openAVPlayer(url: url)
  193. }
  194. }
  195. }
  196. @objc func triggerProgressTask(_ notification: NSNotification) {
  197. guard let userInfo = notification.userInfo as NSDictionary?,
  198. let progressNumber = userInfo["progress"] as? NSNumber
  199. else { return }
  200. let progress = progressNumber.floatValue
  201. if progress == 1 {
  202. self.progressView.progress = 0
  203. } else {
  204. self.progressView.progress = progress
  205. }
  206. }
  207. @objc func uploadStartFile(_ notification: NSNotification) {
  208. /*
  209. guard let userInfo = notification.userInfo as NSDictionary?,
  210. let serverUrl = userInfo["serverUrl"] as? String,
  211. let fileName = userInfo["fileName"] as? String,
  212. let sessionSelector = userInfo["sessionSelector"] as? String
  213. else { return }
  214. */
  215. }
  216. @objc func uploadedFile(_ notification: NSNotification) {
  217. guard let userInfo = notification.userInfo as NSDictionary?,
  218. let ocId = userInfo["ocId"] as? String,
  219. let error = userInfo["error"] as? NKError,
  220. error == .success,
  221. let index = metadatas.firstIndex(where: {$0.ocId == ocId}),
  222. let metadata = NCManageDatabase.shared.getMetadataFromOcId(ocId)
  223. else {
  224. return
  225. }
  226. metadatas[index] = metadata
  227. if currentViewController.metadata.ocId == ocId {
  228. currentViewController.reloadImage()
  229. } else {
  230. modifiedOcId.append(ocId)
  231. }
  232. }
  233. @objc func deleteFile(_ notification: NSNotification) {
  234. guard let userInfo = notification.userInfo as NSDictionary?,
  235. let ocId = userInfo["ocId"] as? String
  236. else { return }
  237. // Stop media
  238. if let ncplayer = currentViewController.ncplayer, ncplayer.isPlay() {
  239. ncplayer.playerPause()
  240. }
  241. let metadatas = self.metadatas.filter { $0.ocId != ocId }
  242. if self.metadatas.count == metadatas.count { return }
  243. self.metadatas = metadatas
  244. if ocId == currentViewController.metadata.ocId {
  245. shiftCurrentPage()
  246. }
  247. }
  248. @objc func renameFile(_ notification: NSNotification) {
  249. guard let userInfo = notification.userInfo as NSDictionary?,
  250. let ocId = userInfo["ocId"] as? String,
  251. let index = metadatas.firstIndex(where: {$0.ocId == ocId}),
  252. let metadata = NCManageDatabase.shared.getMetadataFromOcId(ocId)
  253. else { return }
  254. // Stop media
  255. if let ncplayer = currentViewController.ncplayer, ncplayer.isPlay() {
  256. ncplayer.playerPause()
  257. }
  258. metadatas[index] = metadata
  259. if index == currentIndex {
  260. navigationItem.title = metadata.fileNameView
  261. currentViewController.metadata = metadata
  262. self.currentViewController.metadata = metadata
  263. }
  264. }
  265. @objc func moveFile(_ notification: NSNotification) {
  266. guard let userInfo = notification.userInfo as NSDictionary?,
  267. let ocId = userInfo["ocId"] as? String
  268. else { return }
  269. // Stop media
  270. if let ncplayer = currentViewController.ncplayer, ncplayer.isPlay() {
  271. ncplayer.playerPause()
  272. }
  273. if metadatas.firstIndex(where: {$0.ocId == ocId}) != nil {
  274. deleteFile(notification)
  275. }
  276. }
  277. @objc func hidePlayerToolBar(_ notification: NSNotification) {
  278. if let userInfo = notification.userInfo as NSDictionary?, let ocId = userInfo["ocId"] as? String {
  279. if currentViewController.metadata.ocId == ocId {
  280. changeScreenMode(mode: .full)
  281. }
  282. }
  283. }
  284. @objc func showPlayerToolBar(_ notification: NSNotification) {
  285. if let userInfo = notification.userInfo as NSDictionary?, let ocId = userInfo["ocId"] as? String, let enableTimerAutoHide = userInfo["enableTimerAutoHide"] as? Bool {
  286. if currentViewController.metadata.ocId == ocId {
  287. changeScreenMode(mode: .normal, enableTimerAutoHide: enableTimerAutoHide)
  288. }
  289. }
  290. }
  291. @objc func reloadMediaPage(_ notification: NSNotification) {
  292. self.reloadCurrentPage()
  293. }
  294. @objc func applicationDidBecomeActive(_ notification: NSNotification) {
  295. progressView.progress = 0
  296. }
  297. // MARK: - Command Center
  298. func updateCommandCenter(ncplayer: NCPlayer, metadata: tableMetadata) {
  299. var nowPlayingInfo = [String: Any]()
  300. // Clear
  301. clearCommandCenter()
  302. UIApplication.shared.beginReceivingRemoteControlEvents()
  303. // Add handler for Play Command
  304. MPRemoteCommandCenter.shared().playCommand.isEnabled = true
  305. playCommand = MPRemoteCommandCenter.shared().playCommand.addTarget { _ in
  306. if !ncplayer.isPlay() {
  307. ncplayer.playerPlay()
  308. return .success
  309. }
  310. return .commandFailed
  311. }
  312. // Add handler for Pause Command
  313. MPRemoteCommandCenter.shared().pauseCommand.isEnabled = true
  314. pauseCommand = MPRemoteCommandCenter.shared().pauseCommand.addTarget { _ in
  315. if ncplayer.isPlay() {
  316. ncplayer.playerPause()
  317. return .success
  318. }
  319. return .commandFailed
  320. }
  321. // VIDEO / AUDIO () ()
  322. if metadata.classFile == NKCommon.TypeClassFile.video.rawValue || metadata.classFile == NKCommon.TypeClassFile.audio.rawValue {
  323. MPRemoteCommandCenter.shared().skipForwardCommand.isEnabled = true
  324. skipForwardCommand = MPRemoteCommandCenter.shared().skipForwardCommand.addTarget { event in
  325. let seconds = Float((event as! MPSkipIntervalCommandEvent).interval)
  326. self.currentViewController.playerToolBar?.skip(seconds: seconds)
  327. return.success
  328. }
  329. MPRemoteCommandCenter.shared().skipBackwardCommand.isEnabled = true
  330. skipBackwardCommand = MPRemoteCommandCenter.shared().skipBackwardCommand.addTarget { event in
  331. let seconds = Float((event as! MPSkipIntervalCommandEvent).interval)
  332. self.currentViewController.playerToolBar?.skip(seconds: -seconds)
  333. return.success
  334. }
  335. }
  336. nowPlayingInfo[MPMediaItemPropertyTitle] = metadata.fileNameView
  337. if let image = currentViewController.image {
  338. nowPlayingInfo[MPMediaItemPropertyArtwork] = MPMediaItemArtwork(boundsSize: image.size) { _ in
  339. return image
  340. }
  341. }
  342. MPNowPlayingInfoCenter.default().nowPlayingInfo = nowPlayingInfo
  343. }
  344. func clearCommandCenter() {
  345. UIApplication.shared.endReceivingRemoteControlEvents()
  346. MPNowPlayingInfoCenter.default().nowPlayingInfo = [:]
  347. MPRemoteCommandCenter.shared().playCommand.isEnabled = false
  348. MPRemoteCommandCenter.shared().pauseCommand.isEnabled = false
  349. MPRemoteCommandCenter.shared().skipForwardCommand.isEnabled = false
  350. MPRemoteCommandCenter.shared().skipBackwardCommand.isEnabled = false
  351. MPRemoteCommandCenter.shared().nextTrackCommand.isEnabled = false
  352. MPRemoteCommandCenter.shared().previousTrackCommand.isEnabled = false
  353. if let playCommand = playCommand {
  354. MPRemoteCommandCenter.shared().playCommand.removeTarget(playCommand)
  355. self.playCommand = nil
  356. }
  357. if let pauseCommand = pauseCommand {
  358. MPRemoteCommandCenter.shared().pauseCommand.removeTarget(pauseCommand)
  359. self.pauseCommand = nil
  360. }
  361. if let skipForwardCommand = skipForwardCommand {
  362. MPRemoteCommandCenter.shared().skipForwardCommand.removeTarget(skipForwardCommand)
  363. self.skipForwardCommand = nil
  364. }
  365. if let skipBackwardCommand = skipBackwardCommand {
  366. MPRemoteCommandCenter.shared().skipBackwardCommand.removeTarget(skipBackwardCommand)
  367. self.skipBackwardCommand = nil
  368. }
  369. if let nextTrackCommand = nextTrackCommand {
  370. MPRemoteCommandCenter.shared().nextTrackCommand.removeTarget(nextTrackCommand)
  371. self.nextTrackCommand = nil
  372. }
  373. if let previousTrackCommand = previousTrackCommand {
  374. MPRemoteCommandCenter.shared().previousTrackCommand.removeTarget(previousTrackCommand)
  375. self.previousTrackCommand = nil
  376. }
  377. }
  378. }
  379. // MARK: - UIPageViewController Delegate Datasource
  380. extension NCViewerMediaPage: UIPageViewControllerDelegate, UIPageViewControllerDataSource {
  381. func shiftCurrentPage() {
  382. if metadatas.count == 0 {
  383. self.viewUnload()
  384. return
  385. }
  386. var direction: UIPageViewController.NavigationDirection = .forward
  387. if currentIndex == metadatas.count {
  388. currentIndex -= 1
  389. direction = .reverse
  390. }
  391. let viewerMedia = getViewerMedia(index: currentIndex, metadata: metadatas[currentIndex])
  392. pageViewController.setViewControllers([viewerMedia], direction: direction, animated: true, completion: nil)
  393. }
  394. func reloadCurrentPage() {
  395. let viewerMedia = getViewerMedia(index: currentIndex, metadata: metadatas[currentIndex])
  396. pageViewController.setViewControllers([viewerMedia], direction: .forward, animated: false, completion: nil)
  397. }
  398. func goTo(index: Int, direction: UIPageViewController.NavigationDirection, autoPlay: Bool) {
  399. currentIndex = index
  400. let viewerMedia = getViewerMedia(index: currentIndex, metadata: metadatas[currentIndex])
  401. pageViewController.setViewControllers([viewerMedia], direction: direction, animated: true, completion: nil)
  402. }
  403. func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
  404. if currentIndex == 0 { return nil }
  405. let viewerMedia = getViewerMedia(index: currentIndex - 1, metadata: metadatas[currentIndex - 1])
  406. return viewerMedia
  407. }
  408. func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
  409. if currentIndex == metadatas.count - 1 { return nil }
  410. let viewerMedia = getViewerMedia(index: currentIndex + 1, metadata: metadatas[currentIndex + 1])
  411. return viewerMedia
  412. }
  413. // START TRANSITION
  414. func pageViewController(_ pageViewController: UIPageViewController, willTransitionTo pendingViewControllers: [UIViewController]) {
  415. guard let nextViewController = pendingViewControllers.first as? NCViewerMedia else { return }
  416. nextIndex = nextViewController.index
  417. }
  418. // END TRANSITION
  419. func pageViewController(_ pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) {
  420. if completed && nextIndex != nil {
  421. previousViewControllers.forEach { viewController in
  422. let viewerMedia = viewController as! NCViewerMedia
  423. viewerMedia.ncplayer?.closeAVPlayer()
  424. }
  425. currentIndex = nextIndex!
  426. }
  427. self.nextIndex = nil
  428. }
  429. }
  430. // MARK: - UIGestureRecognizerDelegate
  431. extension NCViewerMediaPage: UIGestureRecognizerDelegate {
  432. func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
  433. if let gestureRecognizer = gestureRecognizer as? UIPanGestureRecognizer {
  434. let velocity = gestureRecognizer.velocity(in: self.view)
  435. var velocityCheck: Bool = false
  436. if UIDevice.current.orientation.isLandscape {
  437. velocityCheck = velocity.x < 0
  438. } else {
  439. velocityCheck = velocity.y < 0
  440. }
  441. if velocityCheck {
  442. return false
  443. }
  444. }
  445. return true
  446. }
  447. func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
  448. if otherGestureRecognizer == currentViewController.scrollView.panGestureRecognizer {
  449. if self.currentViewController.scrollView.contentOffset.y == 0 {
  450. return true
  451. }
  452. }
  453. return false
  454. }
  455. @objc func didPanWith(gestureRecognizer: UIPanGestureRecognizer) {
  456. currentViewController.didPanWith(gestureRecognizer: gestureRecognizer)
  457. }
  458. @objc func didSingleTapWith(gestureRecognizer: UITapGestureRecognizer) {
  459. if currentScreenMode == .full {
  460. changeScreenMode(mode: .normal, enableTimerAutoHide: true)
  461. } else {
  462. changeScreenMode(mode: .full)
  463. }
  464. }
  465. //
  466. // LIVE PHOTO
  467. //
  468. @objc func didLongpressGestureEvent(gestureRecognizer: UITapGestureRecognizer) {
  469. if !currentViewController.metadata.livePhoto { return }
  470. if gestureRecognizer.state == .began {
  471. let fileName = (currentViewController.metadata.fileNameView as NSString).deletingPathExtension + ".mov"
  472. if let metadata = NCManageDatabase.shared.getMetadata(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@ AND fileNameView LIKE[c] %@", currentViewController.metadata.account, currentViewController.metadata.serverUrl, fileName)), CCUtility.fileProviderStorageExists(metadata) {
  473. AudioServicesPlaySystemSound(1519) // peek feedback
  474. currentViewController.playLivePhoto(filePath: CCUtility.getDirectoryProviderStorageOcId(metadata.ocId, fileNameView: metadata.fileNameView)!)
  475. }
  476. } else if gestureRecognizer.state == .ended {
  477. currentViewController.stopLivePhoto()
  478. }
  479. }
  480. }