NCViewerImageDetailView.swift 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. //
  2. // NCViewerImageDetailView.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 31/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 MapKit
  25. import NCCommunication
  26. class NCViewerImageDetailView: UIView {
  27. @IBOutlet weak var separator: UIView!
  28. @IBOutlet weak var sizeLabel: UILabel!
  29. @IBOutlet weak var sizeValue: UILabel!
  30. @IBOutlet weak var dateLabel: UILabel!
  31. @IBOutlet weak var dateValue: UILabel!
  32. @IBOutlet weak var dimLabel: UILabel!
  33. @IBOutlet weak var dimValue: UILabel!
  34. @IBOutlet weak var lensModelLabel: UILabel!
  35. @IBOutlet weak var lensModelValue: UILabel!
  36. @IBOutlet weak var mapHeightConstraint: NSLayoutConstraint!
  37. @IBOutlet weak var mapView: MKMapView!
  38. @IBOutlet weak var locationButton: UIButton!
  39. var localFile: tableLocalFile?
  40. var metadata: tableMetadata?
  41. var latitude: Double = 0
  42. var longitude: Double = 0
  43. var location: String?
  44. var date: NSDate?
  45. var lensModel: String?
  46. var heightMap: CGFloat = 0
  47. var size: Int64 = 0
  48. var image: UIImage?
  49. var imageViewTopConstraintConstant: CGFloat = 0
  50. var imageViewBottomConstraintConstant: CGFloat = 0
  51. var detailViewTopConstraintConstant: CGFloat = 0
  52. override func awakeFromNib() {
  53. super.awakeFromNib()
  54. mapView.layer.cornerRadius = 6
  55. mapView.isZoomEnabled = false
  56. mapView.isScrollEnabled = false
  57. mapView.isUserInteractionEnabled = false
  58. sizeLabel.text = ""
  59. sizeValue.text = ""
  60. dateLabel.text = ""
  61. dateValue.text = ""
  62. dimLabel.text = ""
  63. dimValue.text = ""
  64. lensModelLabel.text = ""
  65. lensModelValue.text = ""
  66. locationButton.setTitle("" , for: .normal)
  67. }
  68. func show(textColor: UIColor?) {
  69. sizeValue.textColor = textColor
  70. dateValue.textColor = textColor
  71. dimValue.textColor = textColor
  72. lensModelValue.textColor = textColor
  73. separator.backgroundColor = NCBrandColor.shared.separator
  74. isHidden = false
  75. }
  76. func hide() {
  77. isHidden = true
  78. imageViewTopConstraintConstant = 0
  79. imageViewBottomConstraintConstant = 0
  80. detailViewTopConstraintConstant = 0
  81. }
  82. func isShow() -> Bool {
  83. return !isHidden
  84. }
  85. func isSavedContraint() -> Bool {
  86. return imageViewTopConstraintConstant != 0 && imageViewBottomConstraintConstant != 0 && detailViewTopConstraintConstant != 0
  87. }
  88. //MARK: - EXIF
  89. func update(metadata: tableMetadata, image: UIImage?, heightMap: CGFloat) {
  90. self.metadata = metadata
  91. self.heightMap = heightMap
  92. self.image = image
  93. self.size = metadata.size
  94. self.date = metadata.date
  95. if metadata.classFile == NCCommunicationCommon.typeClassFile.image.rawValue {
  96. CCUtility.setExif(metadata) { (latitude, longitude, location, date, lensMode) in
  97. self.latitude = latitude
  98. self.longitude = longitude
  99. self.location = location
  100. self.date = date as NSDate?
  101. self.updateContent()
  102. };
  103. }
  104. if let localFile = NCManageDatabase.shared.getTableLocalFile(predicate: NSPredicate(format: "ocId == %@", metadata.ocId)) {
  105. let latitudeString = localFile.exifLatitude
  106. let longitudeString = localFile.exifLongitude
  107. self.latitude = Double(localFile.exifLatitude) ?? 0
  108. self.longitude = Double(localFile.exifLongitude) ?? 0
  109. self.date = localFile.exifDate
  110. self.lensModel = localFile.exifLensModel
  111. if let locationDB = NCManageDatabase.shared.getLocationFromGeoLatitude(latitudeString, longitude: longitudeString) {
  112. location = locationDB
  113. }
  114. }
  115. self.updateContent()
  116. }
  117. //MARK: - Map
  118. func updateContent() {
  119. // Size
  120. sizeLabel.text = NSLocalizedString("_size_", comment: "")
  121. sizeValue.text = CCUtility.transformedSize(self.size)
  122. // Date
  123. if let date = self.date {
  124. let formatter = DateFormatter()
  125. formatter.dateStyle = .full
  126. formatter.timeStyle = .medium
  127. let dateString = formatter.string(from: date as Date)
  128. dateLabel.text = NSLocalizedString("_date_", comment: "")
  129. dateValue.text = dateString
  130. }
  131. // Dimensions / Durations
  132. if metadata?.classFile == NCCommunicationCommon.typeClassFile.image.rawValue {
  133. if let image = self.image {
  134. dimLabel.text = NSLocalizedString("_dimension_", comment: "")
  135. dimValue.text = "\(Int(image.size.width)) x \(Int(image.size.height))"
  136. }
  137. } else if metadata?.classFile == NCCommunicationCommon.typeClassFile.video.rawValue || metadata?.classFile == NCCommunicationCommon.typeClassFile.audio.rawValue {
  138. NCNetworking.shared.getVideoUrl(metadata: metadata!) { url in
  139. if let url = url {
  140. let playerVideo = AVPlayer(url: url)
  141. if let duration = playerVideo.currentItem?.asset.duration {
  142. let durationVideo = Int(CMTimeGetSeconds(duration))
  143. let timer = self.secondsToHoursMinutesSeconds(seconds: durationVideo)
  144. self.dimLabel.text = NSLocalizedString("_duration_", comment: "")
  145. var hh = "\(timer.0)"
  146. var mm = "\(timer.1)"
  147. var ss = "\(timer.2)"
  148. if hh.count == 1 { hh = "0" + hh }
  149. if mm.count == 1 { mm = "0" + mm }
  150. if ss.count == 1 { ss = "0" + ss }
  151. self.dimValue.text = hh + ":" + mm + ":" + ss
  152. }
  153. }
  154. }
  155. }
  156. // Model
  157. if let lensModel = self.lensModel {
  158. lensModelLabel.text = NSLocalizedString("_model_", comment: "")
  159. lensModelValue.text = lensModel
  160. }
  161. // Map
  162. if latitude != -1 && latitude != 0 && longitude != -1 && longitude != 0 {
  163. let annotation = MKPointAnnotation()
  164. annotation.coordinate = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
  165. mapView.addAnnotation(annotation)
  166. mapView.setRegion(MKCoordinateRegion(center: annotation.coordinate, latitudinalMeters: 500, longitudinalMeters: 500), animated: false)
  167. locationButton.setTitle(location, for: .normal)
  168. mapHeightConstraint.constant = self.heightMap
  169. } else {
  170. mapHeightConstraint.constant = 0
  171. }
  172. }
  173. //MARK: - Action
  174. @IBAction func touchLocation(_ sender: Any) {
  175. if latitude != -1 && latitude != 0 && longitude != -1 && longitude != 0 {
  176. let latitude: CLLocationDegrees = self.latitude
  177. let longitude: CLLocationDegrees = self.longitude
  178. let regionDistance:CLLocationDistance = 10000
  179. let coordinates = CLLocationCoordinate2DMake(latitude, longitude)
  180. let regionSpan = MKCoordinateRegion(center: coordinates, latitudinalMeters: regionDistance, longitudinalMeters: regionDistance)
  181. let options = [
  182. MKLaunchOptionsMapCenterKey: NSValue(mkCoordinate: regionSpan.center),
  183. MKLaunchOptionsMapSpanKey: NSValue(mkCoordinateSpan: regionSpan.span)
  184. ]
  185. let placemark = MKPlacemark(coordinate: coordinates, addressDictionary: nil)
  186. let mapItem = MKMapItem(placemark: placemark)
  187. mapItem.name = location
  188. mapItem.openInMaps(launchOptions: options)
  189. }
  190. }
  191. @IBAction func touchFavorite(_ sender: Any) {
  192. }
  193. //MARK: -
  194. func secondsToHoursMinutesSeconds (seconds : Int) -> (Int, Int, Int) {
  195. return (seconds / 3600, (seconds % 3600) / 60, (seconds % 3600) % 60)
  196. }
  197. }