NCViewerMediaZoom.swift 14 KB

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