NCViewerMediaZoom.swift 18 KB

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