NCViewerMediaDetailView.swift 7.8 KB

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