NCViewerMediaZoom.swift 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  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. NotificationCenter.default.addObserver(self, selector: #selector(openDetail(_:)), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterOpenMediaDetail), object: nil)
  112. }
  113. override func viewDidAppear(_ animated: Bool) {
  114. super.viewDidAppear(animated)
  115. if (metadata.classFile == NCCommunicationCommon.typeClassFile.video.rawValue || metadata.classFile == NCCommunicationCommon.typeClassFile.audio.rawValue) {
  116. if let url = NCKTVHTTPCache.shared.getVideoURL(metadata: metadata) {
  117. self.ncplayer = NCPlayer.init(url: url, imageVideoContainer: self.imageVideoContainer, playerToolBar: self.playerToolBar, metadata: self.metadata, detailView: self.detailView)
  118. }
  119. }
  120. // DOWNLOAD
  121. downloadFile()
  122. }
  123. override func viewWillDisappear(_ animated: Bool) {
  124. super.viewWillDisappear(animated)
  125. NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterOpenMediaDetail), object: nil)
  126. }
  127. override func viewDidDisappear(_ animated: Bool) {
  128. super.viewDidDisappear(animated)
  129. self.ncplayer?.videoRemoved()
  130. }
  131. override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
  132. super.viewWillTransition(to: size, with: coordinator)
  133. coordinator.animate(alongsideTransition: { (context) in
  134. // back to the original size
  135. self.scrollView.zoom(to: CGRect(x: 0, y: 0, width: self.scrollView.bounds.width, height: self.scrollView.bounds.height), animated: false)
  136. self.view.layoutIfNeeded()
  137. UIView.animate(withDuration: context.transitionDuration) {
  138. if self.detailView.isShow() {
  139. self.openDetail()
  140. }
  141. }
  142. }) { (_) in }
  143. }
  144. func reload(image: UIImage, metadata: tableMetadata) {
  145. self.image = image
  146. imageVideoContainer.image = image
  147. imageVideoContainer.sourceImage = image
  148. self.metadata = metadata
  149. }
  150. //MARK: - Gesture
  151. @objc func didDoubleTapWith(gestureRecognizer: UITapGestureRecognizer) {
  152. if detailView.isShow() { return }
  153. // NO ZOOM for Audio / Video
  154. if (metadata.classFile == NCCommunicationCommon.typeClassFile.video.rawValue || metadata.classFile == NCCommunicationCommon.typeClassFile.audio.rawValue) && playerToolBar.isShow() {
  155. return
  156. }
  157. let pointInView = gestureRecognizer.location(in: self.imageVideoContainer)
  158. var newZoomScale = self.scrollView.maximumZoomScale
  159. if self.scrollView.zoomScale >= newZoomScale || abs(self.scrollView.zoomScale - newZoomScale) <= 0.01 {
  160. newZoomScale = self.scrollView.minimumZoomScale
  161. }
  162. let width = self.scrollView.bounds.width / newZoomScale
  163. let height = self.scrollView.bounds.height / newZoomScale
  164. let originX = pointInView.x - (width / 2.0)
  165. let originY = pointInView.y - (height / 2.0)
  166. let rectToZoomTo = CGRect(x: originX, y: originY, width: width, height: height)
  167. self.scrollView.zoom(to: rectToZoomTo, animated: true)
  168. }
  169. @objc func didPanWith(gestureRecognizer: UIPanGestureRecognizer) {
  170. let currentLocation = gestureRecognizer.translation(in: self.view)
  171. switch gestureRecognizer.state {
  172. case .began:
  173. // let velocity = gestureRecognizer.velocity(in: self.view)
  174. // gesture moving Up
  175. // if velocity.y < 0 {
  176. // }
  177. break
  178. case .ended:
  179. if detailView.isShow() {
  180. self.imageViewTopConstraint.constant = -imageViewConstraint
  181. self.imageViewBottomConstraint.constant = imageViewConstraint
  182. } else {
  183. self.imageViewTopConstraint.constant = 0
  184. self.imageViewBottomConstraint.constant = 0
  185. }
  186. case .changed:
  187. imageViewTopConstraint.constant = (currentLocation.y - imageViewConstraint)
  188. imageViewBottomConstraint.constant = -(currentLocation.y - imageViewConstraint)
  189. // DISMISS VIEW
  190. if detailView.isHidden && (currentLocation.y > 20) {
  191. viewerMedia?.navigationController?.popViewController(animated: true)
  192. gestureRecognizer.state = .ended
  193. }
  194. // CLOSE DETAIL
  195. if !detailView.isHidden && (currentLocation.y > 20) {
  196. self.closeDetail()
  197. gestureRecognizer.state = .ended
  198. }
  199. // OPEN DETAIL
  200. if detailView.isHidden && (currentLocation.y < -20) {
  201. self.openDetail()
  202. gestureRecognizer.state = .ended
  203. }
  204. default:
  205. break
  206. }
  207. }
  208. }
  209. //MARK: -
  210. extension NCViewerMediaZoom {
  211. @objc func openDetail(_ notification: NSNotification) {
  212. if let userInfo = notification.userInfo as NSDictionary?, let ocId = userInfo["ocId"] as? String, ocId == metadata.ocId {
  213. openDetail()
  214. }
  215. }
  216. private func openDetail() {
  217. CCUtility.setExif(metadata) { (latitude, longitude, location, date, lensModel) in
  218. if (latitude != -1 && latitude != 0 && longitude != -1 && longitude != 0) {
  219. self.detailViewHeighConstraint.constant = self.view.bounds.height / 2
  220. } else {
  221. self.detailViewHeighConstraint.constant = 170
  222. }
  223. self.view.layoutIfNeeded()
  224. self.detailView.show(metadata:self.metadata, image: self.image, textColor: self.viewerMedia?.textColor, latitude: latitude, longitude: longitude, location: location, date: date, lensModel: lensModel, delegate: self)
  225. if let image = self.imageVideoContainer.image {
  226. let ratioW = self.imageVideoContainer.frame.width / image.size.width
  227. let ratioH = self.imageVideoContainer.frame.height / image.size.height
  228. let ratio = ratioW < ratioH ? ratioW : ratioH
  229. let imageHeight = image.size.height * ratio
  230. self.imageViewConstraint = self.detailView.frame.height - ((self.view.frame.height - imageHeight) / 2) + self.view.safeAreaInsets.bottom
  231. if self.imageViewConstraint < 0 { self.imageViewConstraint = 0 }
  232. }
  233. UIView.animate(withDuration: 0.3) {
  234. self.imageViewTopConstraint.constant = -self.imageViewConstraint
  235. self.imageViewBottomConstraint.constant = self.imageViewConstraint
  236. self.detailViewTopConstraint.constant = self.detailViewHeighConstraint.constant
  237. self.view.layoutIfNeeded()
  238. } completion: { (_) in
  239. }
  240. self.scrollView.pinchGestureRecognizer?.isEnabled = false
  241. self.playerToolBar.hide()
  242. }
  243. }
  244. private func closeDetail() {
  245. self.detailView.hide()
  246. imageViewConstraint = 0
  247. UIView.animate(withDuration: 0.3) {
  248. self.imageViewTopConstraint.constant = 0
  249. self.imageViewBottomConstraint.constant = 0
  250. self.detailViewTopConstraint.constant = 0
  251. self.view.layoutIfNeeded()
  252. } completion: { (_) in
  253. }
  254. scrollView.pinchGestureRecognizer?.isEnabled = true
  255. if metadata.classFile == NCCommunicationCommon.typeClassFile.video.rawValue && !metadata.livePhoto && appDelegate.player?.timeControlStatus == .paused {
  256. NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterShowPlayerToolBar, userInfo: ["ocId":metadata.ocId, "enableTimerAutoHide": false])
  257. }
  258. }
  259. func reloadDetail() {
  260. if self.detailView.isShow() {
  261. CCUtility.setExif(metadata) { (latitude, longitude, location, date, lensModel) in
  262. self.detailView.show(metadata:self.metadata, image: self.image, textColor: self.viewerMedia?.textColor, latitude: latitude, longitude: longitude, location: location, date: date, lensModel: lensModel, delegate: self)
  263. }
  264. }
  265. }
  266. func downloadFile() {
  267. let isFolderEncrypted = CCUtility.isFolderEncrypted(metadata.serverUrl, e2eEncrypted: metadata.e2eEncrypted, account: metadata.account, urlBase: metadata.urlBase)
  268. let ext = CCUtility.getExtension(metadata.fileNameView)
  269. if !NCOperationQueue.shared.downloadExists(metadata: metadata) {
  270. viewerMedia?.progressView.progress = 0
  271. }
  272. // DOWNLOAD FILE
  273. 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) {
  274. NCOperationQueue.shared.download(metadata: metadata, selector: "")
  275. }
  276. // DOWNLOAD FILE LIVE PHOTO
  277. if let metadataLivePhoto = NCManageDatabase.shared.getMetadataLivePhoto(metadata: metadata) {
  278. if CCUtility.getAutomaticDownloadImage() && !CCUtility.fileProviderStorageExists(metadataLivePhoto.ocId, fileNameView: metadataLivePhoto.fileNameView) {
  279. NCOperationQueue.shared.download(metadata: metadataLivePhoto, selector: "")
  280. }
  281. }
  282. // DOWNLOAD preview for image
  283. if !CCUtility.fileProviderStoragePreviewIconExists(metadata.ocId, etag: metadata.etag) && metadata.hasPreview && metadata.classFile == NCCommunicationCommon.typeClassFile.image.rawValue {
  284. NCOperationQueue.shared.downloadThumbnail(metadata: metadata, placeholder: false, cell: nil, view: nil)
  285. }
  286. }
  287. }
  288. //MARK: -
  289. extension NCViewerMediaZoom: UIScrollViewDelegate {
  290. func viewForZooming(in scrollView: UIScrollView) -> UIView? {
  291. return imageVideoContainer
  292. }
  293. func scrollViewDidZoom(_ scrollView: UIScrollView) {
  294. if scrollView.zoomScale > 1 {
  295. if let image = imageVideoContainer.image {
  296. let ratioW = imageVideoContainer.frame.width / image.size.width
  297. let ratioH = imageVideoContainer.frame.height / image.size.height
  298. let ratio = ratioW < ratioH ? ratioW : ratioH
  299. let newWidth = image.size.width * ratio
  300. let newHeight = image.size.height * ratio
  301. let conditionLeft = newWidth*scrollView.zoomScale > imageVideoContainer.frame.width
  302. let left = 0.5 * (conditionLeft ? newWidth - imageVideoContainer.frame.width : (scrollView.frame.width - scrollView.contentSize.width))
  303. let conditioTop = newHeight*scrollView.zoomScale > imageVideoContainer.frame.height
  304. let top = 0.5 * (conditioTop ? newHeight - imageVideoContainer.frame.height : (scrollView.frame.height - scrollView.contentSize.height))
  305. scrollView.contentInset = UIEdgeInsets(top: top, left: left, bottom: top, right: left)
  306. }
  307. } else {
  308. scrollView.contentInset = .zero
  309. }
  310. }
  311. func scrollViewDidScroll(_ scrollView: UIScrollView) {
  312. }
  313. }
  314. extension NCViewerMediaZoom: NCViewerMediaDetailViewDelegate {
  315. func downloadFullResolution() {
  316. closeDetail()
  317. NCNetworking.shared.download(metadata: metadata, selector: NCGlobal.shared.selectorOpenDetail) { (_) in }
  318. }
  319. }
  320. //MARK: -
  321. class imageVideoContainerView: UIImageView {
  322. var playerLayer: CALayer?
  323. var metadata: tableMetadata?
  324. var sourceImage: UIImage?
  325. override func layoutSublayers(of layer: CALayer) {
  326. super.layoutSublayers(of: layer)
  327. playerLayer?.frame = self.bounds
  328. }
  329. }