NCViewerImageDetailView.swift 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 dateLabel: UILabel!
  12. @IBOutlet weak var mapView: MKMapView!
  13. @IBOutlet weak var locationButton: UIButton!
  14. var annotation = MKPointAnnotation()
  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. @IBAction func touchLocation(_ sender: Any) {
  27. if self.latitude > 0 && self.longitude > 0 {
  28. openMapForPlace()
  29. }
  30. }
  31. func openMapForPlace() {
  32. let latitude: CLLocationDegrees = self.latitude
  33. let longitude: CLLocationDegrees = self.longitude
  34. let regionDistance:CLLocationDistance = 10000
  35. let coordinates = CLLocationCoordinate2DMake(latitude, longitude)
  36. let regionSpan = MKCoordinateRegion(center: coordinates, latitudinalMeters: regionDistance, longitudinalMeters: regionDistance)
  37. let options = [
  38. MKLaunchOptionsMapCenterKey: NSValue(mkCoordinate: regionSpan.center),
  39. MKLaunchOptionsMapSpanKey: NSValue(mkCoordinateSpan: regionSpan.span)
  40. ]
  41. let placemark = MKPlacemark(coordinate: coordinates, addressDictionary: nil)
  42. let mapItem = MKMapItem(placemark: placemark)
  43. mapItem.name = location
  44. mapItem.openInMaps(launchOptions: options)
  45. }
  46. func updateExifLocal(metadata: tableMetadata) {
  47. DispatchQueue.global().async {
  48. if metadata.typeFile == k_metadataTypeFile_image {
  49. CCExifGeo.sharedInstance()?.setExif(metadata)
  50. }
  51. if let localFile = NCManageDatabase.sharedInstance.getTableLocalFile(predicate: NSPredicate(format: "ocId == %@", metadata.ocId)) {
  52. let latitudeString = localFile.exifLatitude
  53. let longitudeString = localFile.exifLongitude
  54. self.latitude = Double(localFile.exifLatitude) ?? 0
  55. self.longitude = Double(localFile.exifLongitude) ?? 0
  56. self.date = localFile.exifDate
  57. if let location = NCManageDatabase.sharedInstance.getLocationFromGeoLatitude(latitudeString, longitude: longitudeString) {
  58. self.location = location
  59. }
  60. DispatchQueue.main.async {
  61. if self.latitude > 0 && self.longitude > 0 {
  62. if let date = self.date {
  63. let formatter = DateFormatter()
  64. formatter.dateStyle = .full
  65. let dateString = formatter.string(from: date as Date)
  66. formatter.dateFormat = "HH:mm"
  67. let timeString = formatter.string(from: date as Date)
  68. self.dateLabel.text = dateString + ", " + timeString
  69. }
  70. self.annotation.coordinate = CLLocationCoordinate2D(latitude: self.latitude, longitude: self.longitude)
  71. self.mapView.addAnnotation(self.annotation)
  72. self.mapView.setRegion(MKCoordinateRegion(center: self.annotation.coordinate, latitudinalMeters: 500, longitudinalMeters: 500), animated: false)
  73. self.locationButton.setTitle(self.location, for: .normal)
  74. }
  75. }
  76. }
  77. }
  78. }
  79. func hasData() -> Bool {
  80. if self.latitude > 0 && self.longitude > 0 {
  81. return true
  82. } else {
  83. return false
  84. }
  85. }
  86. func show(textColor: UIColor) {
  87. self.dateLabel.textColor = textColor
  88. self.isHidden = false
  89. }
  90. func hide() {
  91. self.isHidden = true
  92. }
  93. func isShow() -> Bool {
  94. return !self.isHidden
  95. }
  96. }