NCViewerMediaPage.swift 25 KB

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