NCViewerImageDetailView.swift 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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 UIKit
  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 lensModelLabel: UILabel!
  34. @IBOutlet weak var lensModelValue: UILabel!
  35. @IBOutlet weak var mapHeightConstraint: NSLayoutConstraint!
  36. @IBOutlet weak var mapView: MKMapView!
  37. @IBOutlet weak var locationButton: UIButton!
  38. var localFile: tableLocalFile?
  39. var metadata: tableMetadata?
  40. var latitude: Double = 0
  41. var longitude: Double = 0
  42. var location: String?
  43. var date: NSDate?
  44. var lensModel: String?
  45. var heightMap: CGFloat = 0
  46. var size: Int64 = 0
  47. var image: UIImage?
  48. var imageViewTopConstraintConstant: CGFloat = 0
  49. var imageViewBottomConstraintConstant: CGFloat = 0
  50. var detailViewTopConstraintConstant: CGFloat = 0
  51. override func awakeFromNib() {
  52. super.awakeFromNib()
  53. mapView.layer.cornerRadius = 6
  54. mapView.isZoomEnabled = false
  55. mapView.isScrollEnabled = false
  56. mapView.isUserInteractionEnabled = false
  57. sizeLabel.text = ""
  58. sizeValue.text = ""
  59. dateLabel.text = ""
  60. dateValue.text = ""
  61. dimLabel.text = ""
  62. dimValue.text = ""
  63. lensModelLabel.text = ""
  64. lensModelValue.text = ""
  65. locationButton.setTitle("" , for: .normal)
  66. }
  67. func show(textColor: UIColor?) {
  68. sizeValue.textColor = textColor
  69. dateValue.textColor = textColor
  70. dimValue.textColor = textColor
  71. lensModelValue.textColor = textColor
  72. separator.backgroundColor = NCBrandColor.shared.separator
  73. isHidden = false
  74. }
  75. func hide() {
  76. isHidden = true
  77. imageViewTopConstraintConstant = 0
  78. imageViewBottomConstraintConstant = 0
  79. detailViewTopConstraintConstant = 0
  80. }
  81. func isShow() -> Bool {
  82. return !isHidden
  83. }
  84. func isSavedContraint() -> Bool {
  85. return imageViewTopConstraintConstant != 0 && imageViewBottomConstraintConstant != 0 && detailViewTopConstraintConstant != 0
  86. }
  87. //MARK: - EXIF
  88. func update(metadata: tableMetadata, image: UIImage?, heightMap: CGFloat) {
  89. self.metadata = metadata
  90. self.heightMap = heightMap
  91. self.image = image
  92. self.size = metadata.size
  93. self.date = metadata.date
  94. if metadata.typeFile == NCGlobal.shared.metadataTypeFileImage {
  95. CCUtility.setExif(metadata) { (latitude, longitude, location, date, lensMode) in
  96. self.latitude = latitude
  97. self.longitude = longitude
  98. self.location = location
  99. self.date = date as NSDate?
  100. self.updateContent()
  101. };
  102. }
  103. if let localFile = NCManageDatabase.shared.getTableLocalFile(predicate: NSPredicate(format: "ocId == %@", metadata.ocId)) {
  104. let latitudeString = localFile.exifLatitude
  105. let longitudeString = localFile.exifLongitude
  106. self.latitude = Double(localFile.exifLatitude) ?? 0
  107. self.longitude = Double(localFile.exifLongitude) ?? 0
  108. self.date = localFile.exifDate
  109. self.lensModel = localFile.exifLensModel
  110. if let locationDB = NCManageDatabase.shared.getLocationFromGeoLatitude(latitudeString, longitude: longitudeString) {
  111. location = locationDB
  112. }
  113. }
  114. self.updateContent()
  115. }
  116. //MARK: - Map
  117. func updateContent() {
  118. // Size
  119. sizeLabel.text = NSLocalizedString("_size_", comment: "")
  120. sizeValue.text = CCUtility.transformedSize(self.size)
  121. // Date
  122. if let date = self.date {
  123. let formatter = DateFormatter()
  124. formatter.dateStyle = .full
  125. formatter.timeStyle = .medium
  126. let dateString = formatter.string(from: date as Date)
  127. dateLabel.text = NSLocalizedString("_date_", comment: "")
  128. dateValue.text = dateString
  129. }
  130. // Dimensions / Durations
  131. if metadata?.typeFile == NCGlobal.shared.metadataTypeFileImage {
  132. if let image = self.image {
  133. dimLabel.text = NSLocalizedString("_dimension_", comment: "")
  134. dimValue.text = "\(Int(image.size.width)) x \(Int(image.size.height))"
  135. }
  136. } else if metadata?.typeFile == NCGlobal.shared.metadataTypeFileVideo || metadata?.typeFile == NCGlobal.shared.metadataTypeFileAudio {
  137. NCNetworking.shared.getVideoUrl(metadata: metadata!) { url in
  138. if let url = url {
  139. let playerVideo = AVPlayer(url: url)
  140. if let duration = playerVideo.currentItem?.asset.duration {
  141. let durationVideo = Int(CMTimeGetSeconds(duration))
  142. let timer = self.secondsToHoursMinutesSeconds(seconds: durationVideo)
  143. self.dimLabel.text = NSLocalizedString("_duration_", comment: "")
  144. var hh = "\(timer.0)"
  145. var mm = "\(timer.1)"
  146. var ss = "\(timer.2)"
  147. if hh.count == 1 { hh = "0" + hh }
  148. if mm.count == 1 { mm = "0" + mm }
  149. if ss.count == 1 { ss = "0" + ss }
  150. self.dimValue.text = hh + ":" + mm + ":" + ss
  151. }
  152. }
  153. }
  154. }
  155. // Model
  156. if let lensModel = self.lensModel {
  157. lensModelLabel.text = NSLocalizedString("_model_", comment: "")
  158. lensModelValue.text = lensModel
  159. }
  160. // Map
  161. if latitude != -1 && latitude != 0 && longitude != -1 && longitude != 0 {
  162. let annotation = MKPointAnnotation()
  163. annotation.coordinate = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
  164. mapView.addAnnotation(annotation)
  165. mapView.setRegion(MKCoordinateRegion(center: annotation.coordinate, latitudinalMeters: 500, longitudinalMeters: 500), animated: false)
  166. locationButton.setTitle(location, for: .normal)
  167. mapHeightConstraint.constant = self.heightMap
  168. } else {
  169. mapHeightConstraint.constant = 0
  170. }
  171. }
  172. //MARK: - Action
  173. @IBAction func touchLocation(_ sender: Any) {
  174. if latitude != -1 && latitude != 0 && longitude != -1 && longitude != 0 {
  175. let latitude: CLLocationDegrees = self.latitude
  176. let longitude: CLLocationDegrees = self.longitude
  177. let regionDistance:CLLocationDistance = 10000
  178. let coordinates = CLLocationCoordinate2DMake(latitude, longitude)
  179. let regionSpan = MKCoordinateRegion(center: coordinates, latitudinalMeters: regionDistance, longitudinalMeters: regionDistance)
  180. let options = [
  181. MKLaunchOptionsMapCenterKey: NSValue(mkCoordinate: regionSpan.center),
  182. MKLaunchOptionsMapSpanKey: NSValue(mkCoordinateSpan: regionSpan.span)
  183. ]
  184. let placemark = MKPlacemark(coordinate: coordinates, addressDictionary: nil)
  185. let mapItem = MKMapItem(placemark: placemark)
  186. mapItem.name = location
  187. mapItem.openInMaps(launchOptions: options)
  188. }
  189. }
  190. @IBAction func touchFavorite(_ sender: Any) {
  191. }
  192. //MARK: -
  193. func secondsToHoursMinutesSeconds (seconds : Int) -> (Int, Int, Int) {
  194. return (seconds / 3600, (seconds % 3600) / 60, (seconds % 3600) % 60)
  195. }
  196. }