NCViewerMedia.swift 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  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. import EasyTipView
  27. import SwiftUI
  28. class NCViewerMedia: UIViewController {
  29. @IBOutlet weak var detailViewTopConstraint: NSLayoutConstraint!
  30. @IBOutlet weak var detailViewHeighConstraint: NSLayoutConstraint!
  31. @IBOutlet weak var imageViewTopConstraint: NSLayoutConstraint!
  32. @IBOutlet weak var imageViewBottomConstraint: NSLayoutConstraint!
  33. @IBOutlet weak var scrollView: UIScrollView!
  34. @IBOutlet weak var imageVideoContainer: imageVideoContainerView!
  35. @IBOutlet weak var statusViewImage: UIImageView!
  36. @IBOutlet weak var statusLabel: UILabel!
  37. @IBOutlet weak var detailView: NCViewerMediaDetailView!
  38. private var _autoPlay: Bool = false
  39. private var tipView: EasyTipView?
  40. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  41. weak var viewerMediaPage: NCViewerMediaPage?
  42. var playerToolBar: NCPlayerToolBar?
  43. var ncplayer: NCPlayer?
  44. var image: UIImage?
  45. var metadata: tableMetadata = tableMetadata()
  46. var index: Int = 0
  47. var doubleTapGestureRecognizer: UITapGestureRecognizer = UITapGestureRecognizer()
  48. var imageViewConstraint: CGFloat = 0
  49. var isDetailViewInitializze: Bool = false
  50. var autoPlay: Bool {
  51. get {
  52. let temp = _autoPlay
  53. _autoPlay = false
  54. return temp
  55. }
  56. set(newVal) {
  57. _autoPlay = newVal
  58. }
  59. }
  60. // MARK: - View Life Cycle
  61. required init?(coder aDecoder: NSCoder) {
  62. super.init(coder: aDecoder)
  63. doubleTapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(didDoubleTapWith(gestureRecognizer:)))
  64. doubleTapGestureRecognizer.numberOfTapsRequired = 2
  65. }
  66. deinit {
  67. print("deinit NCViewerMedia")
  68. NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterOpenMediaDetail), object: nil)
  69. }
  70. override func viewDidLoad() {
  71. super.viewDidLoad()
  72. scrollView.delegate = self
  73. scrollView.maximumZoomScale = 4
  74. scrollView.minimumZoomScale = 1
  75. view.addGestureRecognizer(doubleTapGestureRecognizer)
  76. if NCManageDatabase.shared.getMetadataLivePhoto(metadata: metadata) != nil {
  77. statusViewImage.image = NCUtility.shared.loadImage(named: "livephoto", color: .gray)
  78. statusLabel.text = "LIVE"
  79. } else {
  80. statusViewImage.image = nil
  81. statusLabel.text = ""
  82. }
  83. if metadata.classFile == NCCommunicationCommon.typeClassFile.video.rawValue || metadata.classFile == NCCommunicationCommon.typeClassFile.audio.rawValue {
  84. playerToolBar = Bundle.main.loadNibNamed("NCPlayerToolBar", owner: self, options: nil)?.first as? NCPlayerToolBar
  85. if let playerToolBar = playerToolBar {
  86. view.addSubview(playerToolBar)
  87. playerToolBar.translatesAutoresizingMaskIntoConstraints = false
  88. playerToolBar.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 0).isActive = true
  89. playerToolBar.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor, constant: 0).isActive = true
  90. playerToolBar.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor, constant: 0).isActive = true
  91. playerToolBar.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor, constant: 0).isActive = true
  92. playerToolBar.viewerMediaPage = viewerMediaPage
  93. }
  94. let urlVideo = NCKTVHTTPCache.shared.getVideoURL(metadata: metadata)
  95. if let url = urlVideo.url {
  96. self.ncplayer = NCPlayer.init(url: url, autoPlay: self.autoPlay, isProxy: urlVideo.isProxy, imageVideoContainer: self.imageVideoContainer, playerToolBar: self.playerToolBar, metadata: self.metadata, detailView: self.detailView, viewController: self)
  97. }
  98. }
  99. // TIP
  100. var preferences = EasyTipView.Preferences()
  101. preferences.drawing.foregroundColor = .white
  102. preferences.drawing.backgroundColor = NCBrandColor.shared.nextcloud
  103. preferences.drawing.textAlignment = .left
  104. preferences.drawing.arrowPosition = .top
  105. preferences.drawing.cornerRadius = 10
  106. preferences.animating.dismissTransform = CGAffineTransform(translationX: 0, y: 100)
  107. preferences.animating.showInitialTransform = CGAffineTransform(translationX: 0, y: -100)
  108. preferences.animating.showInitialAlpha = 0
  109. preferences.animating.showDuration = 0.5
  110. preferences.animating.dismissDuration = 0
  111. tipView = EasyTipView(text: NSLocalizedString("_tip_open_mediadetail_", comment: ""), preferences: preferences, delegate: self)
  112. detailViewTopConstraint.constant = 0
  113. detailView.hide()
  114. self.image = nil
  115. self.imageVideoContainer.image = nil
  116. reloadImage()
  117. }
  118. override func viewWillAppear(_ animated: Bool) {
  119. super.viewWillAppear(animated)
  120. viewerMediaPage?.navigationController?.navigationBar.prefersLargeTitles = false
  121. viewerMediaPage?.navigationItem.title = metadata.fileNameView
  122. if metadata.classFile == NCCommunicationCommon.typeClassFile.image.rawValue, let viewerMediaPage = self.viewerMediaPage {
  123. viewerMediaPage.currentScreenMode = viewerMediaPage.saveScreenModeImage
  124. if viewerMediaPage.modifiedOcId.contains(metadata.ocId) {
  125. viewerMediaPage.modifiedOcId.removeAll(where: { $0 == metadata.ocId })
  126. reloadImage()
  127. }
  128. }
  129. if viewerMediaPage?.currentScreenMode == .full {
  130. viewerMediaPage?.navigationController?.setNavigationBarHidden(true, animated: true)
  131. NCUtility.shared.colorNavigationController(viewerMediaPage?.navigationController, backgroundColor: .black, titleColor: .white, tintColor: nil, withoutShadow: false)
  132. viewerMediaPage?.view.backgroundColor = .black
  133. viewerMediaPage?.textColor = .white
  134. viewerMediaPage?.progressView.isHidden = true
  135. } else {
  136. viewerMediaPage?.navigationController?.setNavigationBarHidden(false, animated: true)
  137. NCUtility.shared.colorNavigationController(viewerMediaPage?.navigationController, backgroundColor: NCBrandColor.shared.systemBackground, titleColor: NCBrandColor.shared.label, tintColor: nil, withoutShadow: false)
  138. viewerMediaPage?.view.backgroundColor = NCBrandColor.shared.systemBackground
  139. viewerMediaPage?.textColor = NCBrandColor.shared.label
  140. viewerMediaPage?.progressView.isHidden = false
  141. }
  142. }
  143. override func viewDidAppear(_ animated: Bool) {
  144. super.viewDidAppear(animated)
  145. if metadata.classFile == NCCommunicationCommon.typeClassFile.video.rawValue || metadata.classFile == NCCommunicationCommon.typeClassFile.audio.rawValue {
  146. if let ncplayer = self.ncplayer {
  147. ncplayer.openAVPlayer()
  148. self.viewerMediaPage?.updateCommandCenter(ncplayer: ncplayer, metadata: self.metadata)
  149. }
  150. } else if metadata.classFile == NCCommunicationCommon.typeClassFile.image.rawValue {
  151. viewerMediaPage?.clearCommandCenter()
  152. }
  153. // TIP
  154. if !NCManageDatabase.shared.tipExists(NCGlobal.shared.tipNCViewerMediaDetailView), let view = self.navigationController?.navigationBar {
  155. self.tipView?.show(forView: view)
  156. }
  157. NotificationCenter.default.addObserver(self, selector: #selector(openDetail(_:)), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterOpenMediaDetail), object: nil)
  158. }
  159. override func viewWillDisappear(_ animated: Bool) {
  160. super.viewWillDisappear(animated)
  161. self.tipView?.dismiss()
  162. }
  163. override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
  164. super.viewWillTransition(to: size, with: coordinator)
  165. self.tipView?.dismiss()
  166. coordinator.animate(alongsideTransition: { context in
  167. // back to the original size
  168. self.scrollView.zoom(to: CGRect(x: 0, y: 0, width: self.scrollView.bounds.width, height: self.scrollView.bounds.height), animated: false)
  169. self.view.layoutIfNeeded()
  170. UIView.animate(withDuration: context.transitionDuration) {
  171. if self.detailView.isShow() {
  172. self.openDetail()
  173. }
  174. }
  175. }) { _ in }
  176. }
  177. // MARK: - Image
  178. func reloadImage() {
  179. if let metadata = NCManageDatabase.shared.getMetadataFromOcId(metadata.ocId) {
  180. self.metadata = metadata
  181. loadImage(metadata: metadata)
  182. }
  183. }
  184. func loadImage(metadata: tableMetadata) {
  185. // Download preview
  186. if metadata.hasPreview && !CCUtility.fileProviderStoragePreviewIconExists(metadata.ocId, etag: metadata.etag) {
  187. var etagResource: String?
  188. let fileNamePath = CCUtility.returnFileNamePath(fromFileName: metadata.fileName, serverUrl: metadata.serverUrl, urlBase: metadata.urlBase, account: metadata.account)!
  189. let fileNamePreviewLocalPath = CCUtility.getDirectoryProviderStoragePreviewOcId(metadata.ocId, etag: metadata.etag)!
  190. let fileNameIconLocalPath = CCUtility.getDirectoryProviderStorageIconOcId(metadata.ocId, etag: metadata.etag)!
  191. if FileManager.default.fileExists(atPath: fileNameIconLocalPath) && FileManager.default.fileExists(atPath: fileNamePreviewLocalPath) {
  192. etagResource = metadata.etagResource
  193. }
  194. NCCommunication.shared.downloadPreview(
  195. fileNamePathOrFileId: fileNamePath,
  196. fileNamePreviewLocalPath: fileNamePreviewLocalPath,
  197. widthPreview: NCGlobal.shared.sizePreview,
  198. heightPreview: NCGlobal.shared.sizePreview,
  199. fileNameIconLocalPath: fileNameIconLocalPath,
  200. sizeIcon: NCGlobal.shared.sizeIcon, etag: etagResource,
  201. queue: .main) { _, _, imageIcon, _, etag, errorCode, _ in
  202. if let image = imageIcon, errorCode == 0 {
  203. if self.imageVideoContainer.layer.sublayers?.count == nil {
  204. self.image = image
  205. self.imageVideoContainer.image = image
  206. }
  207. NCManageDatabase.shared.setMetadataEtagResource(ocId: metadata.ocId, etagResource: etag)
  208. }
  209. downloadFile(metadata: metadata)
  210. }
  211. } else {
  212. let image = getImageMetadata(metadata)
  213. if self.metadata.ocId == metadata.ocId && self.imageVideoContainer.layer.sublayers?.count == nil {
  214. self.image = image
  215. self.imageVideoContainer.image = image
  216. }
  217. downloadFile(metadata: metadata)
  218. }
  219. func downloadFile(metadata: tableMetadata) {
  220. if metadata.livePhoto {
  221. let fileName = (metadata.fileNameView as NSString).deletingPathExtension + ".mov"
  222. if let metadata = NCManageDatabase.shared.getMetadata(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@ AND fileNameView LIKE[c] %@", metadata.account, metadata.serverUrl, fileName)), !CCUtility.fileProviderStorageExists(metadata) {
  223. NCNetworking.shared.download(metadata: metadata, selector: "") { _ in }
  224. }
  225. }
  226. guard metadata.classFile == NCCommunicationCommon.typeClassFile.image.rawValue, !CCUtility.fileProviderStorageExists(metadata), metadata.session == "" else { return }
  227. NCNetworking.shared.download(metadata: metadata, selector: "") { _ in
  228. let image = getImageMetadata(metadata)
  229. if self.metadata.ocId == metadata.ocId && self.imageVideoContainer.layer.sublayers?.count == nil {
  230. self.image = image
  231. self.imageVideoContainer.image = image
  232. }
  233. }
  234. }
  235. func getImageMetadata(_ metadata: tableMetadata) -> UIImage? {
  236. if let image = getImage(metadata: metadata) {
  237. return image
  238. }
  239. if metadata.classFile == NCCommunicationCommon.typeClassFile.video.rawValue && !metadata.hasPreview {
  240. NCUtility.shared.createImageFrom(fileNameView: metadata.fileNameView, ocId: metadata.ocId, etag: metadata.etag, classFile: metadata.classFile)
  241. }
  242. if CCUtility.fileProviderStoragePreviewIconExists(metadata.ocId, etag: metadata.etag) {
  243. if let imagePreviewPath = CCUtility.getDirectoryProviderStoragePreviewOcId(metadata.ocId, etag: metadata.etag) {
  244. return UIImage(contentsOfFile: imagePreviewPath)
  245. }
  246. }
  247. if metadata.classFile == NCCommunicationCommon.typeClassFile.video.rawValue {
  248. return UIImage(named: "noPreviewVideo")!.image(color: .gray, size: view.frame.width)
  249. } else if metadata.classFile == NCCommunicationCommon.typeClassFile.audio.rawValue {
  250. return UIImage(named: "noPreviewAudio")!.image(color: .gray, size: view.frame.width)
  251. } else {
  252. return UIImage(named: "noPreview")!.image(color: .gray, size: view.frame.width)
  253. }
  254. }
  255. func getImage(metadata: tableMetadata) -> UIImage? {
  256. let ext = CCUtility.getExtension(metadata.fileNameView)
  257. var image: UIImage?
  258. if CCUtility.fileProviderStorageExists(metadata) && metadata.classFile == NCCommunicationCommon.typeClassFile.image.rawValue {
  259. let previewPath = CCUtility.getDirectoryProviderStoragePreviewOcId(metadata.ocId, etag: metadata.etag)!
  260. let imagePath = CCUtility.getDirectoryProviderStorageOcId(metadata.ocId, fileNameView: metadata.fileNameView)!
  261. if ext == "GIF" {
  262. if !FileManager().fileExists(atPath: previewPath) {
  263. NCUtility.shared.createImageFrom(fileNameView: metadata.fileNameView, ocId: metadata.ocId, etag: metadata.etag, classFile: metadata.classFile)
  264. }
  265. image = UIImage.animatedImage(withAnimatedGIFURL: URL(fileURLWithPath: imagePath))
  266. } else if ext == "SVG" {
  267. if let svgImage = SVGKImage(contentsOfFile: imagePath) {
  268. svgImage.size = CGSize(width: NCGlobal.shared.sizePreview, height: NCGlobal.shared.sizePreview)
  269. if let image = svgImage.uiImage {
  270. if !FileManager().fileExists(atPath: previewPath) {
  271. do {
  272. try image.pngData()?.write(to: URL(fileURLWithPath: previewPath), options: .atomic)
  273. } catch { }
  274. }
  275. return image
  276. } else {
  277. return nil
  278. }
  279. } else {
  280. return nil
  281. }
  282. } else {
  283. NCUtility.shared.createImageFrom(fileNameView: metadata.fileNameView, ocId: metadata.ocId, etag: metadata.etag, classFile: metadata.classFile)
  284. image = UIImage(contentsOfFile: imagePath)
  285. }
  286. }
  287. return image
  288. }
  289. }
  290. // MARK: - Gesture
  291. @objc func didDoubleTapWith(gestureRecognizer: UITapGestureRecognizer) {
  292. if detailView.isShow() { return }
  293. // NO ZOOM for Audio
  294. if metadata.classFile == NCCommunicationCommon.typeClassFile.audio.rawValue { return }
  295. let pointInView = gestureRecognizer.location(in: self.imageVideoContainer)
  296. var newZoomScale = self.scrollView.maximumZoomScale
  297. if self.scrollView.zoomScale >= newZoomScale || abs(self.scrollView.zoomScale - newZoomScale) <= 0.01 {
  298. newZoomScale = self.scrollView.minimumZoomScale
  299. }
  300. let width = self.scrollView.bounds.width / newZoomScale
  301. let height = self.scrollView.bounds.height / newZoomScale
  302. let originX = pointInView.x - (width / 2.0)
  303. let originY = pointInView.y - (height / 2.0)
  304. let rectToZoomTo = CGRect(x: originX, y: originY, width: width, height: height)
  305. self.scrollView.zoom(to: rectToZoomTo, animated: true)
  306. }
  307. @objc func didPanWith(gestureRecognizer: UIPanGestureRecognizer) {
  308. let currentLocation = gestureRecognizer.translation(in: self.view)
  309. switch gestureRecognizer.state {
  310. case .began:
  311. // let velocity = gestureRecognizer.velocity(in: self.view)
  312. // gesture moving Up
  313. // if velocity.y < 0 {
  314. // }
  315. break
  316. case .ended:
  317. if detailView.isShow() {
  318. self.imageViewTopConstraint.constant = -imageViewConstraint
  319. self.imageViewBottomConstraint.constant = imageViewConstraint
  320. } else {
  321. self.imageViewTopConstraint.constant = 0
  322. self.imageViewBottomConstraint.constant = 0
  323. }
  324. case .changed:
  325. imageViewTopConstraint.constant = (currentLocation.y - imageViewConstraint)
  326. imageViewBottomConstraint.constant = -(currentLocation.y - imageViewConstraint)
  327. // DISMISS VIEW
  328. if detailView.isHidden && (currentLocation.y > 20) {
  329. viewerMediaPage?.navigationController?.popViewController(animated: true)
  330. gestureRecognizer.state = .ended
  331. }
  332. // CLOSE DETAIL
  333. if !detailView.isHidden && (currentLocation.y > 20) {
  334. self.closeDetail()
  335. gestureRecognizer.state = .ended
  336. }
  337. // OPEN DETAIL
  338. if detailView.isHidden && (currentLocation.y < -20) {
  339. self.openDetail()
  340. gestureRecognizer.state = .ended
  341. }
  342. default:
  343. break
  344. }
  345. }
  346. }
  347. // MARK: -
  348. extension NCViewerMedia {
  349. @objc func openDetail(_ notification: NSNotification) {
  350. if let userInfo = notification.userInfo as NSDictionary?, let ocId = userInfo["ocId"] as? String, ocId == metadata.ocId {
  351. openDetail()
  352. }
  353. }
  354. private func openDetail() {
  355. self.dismissTip()
  356. CCUtility.setExif(metadata) { latitude, longitude, location, date, lensModel in
  357. if latitude != -1 && latitude != 0 && longitude != -1 && longitude != 0 {
  358. self.detailViewHeighConstraint.constant = self.view.bounds.height / 2
  359. } else {
  360. self.detailViewHeighConstraint.constant = 170
  361. }
  362. self.view.layoutIfNeeded()
  363. self.detailView.show(
  364. metadata: self.metadata,
  365. image: self.image,
  366. textColor: self.viewerMediaPage?.textColor,
  367. mediaMetadata: (latitude: latitude, longitude: longitude, location: location, date: date, lensModel: lensModel),
  368. ncplayer: self.ncplayer,
  369. delegate: self)
  370. if let image = self.imageVideoContainer.image {
  371. let ratioW = self.imageVideoContainer.frame.width / image.size.width
  372. let ratioH = self.imageVideoContainer.frame.height / image.size.height
  373. let ratio = ratioW < ratioH ? ratioW : ratioH
  374. let imageHeight = image.size.height * ratio
  375. self.imageViewConstraint = self.detailView.frame.height - ((self.view.frame.height - imageHeight) / 2) + self.view.safeAreaInsets.bottom
  376. if self.imageViewConstraint < 0 { self.imageViewConstraint = 0 }
  377. }
  378. UIView.animate(withDuration: 0.3) {
  379. self.imageViewTopConstraint.constant = -self.imageViewConstraint
  380. self.imageViewBottomConstraint.constant = self.imageViewConstraint
  381. self.detailViewTopConstraint.constant = self.detailViewHeighConstraint.constant
  382. self.view.layoutIfNeeded()
  383. } completion: { _ in
  384. }
  385. self.scrollView.pinchGestureRecognizer?.isEnabled = false
  386. self.playerToolBar?.hide()
  387. }
  388. }
  389. private func closeDetail() {
  390. self.detailView.hide()
  391. imageViewConstraint = 0
  392. UIView.animate(withDuration: 0.3) {
  393. self.imageViewTopConstraint.constant = 0
  394. self.imageViewBottomConstraint.constant = 0
  395. self.detailViewTopConstraint.constant = 0
  396. self.view.layoutIfNeeded()
  397. } completion: { _ in
  398. }
  399. scrollView.pinchGestureRecognizer?.isEnabled = true
  400. if metadata.classFile == NCCommunicationCommon.typeClassFile.video.rawValue && !metadata.livePhoto && ncplayer?.player?.timeControlStatus == .paused {
  401. NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterShowPlayerToolBar, userInfo: ["ocId": metadata.ocId, "enableTimerAutoHide": false])
  402. }
  403. }
  404. func reloadDetail() {
  405. if self.detailView.isShow() {
  406. CCUtility.setExif(metadata) { (latitude, longitude, location, date, lensModel) in
  407. self.detailView.show(
  408. metadata: self.metadata,
  409. image: self.image,
  410. textColor: self.viewerMediaPage?.textColor,
  411. mediaMetadata: (latitude: latitude, longitude: longitude, location: location, date: date, lensModel: lensModel),
  412. ncplayer: self.ncplayer,
  413. delegate: self)
  414. }
  415. }
  416. }
  417. }
  418. // MARK: -
  419. extension NCViewerMedia: UIScrollViewDelegate {
  420. func viewForZooming(in scrollView: UIScrollView) -> UIView? {
  421. return imageVideoContainer
  422. }
  423. func scrollViewDidZoom(_ scrollView: UIScrollView) {
  424. if scrollView.zoomScale > 1 {
  425. if let image = imageVideoContainer.image {
  426. let ratioW = imageVideoContainer.frame.width / image.size.width
  427. let ratioH = imageVideoContainer.frame.height / image.size.height
  428. let ratio = ratioW < ratioH ? ratioW : ratioH
  429. let newWidth = image.size.width * ratio
  430. let newHeight = image.size.height * ratio
  431. let conditionLeft = newWidth*scrollView.zoomScale > imageVideoContainer.frame.width
  432. let left = 0.5 * (conditionLeft ? newWidth - imageVideoContainer.frame.width : (scrollView.frame.width - scrollView.contentSize.width))
  433. let conditioTop = newHeight*scrollView.zoomScale > imageVideoContainer.frame.height
  434. let top = 0.5 * (conditioTop ? newHeight - imageVideoContainer.frame.height : (scrollView.frame.height - scrollView.contentSize.height))
  435. scrollView.contentInset = UIEdgeInsets(top: top, left: left, bottom: top, right: left)
  436. }
  437. } else {
  438. scrollView.contentInset = .zero
  439. }
  440. }
  441. func scrollViewDidScroll(_ scrollView: UIScrollView) {
  442. }
  443. }
  444. extension NCViewerMedia: NCViewerMediaDetailViewDelegate {
  445. func downloadFullResolution() {
  446. closeDetail()
  447. NCNetworking.shared.download(metadata: metadata, selector: NCGlobal.shared.selectorOpenDetail) { _ in }
  448. }
  449. }
  450. extension NCViewerMedia: EasyTipViewDelegate {
  451. // TIP
  452. func easyTipViewDidTap(_ tipView: EasyTipView) {
  453. NCManageDatabase.shared.addTip(NCGlobal.shared.tipNCViewerMediaDetailView)
  454. }
  455. func easyTipViewDidDismiss(_ tipView: EasyTipView) { }
  456. func dismissTip() {
  457. NCManageDatabase.shared.addTip(NCGlobal.shared.tipNCViewerMediaDetailView)
  458. self.tipView?.dismiss()
  459. }
  460. }
  461. // MARK: -
  462. class imageVideoContainerView: UIImageView {
  463. var playerLayer: CALayer?
  464. var metadata: tableMetadata?
  465. override func layoutSublayers(of layer: CALayer) {
  466. super.layoutSublayers(of: layer)
  467. playerLayer?.frame = self.bounds
  468. }
  469. }