NCViewerMediaDetailView.swift 7.9 KB

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