NCViewerMediaPage.swift 29 KB

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