NCViewerMediaDetailView.swift 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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
  123. if let image = image {
  124. dimLabel.text = NSLocalizedString("_resolution_", comment: "")
  125. dimValue.text = "\(Int(image.size.width)) x \(Int(image.size.height))"
  126. }
  127. dimValue.textColor = textColor
  128. // Model
  129. if let lensModel = lensModel {
  130. lensModelLabel.text = NSLocalizedString("_model_", comment: "")
  131. lensModelValue.text = lensModel
  132. lensModelValue.textColor = textColor
  133. }
  134. // Message
  135. if metadata.isImage && !CCUtility.fileProviderStorageExists(metadata) && metadata.session.isEmpty {
  136. messageButton.setTitle(NSLocalizedString("_try_download_full_resolution_", comment: ""), for: .normal)
  137. messageButton.isHidden = false
  138. } else {
  139. messageButton.setTitle("", for: .normal)
  140. messageButton.isHidden = true
  141. }
  142. // Location
  143. if let location = location {
  144. locationButton.setTitle(location, for: .normal)
  145. locationButton.isHidden = false
  146. } else {
  147. locationButton.setTitle("", for: .normal)
  148. locationButton.isHidden = true
  149. }
  150. self.isHidden = false
  151. }
  152. func hide() {
  153. self.isHidden = true
  154. }
  155. func isShow() -> Bool {
  156. return !self.isHidden
  157. }
  158. // MARK: - Action
  159. @IBAction func touchLocation(_ sender: Any) {
  160. guard latitude != -1, latitude != 0, longitude != -1, longitude != 0 else { return }
  161. let latitude: CLLocationDegrees = self.latitude
  162. let longitude: CLLocationDegrees = self.longitude
  163. let regionDistance: CLLocationDistance = 10000
  164. let coordinates = CLLocationCoordinate2DMake(latitude, longitude)
  165. let regionSpan = MKCoordinateRegion(center: coordinates, latitudinalMeters: regionDistance, longitudinalMeters: regionDistance)
  166. let options = [
  167. MKLaunchOptionsMapCenterKey: NSValue(mkCoordinate: regionSpan.center),
  168. MKLaunchOptionsMapSpanKey: NSValue(mkCoordinateSpan: regionSpan.span)
  169. ]
  170. let placemark = MKPlacemark(coordinate: coordinates, addressDictionary: nil)
  171. let mapItem = MKMapItem(placemark: placemark)
  172. mapItem.name = location
  173. mapItem.openInMaps(launchOptions: options)
  174. }
  175. @IBAction func touchFavorite(_ sender: Any) {
  176. }
  177. @IBAction func touchMessage(_ sender: Any) {
  178. delegate?.downloadFullResolution()
  179. }
  180. }