NCViewerMediaZoom.swift 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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 dismissImageZoom()
  27. }
  28. class NCViewerMediaZoom: UIViewController {
  29. @IBOutlet weak var detailViewConstraint: NSLayoutConstraint!
  30. @IBOutlet weak var imageViewTopConstraint: NSLayoutConstraint!
  31. @IBOutlet weak var imageViewBottomConstraint: NSLayoutConstraint!
  32. @IBOutlet weak var scrollView: UIScrollView!
  33. @IBOutlet weak var imageVideoContainer: imageVideoContainerView!
  34. @IBOutlet weak var statusViewImage: UIImageView!
  35. @IBOutlet weak var statusLabel: UILabel!
  36. @IBOutlet weak var detailView: NCViewerMediaDetailView!
  37. @IBOutlet weak var playerToolBar: NCPlayerToolBar!
  38. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  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. imageVideoContainer.image = image
  72. imageVideoContainer.imageOriginal = image
  73. } else if metadata.classFile == NCCommunicationCommon.typeClassFile.audio.rawValue {
  74. if image == nil {
  75. image = UIImage.init(named: "noPreviewAudio")!.image(color: .gray, size: view.frame.width)
  76. }
  77. imageVideoContainer.image = image
  78. imageVideoContainer.imageOriginal = image
  79. } else {
  80. if image == nil {
  81. image = UIImage.init(named: "noPreview")!.image(color: .gray, size: view.frame.width)
  82. }
  83. imageVideoContainer.image = image
  84. imageVideoContainer.imageOriginal = image
  85. }
  86. if NCManageDatabase.shared.getMetadataLivePhoto(metadata: metadata) != nil {
  87. statusViewImage.image = NCUtility.shared.loadImage(named: "livephoto", color: .gray)
  88. statusLabel.text = "LIVE"
  89. } else {
  90. statusViewImage.image = nil
  91. statusLabel.text = ""
  92. }
  93. detailViewConstraint.constant = 0
  94. detailView.hide()
  95. }
  96. override func viewWillAppear(_ animated: Bool) {
  97. super.viewWillAppear(animated)
  98. viewerMedia?.navigationItem.title = metadata.fileNameView
  99. viewerMedia?.currentViewerMediaZoom = self
  100. viewerMedia?.currentMetadata = metadata
  101. }
  102. override func viewDidAppear(_ animated: Bool) {
  103. super.viewDidAppear(animated)
  104. if (metadata.classFile == NCCommunicationCommon.typeClassFile.video.rawValue || metadata.classFile == NCCommunicationCommon.typeClassFile.audio.rawValue) {
  105. if let url = NCKTVHTTPCache.shared.getVideoURL(metadata: metadata) {
  106. self.ncplayer = NCPlayer.init(url: url, imageVideoContainer: self.imageVideoContainer, playerToolBar: self.playerToolBar, metadata: self.metadata)
  107. self.viewerMedia?.ncplayer = self.ncplayer
  108. }
  109. }
  110. }
  111. override func viewDidDisappear(_ animated: Bool) {
  112. super.viewDidDisappear(animated)
  113. self.ncplayer?.videoRemoved()
  114. }
  115. override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
  116. super.viewWillTransition(to: size, with: coordinator)
  117. coordinator.animate(alongsideTransition: { (context) in
  118. // back to the original size
  119. self.scrollView.zoom(to: CGRect(x: 0, y: 0, width: self.scrollView.bounds.width, height: self.scrollView.bounds.height), animated: false)
  120. self.view.layoutIfNeeded()
  121. UIView.animate(withDuration: context.transitionDuration) {
  122. // resize detail
  123. if self.detailView.isShow() {
  124. self.openDetail()
  125. }
  126. }
  127. }) { (_) in }
  128. }
  129. func reload(image: UIImage, metadata: tableMetadata) {
  130. imageVideoContainer.image = image
  131. self.metadata = metadata
  132. }
  133. //MARK: - Gesture
  134. @objc func didDoubleTapWith(gestureRecognizer: UITapGestureRecognizer) {
  135. if detailView.isShow() { return }
  136. // NO ZOOM for Audio / Video
  137. if (metadata.classFile == NCCommunicationCommon.typeClassFile.video.rawValue || metadata.classFile == NCCommunicationCommon.typeClassFile.audio.rawValue) && !playerToolBar.isHidden {
  138. return
  139. }
  140. let pointInView = gestureRecognizer.location(in: self.imageVideoContainer)
  141. var newZoomScale = self.scrollView.maximumZoomScale
  142. if self.scrollView.zoomScale >= newZoomScale || abs(self.scrollView.zoomScale - newZoomScale) <= 0.01 {
  143. newZoomScale = self.scrollView.minimumZoomScale
  144. }
  145. let width = self.scrollView.bounds.width / newZoomScale
  146. let height = self.scrollView.bounds.height / newZoomScale
  147. let originX = pointInView.x - (width / 2.0)
  148. let originY = pointInView.y - (height / 2.0)
  149. let rectToZoomTo = CGRect(x: originX, y: originY, width: width, height: height)
  150. self.scrollView.zoom(to: rectToZoomTo, animated: true)
  151. }
  152. @objc func didPanWith(gestureRecognizer: UIPanGestureRecognizer) {
  153. let currentLocation = gestureRecognizer.translation(in: self.view)
  154. switch gestureRecognizer.state {
  155. case .began:
  156. var heightMap = (view.bounds.height / 3)
  157. if view.bounds.width < view.bounds.height {
  158. heightMap = (view.bounds.width / 3)
  159. }
  160. detailView.update(metadata: metadata, image: image, heightMap: heightMap)
  161. case .ended:
  162. if detailView.isShow() {
  163. self.imageViewTopConstraint.constant = -imageViewConstraint
  164. self.imageViewBottomConstraint.constant = imageViewConstraint
  165. } else {
  166. self.imageViewTopConstraint.constant = 0
  167. self.imageViewBottomConstraint.constant = 0
  168. }
  169. case .changed:
  170. imageViewTopConstraint.constant = (currentLocation.y - imageViewConstraint)
  171. imageViewBottomConstraint.constant = -(currentLocation.y - imageViewConstraint)
  172. // DISMISS VIEW
  173. if detailView.isHidden && (currentLocation.y > 20) {
  174. delegate?.dismissImageZoom()
  175. gestureRecognizer.state = .ended
  176. }
  177. // CLOSE DETAIL
  178. if !detailView.isHidden && (currentLocation.y > 20) {
  179. self.closeDetail()
  180. gestureRecognizer.state = .ended
  181. }
  182. // OPEN DETAIL
  183. if detailView.isHidden && (currentLocation.y < -20) {
  184. self.openDetail()
  185. gestureRecognizer.state = .ended
  186. }
  187. default:
  188. break
  189. }
  190. }
  191. }
  192. //MARK: -
  193. extension NCViewerMediaZoom {
  194. private func openDetail() {
  195. self.detailView.show(textColor: self.viewerMedia?.textColor)
  196. if let image = imageVideoContainer.image {
  197. let ratioW = imageVideoContainer.frame.width / image.size.width
  198. let ratioH = imageVideoContainer.frame.height / image.size.height
  199. let ratio = ratioW < ratioH ? ratioW : ratioH
  200. let imageHeight = image.size.height * ratio
  201. imageViewConstraint = self.detailView.frame.height - ((self.view.frame.height - imageHeight) / 2) + self.view.safeAreaInsets.bottom
  202. if imageViewConstraint < 0 { imageViewConstraint = 0 }
  203. }
  204. UIView.animate(withDuration: 0.3) {
  205. self.imageViewTopConstraint.constant = -self.imageViewConstraint
  206. self.imageViewBottomConstraint.constant = self.imageViewConstraint
  207. self.detailViewConstraint.constant = self.detailView.frame.height
  208. self.view.layoutIfNeeded()
  209. } completion: { (_) in
  210. }
  211. scrollView.pinchGestureRecognizer?.isEnabled = false
  212. playerToolBar.hideToolBar()
  213. }
  214. private func closeDetail() {
  215. self.detailView.hide()
  216. imageViewConstraint = 0
  217. UIView.animate(withDuration: 0.3) {
  218. self.imageViewTopConstraint.constant = 0
  219. self.imageViewBottomConstraint.constant = 0
  220. self.detailViewConstraint.constant = 0
  221. self.view.layoutIfNeeded()
  222. } completion: { (_) in
  223. }
  224. scrollView.pinchGestureRecognizer?.isEnabled = true
  225. playerToolBar.showToolBar(metadata: metadata, detailView: nil)
  226. }
  227. }
  228. //MARK: -
  229. extension NCViewerMediaZoom: UIScrollViewDelegate {
  230. func viewForZooming(in scrollView: UIScrollView) -> UIView? {
  231. return imageVideoContainer
  232. }
  233. func scrollViewDidZoom(_ scrollView: UIScrollView) {
  234. if scrollView.zoomScale > 1 {
  235. if let image = imageVideoContainer.image {
  236. let ratioW = imageVideoContainer.frame.width / image.size.width
  237. let ratioH = imageVideoContainer.frame.height / image.size.height
  238. let ratio = ratioW < ratioH ? ratioW : ratioH
  239. let newWidth = image.size.width * ratio
  240. let newHeight = image.size.height * ratio
  241. let conditionLeft = newWidth*scrollView.zoomScale > imageVideoContainer.frame.width
  242. let left = 0.5 * (conditionLeft ? newWidth - imageVideoContainer.frame.width : (scrollView.frame.width - scrollView.contentSize.width))
  243. let conditioTop = newHeight*scrollView.zoomScale > imageVideoContainer.frame.height
  244. let top = 0.5 * (conditioTop ? newHeight - imageVideoContainer.frame.height : (scrollView.frame.height - scrollView.contentSize.height))
  245. scrollView.contentInset = UIEdgeInsets(top: top, left: left, bottom: top, right: left)
  246. }
  247. } else {
  248. scrollView.contentInset = .zero
  249. }
  250. }
  251. func scrollViewDidScroll(_ scrollView: UIScrollView) {
  252. }
  253. }
  254. //MARK: -
  255. class imageVideoContainerView: UIImageView {
  256. var playerLayer: CALayer?
  257. var metadata: tableMetadata?
  258. var imageOriginal: UIImage?
  259. override func layoutSublayers(of layer: CALayer) {
  260. super.layoutSublayers(of: layer)
  261. playerLayer?.frame = self.bounds
  262. }
  263. }