NCViewerImageDetailView.swift 5.4 KB

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