NCViewerImageDetailView.swift 5.7 KB

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