NCViewerMediaZoom.swift 17 KB

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