NCViewerMediaZoom.swift 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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 imageViewTopConstraint: NSLayoutConstraint!
  27. @IBOutlet weak var imageViewBottomConstraint: NSLayoutConstraint!
  28. @IBOutlet weak var scrollView: UIScrollView!
  29. @IBOutlet weak var imageVideoContainer: imageVideoContainerView!
  30. @IBOutlet weak var statusViewImage: UIImageView!
  31. @IBOutlet weak var statusLabel: UILabel!
  32. @IBOutlet weak var playerToolBar: NCPlayerToolBar!
  33. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  34. var viewerMedia: NCViewerMedia?
  35. var ncplayer: NCPlayer?
  36. var detailView: NCViewerMediaDetailView?
  37. var detailViewTopConstraint: NSLayoutConstraint?
  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. imageVideoContainer.image = image
  64. imageVideoContainer.sourceImage = image
  65. } else if metadata.classFile == NCCommunicationCommon.typeClassFile.audio.rawValue {
  66. if image == nil {
  67. image = UIImage.init(named: "noPreviewAudio")!.image(color: .gray, size: view.frame.width)
  68. }
  69. imageVideoContainer.image = image
  70. imageVideoContainer.sourceImage = image
  71. } else {
  72. if image == nil {
  73. image = UIImage.init(named: "noPreview")!.image(color: .gray, size: view.frame.width)
  74. }
  75. imageVideoContainer.image = image
  76. imageVideoContainer.sourceImage = image
  77. }
  78. if NCManageDatabase.shared.getMetadataLivePhoto(metadata: metadata) != nil {
  79. statusViewImage.image = NCUtility.shared.loadImage(named: "livephoto", color: .gray)
  80. statusLabel.text = "LIVE"
  81. } else {
  82. statusViewImage.image = nil
  83. statusLabel.text = ""
  84. }
  85. }
  86. override func viewWillAppear(_ animated: Bool) {
  87. super.viewWillAppear(animated)
  88. imageVideoContainer.image = image
  89. viewerMedia?.navigationController?.navigationBar.prefersLargeTitles = false
  90. viewerMedia?.navigationItem.title = metadata.fileNameView
  91. if metadata.classFile == NCCommunicationCommon.typeClassFile.image.rawValue, let viewerMedia = self.viewerMedia {
  92. viewerMedia.currentScreenMode = viewerMedia.saveScreenModeImage
  93. }
  94. if viewerMedia?.currentScreenMode == .full {
  95. viewerMedia?.navigationController?.setNavigationBarHidden(true, animated: true)
  96. NCUtility.shared.colorNavigationController(viewerMedia?.navigationController, backgroundColor: .black, titleColor: .white, tintColor: nil)
  97. viewerMedia?.view.backgroundColor = .black
  98. viewerMedia?.textColor = .white
  99. viewerMedia?.progressView.isHidden = true
  100. } else {
  101. viewerMedia?.navigationController?.setNavigationBarHidden(false, animated: true)
  102. NCUtility.shared.colorNavigationController(viewerMedia?.navigationController, backgroundColor: NCBrandColor.shared.systemBackground, titleColor: NCBrandColor.shared.label, tintColor: nil)
  103. viewerMedia?.view.backgroundColor = NCBrandColor.shared.systemBackground
  104. viewerMedia?.textColor = NCBrandColor.shared.label
  105. viewerMedia?.progressView.isHidden = false
  106. }
  107. }
  108. override func viewDidAppear(_ animated: Bool) {
  109. super.viewDidAppear(animated)
  110. if (metadata.classFile == NCCommunicationCommon.typeClassFile.video.rawValue || metadata.classFile == NCCommunicationCommon.typeClassFile.audio.rawValue) {
  111. if let url = NCKTVHTTPCache.shared.getVideoURL(metadata: metadata) {
  112. self.ncplayer = NCPlayer.init(url: url, imageVideoContainer: self.imageVideoContainer, playerToolBar: self.playerToolBar, metadata: self.metadata, detailView: self.detailView)
  113. }
  114. }
  115. // DOWNLOAD
  116. downloadFile()
  117. }
  118. override func viewDidDisappear(_ animated: Bool) {
  119. super.viewDidDisappear(animated)
  120. self.ncplayer?.videoRemoved()
  121. }
  122. override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
  123. super.viewWillTransition(to: size, with: coordinator)
  124. coordinator.animate(alongsideTransition: { (context) in
  125. // back to the original size
  126. self.scrollView.zoom(to: CGRect(x: 0, y: 0, width: self.scrollView.bounds.width, height: self.scrollView.bounds.height), animated: false)
  127. self.view.layoutIfNeeded()
  128. // UIView.animate(withDuration: context.transitionDuration) {
  129. // resize detail
  130. // if let detailView = self.detailView, detailView.isShow() {
  131. // self.openDetail()
  132. // }
  133. // }
  134. }) { (_) in }
  135. }
  136. func reload(image: UIImage, metadata: tableMetadata) {
  137. self.image = image
  138. imageVideoContainer.image = image
  139. imageVideoContainer.sourceImage = image
  140. self.metadata = metadata
  141. }
  142. //MARK: - Gesture
  143. @objc func didDoubleTapWith(gestureRecognizer: UITapGestureRecognizer) {
  144. if detailView != nil { return }
  145. // NO ZOOM for Audio / Video
  146. if (metadata.classFile == NCCommunicationCommon.typeClassFile.video.rawValue || metadata.classFile == NCCommunicationCommon.typeClassFile.audio.rawValue) && !playerToolBar.isHidden {
  147. return
  148. }
  149. let pointInView = gestureRecognizer.location(in: self.imageVideoContainer)
  150. var newZoomScale = self.scrollView.maximumZoomScale
  151. if self.scrollView.zoomScale >= newZoomScale || abs(self.scrollView.zoomScale - newZoomScale) <= 0.01 {
  152. newZoomScale = self.scrollView.minimumZoomScale
  153. }
  154. let width = self.scrollView.bounds.width / newZoomScale
  155. let height = self.scrollView.bounds.height / newZoomScale
  156. let originX = pointInView.x - (width / 2.0)
  157. let originY = pointInView.y - (height / 2.0)
  158. let rectToZoomTo = CGRect(x: originX, y: originY, width: width, height: height)
  159. self.scrollView.zoom(to: rectToZoomTo, animated: true)
  160. }
  161. @objc func didPanWith(gestureRecognizer: UIPanGestureRecognizer) {
  162. let currentLocation = gestureRecognizer.translation(in: self.view)
  163. let velocity = gestureRecognizer.velocity(in: self.view)
  164. switch gestureRecognizer.state {
  165. case .began:
  166. // gesture moving Up
  167. if velocity.y < 0 {
  168. // var heightMap = (view.bounds.height / 3)
  169. // if view.bounds.width < view.bounds.height {
  170. // heightMap = (view.bounds.width / 3)
  171. // }
  172. // detailView.update(metadata: metadata, image: image, heightMap: heightMap)
  173. }
  174. break
  175. case .ended:
  176. if self.detailView != nil {
  177. self.imageViewTopConstraint.constant = -imageViewConstraint
  178. self.imageViewBottomConstraint.constant = imageViewConstraint
  179. } else {
  180. self.imageViewTopConstraint.constant = 0
  181. self.imageViewBottomConstraint.constant = 0
  182. }
  183. case .changed:
  184. imageViewTopConstraint.constant = (currentLocation.y - imageViewConstraint)
  185. imageViewBottomConstraint.constant = -(currentLocation.y - imageViewConstraint)
  186. // DISMISS VIEW
  187. if detailView == nil && (currentLocation.y > 20) {
  188. viewerMedia?.navigationController?.popViewController(animated: true)
  189. gestureRecognizer.state = .ended
  190. }
  191. // CLOSE DETAIL
  192. if detailView != nil && (currentLocation.y > 20) {
  193. self.closeDetail()
  194. gestureRecognizer.state = .ended
  195. }
  196. // OPEN DETAIL
  197. if detailView == nil && (currentLocation.y < -20) {
  198. self.openDetail()
  199. gestureRecognizer.state = .ended
  200. }
  201. default:
  202. break
  203. }
  204. }
  205. }
  206. //MARK: -
  207. extension NCViewerMediaZoom {
  208. private func openDetail() {
  209. if let detailView = Bundle.main.loadNibNamed("NCViewerMediaDetailView", owner: self, options: nil)?.first as? NCViewerMediaDetailView {
  210. detailView.frame = CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height/2)
  211. self.detailView = detailView
  212. self.view.addSubview(detailView)
  213. detailView.translatesAutoresizingMaskIntoConstraints = false
  214. detailViewTopConstraint = detailView.topAnchor.constraint(equalTo: view.topAnchor, constant: 400)
  215. detailViewTopConstraint?.isActive = true
  216. detailView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 0).isActive = true
  217. detailView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: 0).isActive = true
  218. detailView.heightAnchor.constraint(equalToConstant: self.view.frame.height/2).isActive = true
  219. detailView.update(metadata: metadata, image: image, textColor: self.viewerMedia?.textColor)
  220. if let image = imageVideoContainer.image {
  221. let ratioW = imageVideoContainer.frame.width / image.size.width
  222. let ratioH = imageVideoContainer.frame.height / image.size.height
  223. let ratio = ratioW < ratioH ? ratioW : ratioH
  224. let imageHeight = image.size.height * ratio
  225. imageViewConstraint = detailView.frame.height - ((self.view.frame.height - imageHeight) / 2) + self.view.safeAreaInsets.bottom
  226. if imageViewConstraint < 0 { imageViewConstraint = 0 }
  227. }
  228. UIView.animate(withDuration: 0.3) {
  229. self.imageViewTopConstraint.constant = -self.imageViewConstraint
  230. self.imageViewBottomConstraint.constant = self.imageViewConstraint
  231. self.view.layoutIfNeeded()
  232. } completion: { (_) in
  233. }
  234. scrollView.pinchGestureRecognizer?.isEnabled = false
  235. playerToolBar.hideToolBar()
  236. }
  237. }
  238. private func closeDetail() {
  239. // detailView?.hide()
  240. imageViewConstraint = 0
  241. UIView.animate(withDuration: 0.3) {
  242. self.imageViewTopConstraint.constant = 0
  243. self.imageViewBottomConstraint.constant = 0
  244. self.view.layoutIfNeeded()
  245. } completion: { (_) in
  246. self.detailView?.removeFromSuperview()
  247. self.detailView = nil
  248. }
  249. scrollView.pinchGestureRecognizer?.isEnabled = true
  250. }
  251. func downloadFile() {
  252. let isFolderEncrypted = CCUtility.isFolderEncrypted(metadata.serverUrl, e2eEncrypted: metadata.e2eEncrypted, account: metadata.account, urlBase: metadata.urlBase)
  253. let ext = CCUtility.getExtension(metadata.fileNameView)
  254. if !NCOperationQueue.shared.downloadExists(metadata: metadata) {
  255. viewerMedia?.progressView.progress = 0
  256. }
  257. // DOWNLOAD FILE
  258. 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) {
  259. NCOperationQueue.shared.download(metadata: metadata, selector: "")
  260. }
  261. // DOWNLOAD FILE LIVE PHOTO
  262. if let metadataLivePhoto = NCManageDatabase.shared.getMetadataLivePhoto(metadata: metadata) {
  263. if CCUtility.getAutomaticDownloadImage() && !CCUtility.fileProviderStorageExists(metadataLivePhoto.ocId, fileNameView: metadataLivePhoto.fileNameView) {
  264. NCOperationQueue.shared.download(metadata: metadataLivePhoto, selector: "")
  265. }
  266. }
  267. // DOWNLOAD preview for image
  268. if !CCUtility.fileProviderStoragePreviewIconExists(metadata.ocId, etag: metadata.etag) && metadata.hasPreview && metadata.classFile == NCCommunicationCommon.typeClassFile.image.rawValue {
  269. NCOperationQueue.shared.downloadThumbnail(metadata: metadata, placeholder: false, cell: nil, view: nil)
  270. }
  271. }
  272. }
  273. //MARK: -
  274. extension NCViewerMediaZoom: UIScrollViewDelegate {
  275. func viewForZooming(in scrollView: UIScrollView) -> UIView? {
  276. return imageVideoContainer
  277. }
  278. func scrollViewDidZoom(_ scrollView: UIScrollView) {
  279. if scrollView.zoomScale > 1 {
  280. if let image = imageVideoContainer.image {
  281. let ratioW = imageVideoContainer.frame.width / image.size.width
  282. let ratioH = imageVideoContainer.frame.height / image.size.height
  283. let ratio = ratioW < ratioH ? ratioW : ratioH
  284. let newWidth = image.size.width * ratio
  285. let newHeight = image.size.height * ratio
  286. let conditionLeft = newWidth*scrollView.zoomScale > imageVideoContainer.frame.width
  287. let left = 0.5 * (conditionLeft ? newWidth - imageVideoContainer.frame.width : (scrollView.frame.width - scrollView.contentSize.width))
  288. let conditioTop = newHeight*scrollView.zoomScale > imageVideoContainer.frame.height
  289. let top = 0.5 * (conditioTop ? newHeight - imageVideoContainer.frame.height : (scrollView.frame.height - scrollView.contentSize.height))
  290. scrollView.contentInset = UIEdgeInsets(top: top, left: left, bottom: top, right: left)
  291. }
  292. } else {
  293. scrollView.contentInset = .zero
  294. }
  295. }
  296. func scrollViewDidScroll(_ scrollView: UIScrollView) {
  297. }
  298. }
  299. //MARK: -
  300. class imageVideoContainerView: UIImageView {
  301. var playerLayer: CALayer?
  302. var metadata: tableMetadata?
  303. var sourceImage: UIImage?
  304. override func layoutSublayers(of layer: CALayer) {
  305. super.layoutSublayers(of: layer)
  306. playerLayer?.frame = self.bounds
  307. }
  308. }