NCViewerMediaZoom.swift 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. //
  2. // NCViewerMediaZoom.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 NCCommunication
  25. protocol NCViewerMediaZoomDelegate {
  26. func didAppearImageZoom(viewerMediaZoom: NCViewerMediaZoom, metadata: tableMetadata)
  27. func dismissImageZoom()
  28. }
  29. class NCViewerMediaZoom: UIViewController {
  30. @IBOutlet weak var detailViewConstraint: 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. @IBOutlet weak var playerToolBar: NCPlayerToolBar!
  39. var delegate: NCViewerMediaZoomDelegate?
  40. var viewerMedia: NCViewerMedia?
  41. var ncplayer: NCPlayer?
  42. var image: UIImage?
  43. var metadata: tableMetadata = tableMetadata()
  44. var index: Int = 0
  45. var isShowDetail: Bool = false
  46. var doubleTapGestureRecognizer: UITapGestureRecognizer = UITapGestureRecognizer()
  47. var imageViewConstraint: CGFloat = 0
  48. // MARK: - View Life Cycle
  49. required init?(coder aDecoder: NSCoder) {
  50. super.init(coder: aDecoder)
  51. doubleTapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(didDoubleTapWith(gestureRecognizer:)))
  52. doubleTapGestureRecognizer.numberOfTapsRequired = 2
  53. }
  54. deinit {
  55. print("deinit NCViewerMediaZoom")
  56. }
  57. override func viewDidLoad() {
  58. super.viewDidLoad()
  59. scrollView.delegate = self
  60. scrollView.maximumZoomScale = 4
  61. scrollView.minimumZoomScale = 1
  62. view.addGestureRecognizer(doubleTapGestureRecognizer)
  63. if metadata.classFile == NCCommunicationCommon.typeClassFile.video.rawValue {
  64. if image == nil {
  65. image = UIImage.init(named: "noPreviewVideo")!.image(color: .gray, size: view.frame.width)
  66. }
  67. // Show Video Toolbar
  68. if !metadata.livePhoto {
  69. playerToolBar.isHidden = false
  70. }
  71. } else if metadata.classFile == NCCommunicationCommon.typeClassFile.audio.rawValue {
  72. if image == nil {
  73. image = UIImage.init(named: "noPreviewAudio")!.image(color: .gray, size: view.frame.width)
  74. }
  75. } else {
  76. if image == nil {
  77. image = UIImage.init(named: "noPreview")!.image(color: .gray, size: view.frame.width)
  78. }
  79. }
  80. imageVideoContainer.image = image
  81. if NCManageDatabase.shared.getMetadataLivePhoto(metadata: metadata) != nil {
  82. statusViewImage.image = NCUtility.shared.loadImage(named: "livephoto", color: .gray)
  83. statusLabel.text = "LIVE"
  84. } else {
  85. statusViewImage.image = nil
  86. statusLabel.text = ""
  87. }
  88. var heightMap = (view.bounds.height / 3)
  89. if view.bounds.width < view.bounds.height {
  90. heightMap = (view.bounds.width / 3)
  91. }
  92. detailViewConstraint.constant = 0
  93. detailView.update(metadata: metadata, image: image, heightMap: heightMap)
  94. detailView.hide()
  95. if metadata.classFile == NCCommunicationCommon.typeClassFile.video.rawValue || metadata.classFile == NCCommunicationCommon.typeClassFile.audio.rawValue, let url = NCKTVHTTPCache.shared.getVideoURL(metadata: metadata) {
  96. self.ncplayer = NCPlayer.init(url: url)
  97. self.viewerMedia?.ncplayer = self.ncplayer
  98. }
  99. }
  100. override func viewDidAppear(_ animated: Bool) {
  101. super.viewDidAppear(animated)
  102. delegate?.didAppearImageZoom(viewerMediaZoom: self, metadata: metadata)
  103. /*
  104. if (metadata.classFile == NCCommunicationCommon.typeClassFile.video.rawValue || metadata.classFile == NCCommunicationCommon.typeClassFile.audio.rawValue) && imageVideoContainer.playerLayer == nil {
  105. DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
  106. self.ncplayer?.setupVideoLayer(imageVideoContainer: self.imageVideoContainer, playerToolBar: self.playerToolBar, metadata: self.metadata)
  107. //self.player?.videoPlay()
  108. }
  109. }
  110. */
  111. }
  112. override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
  113. super.viewWillTransition(to: size, with: coordinator)
  114. coordinator.animate(alongsideTransition: { (context) in
  115. // back to the original size
  116. self.scrollView.zoom(to: CGRect(x: 0, y: 0, width: self.scrollView.bounds.width, height: self.scrollView.bounds.height), animated: false)
  117. self.view.layoutIfNeeded()
  118. UIView.animate(withDuration: context.transitionDuration) {
  119. // resize detail
  120. if self.detailView.isShow() {
  121. self.openDetail()
  122. }
  123. }
  124. }) { (_) in }
  125. }
  126. func reload(image: UIImage, metadata: tableMetadata) {
  127. imageVideoContainer.image = image
  128. self.metadata = metadata
  129. }
  130. //MARK: - Gesture
  131. @objc func didDoubleTapWith(gestureRecognizer: UITapGestureRecognizer) {
  132. if detailView.isShow() { return }
  133. // NO ZOOM for Audio / Video
  134. if (metadata.classFile == NCCommunicationCommon.typeClassFile.video.rawValue || metadata.classFile == NCCommunicationCommon.typeClassFile.audio.rawValue) && !playerToolBar.isHidden {
  135. return
  136. }
  137. let pointInView = gestureRecognizer.location(in: self.imageVideoContainer)
  138. var newZoomScale = self.scrollView.maximumZoomScale
  139. if self.scrollView.zoomScale >= newZoomScale || abs(self.scrollView.zoomScale - newZoomScale) <= 0.01 {
  140. newZoomScale = self.scrollView.minimumZoomScale
  141. }
  142. let width = self.scrollView.bounds.width / newZoomScale
  143. let height = self.scrollView.bounds.height / newZoomScale
  144. let originX = pointInView.x - (width / 2.0)
  145. let originY = pointInView.y - (height / 2.0)
  146. let rectToZoomTo = CGRect(x: originX, y: originY, width: width, height: height)
  147. self.scrollView.zoom(to: rectToZoomTo, animated: true)
  148. }
  149. @objc func didPanWith(gestureRecognizer: UIPanGestureRecognizer) {
  150. let currentLocation = gestureRecognizer.translation(in: self.view)
  151. switch gestureRecognizer.state {
  152. case .began:
  153. print("began")
  154. case .ended:
  155. if detailView.isShow() {
  156. self.imageViewTopConstraint.constant = -imageViewConstraint
  157. self.imageViewBottomConstraint.constant = imageViewConstraint
  158. } else {
  159. self.imageViewTopConstraint.constant = 0
  160. self.imageViewBottomConstraint.constant = 0
  161. }
  162. case .changed:
  163. imageViewTopConstraint.constant = (currentLocation.y - imageViewConstraint)
  164. imageViewBottomConstraint.constant = -(currentLocation.y - imageViewConstraint)
  165. // DISMISS VIEW
  166. if detailView.isHidden && (currentLocation.y > 20) {
  167. delegate?.dismissImageZoom()
  168. gestureRecognizer.state = .ended
  169. }
  170. // CLOSE DETAIL
  171. if !detailView.isHidden && (currentLocation.y > 20) {
  172. self.closeDetail()
  173. gestureRecognizer.state = .ended
  174. }
  175. // OPEN DETAIL
  176. if detailView.isHidden && (currentLocation.y < -20) {
  177. self.openDetail()
  178. gestureRecognizer.state = .ended
  179. }
  180. default:
  181. break
  182. }
  183. }
  184. }
  185. //MARK: -
  186. extension NCViewerMediaZoom {
  187. private func openDetail() {
  188. self.detailView.show(textColor: self.viewerMedia?.textColor)
  189. if let image = imageVideoContainer.image {
  190. let ratioW = imageVideoContainer.frame.width / image.size.width
  191. let ratioH = imageVideoContainer.frame.height / image.size.height
  192. let ratio = ratioW < ratioH ? ratioW : ratioH
  193. let imageHeight = image.size.height * ratio
  194. imageViewConstraint = self.detailView.frame.height - ((self.view.frame.height - imageHeight) / 2) + self.view.safeAreaInsets.bottom
  195. if imageViewConstraint < 0 { imageViewConstraint = 0 }
  196. }
  197. UIView.animate(withDuration: 0.3) {
  198. self.imageViewTopConstraint.constant = -self.imageViewConstraint
  199. self.imageViewBottomConstraint.constant = self.imageViewConstraint
  200. self.detailViewConstraint.constant = self.detailView.frame.height
  201. self.view.layoutIfNeeded()
  202. } completion: { (_) in
  203. }
  204. scrollView.pinchGestureRecognizer?.isEnabled = false
  205. playerToolBar.hideToolBar()
  206. }
  207. private func closeDetail() {
  208. self.detailView.hide()
  209. imageViewConstraint = 0
  210. UIView.animate(withDuration: 0.3) {
  211. self.imageViewTopConstraint.constant = 0
  212. self.imageViewBottomConstraint.constant = 0
  213. self.detailViewConstraint.constant = 0
  214. self.view.layoutIfNeeded()
  215. } completion: { (_) in
  216. }
  217. scrollView.pinchGestureRecognizer?.isEnabled = true
  218. playerToolBar.showToolBar(metadata: metadata, detailView: nil)
  219. }
  220. }
  221. //MARK: -
  222. extension NCViewerMediaZoom: UIScrollViewDelegate {
  223. func viewForZooming(in scrollView: UIScrollView) -> UIView? {
  224. return imageVideoContainer
  225. }
  226. func scrollViewDidZoom(_ scrollView: UIScrollView) {
  227. if scrollView.zoomScale > 1 {
  228. if let image = imageVideoContainer.image {
  229. let ratioW = imageVideoContainer.frame.width / image.size.width
  230. let ratioH = imageVideoContainer.frame.height / image.size.height
  231. let ratio = ratioW < ratioH ? ratioW : ratioH
  232. let newWidth = image.size.width * ratio
  233. let newHeight = image.size.height * ratio
  234. let conditionLeft = newWidth*scrollView.zoomScale > imageVideoContainer.frame.width
  235. let left = 0.5 * (conditionLeft ? newWidth - imageVideoContainer.frame.width : (scrollView.frame.width - scrollView.contentSize.width))
  236. let conditioTop = newHeight*scrollView.zoomScale > imageVideoContainer.frame.height
  237. let top = 0.5 * (conditioTop ? newHeight - imageVideoContainer.frame.height : (scrollView.frame.height - scrollView.contentSize.height))
  238. scrollView.contentInset = UIEdgeInsets(top: top, left: left, bottom: top, right: left)
  239. }
  240. } else {
  241. scrollView.contentInset = .zero
  242. }
  243. }
  244. func scrollViewDidScroll(_ scrollView: UIScrollView) {
  245. }
  246. }
  247. //MARK: -
  248. class imageVideoContainerView: UIImageView {
  249. var playerLayer: CALayer?
  250. override func layoutSublayers(of layer: CALayer) {
  251. super.layoutSublayers(of: layer)
  252. playerLayer?.frame = self.bounds
  253. }
  254. }