NCViewerMediaDetailView.swift 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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 NextcloudKit
  26. typealias NCImageMetadata = (latitude: Double, longitude: Double, location: String?, date: Date?, lensModel: String?)
  27. public protocol NCViewerMediaDetailViewDelegate: AnyObject {
  28. func downloadFullResolution()
  29. }
  30. class NCViewerMediaDetailView: UIView {
  31. @IBOutlet weak var separator: UIView!
  32. @IBOutlet weak var sizeLabel: UILabel!
  33. @IBOutlet weak var sizeValue: UILabel!
  34. @IBOutlet weak var dateLabel: UILabel!
  35. @IBOutlet weak var dateValue: UILabel!
  36. @IBOutlet weak var dimLabel: UILabel!
  37. @IBOutlet weak var dimValue: UILabel!
  38. @IBOutlet weak var lensModelLabel: UILabel!
  39. @IBOutlet weak var lensModelValue: UILabel!
  40. @IBOutlet weak var messageButton: UIButton!
  41. @IBOutlet weak var mapContainer: UIView!
  42. @IBOutlet weak var locationButton: UIButton!
  43. var latitude: Double = 0
  44. var longitude: Double = 0
  45. var location: String?
  46. var date: Date?
  47. var lensModel: String?
  48. var metadata: tableMetadata?
  49. var mapView: MKMapView?
  50. var ncplayer: NCPlayer?
  51. weak var delegate: NCViewerMediaDetailViewDelegate?
  52. override func awakeFromNib() {
  53. super.awakeFromNib()
  54. separator.backgroundColor = .separator
  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. messageButton.setTitle("", for: .normal)
  64. locationButton.setTitle("", for: .normal)
  65. }
  66. deinit {
  67. print("deinit NCViewerMediaDetailView")
  68. self.mapView?.removeFromSuperview()
  69. self.mapView = nil
  70. }
  71. func show(metadata: tableMetadata,
  72. image: UIImage?,
  73. textColor: UIColor?,
  74. mediaMetadata: NCImageMetadata,
  75. ncplayer: NCPlayer?,
  76. delegate: NCViewerMediaDetailViewDelegate?) {
  77. self.metadata = metadata
  78. self.latitude = mediaMetadata.latitude
  79. self.longitude = mediaMetadata.longitude
  80. self.location = mediaMetadata.location
  81. self.date = mediaMetadata.date
  82. self.lensModel = mediaMetadata.lensModel
  83. self.ncplayer = ncplayer
  84. self.delegate = delegate
  85. if mapView == nil && (latitude != -1 && latitude != 0 && longitude != -1 && longitude != 0) {
  86. let annotation = MKPointAnnotation()
  87. annotation.coordinate = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
  88. let mapView = MKMapView()
  89. self.mapView = mapView
  90. self.mapContainer.addSubview(mapView)
  91. mapView.translatesAutoresizingMaskIntoConstraints = false
  92. NSLayoutConstraint.activate([
  93. mapView.topAnchor.constraint(equalTo: self.mapContainer.topAnchor),
  94. mapView.bottomAnchor.constraint(equalTo: self.mapContainer.bottomAnchor),
  95. mapView.leadingAnchor.constraint(equalTo: self.mapContainer.leadingAnchor),
  96. mapView.trailingAnchor.constraint(equalTo: self.mapContainer.trailingAnchor)
  97. ])
  98. mapView.layer.cornerRadius = 6
  99. mapView.isZoomEnabled = true
  100. mapView.isScrollEnabled = false
  101. mapView.isUserInteractionEnabled = false
  102. mapView.addAnnotation(annotation)
  103. mapView.setRegion(MKCoordinateRegion(center: annotation.coordinate, latitudinalMeters: 500, longitudinalMeters: 500), animated: false)
  104. }
  105. // Size
  106. sizeLabel.text = NSLocalizedString("_size_", comment: "")
  107. sizeValue.text = CCUtility.transformedSize(metadata.size)
  108. sizeValue.textColor = textColor
  109. // Date
  110. if let date = date {
  111. let formatter = DateFormatter()
  112. formatter.dateStyle = .full
  113. formatter.timeStyle = .medium
  114. let dateString = formatter.string(from: date as Date)
  115. dateLabel.text = NSLocalizedString("_date_", comment: "")
  116. dateValue.text = dateString
  117. } else {
  118. dateLabel.text = NSLocalizedString("_date_", comment: "")
  119. dateValue.text = NSLocalizedString("_not_available_", comment: "")
  120. }
  121. dateValue.textColor = textColor
  122. // Dimension / Duration
  123. if metadata.classFile == NKCommon.TypeClassFile.image.rawValue {
  124. if let image = image {
  125. dimLabel.text = NSLocalizedString("_resolution_", comment: "")
  126. dimValue.text = "\(Int(image.size.width)) x \(Int(image.size.height))"
  127. }
  128. } else if metadata.classFile == NKCommon.TypeClassFile.video.rawValue || metadata.classFile == NKCommon.TypeClassFile.audio.rawValue {
  129. if let durationTime = NCManageDatabase.shared.getVideoDurationTime(metadata: metadata) {
  130. self.dimLabel.text = NSLocalizedString("_duration_", comment: "")
  131. self.dimValue.text = NCUtility.shared.stringFromTime(durationTime)
  132. }
  133. }
  134. dimValue.textColor = textColor
  135. // Model
  136. if let lensModel = lensModel {
  137. lensModelLabel.text = NSLocalizedString("_model_", comment: "")
  138. lensModelValue.text = lensModel
  139. lensModelValue.textColor = textColor
  140. }
  141. // Message
  142. if metadata.classFile == NKCommon.TypeClassFile.image.rawValue && !CCUtility.fileProviderStorageExists(metadata) && metadata.session.isEmpty {
  143. messageButton.setTitle(NSLocalizedString("_try_download_full_resolution_", comment: ""), for: .normal)
  144. messageButton.isHidden = false
  145. } else {
  146. messageButton.setTitle("", for: .normal)
  147. messageButton.isHidden = true
  148. }
  149. // Location
  150. if let location = location {
  151. locationButton.setTitle(location, for: .normal)
  152. locationButton.isHidden = false
  153. } else {
  154. locationButton.setTitle("", for: .normal)
  155. locationButton.isHidden = true
  156. }
  157. self.isHidden = false
  158. }
  159. func hide() {
  160. self.isHidden = true
  161. }
  162. func isShow() -> Bool {
  163. return !self.isHidden
  164. }
  165. // MARK: - Action
  166. @IBAction func touchLocation(_ sender: Any) {
  167. guard latitude != -1, latitude != 0, longitude != -1, longitude != 0 else { return }
  168. let latitude: CLLocationDegrees = self.latitude
  169. let longitude: CLLocationDegrees = self.longitude
  170. let regionDistance: CLLocationDistance = 10000
  171. let coordinates = CLLocationCoordinate2DMake(latitude, longitude)
  172. let regionSpan = MKCoordinateRegion(center: coordinates, latitudinalMeters: regionDistance, longitudinalMeters: regionDistance)
  173. let options = [
  174. MKLaunchOptionsMapCenterKey: NSValue(mkCoordinate: regionSpan.center),
  175. MKLaunchOptionsMapSpanKey: NSValue(mkCoordinateSpan: regionSpan.span)
  176. ]
  177. let placemark = MKPlacemark(coordinate: coordinates, addressDictionary: nil)
  178. let mapItem = MKMapItem(placemark: placemark)
  179. mapItem.name = location
  180. mapItem.openInMaps(launchOptions: options)
  181. }
  182. @IBAction func touchFavorite(_ sender: Any) {
  183. }
  184. @IBAction func touchMessage(_ sender: Any) {
  185. delegate?.downloadFullResolution()
  186. }
  187. }