NCViewerMediaDetailView.swift 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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?, completion: @escaping (_ showMap: Bool)->()) {
  61. func updateContent(date: Date?, lensModel: String?, location: String?, showMap: Bool) {
  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. completion(showMap)
  109. }
  110. if metadata.classFile == NCCommunicationCommon.typeClassFile.image.rawValue {
  111. CCUtility.setExif(metadata) { (latitude, longitude, location, date, lensModel) in
  112. self.latitude = latitude
  113. self.longitude = longitude
  114. self.location = location
  115. if latitude != -1 && latitude != 0 && longitude != -1 && longitude != 0 {
  116. let annotation = MKPointAnnotation()
  117. annotation.coordinate = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
  118. let mapView = MKMapView.init()
  119. mapView.translatesAutoresizingMaskIntoConstraints = false
  120. self.mapContainer.addSubview(mapView)
  121. NSLayoutConstraint.activate([
  122. mapView.topAnchor.constraint(equalTo: self.mapContainer.topAnchor),
  123. mapView.bottomAnchor.constraint(equalTo: self.mapContainer.bottomAnchor),
  124. mapView.leadingAnchor.constraint(equalTo: self.mapContainer.leadingAnchor),
  125. mapView.trailingAnchor.constraint(equalTo: self.mapContainer.trailingAnchor),
  126. ])
  127. mapView.layer.cornerRadius = 6
  128. mapView.isZoomEnabled = false
  129. mapView.isScrollEnabled = false
  130. mapView.isUserInteractionEnabled = false
  131. mapView.addAnnotation(annotation)
  132. mapView.setRegion(MKCoordinateRegion(center: annotation.coordinate, latitudinalMeters: 500, longitudinalMeters: 500), animated: false)
  133. updateContent(date: date, lensModel: lensModel, location: location, showMap: true)
  134. } else {
  135. updateContent(date: date, lensModel: lensModel, location: location, showMap: false)
  136. }
  137. self.isHidden = false
  138. };
  139. } else {
  140. updateContent(date: nil, lensModel: nil, location: nil, showMap: false)
  141. self.isHidden = false
  142. }
  143. }
  144. func hide() {
  145. self.isHidden = true
  146. }
  147. func isShow() -> Bool {
  148. return !self.isHidden
  149. }
  150. func isMapAvailable(metadata: tableMetadata, completion: @escaping (_ available: Bool)->()) {
  151. if metadata.classFile == NCCommunicationCommon.typeClassFile.image.rawValue {
  152. CCUtility.setExif(metadata) { (latitude, longitude, location, date, lensModel) in
  153. if latitude != -1 && latitude != 0 && longitude != -1 && longitude != 0 {
  154. completion(true)
  155. } else {
  156. completion(false)
  157. }
  158. }
  159. } else {
  160. completion(false)
  161. }
  162. }
  163. //MARK: - Action
  164. @IBAction func touchLocation(_ sender: Any) {
  165. if latitude != -1 && latitude != 0 && longitude != -1 && longitude != 0 {
  166. let latitude: CLLocationDegrees = self.latitude
  167. let longitude: CLLocationDegrees = self.longitude
  168. let regionDistance:CLLocationDistance = 10000
  169. let coordinates = CLLocationCoordinate2DMake(latitude, longitude)
  170. let regionSpan = MKCoordinateRegion(center: coordinates, latitudinalMeters: regionDistance, longitudinalMeters: regionDistance)
  171. let options = [
  172. MKLaunchOptionsMapCenterKey: NSValue(mkCoordinate: regionSpan.center),
  173. MKLaunchOptionsMapSpanKey: NSValue(mkCoordinateSpan: regionSpan.span)
  174. ]
  175. let placemark = MKPlacemark(coordinate: coordinates, addressDictionary: nil)
  176. let mapItem = MKMapItem(placemark: placemark)
  177. mapItem.name = location
  178. mapItem.openInMaps(launchOptions: options)
  179. }
  180. }
  181. @IBAction func touchFavorite(_ sender: Any) {
  182. }
  183. //MARK: -
  184. func secondsToHoursMinutesSeconds (seconds : Int) -> (Int, Int, Int) {
  185. return (seconds / 3600, (seconds % 3600) / 60, (seconds % 3600) % 60)
  186. }
  187. }