NCViewerMediaPage.swift 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670
  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 Alamofire
  27. enum ScreenMode {
  28. case full, normal
  29. }
  30. var viewerMediaScreenMode: ScreenMode = .normal
  31. class NCViewerMediaPage: UIViewController {
  32. @IBOutlet weak var progressView: UIProgressView!
  33. /// Parameters
  34. var ocIds: [String] = []
  35. var currentIndex: Int = 0
  36. var delegateViewController: UIViewController?
  37. ///
  38. var modifiedOcId: [String] = []
  39. var nextIndex: Int?
  40. var panGestureRecognizer: UIPanGestureRecognizer!
  41. var singleTapGestureRecognizer: UITapGestureRecognizer!
  42. var longtapGestureRecognizer: UILongPressGestureRecognizer!
  43. var textColor: UIColor = NCBrandColor.shared.textColor
  44. var playCommand: Any?
  45. var pauseCommand: Any?
  46. var skipForwardCommand: Any?
  47. var skipBackwardCommand: Any?
  48. var nextTrackCommand: Any?
  49. var previousTrackCommand: Any?
  50. let utilityFileSystem = NCUtilityFileSystem()
  51. let database = NCManageDatabase.shared
  52. // This prevents the scroll views to scroll when you drag and drop files/images/subjects (from this or other apps)
  53. // https://forums.developer.apple.com/forums/thread/89396 and https://forums.developer.apple.com/forums/thread/115736
  54. var preventScrollOnDragAndDrop = true
  55. var timerAutoHide: Timer?
  56. private var timerAutoHideSeconds: Double = 4
  57. private lazy var moreNavigationItem = UIBarButtonItem(image: NCImageCache.shared.getImageButtonMore(), style: .plain, target: self, action: #selector(openMenuMore))
  58. private lazy var imageDetailNavigationItem = UIBarButtonItem(image: NCUtility().loadImage(named: "info.circle", colors: [NCBrandColor.shared.iconImageColor]), style: .plain, target: self, action: #selector(toggleDetail))
  59. // swiftlint:disable force_cast
  60. var pageViewController: UIPageViewController {
  61. return self.children[0] as! UIPageViewController
  62. }
  63. var currentViewController: NCViewerMedia {
  64. return self.pageViewController.viewControllers![0] as! NCViewerMedia
  65. }
  66. // swiftlint:enable force_cast
  67. private var hideStatusBar: Bool = false {
  68. didSet {
  69. setNeedsStatusBarAppearanceUpdate()
  70. }
  71. }
  72. // MARK: - View Life Cycle
  73. override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
  74. super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
  75. viewerMediaScreenMode = .normal
  76. }
  77. required init?(coder: NSCoder) {
  78. super.init(coder: coder)
  79. viewerMediaScreenMode = .normal
  80. }
  81. override func viewDidLoad() {
  82. super.viewDidLoad()
  83. navigationController?.navigationBar.tintColor = NCBrandColor.shared.iconImageColor
  84. let metadata = database.getMetadataFromOcId(ocIds[currentIndex])!
  85. singleTapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(didSingleTapWith(gestureRecognizer:)))
  86. panGestureRecognizer = UIPanGestureRecognizer(target: self, action: #selector(didPanWith(gestureRecognizer:)))
  87. longtapGestureRecognizer = UILongPressGestureRecognizer()
  88. longtapGestureRecognizer.delaysTouchesBegan = true
  89. longtapGestureRecognizer.minimumPressDuration = 0.3
  90. longtapGestureRecognizer.delegate = self
  91. longtapGestureRecognizer.addTarget(self, action: #selector(didLongpressGestureEvent(gestureRecognizer:)))
  92. pageViewController.delegate = self
  93. pageViewController.dataSource = self
  94. pageViewController.view.addGestureRecognizer(panGestureRecognizer)
  95. pageViewController.view.addGestureRecognizer(singleTapGestureRecognizer)
  96. pageViewController.view.addGestureRecognizer(longtapGestureRecognizer)
  97. progressView.tintColor = NCBrandColor.shared.getElement(account: metadata.account)
  98. progressView.trackTintColor = .clear
  99. progressView.progress = 0
  100. let viewerMedia = getViewerMedia(index: currentIndex, metadata: metadata)
  101. pageViewController.setViewControllers([viewerMedia], direction: .forward, animated: true, completion: nil)
  102. changeScreenMode(mode: viewerMediaScreenMode)
  103. NotificationCenter.default.addObserver(self, selector: #selector(viewUnload), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterChangeUser), object: nil)
  104. NotificationCenter.default.addObserver(self, selector: #selector(pageViewController.enableSwipeGesture), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterEnableSwipeGesture), object: nil)
  105. NotificationCenter.default.addObserver(self, selector: #selector(pageViewController.disableSwipeGesture), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterDisableSwipeGesture), object: nil)
  106. NotificationCenter.default.addObserver(self, selector: #selector(deleteFile(_:)), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterDeleteFile), object: nil)
  107. NotificationCenter.default.addObserver(self, selector: #selector(downloadedFile(_:)), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterDownloadedFile), object: nil)
  108. NotificationCenter.default.addObserver(self, selector: #selector(triggerProgressTask(_:)), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterProgressTask), object: nil)
  109. NotificationCenter.default.addObserver(self, selector: #selector(uploadStartFile(_:)), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterUploadStartFile), object: nil)
  110. NotificationCenter.default.addObserver(self, selector: #selector(uploadedFile(_:)), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterUploadedFile), object: nil)
  111. NotificationCenter.default.addObserver(self, selector: #selector(applicationDidBecomeActive(_:)), name: UIApplication.didBecomeActiveNotification, object: nil)
  112. if NCNetworking.shared.isOnline {
  113. if currentViewController.metadata.isImage {
  114. navigationItem.rightBarButtonItems = [moreNavigationItem, imageDetailNavigationItem]
  115. } else {
  116. navigationItem.rightBarButtonItems = [moreNavigationItem]
  117. }
  118. }
  119. for view in self.pageViewController.view.subviews {
  120. if let scrollView = view as? UIScrollView {
  121. scrollView.delegate = self
  122. }
  123. }
  124. }
  125. deinit {
  126. timerAutoHide?.invalidate()
  127. NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterEnableSwipeGesture), object: nil)
  128. NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterDisableSwipeGesture), object: nil)
  129. NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterDeleteFile), object: nil)
  130. NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterDownloadedFile), object: nil)
  131. NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterProgressTask), object: nil)
  132. NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterUploadStartFile), object: nil)
  133. NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterUploadedFile), object: nil)
  134. NotificationCenter.default.removeObserver(self, name: UIApplication.didBecomeActiveNotification, object: nil)
  135. }
  136. override func viewDidAppear(_ animated: Bool) {
  137. super.viewDidAppear(animated)
  138. startTimerAutoHide()
  139. }
  140. override func viewDidDisappear(_ animated: Bool) {
  141. super.viewDidDisappear(animated)
  142. currentViewController.ncplayer?.playerStop()
  143. timerAutoHide?.invalidate()
  144. clearCommandCenter()
  145. }
  146. override var preferredStatusBarStyle: UIStatusBarStyle {
  147. if viewerMediaScreenMode == .normal {
  148. return .default
  149. } else {
  150. return .lightContent
  151. }
  152. }
  153. override var prefersHomeIndicatorAutoHidden: Bool {
  154. return viewerMediaScreenMode == .full
  155. }
  156. override var prefersStatusBarHidden: Bool {
  157. return hideStatusBar
  158. }
  159. func getViewerMedia(index: Int, metadata: tableMetadata) -> NCViewerMedia {
  160. // swiftlint:disable force_cast
  161. let viewerMedia = UIStoryboard(name: "NCViewerMediaPage", bundle: nil).instantiateViewController(withIdentifier: "NCViewerMedia") as! NCViewerMedia
  162. // swiftlint:enable force_cast
  163. viewerMedia.index = index
  164. viewerMedia.metadata = metadata
  165. viewerMedia.viewerMediaPage = self
  166. viewerMedia.delegate = self
  167. singleTapGestureRecognizer.require(toFail: viewerMedia.doubleTapGestureRecognizer)
  168. return viewerMedia
  169. }
  170. @objc func viewUnload() {
  171. navigationController?.popViewController(animated: true)
  172. }
  173. @objc private func openMenuMore() {
  174. let imageIcon = NCUtility().getImage(ocId: currentViewController.metadata.ocId, etag: currentViewController.metadata.etag, ext: NCGlobal.shared.previewExt512)
  175. NCViewer().toggleMenu(controller: self.tabBarController as? NCMainTabBarController, metadata: currentViewController.metadata, webView: false, imageIcon: imageIcon)
  176. }
  177. @objc private func toggleDetail() {
  178. currentViewController.toggleDetail()
  179. }
  180. func changeScreenMode(mode: ScreenMode) {
  181. let metadata = currentViewController.metadata
  182. let fullscreen = currentViewController.playerToolBar?.isFullscreen ?? false
  183. if mode == .normal {
  184. if fullscreen {
  185. navigationController?.setNavigationBarHidden(true, animated: true)
  186. hideStatusBar = true
  187. progressView.isHidden = true
  188. } else {
  189. navigationController?.setNavigationBarHidden(false, animated: true)
  190. hideStatusBar = false
  191. progressView.isHidden = false
  192. }
  193. if metadata.isAudioOrVideo {
  194. colorNavigationController(backgroundColor: .black, titleColor: NCBrandColor.shared.textColor, tintColor: nil, withoutShadow: false)
  195. currentViewController.playerToolBar?.show()
  196. view.backgroundColor = .black
  197. textColor = .white
  198. } else {
  199. colorNavigationController(backgroundColor: .systemBackground, titleColor: NCBrandColor.shared.textColor, tintColor: nil, withoutShadow: false)
  200. view.backgroundColor = .systemGray6
  201. textColor = NCBrandColor.shared.textColor
  202. }
  203. } else if !currentViewController.detailView.isShown {
  204. navigationController?.setNavigationBarHidden(true, animated: true)
  205. hideStatusBar = true
  206. progressView.isHidden = true
  207. if metadata.isVideo {
  208. currentViewController.playerToolBar?.hide()
  209. }
  210. view.backgroundColor = .black
  211. textColor = .white
  212. }
  213. if fullscreen {
  214. pageViewController.disableSwipeGesture()
  215. } else {
  216. pageViewController.enableSwipeGesture()
  217. }
  218. viewerMediaScreenMode = mode
  219. print("Screen mode: \(viewerMediaScreenMode)")
  220. startTimerAutoHide()
  221. setNeedsStatusBarAppearanceUpdate()
  222. setNeedsUpdateOfHomeIndicatorAutoHidden()
  223. currentViewController.reloadDetail()
  224. }
  225. @objc func startTimerAutoHide() {
  226. timerAutoHide?.invalidate()
  227. timerAutoHide = Timer.scheduledTimer(timeInterval: timerAutoHideSeconds, target: self, selector: #selector(autoHide), userInfo: nil, repeats: true)
  228. }
  229. @objc func autoHide() {
  230. let metadata = currentViewController.metadata
  231. if metadata.isVideo, viewerMediaScreenMode == .normal {
  232. changeScreenMode(mode: .full)
  233. }
  234. }
  235. func colorNavigationController(backgroundColor: UIColor, titleColor: UIColor, tintColor: UIColor?, withoutShadow: Bool) {
  236. let appearance = UINavigationBarAppearance()
  237. appearance.titleTextAttributes = [.foregroundColor: titleColor]
  238. appearance.largeTitleTextAttributes = [.foregroundColor: titleColor]
  239. if withoutShadow {
  240. appearance.shadowColor = .clear
  241. appearance.shadowImage = UIImage()
  242. }
  243. if let tintColor = tintColor {
  244. navigationController?.navigationBar.tintColor = tintColor
  245. }
  246. navigationController?.view.backgroundColor = backgroundColor
  247. navigationController?.navigationBar.barTintColor = titleColor
  248. navigationController?.navigationBar.standardAppearance = appearance
  249. navigationController?.navigationBar.compactAppearance = appearance
  250. navigationController?.navigationBar.scrollEdgeAppearance = appearance
  251. }
  252. // MARK: - NotificationCenter
  253. @objc func downloadedFile(_ notification: NSNotification) {
  254. guard let userInfo = notification.userInfo as NSDictionary?,
  255. let ocId = userInfo["ocId"] as? String
  256. else {
  257. return
  258. }
  259. self.progressView.progress = 0
  260. let metadata = self.currentViewController.metadata
  261. guard metadata.ocId == ocId, self.utilityFileSystem.fileProviderStorageExists(metadata) else { return }
  262. if metadata.isAudioOrVideo, let ncplayer = self.currentViewController.ncplayer {
  263. let url = URL(fileURLWithPath: self.utilityFileSystem.getDirectoryProviderStorageOcId(metadata.ocId, fileNameView: metadata.fileNameView))
  264. if ncplayer.isPlaying() {
  265. ncplayer.playerPause()
  266. DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {
  267. ncplayer.openAVPlayer(url: url)
  268. ncplayer.playerPlay()
  269. }
  270. } else {
  271. ncplayer.openAVPlayer(url: url)
  272. }
  273. } else if metadata.isImage {
  274. self.currentViewController.loadImage()
  275. }
  276. }
  277. @objc func triggerProgressTask(_ notification: NSNotification) {
  278. guard let userInfo = notification.userInfo as NSDictionary?,
  279. let progressNumber = userInfo["progress"] as? NSNumber
  280. else { return }
  281. let progress = progressNumber.floatValue
  282. if progress == 1 {
  283. self.progressView.progress = 0
  284. } else {
  285. self.progressView.progress = progress
  286. }
  287. }
  288. @objc func uploadStartFile(_ notification: NSNotification) { }
  289. @objc func uploadedFile(_ notification: NSNotification) {
  290. guard let userInfo = notification.userInfo as NSDictionary?,
  291. let ocId = userInfo["ocId"] as? String,
  292. let error = userInfo["error"] as? NKError,
  293. error == .success
  294. else { return }
  295. if self.currentViewController.metadata.ocId == ocId {
  296. self.currentViewController.loadImage()
  297. } else {
  298. self.modifiedOcId.append(ocId)
  299. }
  300. }
  301. @objc func deleteFile(_ notification: NSNotification) {
  302. guard let userInfo = notification.userInfo as NSDictionary?,
  303. let error = userInfo["error"] as? NKError else { return }
  304. if error != .success {
  305. NCContentPresenter().showError(error: error)
  306. }
  307. if let ncplayer = currentViewController.ncplayer, ncplayer.isPlaying() {
  308. ncplayer.playerPause()
  309. }
  310. self.viewUnload()
  311. }
  312. @objc func applicationDidBecomeActive(_ notification: NSNotification) {
  313. progressView.progress = 0
  314. changeScreenMode(mode: .normal)
  315. }
  316. // MARK: - Command Center
  317. func updateCommandCenter(ncplayer: NCPlayer, title: String) {
  318. var nowPlayingInfo = [String: Any]()
  319. UIApplication.shared.beginReceivingRemoteControlEvents()
  320. // Add handler for Play Command
  321. MPRemoteCommandCenter.shared().playCommand.isEnabled = true
  322. playCommand = MPRemoteCommandCenter.shared().playCommand.addTarget { _ in
  323. if !ncplayer.isPlaying() {
  324. ncplayer.playerPlay()
  325. return .success
  326. }
  327. return .commandFailed
  328. }
  329. // Add handler for Pause Command
  330. MPRemoteCommandCenter.shared().pauseCommand.isEnabled = true
  331. pauseCommand = MPRemoteCommandCenter.shared().pauseCommand.addTarget { _ in
  332. if ncplayer.isPlaying() {
  333. ncplayer.playerPause()
  334. return .success
  335. }
  336. return .commandFailed
  337. }
  338. // >>
  339. MPRemoteCommandCenter.shared().skipForwardCommand.isEnabled = true
  340. skipForwardCommand = MPRemoteCommandCenter.shared().skipForwardCommand.addTarget { event in
  341. let seconds = Int32((event as? MPSkipIntervalCommandEvent)?.interval ?? 0)
  342. ncplayer.player.jumpForward(seconds)
  343. return.success
  344. }
  345. // <<
  346. MPRemoteCommandCenter.shared().skipBackwardCommand.isEnabled = true
  347. skipBackwardCommand = MPRemoteCommandCenter.shared().skipBackwardCommand.addTarget { event in
  348. let seconds = Int32((event as? MPSkipIntervalCommandEvent)?.interval ?? 0)
  349. ncplayer.player.jumpBackward(seconds)
  350. return.success
  351. }
  352. nowPlayingInfo[MPMediaItemPropertyTitle] = title
  353. if let image = currentViewController.image {
  354. nowPlayingInfo[MPMediaItemPropertyArtwork] = MPMediaItemArtwork(boundsSize: image.size) { _ in
  355. return image
  356. }
  357. }
  358. MPNowPlayingInfoCenter.default().nowPlayingInfo = nowPlayingInfo
  359. }
  360. func clearCommandCenter() {
  361. UIApplication.shared.endReceivingRemoteControlEvents()
  362. MPNowPlayingInfoCenter.default().nowPlayingInfo = [:]
  363. MPRemoteCommandCenter.shared().playCommand.isEnabled = false
  364. MPRemoteCommandCenter.shared().pauseCommand.isEnabled = false
  365. MPRemoteCommandCenter.shared().skipForwardCommand.isEnabled = false
  366. MPRemoteCommandCenter.shared().skipBackwardCommand.isEnabled = false
  367. MPRemoteCommandCenter.shared().nextTrackCommand.isEnabled = false
  368. MPRemoteCommandCenter.shared().previousTrackCommand.isEnabled = false
  369. if let playCommand = playCommand {
  370. MPRemoteCommandCenter.shared().playCommand.removeTarget(playCommand)
  371. self.playCommand = nil
  372. }
  373. if let pauseCommand = pauseCommand {
  374. MPRemoteCommandCenter.shared().pauseCommand.removeTarget(pauseCommand)
  375. self.pauseCommand = nil
  376. }
  377. if let skipForwardCommand = skipForwardCommand {
  378. MPRemoteCommandCenter.shared().skipForwardCommand.removeTarget(skipForwardCommand)
  379. self.skipForwardCommand = nil
  380. }
  381. if let skipBackwardCommand = skipBackwardCommand {
  382. MPRemoteCommandCenter.shared().skipBackwardCommand.removeTarget(skipBackwardCommand)
  383. self.skipBackwardCommand = nil
  384. }
  385. if let nextTrackCommand = nextTrackCommand {
  386. MPRemoteCommandCenter.shared().nextTrackCommand.removeTarget(nextTrackCommand)
  387. self.nextTrackCommand = nil
  388. }
  389. if let previousTrackCommand = previousTrackCommand {
  390. MPRemoteCommandCenter.shared().previousTrackCommand.removeTarget(previousTrackCommand)
  391. self.previousTrackCommand = nil
  392. }
  393. }
  394. }
  395. // MARK: - UIPageViewController Delegate Datasource
  396. extension NCViewerMediaPage: UIPageViewControllerDelegate, UIPageViewControllerDataSource {
  397. func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
  398. guard currentIndex > 0,
  399. let metadata = database.getMetadataFromOcId(ocIds[currentIndex - 1]) else { return nil }
  400. let viewerMedia = getViewerMedia(index: currentIndex - 1, metadata: metadata)
  401. return viewerMedia
  402. }
  403. func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
  404. guard currentIndex < ocIds.count - 1,
  405. let metadata = database.getMetadataFromOcId(ocIds[currentIndex + 1]) else { return nil }
  406. let viewerMedia = getViewerMedia(index: currentIndex + 1, metadata: metadata)
  407. return viewerMedia
  408. }
  409. // START TRANSITION
  410. func pageViewController(_ pageViewController: UIPageViewController, willTransitionTo pendingViewControllers: [UIViewController]) {
  411. guard let nextViewController = pendingViewControllers.first as? NCViewerMedia else { return }
  412. nextIndex = nextViewController.index
  413. if nextViewController.metadata.isImage {
  414. navigationItem.rightBarButtonItems = [moreNavigationItem, imageDetailNavigationItem]
  415. } else {
  416. navigationItem.rightBarButtonItems = [moreNavigationItem]
  417. }
  418. if nextViewController.detailView.isShown {
  419. changeScreenMode(mode: .normal)
  420. }
  421. }
  422. // END TRANSITION
  423. func pageViewController(_ pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) {
  424. if completed && nextIndex != nil {
  425. previousViewControllers.forEach { viewController in
  426. let viewerMedia = viewController as? NCViewerMedia
  427. viewerMedia?.ncplayer?.playerStop()
  428. viewerMedia?.closeDetail()
  429. }
  430. currentIndex = nextIndex!
  431. }
  432. changeScreenMode(mode: viewerMediaScreenMode)
  433. startTimerAutoHide()
  434. self.nextIndex = nil
  435. }
  436. }
  437. // MARK: - UIGestureRecognizerDelegate
  438. extension NCViewerMediaPage: UIGestureRecognizerDelegate {
  439. func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
  440. if let gestureRecognizer = gestureRecognizer as? UIPanGestureRecognizer {
  441. let velocity = gestureRecognizer.velocity(in: self.view)
  442. var velocityCheck: Bool = false
  443. if UIDevice.current.orientation.isLandscape {
  444. velocityCheck = velocity.x < 0
  445. } else {
  446. velocityCheck = velocity.y < 0
  447. }
  448. if velocityCheck {
  449. return false
  450. }
  451. }
  452. return true
  453. }
  454. func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
  455. if otherGestureRecognizer == currentViewController.scrollView.panGestureRecognizer {
  456. if self.currentViewController.scrollView.contentOffset.y == 0 {
  457. return true
  458. }
  459. }
  460. return false
  461. }
  462. @objc func didPanWith(gestureRecognizer: UIPanGestureRecognizer) {
  463. currentViewController.didPanWith(gestureRecognizer: gestureRecognizer)
  464. }
  465. @objc func didSingleTapWith(gestureRecognizer: UITapGestureRecognizer) {
  466. if currentViewController.detailView.isShown { return }
  467. if viewerMediaScreenMode == .full {
  468. changeScreenMode(mode: .normal)
  469. } else {
  470. changeScreenMode(mode: .full)
  471. }
  472. }
  473. // MARK: - Live Photo
  474. @objc func didLongpressGestureEvent(gestureRecognizer: UITapGestureRecognizer) {
  475. if !currentViewController.metadata.isLivePhoto || currentViewController.detailView.isShown { return }
  476. if gestureRecognizer.state == .began {
  477. if let metadataLive = NCManageDatabase.shared.getMetadataLivePhoto(metadata: currentViewController.metadata),
  478. utilityFileSystem.fileProviderStorageExists(metadataLive) {
  479. AudioServicesPlaySystemSound(1519) // peek feedback
  480. currentViewController.playLivePhoto(filePath: utilityFileSystem.getDirectoryProviderStorageOcId(metadataLive.ocId, fileNameView: metadataLive.fileName))
  481. }
  482. } else if gestureRecognizer.state == .ended {
  483. currentViewController.stopLivePhoto()
  484. }
  485. }
  486. }
  487. extension UIPageViewController {
  488. @objc func enableSwipeGesture() {
  489. for view in self.view.subviews {
  490. if let subView = view as? UIScrollView {
  491. subView.isScrollEnabled = true
  492. }
  493. }
  494. }
  495. @objc func disableSwipeGesture() {
  496. for view in self.view.subviews {
  497. if let subView = view as? UIScrollView {
  498. subView.isScrollEnabled = false
  499. }
  500. }
  501. }
  502. }
  503. extension NCViewerMediaPage: NCViewerMediaViewDelegate {
  504. func didOpenDetail() {
  505. changeScreenMode(mode: .normal)
  506. imageDetailNavigationItem.image = NCUtility().loadImage(named: "info.circle.fill")
  507. }
  508. func didCloseDetail() {
  509. imageDetailNavigationItem.image = NCUtility().loadImage(named: "info.circle")
  510. }
  511. }
  512. extension NCViewerMediaPage: UIScrollViewDelegate {
  513. func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
  514. preventScrollOnDragAndDrop = false
  515. }
  516. func scrollViewDidScroll(_ scrollView: UIScrollView) {
  517. if preventScrollOnDragAndDrop {
  518. scrollView.setContentOffset(CGPoint(x: view.frame.width + 10, y: 0), animated: false)
  519. }
  520. }
  521. func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
  522. if !decelerate {
  523. preventScrollOnDragAndDrop = true
  524. }
  525. }
  526. func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
  527. preventScrollOnDragAndDrop = true
  528. }
  529. }