NCViewerMediaZoom.swift 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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. self.ncplayer = nil
  53. }
  54. override func viewDidLoad() {
  55. super.viewDidLoad()
  56. scrollView.delegate = self
  57. scrollView.maximumZoomScale = 4
  58. scrollView.minimumZoomScale = 1
  59. view.addGestureRecognizer(doubleTapGestureRecognizer)
  60. if metadata.classFile == NCCommunicationCommon.typeClassFile.video.rawValue {
  61. if image == nil {
  62. image = UIImage.init(named: "noPreviewVideo")!.image(color: .gray, size: view.frame.width)
  63. }
  64. // Show Video Toolbar
  65. if !metadata.livePhoto {
  66. playerToolBar.isHidden = false
  67. }
  68. imageVideoContainer.image = image
  69. imageVideoContainer.sourceImage = image
  70. } else if metadata.classFile == NCCommunicationCommon.typeClassFile.audio.rawValue {
  71. if image == nil {
  72. image = UIImage.init(named: "noPreviewAudio")!.image(color: .gray, size: view.frame.width)
  73. }
  74. // Show Video Toolbar
  75. playerToolBar.isHidden = false
  76. imageVideoContainer.image = image
  77. imageVideoContainer.sourceImage = image
  78. } else {
  79. if image == nil {
  80. image = UIImage.init(named: "noPreview")!.image(color: .gray, size: view.frame.width)
  81. }
  82. imageVideoContainer.image = image
  83. imageVideoContainer.sourceImage = image
  84. }
  85. if NCManageDatabase.shared.getMetadataLivePhoto(metadata: metadata) != nil {
  86. statusViewImage.image = NCUtility.shared.loadImage(named: "livephoto", color: .gray)
  87. statusLabel.text = "LIVE"
  88. } else {
  89. statusViewImage.image = nil
  90. statusLabel.text = ""
  91. }
  92. detailViewConstraint.constant = 0
  93. detailView.hide()
  94. }
  95. override func viewWillAppear(_ animated: Bool) {
  96. super.viewWillAppear(animated)
  97. viewerMedia?.navigationItem.title = metadata.fileNameView
  98. imageVideoContainer.image = image
  99. }
  100. override func viewDidAppear(_ animated: Bool) {
  101. super.viewDidAppear(animated)
  102. if (metadata.classFile == NCCommunicationCommon.typeClassFile.video.rawValue || metadata.classFile == NCCommunicationCommon.typeClassFile.audio.rawValue) {
  103. if let url = NCKTVHTTPCache.shared.getVideoURL(metadata: metadata) {
  104. self.ncplayer = NCPlayer.init(url: url, imageVideoContainer: self.imageVideoContainer, playerToolBar: self.playerToolBar, metadata: self.metadata)
  105. self.viewerMedia?.ncplayer = self.ncplayer
  106. }
  107. }
  108. // DOWNLOAD
  109. downloadFile()
  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. self.image = image
  131. imageVideoContainer.image = image
  132. imageVideoContainer.sourceImage = image
  133. self.metadata = metadata
  134. }
  135. //MARK: - Gesture
  136. @objc func didDoubleTapWith(gestureRecognizer: UITapGestureRecognizer) {
  137. if detailView.isShow() { return }
  138. // NO ZOOM for Audio / Video
  139. if (metadata.classFile == NCCommunicationCommon.typeClassFile.video.rawValue || metadata.classFile == NCCommunicationCommon.typeClassFile.audio.rawValue) && !playerToolBar.isHidden {
  140. return
  141. }
  142. let pointInView = gestureRecognizer.location(in: self.imageVideoContainer)
  143. var newZoomScale = self.scrollView.maximumZoomScale
  144. if self.scrollView.zoomScale >= newZoomScale || abs(self.scrollView.zoomScale - newZoomScale) <= 0.01 {
  145. newZoomScale = self.scrollView.minimumZoomScale
  146. }
  147. let width = self.scrollView.bounds.width / newZoomScale
  148. let height = self.scrollView.bounds.height / newZoomScale
  149. let originX = pointInView.x - (width / 2.0)
  150. let originY = pointInView.y - (height / 2.0)
  151. let rectToZoomTo = CGRect(x: originX, y: originY, width: width, height: height)
  152. self.scrollView.zoom(to: rectToZoomTo, animated: true)
  153. }
  154. @objc func didPanWith(gestureRecognizer: UIPanGestureRecognizer) {
  155. let currentLocation = gestureRecognizer.translation(in: self.view)
  156. let velocity = gestureRecognizer.velocity(in: self.view)
  157. switch gestureRecognizer.state {
  158. case .began:
  159. // gesture moving Up
  160. if velocity.y < 0 {
  161. var heightMap = (view.bounds.height / 3)
  162. if view.bounds.width < view.bounds.height {
  163. heightMap = (view.bounds.width / 3)
  164. }
  165. detailView.update(metadata: metadata, image: image, heightMap: heightMap)
  166. }
  167. case .ended:
  168. if detailView.isShow() {
  169. self.imageViewTopConstraint.constant = -imageViewConstraint
  170. self.imageViewBottomConstraint.constant = imageViewConstraint
  171. } else {
  172. self.imageViewTopConstraint.constant = 0
  173. self.imageViewBottomConstraint.constant = 0
  174. }
  175. case .changed:
  176. imageViewTopConstraint.constant = (currentLocation.y - imageViewConstraint)
  177. imageViewBottomConstraint.constant = -(currentLocation.y - imageViewConstraint)
  178. // DISMISS VIEW
  179. if detailView.isHidden && (currentLocation.y > 20) {
  180. viewerMedia?.navigationController?.popViewController(animated: true)
  181. gestureRecognizer.state = .ended
  182. }
  183. // CLOSE DETAIL
  184. if !detailView.isHidden && (currentLocation.y > 20) {
  185. self.closeDetail()
  186. gestureRecognizer.state = .ended
  187. }
  188. // OPEN DETAIL
  189. if detailView.isHidden && (currentLocation.y < -20) {
  190. self.openDetail()
  191. gestureRecognizer.state = .ended
  192. }
  193. default:
  194. break
  195. }
  196. }
  197. }
  198. //MARK: -
  199. extension NCViewerMediaZoom {
  200. private func openDetail() {
  201. self.detailView.show(textColor: self.viewerMedia?.textColor)
  202. if let image = imageVideoContainer.image {
  203. let ratioW = imageVideoContainer.frame.width / image.size.width
  204. let ratioH = imageVideoContainer.frame.height / image.size.height
  205. let ratio = ratioW < ratioH ? ratioW : ratioH
  206. let imageHeight = image.size.height * ratio
  207. imageViewConstraint = self.detailView.frame.height - ((self.view.frame.height - imageHeight) / 2) + self.view.safeAreaInsets.bottom
  208. if imageViewConstraint < 0 { imageViewConstraint = 0 }
  209. }
  210. UIView.animate(withDuration: 0.3) {
  211. self.imageViewTopConstraint.constant = -self.imageViewConstraint
  212. self.imageViewBottomConstraint.constant = self.imageViewConstraint
  213. self.detailViewConstraint.constant = self.detailView.frame.height
  214. self.view.layoutIfNeeded()
  215. } completion: { (_) in
  216. }
  217. scrollView.pinchGestureRecognizer?.isEnabled = false
  218. playerToolBar.hideToolBar()
  219. }
  220. private func closeDetail() {
  221. self.detailView.hide()
  222. imageViewConstraint = 0
  223. UIView.animate(withDuration: 0.3) {
  224. self.imageViewTopConstraint.constant = 0
  225. self.imageViewBottomConstraint.constant = 0
  226. self.detailViewConstraint.constant = 0
  227. self.view.layoutIfNeeded()
  228. } completion: { (_) in
  229. }
  230. scrollView.pinchGestureRecognizer?.isEnabled = true
  231. playerToolBar.showToolBar(metadata: metadata, detailView: nil)
  232. }
  233. func downloadFile() {
  234. let isFolderEncrypted = CCUtility.isFolderEncrypted(metadata.serverUrl, e2eEncrypted: metadata.e2eEncrypted, account: metadata.account, urlBase: metadata.urlBase)
  235. let ext = CCUtility.getExtension(metadata.fileNameView)
  236. if !NCOperationQueue.shared.downloadExists(metadata: metadata) {
  237. viewerMedia?.progressView.progress = 0
  238. }
  239. // DOWNLOAD FILE
  240. 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) {
  241. NCOperationQueue.shared.download(metadata: metadata, selector: "")
  242. }
  243. // DOWNLOAD FILE LIVE PHOTO
  244. if let metadataLivePhoto = NCManageDatabase.shared.getMetadataLivePhoto(metadata: metadata) {
  245. if CCUtility.getAutomaticDownloadImage() && !CCUtility.fileProviderStorageExists(metadataLivePhoto.ocId, fileNameView: metadataLivePhoto.fileNameView) {
  246. NCOperationQueue.shared.download(metadata: metadataLivePhoto, selector: "")
  247. }
  248. }
  249. // DOWNLOAD preview for image
  250. if !CCUtility.fileProviderStoragePreviewIconExists(metadata.ocId, etag: metadata.etag) && metadata.hasPreview && metadata.classFile == NCCommunicationCommon.typeClassFile.image.rawValue {
  251. NCOperationQueue.shared.downloadThumbnail(metadata: metadata, placeholder: false, cell: nil, view: nil)
  252. }
  253. }
  254. }
  255. //MARK: -
  256. extension NCViewerMediaZoom: UIScrollViewDelegate {
  257. func viewForZooming(in scrollView: UIScrollView) -> UIView? {
  258. return imageVideoContainer
  259. }
  260. func scrollViewDidZoom(_ scrollView: UIScrollView) {
  261. if scrollView.zoomScale > 1 {
  262. if let image = imageVideoContainer.image {
  263. let ratioW = imageVideoContainer.frame.width / image.size.width
  264. let ratioH = imageVideoContainer.frame.height / image.size.height
  265. let ratio = ratioW < ratioH ? ratioW : ratioH
  266. let newWidth = image.size.width * ratio
  267. let newHeight = image.size.height * ratio
  268. let conditionLeft = newWidth*scrollView.zoomScale > imageVideoContainer.frame.width
  269. let left = 0.5 * (conditionLeft ? newWidth - imageVideoContainer.frame.width : (scrollView.frame.width - scrollView.contentSize.width))
  270. let conditioTop = newHeight*scrollView.zoomScale > imageVideoContainer.frame.height
  271. let top = 0.5 * (conditioTop ? newHeight - imageVideoContainer.frame.height : (scrollView.frame.height - scrollView.contentSize.height))
  272. scrollView.contentInset = UIEdgeInsets(top: top, left: left, bottom: top, right: left)
  273. }
  274. } else {
  275. scrollView.contentInset = .zero
  276. }
  277. }
  278. func scrollViewDidScroll(_ scrollView: UIScrollView) {
  279. }
  280. }
  281. //MARK: -
  282. class imageVideoContainerView: UIImageView {
  283. var playerLayer: CALayer?
  284. var metadata: tableMetadata?
  285. var sourceImage: UIImage?
  286. override func layoutSublayers(of layer: CALayer) {
  287. super.layoutSublayers(of: layer)
  288. playerLayer?.frame = self.bounds
  289. }
  290. }