NCViewerImageDetailView.swift 7.2 KB

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