NCViewerImageDetailView.swift 8.3 KB

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