NCViewerMediaDetailView.swift 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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 messageLabel: UILabel!
  37. @IBOutlet weak var mapView: MKMapView!
  38. @IBOutlet weak var locationButton: UIButton!
  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 size: Int64 = 0
  46. var image: UIImage?
  47. override func awakeFromNib() {
  48. super.awakeFromNib()
  49. sizeLabel.text = ""
  50. sizeValue.text = ""
  51. dateLabel.text = ""
  52. dateValue.text = ""
  53. dimLabel.text = ""
  54. dimValue.text = ""
  55. lensModelLabel.text = ""
  56. lensModelValue.text = ""
  57. messageLabel.text = ""
  58. messageLabel.textColor = NCBrandColor.shared.brand
  59. locationButton.setTitle("" , for: .normal)
  60. }
  61. deinit {
  62. print("deinit NCViewerMediaDetailView")
  63. }
  64. func textColor(_ textColor: UIColor?) {
  65. sizeValue.textColor = textColor
  66. dateValue.textColor = textColor
  67. dimValue.textColor = textColor
  68. lensModelValue.textColor = textColor
  69. separator.backgroundColor = NCBrandColor.shared.separator
  70. }
  71. func show(textColor: UIColor?) {
  72. self.textColor(textColor)
  73. }
  74. //MARK: - EXIF
  75. func update(metadata: tableMetadata, image: UIImage?, textColor: UIColor?) {
  76. self.metadata = metadata
  77. self.image = image
  78. self.size = metadata.size
  79. self.textColor(textColor)
  80. if metadata.classFile == NCCommunicationCommon.typeClassFile.image.rawValue {
  81. CCUtility.setExif(metadata) { (latitude, longitude, location, date, lensModel) in
  82. self.latitude = latitude
  83. self.longitude = longitude
  84. self.location = location
  85. self.date = date as NSDate?
  86. self.lensModel = lensModel
  87. self.updateContent()
  88. };
  89. } else {
  90. self.updateContent()
  91. }
  92. }
  93. //MARK: - Map
  94. func updateContent() {
  95. // Size
  96. sizeLabel.text = NSLocalizedString("_size_", comment: "")
  97. sizeValue.text = CCUtility.transformedSize(self.size)
  98. // Date
  99. if let date = self.date {
  100. let formatter = DateFormatter()
  101. formatter.dateStyle = .full
  102. formatter.timeStyle = .medium
  103. let dateString = formatter.string(from: date as Date)
  104. dateLabel.text = NSLocalizedString("_date_", comment: "")
  105. dateValue.text = dateString
  106. } else {
  107. dateLabel.text = NSLocalizedString("_date_", comment: "")
  108. dateValue.text = NSLocalizedString("_not_available_", comment: "")
  109. }
  110. // Dimension / Duration
  111. if metadata?.classFile == NCCommunicationCommon.typeClassFile.image.rawValue {
  112. if let image = self.image {
  113. dimLabel.text = NSLocalizedString("_resolution_", comment: "")
  114. dimValue.text = "\(Int(image.size.width)) x \(Int(image.size.height))"
  115. }
  116. } else if metadata?.classFile == NCCommunicationCommon.typeClassFile.video.rawValue || metadata?.classFile == NCCommunicationCommon.typeClassFile.audio.rawValue {
  117. if let durationTime = NCManageDatabase.shared.getVideoDurationTime(metadata: metadata) {
  118. self.dimLabel.text = NSLocalizedString("_duration_", comment: "")
  119. self.dimValue.text = NCUtility.shared.stringFromTime(durationTime)
  120. }
  121. }
  122. // Model
  123. if let lensModel = self.lensModel {
  124. lensModelLabel.text = NSLocalizedString("_model_", comment: "")
  125. lensModelValue.text = lensModel
  126. }
  127. // Message
  128. if let metadata = self.metadata, metadata.classFile == NCCommunicationCommon.typeClassFile.image.rawValue && !CCUtility.fileProviderStorageExists(metadata.ocId, fileNameView: metadata.fileNameView) && metadata.session == "" {
  129. messageLabel.text = NSLocalizedString("_try_download_full_resolution_", comment: "")
  130. } else {
  131. messageLabel.text = ""
  132. }
  133. // Map
  134. if latitude != -1 && latitude != 0 && longitude != -1 && longitude != 0 {
  135. let annotation = MKPointAnnotation()
  136. annotation.coordinate = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
  137. mapView.layer.cornerRadius = 6
  138. mapView.isZoomEnabled = false
  139. mapView.isScrollEnabled = false
  140. mapView.isUserInteractionEnabled = false
  141. mapView.addAnnotation(annotation)
  142. mapView.setRegion(MKCoordinateRegion(center: annotation.coordinate, latitudinalMeters: 500, longitudinalMeters: 500), animated: false)
  143. locationButton.setTitle(location, for: .normal)
  144. }
  145. }
  146. //MARK: - Action
  147. @IBAction func touchLocation(_ sender: Any) {
  148. if latitude != -1 && latitude != 0 && longitude != -1 && longitude != 0 {
  149. let latitude: CLLocationDegrees = self.latitude
  150. let longitude: CLLocationDegrees = self.longitude
  151. let regionDistance:CLLocationDistance = 10000
  152. let coordinates = CLLocationCoordinate2DMake(latitude, longitude)
  153. let regionSpan = MKCoordinateRegion(center: coordinates, latitudinalMeters: regionDistance, longitudinalMeters: regionDistance)
  154. let options = [
  155. MKLaunchOptionsMapCenterKey: NSValue(mkCoordinate: regionSpan.center),
  156. MKLaunchOptionsMapSpanKey: NSValue(mkCoordinateSpan: regionSpan.span)
  157. ]
  158. let placemark = MKPlacemark(coordinate: coordinates, addressDictionary: nil)
  159. let mapItem = MKMapItem(placemark: placemark)
  160. mapItem.name = location
  161. mapItem.openInMaps(launchOptions: options)
  162. }
  163. }
  164. @IBAction func touchFavorite(_ sender: Any) {
  165. }
  166. //MARK: -
  167. func secondsToHoursMinutesSeconds (seconds : Int) -> (Int, Int, Int) {
  168. return (seconds / 3600, (seconds % 3600) / 60, (seconds % 3600) % 60)
  169. }
  170. }