NCViewerMedia.swift 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623
  1. //
  2. // NCViewerMedia.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 24/10/2020.
  6. // Copyright © 2020 Marino Faggiana. All rights reserved.
  7. //
  8. // Author Marino Faggiana <marino.faggiana@nextcloud.com>
  9. //
  10. // This program is free software: you can redistribute it and/or modify
  11. // it under the terms of the GNU General Public License as published by
  12. // the Free Software Foundation, either version 3 of the License, or
  13. // (at your option) any later version.
  14. //
  15. // This program is distributed in the hope that it will be useful,
  16. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. // GNU General Public License for more details.
  19. //
  20. // You should have received a copy of the GNU General Public License
  21. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. //
  23. import UIKit
  24. import SVGKit
  25. import NCCommunication
  26. class NCViewerMedia: UIViewController {
  27. @IBOutlet weak var progressView: UIProgressView!
  28. enum ScreenMode {
  29. case full, normal
  30. }
  31. var currentScreenMode: ScreenMode = .normal
  32. var saveScreenModeImage: ScreenMode = .normal
  33. var pageViewController: UIPageViewController {
  34. return self.children[0] as! UIPageViewController
  35. }
  36. var currentViewController: NCViewerMediaZoom {
  37. return self.pageViewController.viewControllers![0] as! NCViewerMediaZoom
  38. }
  39. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  40. var metadatas: [tableMetadata] = []
  41. var currentIndex = 0
  42. var nextIndex: Int?
  43. var IndexInPlay: Int = -1
  44. var ncplayerLivePhoto: NCPlayer?
  45. var panGestureRecognizer: UIPanGestureRecognizer!
  46. var singleTapGestureRecognizer: UITapGestureRecognizer!
  47. var longtapGestureRecognizer: UILongPressGestureRecognizer!
  48. var cache: [Int:NCViewerMediaZoom] = [:]
  49. let cacheSize: Int = 5
  50. var textColor: UIColor = NCBrandColor.shared.label
  51. // MARK: - View Life Cycle
  52. override func viewDidLoad() {
  53. super.viewDidLoad()
  54. navigationItem.rightBarButtonItem = UIBarButtonItem.init(image: UIImage(named: "more")!.image(color: NCBrandColor.shared.label, size: 25), style: .plain, target: self, action: #selector(self.openMenuMore))
  55. singleTapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(didSingleTapWith(gestureRecognizer:)))
  56. panGestureRecognizer = UIPanGestureRecognizer(target: self, action: #selector(didPanWith(gestureRecognizer:)))
  57. longtapGestureRecognizer = UILongPressGestureRecognizer()
  58. longtapGestureRecognizer.delaysTouchesBegan = true
  59. longtapGestureRecognizer.minimumPressDuration = 0.3
  60. longtapGestureRecognizer.delegate = self
  61. longtapGestureRecognizer.addTarget(self, action: #selector(didLongpressGestureEvent(gestureRecognizer:)))
  62. pageViewController.delegate = self
  63. pageViewController.dataSource = self
  64. pageViewController.view.addGestureRecognizer(panGestureRecognizer)
  65. pageViewController.view.addGestureRecognizer(singleTapGestureRecognizer)
  66. pageViewController.view.addGestureRecognizer(longtapGestureRecognizer)
  67. // save cache
  68. if let viewerMediaZoom = getCache(index: currentIndex) {
  69. pageViewController.setViewControllers([viewerMediaZoom], direction: .forward, animated: true, completion: nil)
  70. } else {
  71. let viewerMediaZoom = setCache(index: currentIndex, image: getImageMetadata(metadatas[currentIndex]), metadata: metadatas[currentIndex], direction: .forward)
  72. pageViewController.setViewControllers([viewerMediaZoom], direction: .forward, animated: true, completion: nil)
  73. }
  74. NotificationCenter.default.addObserver(self, selector: #selector(changeTheming), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterChangeTheming), object: nil)
  75. NotificationCenter.default.addObserver(self, selector: #selector(viewUnload), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterMenuDetailClose), object: nil)
  76. progressView.tintColor = NCBrandColor.shared.brandElement
  77. progressView.trackTintColor = .clear
  78. progressView.progress = 0
  79. }
  80. override func viewWillAppear(_ animated: Bool) {
  81. super.viewWillAppear(animated)
  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(uploadedFile(_:)), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterUploadedFile), object: nil)
  87. NotificationCenter.default.addObserver(self, selector: #selector(triggerProgressTask(_:)), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterProgressTask), object:nil)
  88. NotificationCenter.default.addObserver(self, selector: #selector(downloadedThumbnail(_:)), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterDownloadedThumbnail), 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. }
  92. override func viewWillDisappear(_ animated: Bool) {
  93. super.viewWillDisappear(animated)
  94. // Save time video
  95. if let ncplayer = currentViewController.ncplayer, ncplayer.isPlay() {
  96. ncplayer.playerPause()
  97. ncplayer.saveCurrentTime()
  98. }
  99. NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterDeleteFile), object: nil)
  100. NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterRenameFile), object: nil)
  101. NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterMoveFile), object: nil)
  102. NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterDownloadedFile), object: nil)
  103. NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterUploadedFile), object: nil)
  104. NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterProgressTask), object: nil)
  105. NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterDownloadedThumbnail), object: nil)
  106. }
  107. override var preferredStatusBarStyle: UIStatusBarStyle {
  108. if currentScreenMode == .normal {
  109. return .default
  110. } else {
  111. return .lightContent
  112. }
  113. }
  114. // MARK: -
  115. func getCache(index: Int) -> NCViewerMediaZoom? {
  116. return cache[index]
  117. }
  118. func setCache(index: Int, image: UIImage?, metadata: tableMetadata, direction: UIPageViewController.NavigationDirection) -> NCViewerMediaZoom {
  119. let viewerMediaZoom = UIStoryboard(name: "NCViewerMedia", bundle: nil).instantiateViewController(withIdentifier: "NCViewerMediaZoom") as! NCViewerMediaZoom
  120. viewerMediaZoom.index = index
  121. viewerMediaZoom.image = image
  122. viewerMediaZoom.metadata = metadata
  123. viewerMediaZoom.viewerMedia = self
  124. singleTapGestureRecognizer.require(toFail: viewerMediaZoom.doubleTapGestureRecognizer)
  125. if cache.count < cacheSize {
  126. cache[index] = viewerMediaZoom
  127. } else {
  128. let sortedCache = cache.sorted { (first, second) -> Bool in
  129. return first.key < second.key
  130. }
  131. if direction == .forward, let firstCache = sortedCache.first {
  132. cache[firstCache.key] = nil
  133. } else if direction == .reverse, let lastCache = sortedCache.last {
  134. cache[lastCache.key] = nil
  135. }
  136. cache[index] = viewerMediaZoom
  137. }
  138. return viewerMediaZoom
  139. }
  140. @objc func viewUnload() {
  141. navigationController?.popViewController(animated: true)
  142. }
  143. @objc func openMenuMore() {
  144. let imageIcon = UIImage(contentsOfFile: CCUtility.getDirectoryProviderStorageIconOcId(currentViewController.metadata.ocId, etag: currentViewController.metadata.etag))
  145. NCViewer.shared.toggleMenu(viewController: self, metadata: currentViewController.metadata, webView: false, imageIcon: imageIcon)
  146. }
  147. deinit {
  148. print("deinit NCViewerMedia")
  149. }
  150. func changeScreenMode(mode: ScreenMode, enableTimerAutoHide: Bool = false) {
  151. if mode == .normal {
  152. navigationController?.setNavigationBarHidden(false, animated: true)
  153. progressView.isHidden = false
  154. if !currentViewController.detailView.isShow() {
  155. currentViewController.playerToolBar.show(enableTimerAutoHide: enableTimerAutoHide)
  156. }
  157. NCUtility.shared.colorNavigationController(navigationController, backgroundColor: NCBrandColor.shared.systemBackground, titleColor: NCBrandColor.shared.label, tintColor: nil, withoutShadow: false)
  158. view.backgroundColor = NCBrandColor.shared.systemBackground
  159. textColor = NCBrandColor.shared.label
  160. } else {
  161. navigationController?.setNavigationBarHidden(true, animated: true)
  162. progressView.isHidden = true
  163. currentViewController.playerToolBar.hide()
  164. view.backgroundColor = .black
  165. textColor = .white
  166. }
  167. currentScreenMode = mode
  168. if currentViewController.metadata.classFile == NCCommunicationCommon.typeClassFile.image.rawValue {
  169. saveScreenModeImage = mode
  170. }
  171. setNeedsStatusBarAppearanceUpdate()
  172. currentViewController.reloadDetail()
  173. }
  174. //MARK: - NotificationCenter
  175. @objc func downloadedFile(_ notification: NSNotification) {
  176. if let userInfo = notification.userInfo as NSDictionary?, let ocId = userInfo["ocId"] as? String, let metadata = NCManageDatabase.shared.getMetadataFromOcId(ocId), let errorCode = userInfo["errorCode"] as? Int {
  177. if errorCode == 0 && currentViewController.metadata.ocId == ocId, let image = getImageMetadata(metadata) {
  178. currentViewController.reload(image: image, metadata: metadata)
  179. if self.metadatas.first(where: { $0.ocId == metadata.ocId }) != nil {
  180. progressView.progress = 0
  181. }
  182. }
  183. }
  184. }
  185. @objc func downloadedThumbnail(_ notification: NSNotification) {
  186. if let userInfo = notification.userInfo as NSDictionary?, let ocId = userInfo["ocId"] as? String, let metadata = NCManageDatabase.shared.getMetadataFromOcId(ocId) {
  187. if currentViewController.metadata.ocId == ocId, let image = getImageMetadata(metadata) {
  188. currentViewController.reload(image: image, metadata: metadata)
  189. }
  190. }
  191. }
  192. @objc func uploadedFile(_ notification: NSNotification) {
  193. if let userInfo = notification.userInfo as NSDictionary? {
  194. if let ocId = userInfo["ocId"] as? String, let metadata = NCManageDatabase.shared.getMetadataFromOcId(ocId), let errorCode = userInfo["errorCode"] as? Int {
  195. if errorCode == 0 && metadata.ocId == currentViewController.metadata.ocId {
  196. //self.reloadCurrentPage()
  197. }
  198. }
  199. }
  200. }
  201. @objc func triggerProgressTask(_ notification: NSNotification) {
  202. if let userInfo = notification.userInfo as NSDictionary? {
  203. if let serverUrl = userInfo["serverUrl"] as? String, let fileName = userInfo["fileName"] as? String, let progressNumber = userInfo["progress"] as? NSNumber {
  204. if self.metadatas.first(where: { $0.serverUrl == serverUrl && $0.fileName == fileName}) != nil {
  205. let progress = progressNumber.floatValue
  206. if progress == 1 {
  207. self.progressView.progress = 0
  208. } else {
  209. self.progressView.progress = progress
  210. }
  211. }
  212. }
  213. }
  214. }
  215. @objc func deleteFile(_ notification: NSNotification) {
  216. if let userInfo = notification.userInfo as NSDictionary? {
  217. if let ocId = userInfo["ocId"] as? String {
  218. let metadatas = self.metadatas.filter { $0.ocId != ocId }
  219. if self.metadatas.count == metadatas.count { return }
  220. self.metadatas = metadatas
  221. if ocId == currentViewController.metadata.ocId {
  222. if !shiftCurrentPage() {
  223. self.viewUnload()
  224. }
  225. }
  226. }
  227. }
  228. }
  229. @objc func renameFile(_ notification: NSNotification) {
  230. if let userInfo = notification.userInfo as NSDictionary? {
  231. if let ocId = userInfo["ocId"] as? String, let metadata = NCManageDatabase.shared.getMetadataFromOcId(ocId) {
  232. if let index = metadatas.firstIndex(where: {$0.ocId == metadata.ocId}) {
  233. metadatas[index] = metadata
  234. if index == currentIndex {
  235. navigationItem.title = metadata.fileNameView
  236. currentViewController.metadata = metadata
  237. self.currentViewController.metadata = metadata
  238. }
  239. }
  240. }
  241. }
  242. }
  243. @objc func moveFile(_ notification: NSNotification) {
  244. if let userInfo = notification.userInfo as NSDictionary? {
  245. if let ocId = userInfo["ocId"] as? String, let metadata = NCManageDatabase.shared.getMetadataFromOcId(ocId) {
  246. if metadatas.firstIndex(where: {$0.ocId == metadata.ocId}) != nil {
  247. deleteFile(notification)
  248. }
  249. }
  250. }
  251. }
  252. @objc func changeTheming() {
  253. }
  254. @objc func hidePlayerToolBar(_ notification: NSNotification) {
  255. if let userInfo = notification.userInfo as NSDictionary?, let ocId = userInfo["ocId"] as? String {
  256. if currentViewController.metadata.ocId == ocId {
  257. changeScreenMode(mode: .full)
  258. }
  259. }
  260. }
  261. @objc func showPlayerToolBar(_ notification: NSNotification) {
  262. if let userInfo = notification.userInfo as NSDictionary?, let ocId = userInfo["ocId"] as? String, let enableTimerAutoHide = userInfo["enableTimerAutoHide"] as? Bool{
  263. if currentViewController.metadata.ocId == ocId, let ncplayer = currentViewController.ncplayer, !ncplayer.isPictureInPictureActive() {
  264. changeScreenMode(mode: .normal, enableTimerAutoHide: enableTimerAutoHide)
  265. }
  266. }
  267. }
  268. //MARK: - Image
  269. func getImageMetadata(_ metadata: tableMetadata) -> UIImage? {
  270. if let image = getImage(metadata: metadata) {
  271. return image
  272. }
  273. if metadata.classFile == NCCommunicationCommon.typeClassFile.video.rawValue && !metadata.hasPreview {
  274. NCUtility.shared.createImageFrom(fileName: metadata.fileNameView, ocId: metadata.ocId, etag: metadata.etag, classFile: metadata.classFile)
  275. }
  276. if CCUtility.fileProviderStoragePreviewIconExists(metadata.ocId, etag: metadata.etag) {
  277. if let imagePreviewPath = CCUtility.getDirectoryProviderStoragePreviewOcId(metadata.ocId, etag: metadata.etag) {
  278. return UIImage.init(contentsOfFile: imagePreviewPath)
  279. }
  280. }
  281. return nil
  282. }
  283. private func getImage(metadata: tableMetadata) -> UIImage? {
  284. let ext = CCUtility.getExtension(metadata.fileNameView)
  285. var image: UIImage?
  286. if CCUtility.fileProviderStorageExists(metadata.ocId, fileNameView: metadata.fileNameView) && metadata.classFile == NCCommunicationCommon.typeClassFile.image.rawValue {
  287. let previewPath = CCUtility.getDirectoryProviderStoragePreviewOcId(metadata.ocId, etag: metadata.etag)!
  288. let imagePath = CCUtility.getDirectoryProviderStorageOcId(metadata.ocId, fileNameView: metadata.fileNameView)!
  289. if ext == "GIF" {
  290. if !FileManager().fileExists(atPath: previewPath) {
  291. NCUtility.shared.createImageFrom(fileName: metadata.fileNameView, ocId: metadata.ocId, etag: metadata.etag, classFile: metadata.classFile)
  292. }
  293. image = UIImage.animatedImage(withAnimatedGIFURL: URL(fileURLWithPath: imagePath))
  294. } else if ext == "SVG" {
  295. if let svgImage = SVGKImage(contentsOfFile: imagePath) {
  296. svgImage.size = CGSize(width: NCGlobal.shared.sizePreview, height: NCGlobal.shared.sizePreview)
  297. if let image = svgImage.uiImage {
  298. if !FileManager().fileExists(atPath: previewPath) {
  299. do {
  300. try image.pngData()?.write(to: URL(fileURLWithPath: previewPath), options: .atomic)
  301. } catch { }
  302. }
  303. return image
  304. } else {
  305. return nil
  306. }
  307. } else {
  308. return nil
  309. }
  310. } else {
  311. NCUtility.shared.createImageFrom(fileName: metadata.fileNameView, ocId: metadata.ocId, etag: metadata.etag, classFile: metadata.classFile)
  312. image = UIImage.init(contentsOfFile: imagePath)
  313. }
  314. }
  315. return image
  316. }
  317. }
  318. //MARK: - UIPageViewController Delegate Datasource
  319. extension NCViewerMedia: UIPageViewControllerDelegate, UIPageViewControllerDataSource {
  320. func shiftCurrentPage() -> Bool {
  321. if metadatas.count == 0 { return false }
  322. cache.removeAll()
  323. var direction: UIPageViewController.NavigationDirection = .forward
  324. if currentIndex == metadatas.count {
  325. currentIndex -= 1
  326. direction = .reverse
  327. }
  328. if let viewerMediaZoom = getCache(index: currentIndex) {
  329. pageViewController.setViewControllers([viewerMediaZoom], direction: direction, animated: true, completion: nil)
  330. } else {
  331. let viewerMediaZoom = setCache(index: currentIndex, image: getImageMetadata(metadatas[currentIndex]), metadata: metadatas[currentIndex], direction: direction)
  332. pageViewController.setViewControllers([viewerMediaZoom], direction: direction, animated: true, completion: nil)
  333. }
  334. return true
  335. }
  336. func goTo(index: Int, direction: UIPageViewController.NavigationDirection, autoPlay: Bool) {
  337. currentIndex = index
  338. if let viewerMediaZoom = getCache(index: currentIndex) {
  339. viewerMediaZoom.autoPlay = autoPlay
  340. pageViewController.setViewControllers([viewerMediaZoom], direction: direction, animated: true, completion: nil)
  341. } else {
  342. let viewerMediaZoom = setCache(index: currentIndex, image: getImageMetadata(metadatas[currentIndex]), metadata: metadatas[currentIndex], direction: direction)
  343. viewerMediaZoom.autoPlay = autoPlay
  344. pageViewController.setViewControllers([viewerMediaZoom], direction: direction, animated: true, completion: nil)
  345. }
  346. }
  347. func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
  348. if currentIndex == 0 { return nil }
  349. if let viewerMediaZoom = getCache(index: currentIndex - 1) {
  350. return viewerMediaZoom
  351. } else {
  352. let viewerMediaZoom = setCache(index: currentIndex-1, image: getImageMetadata(metadatas[currentIndex-1]), metadata: metadatas[currentIndex-1], direction: .reverse)
  353. return viewerMediaZoom
  354. }
  355. }
  356. func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
  357. if currentIndex == metadatas.count - 1 { return nil }
  358. if let viewerMediaZoom = getCache(index: currentIndex + 1) {
  359. return viewerMediaZoom
  360. } else {
  361. let viewerMediaZoom = setCache(index: currentIndex+1, image: getImageMetadata(metadatas[currentIndex+1]), metadata: metadatas[currentIndex+1], direction: .forward)
  362. return viewerMediaZoom
  363. }
  364. }
  365. // START TRANSITION
  366. func pageViewController(_ pageViewController: UIPageViewController, willTransitionTo pendingViewControllers: [UIViewController]) {
  367. // Save time video
  368. if let ncplayer = currentViewController.ncplayer, ncplayer.isPlay() {
  369. ncplayer.saveCurrentTime()
  370. }
  371. guard let nextViewController = pendingViewControllers.first as? NCViewerMediaZoom else { return }
  372. nextIndex = nextViewController.index
  373. }
  374. // END TRANSITION
  375. func pageViewController(_ pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) {
  376. if (completed && nextIndex != nil) {
  377. previousViewControllers.forEach { viewController in
  378. let viewerMediaZoom = viewController as! NCViewerMediaZoom
  379. viewerMediaZoom.ncplayer?.videoRemoved()
  380. viewerMediaZoom.playerToolBar?.disableCommandCenter()
  381. viewerMediaZoom.scrollView.zoomScale = viewerMediaZoom.scrollView.minimumZoomScale
  382. }
  383. currentIndex = nextIndex!
  384. }
  385. self.nextIndex = nil
  386. }
  387. }
  388. //MARK: - UIGestureRecognizerDelegate
  389. extension NCViewerMedia: UIGestureRecognizerDelegate {
  390. func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
  391. if let gestureRecognizer = gestureRecognizer as? UIPanGestureRecognizer {
  392. let velocity = gestureRecognizer.velocity(in: self.view)
  393. var velocityCheck : Bool = false
  394. if UIDevice.current.orientation.isLandscape {
  395. velocityCheck = velocity.x < 0
  396. }
  397. else {
  398. velocityCheck = velocity.y < 0
  399. }
  400. if velocityCheck {
  401. return false
  402. }
  403. }
  404. return true
  405. }
  406. func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
  407. if otherGestureRecognizer == currentViewController.scrollView.panGestureRecognizer {
  408. if self.currentViewController.scrollView.contentOffset.y == 0 {
  409. return true
  410. }
  411. }
  412. return false
  413. }
  414. @objc func didPanWith(gestureRecognizer: UIPanGestureRecognizer) {
  415. currentViewController.didPanWith(gestureRecognizer: gestureRecognizer)
  416. }
  417. @objc func didSingleTapWith(gestureRecognizer: UITapGestureRecognizer) {
  418. if let pictureInPictureController = currentViewController.ncplayer?.pictureInPictureController {
  419. if pictureInPictureController.isPictureInPictureActive {
  420. pictureInPictureController.stopPictureInPicture()
  421. }
  422. }
  423. if currentScreenMode == .full {
  424. changeScreenMode(mode: .normal, enableTimerAutoHide: true)
  425. } else {
  426. changeScreenMode(mode: .full)
  427. }
  428. }
  429. //
  430. // LIVE PHOTO
  431. //
  432. @objc func didLongpressGestureEvent(gestureRecognizer: UITapGestureRecognizer) {
  433. if !currentViewController.metadata.livePhoto { return }
  434. if gestureRecognizer.state == .began {
  435. currentViewController.updateViewConstraints()
  436. currentViewController.statusViewImage.isHidden = true
  437. currentViewController.statusLabel.isHidden = true
  438. let fileName = (currentViewController.metadata.fileNameView as NSString).deletingPathExtension + ".mov"
  439. if let metadata = NCManageDatabase.shared.getMetadata(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@ AND fileNameView LIKE[c] %@", currentViewController.metadata.account, currentViewController.metadata.serverUrl, fileName)) {
  440. if CCUtility.fileProviderStorageExists(metadata.ocId, fileNameView: metadata.fileNameView) {
  441. AudioServicesPlaySystemSound(1519) // peek feedback
  442. if let url = NCKTVHTTPCache.shared.getVideoURL(metadata: metadata) {
  443. self.ncplayerLivePhoto = NCPlayer.init(url: url, autoPlay: true, imageVideoContainer: self.currentViewController.imageVideoContainer, playerToolBar: nil, metadata: metadata, detailView: nil)
  444. }
  445. } else {
  446. let serverUrlFileName = metadata.serverUrl + "/" + metadata.fileNameView
  447. let fileNameLocalPath = CCUtility.getDirectoryProviderStorageOcId(metadata.ocId, fileNameView: metadata.fileNameView)!
  448. NCCommunication.shared.download(serverUrlFileName: serverUrlFileName, fileNameLocalPath: fileNameLocalPath, requestHandler: { (_) in
  449. }, taskHandler: { (_) in
  450. }, progressHandler: { (progress) in
  451. self.progressView.progress = Float(progress.fractionCompleted)
  452. }) { (account, etag, date, length, allHeaderFields, error, errorCode, errorDescription) in
  453. self.progressView.progress = 0
  454. if errorCode == 0 && account == metadata.account {
  455. NCManageDatabase.shared.addLocalFile(metadata: metadata)
  456. if gestureRecognizer.state == .changed || gestureRecognizer.state == .began {
  457. AudioServicesPlaySystemSound(1519) // peek feedback
  458. if let url = NCKTVHTTPCache.shared.getVideoURL(metadata: metadata) {
  459. self.ncplayerLivePhoto = NCPlayer.init(url: url, autoPlay: true, imageVideoContainer: self.currentViewController.imageVideoContainer, playerToolBar: nil, metadata: metadata, detailView: nil)
  460. }
  461. }
  462. }
  463. }
  464. }
  465. }
  466. } else if gestureRecognizer.state == .ended {
  467. currentViewController.statusViewImage.isHidden = false
  468. currentViewController.statusLabel.isHidden = false
  469. self.ncplayerLivePhoto?.videoRemoved()
  470. }
  471. }
  472. }