NCViewerImageDetailView.swift 7.7 KB

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