NCViewerImageDetailView.swift 6.3 KB

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