NCViewerMediaDetailView.swift 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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 {
  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 delegate: NCViewerMediaDetailViewDelegate?
  50. override func awakeFromNib() {
  51. super.awakeFromNib()
  52. separator.backgroundColor = NCBrandColor.shared.separator
  53. sizeLabel.text = ""
  54. sizeValue.text = ""
  55. dateLabel.text = ""
  56. dateValue.text = ""
  57. dimLabel.text = ""
  58. dimValue.text = ""
  59. lensModelLabel.text = ""
  60. lensModelValue.text = ""
  61. messageButton.setTitle("" , for: .normal)
  62. locationButton.setTitle("" , for: .normal)
  63. }
  64. deinit {
  65. print("deinit NCViewerMediaDetailView")
  66. self.mapView?.removeFromSuperview()
  67. self.mapView = nil
  68. }
  69. func show(metadata: tableMetadata, image: UIImage?, textColor: UIColor?, latitude: Double, longitude: Double, location: String?, date: Date?, lensModel: String?, delegate: NCViewerMediaDetailViewDelegate?) {
  70. self.metadata = metadata
  71. self.latitude = latitude
  72. self.longitude = longitude
  73. self.location = location
  74. self.date = date
  75. self.lensModel = lensModel
  76. self.delegate = delegate
  77. if mapView == nil && (latitude != -1 && latitude != 0 && longitude != -1 && longitude != 0) {
  78. let annotation = MKPointAnnotation()
  79. annotation.coordinate = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
  80. self.mapView = MKMapView.init()
  81. if let mapView = self.mapView {
  82. mapView.translatesAutoresizingMaskIntoConstraints = false
  83. self.mapContainer.addSubview(mapView)
  84. NSLayoutConstraint.activate([
  85. mapView.topAnchor.constraint(equalTo: self.mapContainer.topAnchor),
  86. mapView.bottomAnchor.constraint(equalTo: self.mapContainer.bottomAnchor),
  87. mapView.leadingAnchor.constraint(equalTo: self.mapContainer.leadingAnchor),
  88. mapView.trailingAnchor.constraint(equalTo: self.mapContainer.trailingAnchor),
  89. ])
  90. mapView.layer.cornerRadius = 6
  91. mapView.isZoomEnabled = true
  92. mapView.isScrollEnabled = false
  93. mapView.isUserInteractionEnabled = false
  94. mapView.addAnnotation(annotation)
  95. mapView.setRegion(MKCoordinateRegion(center: annotation.coordinate, latitudinalMeters: 500, longitudinalMeters: 500), animated: false)
  96. }
  97. }
  98. // Size
  99. sizeLabel.text = NSLocalizedString("_size_", comment: "")
  100. sizeValue.text = CCUtility.transformedSize(metadata.size)
  101. sizeValue.textColor = textColor
  102. // Date
  103. if let date = date {
  104. let formatter = DateFormatter()
  105. formatter.dateStyle = .full
  106. formatter.timeStyle = .medium
  107. let dateString = formatter.string(from: date as Date)
  108. dateLabel.text = NSLocalizedString("_date_", comment: "")
  109. dateValue.text = dateString
  110. } else {
  111. dateLabel.text = NSLocalizedString("_date_", comment: "")
  112. dateValue.text = NSLocalizedString("_not_available_", comment: "")
  113. }
  114. dateValue.textColor = textColor
  115. // Dimension / Duration
  116. if metadata.classFile == NCCommunicationCommon.typeClassFile.image.rawValue {
  117. if let image = image {
  118. dimLabel.text = NSLocalizedString("_resolution_", comment: "")
  119. dimValue.text = "\(Int(image.size.width)) x \(Int(image.size.height))"
  120. }
  121. } else if metadata.classFile == NCCommunicationCommon.typeClassFile.video.rawValue || metadata.classFile == NCCommunicationCommon.typeClassFile.audio.rawValue {
  122. if let durationTime = NCManageDatabase.shared.getVideoDurationTime(metadata: metadata) {
  123. self.dimLabel.text = NSLocalizedString("_duration_", comment: "")
  124. self.dimValue.text = NCUtility.shared.stringFromTime(durationTime)
  125. }
  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.classFile == NCCommunicationCommon.typeClassFile.image.rawValue && !CCUtility.fileProviderStorageExists(metadata.ocId, fileNameView: metadata.fileNameView) && metadata.session == "" {
  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. if latitude != -1 && latitude != 0 && longitude != -1 && longitude != 0 {
  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. }
  176. @IBAction func touchFavorite(_ sender: Any) {
  177. }
  178. @IBAction func touchMessage(_ sender: Any) {
  179. delegate?.downloadFullResolution()
  180. }
  181. //MARK: -
  182. func secondsToHoursMinutesSeconds (seconds : Int) -> (Int, Int, Int) {
  183. return (seconds / 3600, (seconds % 3600) / 60, (seconds % 3600) % 60)
  184. }
  185. }