NCViewerMediaDetailView.swift 8.2 KB

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