NCViewerMediaDetailView.swift 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. //
  2. // NCViewerMediaDetailView.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 NCViewerMediaDetailView: 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 textColor(_ 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. }
  72. func show(textColor: UIColor?) {
  73. self.textColor(textColor)
  74. isHidden = false
  75. }
  76. func hide() {
  77. isHidden = true
  78. }
  79. func isShow() -> Bool {
  80. return !isHidden
  81. }
  82. //MARK: - EXIF
  83. func update(metadata: tableMetadata, image: UIImage?, heightMap: CGFloat) {
  84. self.metadata = metadata
  85. self.heightMap = heightMap
  86. self.image = image
  87. self.size = metadata.size
  88. if metadata.classFile == NCCommunicationCommon.typeClassFile.image.rawValue {
  89. CCUtility.setExif(metadata) { (latitude, longitude, location, date, lensModel) in
  90. self.latitude = latitude
  91. self.longitude = longitude
  92. self.location = location
  93. self.date = date as NSDate?
  94. self.lensModel = lensModel
  95. self.updateContent()
  96. };
  97. } else {
  98. self.updateContent()
  99. }
  100. }
  101. //MARK: - Map
  102. func updateContent() {
  103. // Size
  104. sizeLabel.text = NSLocalizedString("_size_", comment: "")
  105. sizeValue.text = CCUtility.transformedSize(self.size)
  106. // Date
  107. if let date = self.date {
  108. let formatter = DateFormatter()
  109. formatter.dateStyle = .full
  110. formatter.timeStyle = .medium
  111. let dateString = formatter.string(from: date as Date)
  112. dateLabel.text = NSLocalizedString("_date_", comment: "")
  113. dateValue.text = dateString
  114. } else {
  115. dateLabel.text = NSLocalizedString("_date_", comment: "")
  116. dateValue.text = NSLocalizedString("_not_available_", comment: "")
  117. }
  118. // Dimension / Duration
  119. if metadata?.classFile == NCCommunicationCommon.typeClassFile.image.rawValue {
  120. if let image = self.image {
  121. dimLabel.text = NSLocalizedString("_resolution_", comment: "")
  122. dimValue.text = "\(Int(image.size.width)) x \(Int(image.size.height))"
  123. }
  124. } else if metadata?.classFile == NCCommunicationCommon.typeClassFile.video.rawValue || metadata?.classFile == NCCommunicationCommon.typeClassFile.audio.rawValue {
  125. if let durationTime = NCManageDatabase.shared.getVideoDurationTime(metadata: metadata) {
  126. self.dimLabel.text = NSLocalizedString("_duration_", comment: "")
  127. self.dimValue.text = NCUtility.shared.stringFromTime(durationTime)
  128. }
  129. }
  130. // Model
  131. if let lensModel = self.lensModel {
  132. lensModelLabel.text = NSLocalizedString("_model_", comment: "")
  133. lensModelValue.text = lensModel
  134. }
  135. // Map
  136. if latitude != -1 && latitude != 0 && longitude != -1 && longitude != 0 {
  137. let annotation = MKPointAnnotation()
  138. annotation.coordinate = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
  139. mapView.addAnnotation(annotation)
  140. mapView.setRegion(MKCoordinateRegion(center: annotation.coordinate, latitudinalMeters: 500, longitudinalMeters: 500), animated: false)
  141. locationButton.setTitle(location, for: .normal)
  142. mapHeightConstraint.constant = self.heightMap
  143. } else {
  144. mapHeightConstraint.constant = 0
  145. }
  146. }
  147. //MARK: - Action
  148. @IBAction func touchLocation(_ sender: Any) {
  149. if latitude != -1 && latitude != 0 && longitude != -1 && longitude != 0 {
  150. let latitude: CLLocationDegrees = self.latitude
  151. let longitude: CLLocationDegrees = self.longitude
  152. let regionDistance:CLLocationDistance = 10000
  153. let coordinates = CLLocationCoordinate2DMake(latitude, longitude)
  154. let regionSpan = MKCoordinateRegion(center: coordinates, latitudinalMeters: regionDistance, longitudinalMeters: regionDistance)
  155. let options = [
  156. MKLaunchOptionsMapCenterKey: NSValue(mkCoordinate: regionSpan.center),
  157. MKLaunchOptionsMapSpanKey: NSValue(mkCoordinateSpan: regionSpan.span)
  158. ]
  159. let placemark = MKPlacemark(coordinate: coordinates, addressDictionary: nil)
  160. let mapItem = MKMapItem(placemark: placemark)
  161. mapItem.name = location
  162. mapItem.openInMaps(launchOptions: options)
  163. }
  164. }
  165. @IBAction func touchFavorite(_ sender: Any) {
  166. }
  167. //MARK: -
  168. func secondsToHoursMinutesSeconds (seconds : Int) -> (Int, Int, Int) {
  169. return (seconds / 3600, (seconds % 3600) / 60, (seconds % 3600) % 60)
  170. }
  171. }