NCViewerImageDetailView.swift 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. import Foundation
  9. import MapKit
  10. class NCViewerImageDetailView: UIView {
  11. @IBOutlet weak var detailViewHeightConstraint: NSLayoutConstraint!
  12. @IBOutlet weak var dateLabel: UILabel!
  13. @IBOutlet weak var mapView: MKMapView!
  14. @IBOutlet weak var locationButton: UIButton!
  15. var latitude: Double = 0
  16. var longitude: Double = 0
  17. var location: String?
  18. var date: NSDate?
  19. override func awakeFromNib() {
  20. super.awakeFromNib()
  21. mapView.layer.cornerRadius = 6
  22. mapView.isZoomEnabled = false
  23. mapView.isScrollEnabled = false
  24. mapView.isUserInteractionEnabled = false
  25. }
  26. func hasData() -> Bool {
  27. if latitude > 0 && longitude > 0 {
  28. return true
  29. } else {
  30. return false
  31. }
  32. }
  33. func show(height: CGFloat, textColor: UIColor) {
  34. detailViewHeightConstraint.constant = height
  35. dateLabel.textColor = textColor
  36. isHidden = false
  37. }
  38. func hide() {
  39. isHidden = true
  40. }
  41. func isShow() -> Bool {
  42. return !isHidden
  43. }
  44. //MARK: - EXIF
  45. func updateExifLocal(metadata: tableMetadata) {
  46. if metadata.typeFile == k_metadataTypeFile_image {
  47. CCUtility.setExif(metadata) { (latitude, longitude, location, date) in
  48. if latitude > 0 && longitude > 0 {
  49. self.latitude = latitude
  50. self.longitude = longitude
  51. self.location = location
  52. self.date = date as NSDate?
  53. self.updateContent()
  54. }
  55. };
  56. }
  57. if let localFile = NCManageDatabase.sharedInstance.getTableLocalFile(predicate: NSPredicate(format: "ocId == %@", metadata.ocId)) {
  58. let latitudeString = localFile.exifLatitude
  59. let longitudeString = localFile.exifLongitude
  60. self.latitude = Double(localFile.exifLatitude) ?? 0
  61. self.longitude = Double(localFile.exifLongitude) ?? 0
  62. self.date = localFile.exifDate
  63. if let locationDB = NCManageDatabase.sharedInstance.getLocationFromGeoLatitude(latitudeString, longitude: longitudeString) {
  64. location = locationDB
  65. }
  66. self.updateContent()
  67. }
  68. }
  69. //MARK: - Map
  70. func updateContent() {
  71. let annotation = MKPointAnnotation()
  72. if let date = self.date {
  73. let formatter = DateFormatter()
  74. formatter.dateStyle = .full
  75. let dateString = formatter.string(from: date as Date)
  76. formatter.dateFormat = "HH:mm"
  77. let timeString = formatter.string(from: date as Date)
  78. self.dateLabel.text = dateString + ", " + timeString
  79. }
  80. annotation.coordinate = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
  81. mapView.addAnnotation(annotation)
  82. mapView.setRegion(MKCoordinateRegion(center: annotation.coordinate, latitudinalMeters: 500, longitudinalMeters: 500), animated: false)
  83. locationButton.setTitle(location, for: .normal)
  84. }
  85. //MARK: - Action
  86. @IBAction func touchLocation(_ sender: Any) {
  87. if self.latitude > 0 && self.longitude > 0 {
  88. let latitude: CLLocationDegrees = self.latitude
  89. let longitude: CLLocationDegrees = self.longitude
  90. let regionDistance:CLLocationDistance = 10000
  91. let coordinates = CLLocationCoordinate2DMake(latitude, longitude)
  92. let regionSpan = MKCoordinateRegion(center: coordinates, latitudinalMeters: regionDistance, longitudinalMeters: regionDistance)
  93. let options = [
  94. MKLaunchOptionsMapCenterKey: NSValue(mkCoordinate: regionSpan.center),
  95. MKLaunchOptionsMapSpanKey: NSValue(mkCoordinateSpan: regionSpan.span)
  96. ]
  97. let placemark = MKPlacemark(coordinate: coordinates, addressDictionary: nil)
  98. let mapItem = MKMapItem(placemark: placemark)
  99. mapItem.name = location
  100. mapItem.openInMaps(launchOptions: options)
  101. }
  102. }
  103. }